fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. string txt , put;
  6. int lps[1000100];
  7. int kmp(){
  8. int len = 0;
  9. for(int i=0;i<txt.size();i++){
  10. while(len > 0 && txt[i]!=put[len])
  11. len=lps[len-1];
  12. if(txt[i]==put[len])
  13. ++len;
  14. if(i==(txt.size()-1)) return len;
  15. }
  16. }
  17. void buildLps(){
  18. int len = 0;
  19. lps[0]=0;
  20. for(int i=1;i<put.size();i++){
  21. while(len > 0 && put[i]!=put[len])
  22. len=lps[len-1];
  23. if(put[i]==put[len])
  24. ++len;
  25. lps[i]=len;
  26. }
  27. }
  28. int main(){
  29. while(cin>>txt){
  30. put=txt;
  31. reverse(put.begin(),put.end());
  32. buildLps();
  33. int need = kmp();
  34. cout<<txt;
  35. for(int i=need;i<put.size();i++) cout<<put[i];
  36. cout<<endl;
  37. }
  38. return 0;
  39. }
Success #stdin #stdout 0s 19144KB
stdin
Standard input is empty
stdout
Standard output is empty