题意:让你把一个字符串反转,然后两个合并并去掉重复部分;就是求出末尾最长回文串;
题解:KMP;将原字符串反转,求出反转后的字符串的失配函数,然后与原字符串匹配;
参考代码:
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define clr(a,val) memset(a,val,sizeof(a)) 4 #define lowbit(x) x&-x 5 #define eps 1e-6 6 #define RI register int 7 typedef long long ll; 8 const int INF=0x3f3f3f3f; 9 const int maxn=1e6+10; 10 string str,rev; 11 int nxt[maxn]; 12 void getnext(string s) 13 { 14 nxt[0]=0;nxt[1]=0; 15 for(int i=1,len=s.length();i<len;++i) 16 { 17 int j=nxt[i]; 18 while(j&&s[i]!=s[j]) j=nxt[j]; 19 nxt[i+1]=s[i]==s[j]?j+1:0; 20 } 21 } 22 void solve() 23 { 24 int x=0,y=0,ans=0; 25 int len=str.length(); 26 while(y<len) 27 { 28 if(rev[x]==str[y]) 29 { 30 x++,y++; 31 ans=x; 32 } 33 else if(x>0) x=nxt[x]; 34 else x=0,y++; 35 } 36 cout<<str<<rev.substr(ans)<<endl; 37 } 38 39 int main() 40 { 41 while(cin>>str) 42 { 43 rev=str; 44 reverse(rev.begin(),rev.end()); 45 getnext(rev); 46 solve(); 47 } 48 49 return 0; 50 }