Advertisement
Okorosso

S3: Нормальная палиндромика

Jul 1st, 2021
1,235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <string>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. vector<int> prefix_function(string s) {
  10.     using namespace std;
  11.     int n = s.length();
  12.     vector<int> p(n);
  13.     for (int i = 1; i < n; i++) {
  14.         int j = p[i - 1];
  15.         while (j > 0 && s[i] != s[j])
  16.             j = p[j - 1];
  17.         if (s[i] == s[j])
  18.             ++j;
  19.         p[i] = j;
  20.     }
  21.     return p;
  22. }
  23.  
  24. int main() {
  25.     ifstream fin("input.txt");
  26.     ofstream fout("output.txt");
  27.  
  28.     string str, rStr, uniStr;
  29.     getline(fin, str);
  30.     rStr = str;
  31.     reverse(rStr.begin(), rStr.end());
  32.     uniStr = rStr + "#" + str;
  33.  
  34.     vector<int> prefixVec = prefix_function(uniStr);
  35.  
  36.     for (int i = prefixVec[uniStr.size() - 1]; i < str.size(); i++) {
  37.         fout << rStr[i];
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement