链接:https://nanti.jisuanke.com/t/41386
来源:The Preliminary Contest for ICPC Asia Xuzhou 2019
题意:给我们两个字符串 t s,1. lent == lens:t == s 输出 jntm! 否则输出 friend! 2. lent > lens s 是 t 的子串 输出 my child! 否则输出 oh, child! 3. lent ≤ lens t 是 s 的子串 输出 my teacher! 否则输出 senior!
//stl - string
#include<bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
string t;int q;
cin>>t>>q;
int lent=t.length();
while(q--){
string s;
cin>>s;
int lens=s.length();
if(lens==lent){
if(s==t) cout<<"jntm!"<<'\n';
else cout<<"friend!"<<'\n';
}else if(lent>lens){
string c=t.substr(0,lens);
if(c==s) cout<<"my child!"<<'\n';
else cout<<"oh, child!"<<'\n';
}else{
string c=s.substr(0,lent);
if(c==t) cout<<"my teacher!"<<'\n';
else cout<<"senior!"<<'\n';
}
}
return 0;
}
//kmp - 子串位置
#include<bits/stdc++.h>
using namespace std;
const int Max_n=1e5+10;
char t[Max_n],s[Max_n];
int net[Max_n];
void get_net(int t,char *b){
net[0]=-1;
for(int i=0,j=-1;i<t;){
if(j==-1||b[i]==b[j]){
i++;j++;
net[i]=j;
}else j=net[j];
}
}
int kmp(int s,int t,char *a,char *b){
int i=0,j=0;
while(i<s&&j<t){
if(j==-1||a[i]==b[j]){
i++;j++;
}else j=net[j];
}
return j>=t?i-t:-1;
}
int main(){
scanf("%s",t);
int q; scanf("%d",&q);
while(q--){
scanf("%s",s);
int lent=strlen(t),lens=strlen(s);
if(lent==lens){ get_net(lens,s);
printf("%s\n",kmp(lent,lens,t,s)==-1?"friend!":"jntm!");
}else if(lent>lens){ get_net(lens,s);
printf("%s\n",kmp(lent,lens,t,s)==-1?"oh, child!":"my child!");
}else{ get_net(lent,t);
printf("%s\n",kmp(lens,lent,s,t)==-1?"senior!":"my teacher!");
}
}
return 0;
}