fork download
  1. #include<bits/stdc++.h>
  2. using namespace std ;
  3.  
  4. // void comp(vector<char> &res, string str)
  5. // {
  6. // int j = 0;
  7. // int i = max(0 , (int)res.size() - (int)str.length()) ;
  8. // bool set = false;
  9.  
  10. // while(i < res.size() && j < str.length())
  11. // {
  12. // if(res[i] == str[j])
  13. // {
  14. // j++;
  15. // set = true ;
  16. // }
  17. // else
  18. // {
  19. // if(set)
  20. // {
  21. // j = 0 ;
  22. // i-- ;
  23. // }
  24. // set = false;
  25. // }
  26. // i++ ;
  27. // }
  28.  
  29.  
  30. // for(int i = j ; i < str.length() ; i++)
  31. // res.push_back(str[i]) ;
  32. // }
  33.  
  34. // void comp(vector<char> &res, string str)
  35. // {
  36. // reverse(str.begin() , str.end()) ;
  37. // int i = res.size() - 1 ;
  38. // int j = str.length() - 1;
  39. // while(i >= 0 && j >= 0 &&(res[i] == str[j]))
  40. // {
  41. // i-- ;
  42. // j--;
  43. // }
  44. // str = str.substr(0, j+1);
  45. // for(int i = str.length() - 1 ; i >= 0 ; i--)
  46. // res.push_back(str[i]) ;
  47.  
  48. // }
  49.  
  50. void comp(vector<char> &res , string str)
  51. {
  52. int t = str.length() ;
  53. int i = max(0 , (int)res.size() - (int)str.length()) ;
  54.  
  55. vector<char> vv ;
  56. for(auto u : str) vv.push_back(u) ;
  57.  
  58. for(i; i < res.size() ; i++)
  59. {
  60. vv.push_back(res[i]) ;
  61. }
  62.  
  63.  
  64. // cout << str <<endl;
  65. int n = vv.size() ;
  66. int arr[n] ;
  67. arr[0] = 0 ;
  68. i = 0 ;
  69.  
  70. for(int j = 1 ; j < n ; j++)
  71. {
  72. if(vv[i] == vv[j])
  73. {
  74. arr[j]= i +1 ;
  75. i++ ;
  76. }
  77. else
  78. {
  79. if(i)i = arr[i-1] ;
  80. if(vv[i] == vv[j])
  81. {
  82. arr[j] = i + 1 ;
  83. i++ ;
  84. }
  85. else
  86. arr[j] = 0 ;
  87. }
  88. }
  89.  
  90. // cout << str << endl;
  91.  
  92. // for(int i = 0 ; i < n ; i++)
  93. // cout<< arr[i] << " " ;
  94. // cout << endl;
  95.  
  96. if(arr[n-1] == n-1)
  97. str = str.substr(n-t , t-(n-t)) ;
  98. else
  99. str = str.substr(arr[n-1] , t - arr[n-1]);
  100.  
  101. for(int i = 0 ; i < str.length(); i++)
  102. res.push_back(str[i]) ;
  103. }
  104.  
  105.  
  106. int main()
  107. {
  108. std::ios::sync_with_stdio(false);
  109. cin.tie(NULL);
  110.  
  111. int n ;
  112. cin >> n ;
  113. string arr[n] ;
  114. for(int i = 0 ; i < n ; i++) cin >> arr[i] ;
  115. vector<char> vec ;
  116.  
  117. for(int i = 0 ; i < arr[0].length() ; i++) vec.push_back(arr[0][i]) ;
  118.  
  119. for(int i = 1 ; i < n ; i++)
  120. {
  121. comp(vec , arr[i]) ;
  122. }
  123.  
  124. for(int i = 0 ; i < vec.size() ; i++) cout << vec[i] ;
  125.  
  126.  
  127. return 0 ;
  128. }
  129.  
Success #stdin #stdout 0s 4536KB
stdin
5
I want to order pizza
stdout
Iwantorderpizza