当前位置:网站首页>2022pta平时训练题(1~10题字符串处理问题)
2022pta平时训练题(1~10题字符串处理问题)
2022-07-26 10:26:00 【真题OK撒】
目录
7-1 h0105. 弟弟的作业
你的弟弟刚做完了“100以内数的加减法”这部分的作业,请你帮他检查一下。每道题目(包括弟弟的答案)的格式为a+b=c或者a-b=c,其中a和b是作业中给出的,均为不超过100的非负整数;c是弟弟算出的答案,可能是不超过200的非负整数,也可能是单个字符"?",表示他不会算。
输入格式:
输入文件包含不超过100行,以文件结束符结尾。每行包含一道题目,格式保证符合上述规定,且不包含任何空白字符。输入的所有整数均不含前导0。
输出格式:
输出仅一行,包含一个非负整数,即弟弟答对的题目数量。
输入样例:
1+2=3
3-1=5
6+7=?
99-0=99
输出样例:
2
#include<bits/stdc++.h>
using namespace std;
int main() {
int x ,y ,cnt = 0;
char c1 ,c2;
string z;
while (cin >> x >> c1 >> y >> c2 >> z) {
int num = 0;
for (int i = 0; i < z.size(); i++) {
num = (z[i] - '0') + num * 10;
}
if (c1 == '+' && x + y == num)
{
cnt++;
}
else if (c1 == '-' && x - y == num)
{
cnt++;
}
else if (c1 == '*' && x * y == num)
{
cnt++;
}
else if (c1 == '/' && x / y == num)
{
cnt++;
}
}
cout << cnt;
return 0;
}7-2 h0100. 字符串连接
一种语言是一个字符串组成的集合。两种语言的拼接是在第一种语言的字符串的结尾处拼接第二种语言的字符串而构成的所有字符串的集合。
• 例如,如果给出两种语言 A 和 B :
• A = {cat, dog, mouse} ;
• B = {rat, bat} ;
• 则 A 和 B 的连接是:
• C = {catrat, catbat, dograt, dogbat, mouserat, mousebat} 。
• 给出两种语言,请您计算两种语言拼接所产生的字符串的数目。
输入格式:
输入有多个测试用例。输入的第一行给出测试用例的数目 T ( 1≤T≤25 )。接下来给出 T 个测试用例。每个测试用例的第一行给出两个整数, M 和N ( M, N < 1500 ),是每种语言中字符串的数量。然后, M 行给出第一种语言的字符串;接下来的 N 行给出了第二种语言的字符串。本题设定字符串仅由小写字母( 'a' 到 'z' )组成,长度小于 10 个字符,并且每个字符串在一行中给出,没有任何前导或尾随的空格。
输入语言中的字符串可能不会被排序,并且不会有重复的字符串。
输出格式:
对于每个测试用例,输出一行。每个测试用例的输出是以测试用例的序列号开始,然后给出在第一种语言的字符串之后拼接第二种语言中的字符串所产生的字符串数。
输入样例:
2
3 2
cat
dog
mouse
rat
bat
1 1
abc
cab
输出样例:
Case 1: 6
Case 2: 1
#include<bits/stdc++.h>
using namespace std;
int main() {
int t; cin >> t;
for (int k = 1; k <= t; k++){
set<string>st;
int n ,m; cin >> n >> m;
vector<string>v;
while (n--) {
string s; cin >> s;
v.push_back(s);
}
vector<string>u;
while (m--) {
string s; cin >> s;
u.push_back(s);
}
for (auto i : v) {
for (auto j : u) {
string s = i + j;
st.insert(s);
}
}
cout << "Case " << k << ": " << st.size() << endl;
}
return 0;
}7-3 h0083. 只出现一次的字符
给你一个只包含小写字母的字符串。
请你判断是否存在只在字符串中出现过一次的字符。
如果存在,则输出满足条件的字符中位置最靠前的那个。
如果没有,输出”no”。
输入格式:
共一行,包含一个由小写字母构成的字符串。
数据保证字符串的长度不超过100000。
输出格式:
输出满足条件的第一个字符。
如果没有,则输出”no”。
输入样例:
abceabcd
输出样例:
e#include<bits/stdc++.h>
using namespace std;
struct ll {
char c;
int cnt;
};
int main() {
string s ,res; cin >> s;
int flag = 0;
vector<ll>v;
map<char,int>mp;
for (int i = 0; i < s.size(); i++) {
v.push_back({s[i] ,1});
mp[s[i]]++;
}
for (int i = 0; i < s.size(); i++) {
v[i].cnt = mp[v[i].c];
}
for (int i = 0; i < s.size(); i++) {
if (v[i].cnt == 1) {
res = v[i].c;
flag = 1;
break;
}
}
if (flag) cout << res << endl;
else cout << "no" << endl;
return 0;
}7-4 h0084. 单词替换
输入一个字符串,以回车结束(字符串长度不超过100)。
该字符串由若干个单词组成,单词之间用一个空格隔开,所有单词区分大小写。
现需要将其中的某个单词替换成另一个单词,并输出替换之后的字符串。
输入格式:
输入共3行。
第1行是包含多个单词的字符串 s;
第2行是待替换的单词a(长度不超过100);
第3行是a将被替换的单词b(长度不超过100)。
输出格式:
共一行,输出将s中所有单词a替换成b之后的字符串。
输入样例:
You want someone to help you
You
I
输出样例:
I want someone to help you #include<bits/stdc++.h>
using namespace std;
int main(){
string s ,a ,b;
getline(cin ,s);
cin >> a;
cin >> b;
vector<string>v;
stringstream t(s);
while (t >> s) v.push_back(s);
for (int i = 0; i <v.size(); i++) {
if (v[i] == a) cout << b << ' ';
else cout << v[i] << ' ';
}
return 0;
}7-5 h0092. 字符串中最长的连续出现的字符
求一个字符串中最长的连续出现的字符,输出该字符及其出现次数,字符串中无空白字符(空格、回车和tab),如果这样的字符不止一个,则输出第一个。
输入格式:
第一行输入整数N,表示测试数据的组数。
每组数据占一行,包含一个不含空白字符的字符串,字符串长度不超过200。
输出格式:
共一行,输出最长的连续出现的字符及其出现次数,中间用空格隔开。
输入样例:
2
aaaaabbbbbcccccccdddddddddd
abcdefghigk
输出样例:
d 10
a 1#include<bits/stdc++.h>
using namespace std;
int main() {
int n; cin >> n;
while (n--) {
string s; cin >> s;
char res;
int cnt = 0;
for (int i = 0; i < s.size(); i++) {
int j = i;
while (s[j] == s[i]) j++;
if (j - i > cnt) {
cnt = j - i;
res = s[i];
}
i = j -1;
}
cout << res << ' ' << cnt << endl;
}
return 0;
}7-6 h0086. 字符串移位包含问题(循环位移算法)
对于一个字符串来说,定义一次循环移位操作为:将字符串的第一个字符移动到末尾形成新的字符串。
给定两个字符串s1和s2,要求判定其中一个字符串是否是另一字符串通过若干次循环移位后的新字符串的子串。
例如CDAA是由AABCD两次移位后产生的新串BCDAA的子串,而ABCD与ACBD则不能通过多次移位来得到其中一个字符串是新串的子串。
输入格式:
共一行,包含两个字符串,中间由单个空格隔开。
字符串只包含字母和数字,长度不超过30。
输出格式:
如果一个字符串是另一字符串通过若干次循环移位产生的新串的子串,则输出true,否则输出false。
输入样例:
AABCD CDAA
输出样例:
true
#include<bits/stdc++.h>
using namespace std;
int main() {
string a ,b; cin >> a >> b;
if (a.size() < b.size()) swap(a ,b);
string s = a + a;
if(s.find(b) != -1) {
cout << "true" << endl;
} else {
cout << "false" << endl;
}
return 0;
}边栏推荐
- [Halcon vision] array
- Some descriptions of DS V2 push down in spark
- 【Halcon视觉】图像灰度变化
- The reason why go language is particularly slow to develop run and build commands
- 单元测试,到底什么是单元测试,为什么单测这么难写
- Okaleido生态核心权益OKA,尽在聚变Mining模式
- canvas上传图片base64-有裁剪功能-Jcrop.js
- 关于模板函数声明与定义的问题[通俗易懂]
- Introduction to latex, EPS picture bounding box
- INSTALL_ FAILED_ SHARED_ USER_ Incompatible error resolution
猜你喜欢
随机推荐
【Halcon视觉】阈值分割
Use spiel expressions in custom annotations to dynamically obtain method parameters or execute methods
What is wrong about the description of function templates (how to solve link format errors)
equals与==的区别
Common errors when starting projects in uniapp ---appid
Mlx90640 infrared thermal imager temperature sensor module development notes (6)
Prevent XSS attacks
Review of database -- 3. SQL language
[Halcon vision] image filtering
[Qualcomm][Network] qti服务分析
About the declaration and definition of template functions [easy to understand]
Necessary for beginners: debug breakpoint debugging skills in idea and common breakpoint skills
结构体操作报错:Segmentation fault (core dumped)
C language course design Tetris (Part 2)
移动端双指缩放事件(原生),e.originalEvent.touches
【Halcon视觉】形态学膨胀
【Halcon视觉】仿射变换
Review of database -- 1. Overview
Unit test, what is unit test and why is it so difficult to write a single test
Uniapp common error [wxml file compilation error]./pages/home/home Wxml and using MySQL front provided by phpstudy to establish an independent MySQL database and a detailed tutorial for independent da




![[Halcon vision] software programming ideas](/img/9b/a27338689ee4598dac88f6e5d92053.png)

![[Halcon vision] programming logic](/img/1a/b6daac946fbefd8337355dc8b7873e.png)


