ps: Title Source: acwing!!! y total yyds!!!
8. String matching
subjectGive two strings a and b of the same length.
If the character a[i] on string a is the same as the character b[i] on string b at a certain position ii, the character at this position is matched.
If the ratio of the number of matching positions of two strings to the total length of the strings is greater than or equal to k, the two strings are said to match.
Now please judge whether the given two strings match.
Input formatThe first line contains a floating-point number k, the second line contains string a, and the third line contains string b.
The string you entered does not contain spaces.
Output formatIf the two strings match, yes is output.
Otherwise, no is output.
Data range0 ≤ k ≤ 1 ,
The length of the string does not exceed 100.
0.4 abcde xbacdOutput example:
noAC code
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int main() { string a,b; double k; cin>>k>>a>>b; int cnt=0; for(int i=0;i<a.size();i++) { if(a[i]==b[i]) { cnt++; } } if((double)cnt/a.size()>=k) { printf("yes"); } else printf("no"); return 0; }
9. Ignore case and compare string size
subjectGenerally, we can use strcmp to compare the size of two strings. The comparison method is to compare the two strings from front to back character by character (according to the size of ASCII code value) until different characters appear or \ 0 is encountered.
If all characters are the same, they are considered the same; If different characters appear, the comparison result of the first different character shall prevail.
But sometimes, when we compare the size of strings, we want to ignore the size of letters. For example, hello and hello are equal when ignoring the case of letters.
Please write a program to compare the case of two strings ignoring the case of letters.
Input formatThe input is two lines, one string per line, a total of two strings. Note that the string may contain spaces.
The data guarantees that the length of each string does not exceed 80.
Output formatIf the first string is smaller than the second string, output a character <.
If the first string is larger than the second string, output a character >.
If two strings are equal, output a character =.
Input example:Hello helloOutput example:
=AC code
#include<iostream> #include<cstring> #include<cstdio> using namespace std; int main() { char a[100],b[100]; fgets(a,100,stdin); fgets(b,100,stdin); if(a[strlen(a)-1]=='\n')a[strlen(a)-1]=0;//Remove the carriage return at the end if(b[strlen(b)-1]=='\n')b[strlen(b)-1]=0;//Remove the carriage return at the end for(int i=0;a[i];i++) { if(a[i]>='A'&&a[i]<='Z') { a[i]+=32; } } for(int i=0;b[i];i++) { if(b[i]>='A'&&b[i]<='Z') { b[i]+=32; } } int t=strcmp(a,b); if(t==0) puts("="); else if(t<0) puts("<"); else puts(">"); return 0; }
10. Remove redundant spaces
subjectEnter a string, which may contain multiple consecutive spaces. Please remove the extra spaces and leave only one space.
Input formatA line containing a string.
Output formatOutput the string after removing redundant spaces, accounting for one line.
Data rangeThe length of the input string does not exceed 200.
Ensure that there are no spaces at the beginning and end of the input string.
Hello world.This is c language.Output example:
Hello world.This is c language.thinking
Here is a tip that can make full use of the features of cin and cout, so easy!
AC code#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int main() { string a; while(cin>>a) { cout<<a<<' '; } return 0; }
11. Information encryption
subjectIn the process of transmitting information, in order to ensure the security of information, we need to encrypt the original information to form encrypted information, so that the information content will not be stolen by the listener.
Now give a string and encrypt it.
The encryption rules are as follows:
- The lowercase letter in the string, a is encrypted as b, b is encrypted as c,..., y is encrypted as z, and z is encrypted as a.
- The uppercase letter in the string. A is encrypted as B, B is encrypted as C,..., Y is encrypted as Z, and Z is encrypted as a.
- Other characters in the string are not processed.
Please output the encrypted string.
Input formatA line containing a string. Note that the string may contain spaces.
Output formatOutput encrypted string.
Data rangeThe length of the input string does not exceed 100.
Input example:Hello! How are you!Output example:
Ifmmp! Ipx bsf zpv!AC code
#include<iostream> #include<cstdio> #include<string> #include<algorithm> using namespace std; int main(){ string s1; getline(cin,s1); //cout << s1.size()<<endl; for(int i =0;i<s1.size();i++){ if(s1[i] == 'Z') s1[i]='A'; else if (s1[i] == 'z') s1[i]='a'; else if((s1[i] >= 'a' && s1[i] <= 'z')||(s1[i] >= 'A' &&s1[i] <= 'Z')) s1[i] = (char)((int)s1[i]+1); } cout << s1 <<endl; return 0; }
12. Output string
subjectGiven a string a, please output string b according to the following requirements.
Given the ASCII value of the first character of string a plus the ASCII value of the second character, the first character of b is obtained;
Given the ASCII value of the second character of string a plus the ASCII value of the third character, the second character of b is obtained;
...
Given the ASCII value of the penultimate character of string a plus the ASCII value of the last character, the penultimate character of b is obtained;
Given the ASCII value of the last character of string a plus the ASCII value of the first character, the last character of b is obtained.
Input formatEnter a line containing the string a. Note that the string may contain spaces.
The data guarantees that the ASCII value of characters in the string does not exceed 63.
Output formatThe output is a line containing the string b.
Data rangeLength of 2 ≤ a ≤ 100
Input example:1 2 3Output example:
QRRSdAC code
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int main() { string a; getline(cin,a); for(int i=0;i<a.size()-1;i++) { cout<<char(a[i]+a[i+1]); } cout<<char(a[a.size()-1]+a[0]); return 0; }
13. Word replacement
subjectEnter a string that ends with a carriage return (the string length does not exceed 100).
The string consists of several words separated by a space. All words are case sensitive.
Now you need to replace one word with another and output the replaced string.
Input formatEnter a total of 3 lines.
The first line is a string containing multiple words s;
The second line is the word a to be replaced (no longer than 100);
Line 3 is the word B (no longer than 100) where a will be replaced.
Output formatA total of one line, output the string after replacing all words a in s with b.
Input example:You want someone to help you You IOutput example:
I want someone to help youthinking
Replace a string with a character stream
Just use ssin as a cin
#include <sstream> stringstream ssin(s);AC code
#include<iostream> #include<sstream> using namespace std; int main() { string s,a,b; getline(cin,s); cin>>a>>b; stringstream ssin(s); string str; while(ssin>>str) { if(str==a) cout<<b<<' '; else cout<<str<<' '; } return 0; }
14. The longest consecutive character in a string
subjectFind the longest consecutive character in a string and output the character and its occurrence times. There are no white space characters (space, carriage return and tab) in the string. If there is more than one such character, the first one will be output.
Input formatIn the first line, enter the integer N, which represents the number of groups of test data.
Each group of data occupies one line and contains a string without white space characters. The length of the string shall not exceed 200.
Output formatA total of one line, output the longest consecutive characters and their occurrence times, separated by spaces.
Input example:2 aaaaabbbbbcccccccdddddddddd abcdefghigkOutput example:
d 10 a 1AC code
#include<iostream> using namespace std; int main() { int n; cin>>n; while(n--) { string str; cin>>str; int cnt=0; char c; for(int i=0;i<str.size();i++)//The first kind of double pointer algorithm { int j=i; while(j<str.size()&&str[j]==str[i]) j++; if(j-i>cnt) { cnt=j-i; c=str[i]; } i=j-1; } cout<<c<<' '<<cnt<<endl; } return 0; }
15. Longest word
subjectA simple English sentence ending with. Separated by spaces between words without abbreviations and other special forms. Find the longest word in the sentence.
Input formatEnter this simple English sentence with a length of no more than 500.
Output formatThe longest word in the sentence. If more than one, the first one is output.
Input example:I am a student of Peking University.Output example:
UniversityAC code
#include<iostream> using namespace std; int main() { string res,str; while(cin>>str) { if(str.back()=='.') str.pop_back(); if(str.size()>res.size()) res=str; } cout<<res<<endl; return 0; }
16. Inverted words
subjectWrite a program, read in a line of English (only letters and spaces, separated by a single space between words), reverse the order of all words and output, still separated by a single space.
Input formatInput as a string (string length up to 100).
Output formatThe output is a string sorted as required.
Input example:I am a studentOutput example:
student a am IAC code
#include<iostream> using namespace std; int main() { string str[100]; int n=0; while(cin>>str[n]) n++; for(int i=n-1;i>=0;i--) { cout<<str[i]<<' '; } cout<<endl; return 0; }
17. String shift inclusion problem
Title (this is a little difficult)For a string, define a cyclic shift operation as: move the first character of the string to the end to form a new string.
Given two strings s1 and s2, it is required to determine whether one string is a substring of a new string after the other string is shifted several times.
For example, CDAA is a substring of BCDAA, a new string generated after two shifts of AABCD, while ABCD and ACBD cannot obtain a substring of the new string through multiple shifts.
Input formatA line containing two strings separated by a single space.
The string contains only letters and numbers and is no longer than 30.
Output formatIf a string is a substring of a new string generated by several cyclic shifts of another string, true is output; otherwise, false is output.
Input example:AABCD CDAAOutput example:
trueAC code
#include <iostream> #include <algorithm> using namespace std; int main() { string a, b; cin >> a >> b; if (a.size() < b.size()) swap(a, b); for (int i = 0; i < a.size(); i ++ ) { a = a.substr(1) + a[0]; for (int j = 0; j + b.size() <= a.size(); j ++ ) { int k = 0; for (; k < b.size(); k ++ ) if (a[j + k] != b[k]) break; if (k == b.size()) { puts("true"); return 0; } } } puts("false"); return 0; }
18. String power
Title (this is also a little difficult)Given two strings a and b, we define a × b is their connection.
For example, if a=abc and b=def, then a × b=abcdef.
If we consider the connection as multiplication, the power of a nonnegative integer will be defined in the usual way: a0 = ` ` (empty string), a(n+1)=a × (an).
Input formatThe input contains multiple groups of test samples, each of which occupies one line.
Each set of samples contains a string s, whose length does not exceed 100.
The final test sample will be followed by a dot as a line.
Output formatFor each s, you need to output the maximum n so that there is a string a, let s=an.
Input example:abcd aaaa ababab .Output example:
1 4 3AC code
#include<iostream> using namespace std; int main() { string str; while(cin>>str,str!=".") { int len=str.size(); for(int n=len;n;n--) { if(len%n==0) { int m=len/n; string s=str.substr(0,m); string r; for(int i=0;i<n;i++) { r+=s; } if(r==str) { cout<<n<<endl; break; } } } } return 0; }
19. Maximum span of string
Title (this is also difficult)There are three strings S, S1 and S2, where the length of s does not exceed 300 and the length of S1 and S2 does not exceed 10.
Now, we want to detect whether S1 and S2 appear in s at the same time, and S1 is located on the left of S2 and does not cross each other in S (that is, the right boundary point of S1 is on the left of the left boundary point of S2).
Calculate the maximum span that meets the above conditions (i.e., the maximum spacing: the number of characters between the starting point of the rightmost S2 and the ending point of the leftmost S1).
If there is no S1 and S2 that meet the conditions, output − 1.
For example, S= abcd123ab888efghij45ef67kl, S1= ab, S2= ef, where S1 occurs twice in s, S2 also occurs twice in s, and the maximum span is 18.
Input formatEnter a line containing three strings S, S1 and S2 separated by commas.
The data guarantees that the three strings do not contain spaces and commas.
Output formatOutputs an integer representing the maximum span.
If no S1 and S2 that meet the conditions exist, output − 1.
Input example:abcd123ab888efghij45ef67kl,ab,efOutput example:
18AC code
#include<iostream> using namespace std; int main() { string s,s1,s2; char c; while(cin>>c,c!=',') { s+=c; } while(cin>>c,c!=',') { s1+=c; } while(cin>>c) { s2+=c; } if(s.size()< s1.size()||s.size()<s2.size()) { puts("-1"); } else { int l=0; while(l+s1.size()<=s.size()) { int k=0; while(k<s1.size()) { if(s[l+k]!=s1[k]) { break; } else k++; } if(k==s1.size()) break; else l++; } int r = s.size() - s2.size(); while (r >= 0) { int k = 0; while (k < s2.size()) { if (s[r + k] != s2[k]) break; k ++ ; } if (k == s2.size()) break; r -- ; } l += s1.size() - 1; if (l >= r) puts("-1"); else printf("%d\n", r - l - 1); } return 0; }
20. Longest common string suffix
Title (still a little difficult)Give several strings and output the longest common suffix of these strings.
Input formatIt consists of several sets of inputs.
The first line of each set of inputs is an integer N.
When N is 0, the input ends, otherwise there will be N lines of input, and each line is a string (the string does not contain blank characters).
The length of each string shall not exceed 200.
Output formatA total of one line, the longest common suffix of N strings (may be empty).
Data range1≤N≤200
Input example:3 baba aba cba 2 aa cc 2 aa a 0Output example:
ba aAC code
#include <iostream> using namespace std; const int N = 200; int n; string str[N]; int main() { while (cin >> n, n) { int len = 1000; for (int i = 0; i < n; i ++ ) { cin >> str[i]; if (len > str[i].size()) len = str[i].size(); } while (len) { bool success = true; for (int i = 1; i < n; i ++ ) { bool is_same = true; for (int j = 1; j <= len; j ++ ) if (str[0][str[0].size() - j] != str[i][str[i].size() - j]) { is_same = false; break; } if (!is_same) { success = false; break; } } if (success) { break; } else len -- ; } cout << str[0].substr(str[0].size() - len) << endl; } return 0; }