Advertisement
Okorosso

S1: Префикс-функция

Jul 1st, 2021
783
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.59 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8.  
  9. int main() {
  10.     ifstream fin("input.txt");
  11.     ofstream fout("output.txt");
  12.  
  13.     string str;
  14.     getline(fin, str);
  15.  
  16.     vector<unsigned long long int> s(str.length(), 0);
  17.     unsigned long long int b, count = 0;
  18.     for (int i = 1; i < str.length(); i++) {
  19.         b = s[i - 1];
  20.         while (b > 0 and str[i] != str[b]) {
  21.             b = s[b - 1];
  22.         }
  23.         if (str[i] == str[b])
  24.             ++b;
  25.         s[i] = b;
  26.  
  27.         count += b;
  28.     }
  29.  
  30.     fout << count;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement