Advertisement
nicuvlad76

Untitled

Feb 5th, 2023
582
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.08 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string>
  3. #include<iostream>
  4. #include<vector>
  5. #include<cstring>
  6. #include<cassert>
  7. #include<fstream>
  8.  
  9. #define maxlen 70005
  10. #define maxwords 100005
  11. #define maxdictlen 500005
  12. #define maxsqrt 273
  13. #define base 73
  14. #define mods 2
  15.  
  16. using namespace std;
  17.  
  18.  
  19. ifstream fin("sir3.in");
  20. ofstream fout("sir3.out");
  21.  
  22.  
  23.  
  24. int n,dictSize,sqrtSize,bucati,op;
  25. string sir;
  26. string sqrtw[maxsqrt];
  27. int lung[maxwords],dictHash[maxwords][5],H[maxsqrt][mods],p[mods][maxlen];
  28.  
  29. int mod[mods] = {100003,500009};
  30.  
  31. inline void getWordHash ( string& , int *dictHash );
  32.  
  33. inline void read () {
  34.  
  35.     fin >> sir;
  36.     fin >> dictSize;
  37.     for ( int i = 1 ; i <= dictSize ; ++i ){
  38.         string word;
  39.         fin >> word;
  40.         lung[i] = (int)word.size();
  41.         getWordHash(word,dictHash[i]);
  42.     }
  43.     fin >> op;
  44. }
  45.  
  46. inline void getWordHash ( string& word , int *h ){
  47.  
  48.     for ( int j = 0 ; j < mods ; ++j ){
  49.         for ( int i = 0 ; i < (int)word.size() ; ++i ){
  50.             h[j] = (h[j]*base + word[i])%mod[j];
  51.         }
  52.     }
  53. }
  54.  
  55. inline void buildSqrt () {
  56.  
  57.     for ( int i = 1 ; i <= bucati ; ++i ){
  58.         sqrtw[i].clear();
  59.         for ( int j = 0 ; j < mods ; ++j ){
  60.             H[i][j] = 0;
  61.         }
  62.     }
  63.  
  64.     sqrtSize = 1;
  65.     while ( (sqrtSize+1)*(sqrtSize+1) <= (int)sir.size() ){
  66.         ++sqrtSize;
  67.     }
  68.  
  69.     bucati = 1;
  70.     for ( int i = 0 ; i < (int)sir.size() ; ++i ){
  71.  
  72.         if ( (int)sqrtw[bucati].size() == sqrtSize ){
  73.             ++bucati;
  74.         }
  75.         sqrtw[bucati].push_back(sir[i]);
  76.         for ( int j = 0 ; j < mods ; ++j ){
  77.             H[bucati][j] = (H[bucati][j]*base + sir[i])%mod[j];
  78.         }
  79.     }
  80. }
  81.  
  82. inline void rebuildHash ( const int &ind ){
  83.  
  84.     for ( int i = 0 ; i < mods ; ++i ){
  85.         H[ind][i] = 0;
  86.         for ( int j = 0 ; j < (int)sqrtw[ind].size() ; ++j ){
  87.             H[ind][i] = (H[ind][i]*base + sqrtw[ind][j])%mod[i];
  88.         }
  89.     }
  90. }
  91.  
  92. inline void insereaza ( int poz , const char &ch ){
  93.  
  94.     for ( int i = 1 ; i <= bucati ; ++i ){
  95.         if ( (int)sqrtw[i].size()+1 >= poz ){
  96.             string s; s.push_back(ch);
  97.             sqrtw[i].insert(poz-1,s);
  98.             rebuildHash(i);
  99.             return ;
  100.         }
  101.         else{
  102.             poz -= (int)sqrtw[i].size();
  103.         }
  104.     }
  105.  
  106.     assert(false);
  107. }
  108.  
  109. inline void getHash ( const int &st , const int &dr , vector<int>&localH ){
  110.  
  111.     int current_st = 1;
  112.     for ( int i = 1 ; i <= bucati && current_st <= dr ; ++i ){
  113.         int current_dr = current_st + (int)sqrtw[i].size() - 1;
  114.  
  115.         int comst = max(current_st,st),comdr = min(current_dr,dr);
  116.         if ( comst == current_st && comdr == current_dr ){
  117.  
  118.             for ( int j = 0 ; j < mods ; ++j ){
  119.                 localH[j] = (1LL*localH[j]*p[j][sqrtw[i].size()] + H[i][j])%mod[j];
  120.             }
  121.         }
  122.         else{
  123.  
  124.             for ( int k = comst-current_st ; k <= comdr-current_st ; ++k ){
  125.                 for ( int j = 0 ; j < mods ; ++j ){
  126.                     localH[j] = (localH[j]*base + sqrtw[i][k])%mod[j];
  127.                 }
  128.             }
  129.         }
  130.  
  131.         current_st += (int)sqrtw[i].size();
  132.     }
  133. }
  134.  
  135. inline int solve ( int poz , int ind ){
  136.  
  137.     int dr = poz; int st = dr-lung[ind]+1;
  138.  
  139.     vector<int>localH(mods,0);
  140.     getHash(st,dr,localH);
  141.  
  142.     for ( int j = 0 ; j < mods ; ++j ){
  143.         if ( localH[j] != dictHash[ind][j] ){
  144.             return 0;
  145.         }
  146.     }
  147.     return 1;
  148. }
  149.  
  150. inline void precomputeBasePowers () {
  151.  
  152.     for ( int j = 0 ; j < mods ; ++j ){
  153.         p[j][0] = 1;
  154.     }
  155.     for ( int i = 1 ; i < maxlen ; ++i ){
  156.         for ( int j = 0 ; j < mods ; ++j ){
  157.             p[j][i] = (p[j][i-1]*base)%mod[j];
  158.         }
  159.     }
  160. }
  161.  
  162. inline void recompute () {
  163.  
  164.     sir = "";
  165.     for ( int i = 1 ; i <= bucati ; ++i ){
  166.         sir.append(sqrtw[i]);
  167.     }
  168.     buildSqrt();
  169. }
  170.  
  171. inline void solve () {
  172.  
  173.     buildSqrt();
  174.  
  175.     int last_ans = 0;
  176.     int a, b;
  177.     while ( op-- ){
  178.         int tip; fin >> tip;
  179.  
  180.         if ( tip == 0 ){
  181.             int poz; char ch;
  182.             fin >> poz >> ch >> a >> b;
  183.             if(last_ans == 0)
  184.                 poz ^= a;
  185.             else
  186.                 poz ^= b;
  187.  
  188.             insereaza(poz,ch);
  189.  
  190.             --sqrtSize;
  191.             if ( !sqrtSize )    recompute();
  192.         }
  193.         else{
  194.             int poz,ind;
  195.             fin >> poz >> ind >> a >> b;
  196.             if(last_ans == 0)
  197.                 poz ^= a;
  198.             else
  199.                 poz ^= b;
  200.  
  201.  
  202.             last_ans = solve(poz, ind);
  203.             fout << last_ans << "\n";
  204.         }
  205.     }
  206. }
  207.  
  208. int main () {
  209.  
  210.     read();
  211.     precomputeBasePowers();
  212.     solve();
  213.     return 0;
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement