当前位置:网站首页>2022pta usual training questions (1-10 string processing questions)
2022pta usual training questions (1-10 string processing questions)
2022-07-26 10:29:00 【True question OK】
Catalog
7-1 h0105. My brother's homework
7-3 h0083. Characters that appear only once
7-5 h0092. The longest consecutive character in a string
7-6 h0086. String shift contains problems ( Cyclic displacement algorithm )
7-1 h0105. My brother's homework
Your brother has just finished “100 Addition and subtraction of inner numbers ” This part of the assignment , Please check it for him . Each topic ( Including my brother's answer ) The format is a+b=c perhaps a-b=c, among a and b Is given in the assignment , No more than 100 Non-negative integer ;c It's my brother's answer , Probably no more than 200 Non-negative integer , It could be a single character "?", Means he won't count .
Input format :
The input file contains no more than 100 That's ok , End with end of file . Each line contains a topic , The format guarantees compliance with the above provisions , And does not contain any white space characters . All integers entered do not contain a leading 0.
Output format :
Output only one line , Contains a non negative integer , That is, the number of questions my brother answered correctly .
sample input :
1+2=3
3-1=5
6+7=?
99-0=99
sample output :
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. String connection
A language is a set of strings . Splicing of two languages is a collection of all strings formed by splicing strings of the second language at the end of the strings of the first language .
• for example , If two languages are given A and B :
• A = {cat, dog, mouse} ;
• B = {rat, bat} ;
• be A and B The connection is :
• C = {catrat, catbat, dograt, dogbat, mouserat, mousebat} .
• Give two languages , Please calculate the number of strings produced by the splicing of two languages .
Input format :
The input has multiple test cases . The first line of input gives the number of test cases T ( 1≤T≤25 ). Next, I will give T Test cases . The first line of each test case gives two integers , M and N ( M, N < 1500 ), Is the number of strings in each language . then , M Line gives the string of the first language ; Next N Line gives the string of the second language . The set string of this question is only composed of lowercase letters ( 'a' To 'z' ) form , The length is less than 10 Characters , And each string is given on one line , There are no leading or trailing spaces .
Strings in the input language may not be sorted , And there will be no duplicate strings .
Output format :
For each test case , Output one line . The output of each test case starts with the serial number of the test case , Then give the number of strings produced by splicing strings in the second language after strings in the first language .
sample input :
2
3 2
cat
dog
mouse
rat
bat
1 1
abc
cab
sample output :
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. Characters that appear only once
Give you a string containing only lowercase letters .
Please judge whether there is a character that only appears once in the string .
If there is , Then the character with the highest position among the characters that meet the conditions will be output .
without , Output ”no”.
Input format :
All in one line , Contains a string of lowercase letters .
The data guarantees that the length of the string does not exceed 100000.
Output format :
Output the first character that meets the condition .
without , The output ”no”.
sample input :
abceabcd
sample output :
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. Word substitution
Enter a string , End with a carriage return ( The string length does not exceed 100).
The string consists of several words , Words are separated by a space , All words are case sensitive .
Now we need to replace one word with another , And output the replaced string .
Input format :
Input common 3 That's ok .
The first 1 A line is a string containing multiple words s;
The first 2 Line is the word to be replaced a( Length not exceeding 100);
The first 3 Line is a The word to be replaced b( Length not exceeding 100).
Output format :
All in one line , The output will s All words in a Replace with b String after .
sample input :
You want someone to help you
You
I
sample output :
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. The longest consecutive character in a string
Find the longest consecutive character in a string , Output the character and its occurrence times , There are no white space characters in the string ( Space 、 Return and tab), If there is more than one such character , Then the first one is output .
Input format :
Enter the integer in the first line N, Number of groups representing test data .
Each group of data occupies one row , Contains a string that does not contain white space characters , The string length does not exceed 200.
Output format :
All in one line , Output the longest consecutive characters and their occurrences , Space separates the middle .
sample input :
2
aaaaabbbbbcccccccdddddddddd
abcdefghigk
sample output :
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. String shift contains problems ( Cyclic displacement algorithm )
For a string , Define a cyclic shift operation as : Move the first character of a string to the end to form a new string .
Given two strings s1 and s2, It is required to determine whether one of the strings is a substring of the new string after the other string has been circularly shifted for several times .
for example CDAA By AABCD A new string produced after two shifts BCDAA The string of , and ABCD And ACBD You can't get that one of the strings is a substring of the new string through multiple shifts .
Input format :
All in one line , Contains two strings , Separated by a single space .
The string contains only letters and numbers , Length not exceeding 30.
Output format :
If a string is a substring of a new string generated by several cyclic shifts of another string , The output true, Otherwise output false.
sample input :
AABCD CDAA
sample output :
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;
}
边栏推荐
- 函数模板参数(函数参数在哪)
- MD5加密
- Deduct daily question 838 of a certain day
- modelsim 安装教程(应用未安装)
- Unit test, what is unit test and why is it so difficult to write a single test
- 详细解析js中的混合方式构造对象(构造加属性,原型加方法)
- 【Halcon视觉】阈值分割
- Learning about opencv (1)
- 原生JS-获取transform值 x y z及rotate旋转角度
- INSTALL_FAILED_SHARED_USER_INCOMPATIBLE错误解决方式
猜你喜欢
随机推荐
Tradingview 使用教程
3.1 leetcode daily question 6
Okaleido生态核心权益OKA,尽在聚变Mining模式
Inheritance method of simplified constructor (II) - class inheritance in ES6
链式方法调用的事务问题剖析
Agenda express | list of sub forum agenda on July 27
我们的Web3创业项目,黄了
【Halcon视觉】形态学腐蚀
一些你不知道的 web API
Learning about tensor (III)
The problem of incomplete or partial display of the last recyclerview is solved
Learning about opencv (4)
【Halcon视觉】图像的傅里叶变换
函数模板参数(函数参数在哪)
[qualcomm][network] QTI service analysis
The software cannot be opened
【Halcon视觉】算子的结构
Basics of data communication - basic knowledge of network
mysql 进不去了怎么办
【Halcon视觉】软件编程思路