luvk1412

Untitled

Apr 23rd, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.38 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define endl '\n'
  4. #define FIO ios_base::sync_with_stdio(false);cin.tie(NULL)
  5.  
  6.  
  7. #define HM 1000000007
  8. #define HA 100000007
  9. #define HB 101
  10.  
  11. int hext(int h, int ch) {
  12.     return (1ll * h * HA + ch + HB) % HM;
  13. }
  14.  
  15. int main() {
  16.     FIO;
  17.     int N;
  18.     string S;
  19.     cin >> S >> N;
  20.     unordered_map<int, unordered_map<int, vector<string>>> T;
  21.     for (int i = 0; i < N; i++) {
  22.         string ti;
  23.         cin >> ti;
  24.         int hsh = 0;
  25.         for (int j = 0; j < ti.size(); j++) {
  26.             hsh = hext(hsh, ti[j] - 'a');
  27.         }
  28.         T[ti.size()][hsh].push_back(ti);
  29.     }
  30.     string R;
  31.     vector<int> H(1, 0);
  32.     vector<int> HAPW(1, 1);
  33.     for (int i = 0; i < S.size(); i++) {
  34.         int ch = S[i] - 'a';
  35.         H.push_back(hext(H.back(), ch));
  36.         HAPW.push_back((1ll * HAPW.back() * HA) % HM);
  37.         R += S[i];
  38.         for(auto& it : T){
  39.             int len = it.first;
  40.             if (len > R.size()) {
  41.             continue;
  42.             }
  43.             int hsub = (1ll * H[R.size() - len] * HAPW[len]) % HM;
  44.             int hsh = (HM + H.back() - hsub) % HM;
  45.             bool found = false;
  46.             auto jt = it.second.find(hsh);
  47.             if(jt != it.second.end()){
  48.                 for(string &t : jt->second) {
  49.                     if (t == R.substr(R.size() - len)) {
  50.                         R.resize(R.size() - len);
  51.                         H.resize(H.size() - len);
  52.                         HAPW.resize(HAPW.size() - len);
  53.                         found = true;
  54.                         break;
  55.                     }
  56.                 }
  57.             }
  58.             if(found){
  59.                 break;
  60.             }
  61.         }
  62.     }
  63.     cout << R << endl;
  64.     return 0;
  65. }
Add Comment
Please, Sign In to add comment