Advertisement
Guest User

poly.cpp

a guest
Jun 27th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. #include <fstream>
  2. #include <vector>
  3. #include <strings.h>
  4. #include <string>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. int main() {
  10. ifstream fr("input.txt");
  11. ofstream fw("output.txt");
  12.  
  13. string s;
  14. fr >> s;
  15.  
  16. string s1 = s;
  17. reverse(s1.begin(), s1.end());
  18.  
  19. string t = s1 + "#" + s;
  20.  
  21. int max_num = 0;
  22.  
  23. int size = t.size();
  24.  
  25. int arrPref[size];
  26. arrPref[0] = 0;
  27.  
  28. int k = 0;
  29.  
  30. for (int q = 1; q < size; q++) {
  31. k = arrPref[q - 1];
  32. while (k > 0 && t[k] != t[q]) {
  33. k = arrPref[k - 1];
  34. }
  35.  
  36. if (t[q] == t[k]) {
  37. k++;
  38. }
  39.  
  40. arrPref[q] = k;
  41. if (k > max_num) {
  42. max_num = k;
  43. }
  44. }
  45.  
  46. if (max_num == s1.length()) {
  47. fw << "";
  48. }
  49. else {
  50. s = s1.substr(max_num);
  51. fw << s;
  52. }
  53.  
  54. fr.close();
  55. fw.close();
  56.  
  57. return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement