//错了好几次,
//1、 res>=s1.length(),应该是大于abc abc合并的情况可以证明
//2、 少写了res>s2.length(),abcabc abc这个反例可以证明
#include<iostream>
#include<string>
using namespace std;
string s1,s2;
int next[50005*2];
void get_next(string str,int plen,int* next){
int i=0;
next[0]=-1;
int j=-1;
//多求一位
while(i<plen){
if(j==-1||str[i]==str[j]){
++i;
++j;
next[i]=j;
}
else
j=next[j];
}
}
int main(){
while(cin>>s1>>s2){
string s=s1+s2;
get_next(s,s.length(),next);
int res=next[s.length()];
while(res>s1.length()||res>s2.length())
res=next[res];
if(res!=0){
for(int j=0;j<res;j++)
cout<<s1[j];
cout<<" "<<res<<endl;
}
else
cout<<res<<endl;
}
return 0;
}
hdu 2594
最新推荐文章于 2021-08-26 20:34:43 发布