String topic group 22 solution

Posted by luzlin on Sat, 08 Jan 2022 10:49:53 +0100

A - identical test

The teacher hates it when someone plagiarizes in the exam. Since the electronic marking, it is much easier for teachers to find the same papers. As long as they input the answers of two people into the computer, compare them character by character, and find out the same positions at a glance.

Input format

22 lines, each line contains a string of characters (no longer than 200200).

Output format

11 lines, containing several numbers separated by spaces, indicating where the same characters appear.

Sample Input

I am  suantoujun.
I am  huayemei.

Sample Output

1 2 3 4 5 6 8 9
​​​​​​​#include<stdio.h>
int main()
{
char a,b;
gets(a),gets(b);
for(int i=0;a[i]!='\0';i++)
if(a[i]==b[i])
printf("%d ",i+1);
return 0;
}

B - initial capital

For all words in a string, if the first letter of the word is not uppercase, the first letter of the word is changed to uppercase. In the string, words are separated by white space characters, including space (''), tab ('\ t'), carriage return ('\ r') and line feed ('\ n').

Input

Enter a line: the string to be processed (length less than 80).

Output

Output one line: converted string.

Sample Input

Sample Output

#include<stdio.h>
#include<string.h>
int main()
{
char a;
scanf("%[^\n]",a);
if(a>='a'&&a<='z')
a-=32;
for(int i=1;i<strlen(a)-1;i++)
if(a[i]==' '&&a[i+1]>='a'&&a[i+1]<='z')
a[i+1]-=32;
printf("%s",a);
return 0;
}

C - case conversion

Read in some strings and convert lowercase letters into uppercase letters (other characters remain unchanged).

input

The input is multiple lines, and each line is a string. The string is only composed of letters and numbers, and the length does not exceed 80. Input ends with "End of file".

output

For each line of input, the converted string is output.

Hello
ICPC2004
12345abcde

Output example

HELLO
ICPC2004
12345ABCDE
#include<stdio.h>
int main()
{
char a;
while(scanf("%s",a)==1){
for(int i=0;a[i]!='\0';i++)
if(a[i]>='a'&&a[i]<='z')
a[i]-=32;
printf("%s\n",a);
}
return 0;
}

D - Digital inversion

Given an integer, please reverse the numbers in each bit of the number to get a new number. The new number should also meet the common form of an integer, that is, unless the given original number is zero, the highest digit of the new number obtained after inversion should not be zero (see example 22).

Input format

Enter a total of 11 lines, an integer N.

Output format

Output a total of 1 line, an integer, representing the new number after inversion.

Data range

-1,000,000,000 <= N <= 1,000,000,000−1,000,000,000 <= N <= 1,000,000,000.

Sample Input

123

Sample Output

321

Sample Input 2

-380

Sample Output 2

-83
#include<stdio.h>
int main()
{
int n,sum=0;
scanf("%d",&n);
while(n){
sum=sum*10+n%10;
n/=10;
}
printf("%d",sum);
}

E - delete word suffix

Given a word, if the word ends with an er, ly or ing suffix, delete the suffix (the title ensures that the word length after deleting the suffix is not 00), otherwise no operation will be performed.

Input format

Enter a line containing one word (there is no space in the middle of the word, and the maximum length of each word is 3232).

Output format

Output the words processed according to the topic requirements.

Sample Input

referer

Sample Output

refer
#include<stdio.h>
#include<string.h>
int main()
{
char a;
int l;
scanf("%s",a);
l=strlen(a)-1;
if(a[l]=='r'&&a[l-1]=='e'||a[l]=='y'&&a[l-1]=='l')
a[l-1]='\0';
if(a[l-2]=='i'&&a[l-1]=='n'&&a[l]=='g')
a[l-2]='\0';
printf("%s",a);
return 0;
}

F - judge whether the string is palindrome

Enter a string and output whether the string is palindrome. Palindromes are strings that are read in sequence and read backwards.

Input format

The input is a line of string (there is no blank character in the string, and the length of the string does not exceed 100100).

Output format

If the string is palindrome, output "yes"; Otherwise, output "no".

Sample Input

abcdedcba

Sample Output

yes
#include<stdio.h>
#include<string.h>
int main()
{
char a;
int len,i;
scanf("%s",a);
len=strlen(a)-1;
for(i=0;i<=len/2;i++)
if(a[len-i]!=a[i])
break;
if(i<=len/2)
printf("no");
else
printf("yes");
return 0;
}

G - basic data structure - stack (1)

Give you a string of characters, no more than 50 characters, which may include parentheses, numbers, letters, punctuation marks and spaces. Your task is to check whether the (), [], {} in this string of characters match.

Input format

There are multiple groups of input data. Each group of data does not exceed 100 characters and contains one or more (,), [,], {,}. Processing to the end of the file.

Output format

If it matches, it outputs "yes", and if it does not match, it outputs "no"

Sample Input

sin(20+10)
{[}]

Sample Output

yes
no
#include <bits/stdc++.h>
using namespace std;
char s,c;
int main()
{
while(gets(s)/*!=NULL*/){
int i,top=0,l=strlen(s);//Top stands for the top of the stack
for(i=0;i<l;i++){
if(s[i]=='('||s[i]=='['||s[i]=='{'){
c[top]=s[i];
top++;//When the first parenthesis is encountered, it will be put on the stack
}
else if(s[i]==')'||s[i]==']'||s[i]=='}'){
if(s[i]==c[top-1]+1||s[i]==c[top-1]+2){//Find the relationship according to the ascll code value (40, 41, 91, 93123125)
top--;//If the match is successful, it will be out of the stack
}
else
break;
}
}
if(i==l&&!top)//Finally, if the stack is empty, all matching is successful
cout<<"yes"<<endl;
else
cout<<"no"<<endl;
}
return 0;
}

H - dictionary order

Give you two different strings. If the dictionary order of the first string is less than that of the second string, output YES. If the dictionary order of the first string is greater than that of the second string, output NO.

Input

Two lines. The first line is a string, and the second line is a string. Ensure that the length of the string does not exceed 10000. Ensure that two strings are not exactly equal.

Output

If the dictionary order of the first string is less than the second string, YES is output. If the dictionary order of the first string is greater than the second string, NO is output.

Sample Input

abc
abe

Sample Output

YES
#include<stdio.h>
#include<string.h>
char a,b;
int main()
{
scanf("%s%s",a,b);
if(strcmp(a,b)>0)
printf("NO");
else
printf("YES");
return 0;
}

I - verify substring

Enter two strings to verify that one string is a substring of the other.

Input format

Enter two strings, each on one line, no longer than 200200 and no spaces.

Output format

If the first string is s_1s1 is the second string s_2s2, then "(s1) is substring of (s2)";

Otherwise, if the second string s2 is a substring of the first string s1, output "(s2) is substring of (s1)";

Otherwise, output "No substring".

Sample Input

abc
dddncabca

Sample Output

abc is substring of dddncabca
#include<stdio.h>
#include<string.h>
int main()
{
int l1,l2;
char s1,s2;
gets(s1),gets(s2);
l1=strlen(s1),l2=strlen(s2);
if(l1<l2&&strstr(s2,s1))
printf("%s is substring of %s",s1,s2);
else if(l1>l2&&strstr(s1,s2))
printf("%s is substring of %s",s2,s1);
else
printf("No substring");
return 0;
}

J - substring lookup

Title Description

This is a template question.

Given a string AA and a string BB, find the number of occurrences of BB in AA. The characters in AA and BB are English uppercase or lowercase letters.

BB S at different positions in AA can overlap.

Input format

The input consists of two lines: String AA and string BB.

Output format

Output an integer indicating the number of occurrences of BB in AA.

Sample

InputOutput
zyzyzyz zyz3

Data range and tips

1 < = A, the length of B < = 10 ^ 6, A and B contain only uppercase and lowercase letters.

kmp

#include<bits/stdc++.h>
using namespace std;
#define M 1000000
char s[M],p[M];
int nxt[M];
void kmp(){
int len=strlen(p);
for(int i=1,j=0;p[i];i++){
while(j&&p[j]!=p[i])
j=nxt[j];
nxt[i+1]=p[j]==p[i]?++j:0;
}
int tot=0;
for(int i=0,j=0;s[i];i++){
while(j&&p[j]!=s[i])
j=nxt[j];
if (p[j]==s[i]&&++j==len){
tot++;
j=nxt[j];
}
}
printf("%d",tot);
}
int main()
{
scanf("%s %s",s,p);
kmp();
return 0;
}

K - cut cloth strip

Title Description

There are some patterns in a piece of floral cloth, and there is a small decorative strip that can be used directly, and there are also some patterns in it. For a given calico strip and small trim strip, how many small trim strips can be cut out from the calico strip as much as possible?

Input format

The input data is multiple groups of data and ends when the # character is read. Each group of data has only one row, which is a cloth strip and a small decoration strip separated by spaces. Both lace strips and small trim strips are represented by visible ASCII characters, which will not exceed 1000 characters.

Note: this # should be a single character. If a string begins with #, it does not mean the end of reading!

Output format

For each group of data, an integer is output in one line, indicating the maximum number of small trim strips that can be cut from the pattern cloth.

Sample

InputOutput
abcde a3 aaaaaa aa #0 3

Data range and tips

For all data, string length \ leq 1000 ≤ 1000.

1. Replace char with string

#include<bits/stdc++.h>
using namespace std;
int main()
{
string s,t;
while(cin>>s){
if(s=='#'&&s=='\0')
break;
cin>>t;
int cnt=0;
for(int i=0; i<s.size();i++){
int j=0,k=i;
while(s[k]==t[j]&&k<s.size()){
++k;++j;
}
if(j==t.size()){
cnt++;
i=k-1;
}
}
cout<<cnt<<endl;
}
return 0;
}

2. find function in STL

#include<bits/stdc++.h>
using namespace std;
int main()
{
string s,t;
while(cin>>s&&!(s=='#'&&s=='\0')){
cin>>t;
int cnt=0;
while (s.find(t)!=string::npos){        //s.find(t) returns string::npos if it cannot be found
cnt++;
s.erase(s.find(t),t.size());    //s.erase cuts off the ribbon that has been found
}
cout<<cnt<<endl;
}
}

L - longest palindrome substring

Enter a string Str and output the length of the longest palindrome substring in Str.

Palindrome string: refers to the left-right symmetrical strings such as aba, abba, cccbccc and aaaa.

Substring of a string: the substring of a string refers to the substring (character) composed of a continuous part of the (character) string. For example, the substring of abc: empty string, a, b, c, ab, bc, abc

Input format

Enter Str (length of Str < = 1000)

Output format

Output the length L of the longest palindrome substring.

Sample Input

daabaac

Sample Output

5

Midpoint diffusion

#include<bits/stdc++.h>
using namespace std;
int main()
{
string str;
getline(cin,str);
int res=0;
for(int i=0;i<str.length();i++){
int l=i-1,r=i+1;//It should be considered that the length of palindrome substring may be even or odd
while(l>=0&&r<str.length()&&str[l]==str[r]){
l--;
r++;
}
res=max(res,r-l-1);
l=i,r=i+1;
while(l>=0&&r<str.length()&&str[l]==str[r]){
l--;
r++;
}
res=max(res,r-l-1);
}
cout<<res<<endl;
return 0;
}

Topics: Algorithm