hdu3973 AC's String string hash tree line +

Topic links: http://icpc.njust.edu.cn/Problem/Hdu/3973/

Is intended to question: given a pattern string, then some of the strings of a given set, the operation is divided into two, one is a replacement pattern character string, there is a query string model [l, r] interval there are no strings in the string collection.

Due to the large amount of data can only O (nlogn) the complexity of the algorithm to come through, we first think of the operating range of the query tree line that can be done, but how about a unique substring of it? This requires a hash of said string, my approach is to hash a string by string becomes 31 decimal and let it overflow, it is simply the modulo 2 ^ 64. Then we think about how to merge the sub-interval it? According to hash thinking we can easily think: If the right section of the hash value hash1, hash value of the interval left hash2, the right interval length is len, then hash = hash2 * 31 ^ len + hash1 after the merger, according to this the strategy can be aware of any hash value substring interval.

code show as below:

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 typedef unsigned int ui;
  4 typedef long long ll;
  5 typedef unsigned long long ull;
  6 #define pf printf
  7 #define mem(a,b) memset(a,b,sizeof(a))
  8 #define prime1 1e9+7
  9 #define prime2 1e9+9
 10 #define pi 3.14159265
 11 #define lson l,mid,rt<<1
 12 #define rson mid+1,r,rt<<1|1
 13 #define scand(x) scanf("%llf",&x) 
 14 #define f(i,a,b) for(int i=a;i<=b;i++)
 15 #define scan(a) scanf("%d",&a)
 16 #define dbg(args) cout<<#args<<":"<<args<<endl;
 17 #define pb(i) push_back(i)
 18 #define ppb(x) pop_back(x)
 19 #define inf 0x3f3f3f3f
 20 #define maxn 100010
 21 #define maxm 2000005
 22 int n,m,x,y;
 23 ll pp=31;
 24 ll t[maxn<<2], P [MAXM];
 25  char S [MAXM], MAXM A [];
 26 is  void the init ()
 27  {
 28      P [ 0 ] = . 1 ;
 29      F (I, . 1 , maxm- . 1 )
 30      {
 31 is          P [ I] = P [I- . 1 ] * PP;
 32      }
 33 is  }
 34 is LL GetHash ( char * S) // Get the hash value string 
35  {
 36      LL ANS = 0 ;
 37 [      F (I, 0 , strlen ( s) -. 1 )
 38 is      {
 39          ANS ANS * PP = S + [I] - ' A ' + . 1 ; // hash value natural overflow, i.e. for modulo 2 ^ 64 
40      }
 41 is      return ANS;
 42 is  }
 43 is  void a pushup ( int RT, int len) // pass the root node and the right sub-interval length 
44 is  {
 45      T [RT] = T [RT << . 1 ] * P [len] + T [RT << . 1 | . 1 ];
 46 is  }
 47  void Build ( int L, int R & lt,int rt)
 48 {
 49     if(l==r)
 50     {
 51         t[rt]=a[l]-'a'+1;
 52         return;
 53     }
 54     int mid=l+r>>1;
 55     build(lson);
 56     build(rson);
 57     pushup(rt,r-mid);
 58 }
 59 void update(int l,int r,int rt,int pos,char C)
 60 {
 61     if(l==r)
 62     {
 63         t[rt]=C-'a'+1;
 64         return;
 65     }
 66     int mid=l+r>>1;
 67     if(pos<=mid)update(lson,pos,C);
 68     else update(rson,pos,C);
 69     pushup(rt,r-mid);
 70 }
 71 ll query(int l,int r,int rt,intL, int R & lt)
 72  {
 73 is      IF (L> = R & lt && L <= R & lt) // Only when the interval does not need to completely coincide additional operations when removing the hash value 
74      {
 75          return T [RT];
 76      }
 77      int MID = + R & lt >> L . 1 ;
 78      IF (R & lt <= MID) return Query (LSON, L, R & lt); // divided into three cases, as required by the combined coefficient when 
79      the else  IF (L> MID) return Query ( rson, L, R & lt);
 80      return   Query (LSON, L, MID) * P [R & lt-MID] + Query (rson, MID + . 1 , R & lt);
 81  }
 82  int main()
 83 {
 84     //freopen("input.txt","r",stdin);
 85     //freopen("output.txt","w",stdout);
 86     std::ios::sync_with_stdio(false);
 87     int tt;
 88     scan(tt);
 89     init();
 90     f(kk,1,tt)
 91     {
 92         set<ll> map;//字符串到hash值的映射表 
 93         scan(n);
 94         f(i,1,n)
 95         {
 96             scanf("%s",s);
 97             map.insert(gethash(s));
 98         }
 99         scanf("%s",a+1);
100         int len=strlen(a+1);
101         build(1,len,1);
102         scan(m);
103         char q[3];
104         pf("Case #%d:\n",kk); 
105         while(m--)
106         {
107             
108             Scanf ( " % S " , Q);
 109              IF (Q [ 0 ] == ' Q ' )
 110              {
 111                  Scan (X);
 112                  Scan (Y);
 113                  X ++; // Note that the left end of the code segment tree starting from 1 
114                  Y ++ ;
 115              //     dbg (Query (1, len, 1, X, Y)); 
1 16                  IF (map.find (Query ( 1 , len, 1 !, X, Y)) = Map .end ())
 117                  PF ( " Yes \ n- ");
118                 else pf("No\n");
119             }
120             else if(q[0]=='C')
121             {
122                 char str[3];
123                 scan(x);
124                 x++;
125                 scanf("%s",str);
126                 update(1,len,1,x,str[0]);
127             }
128          } 
129     }
130  } 

 

Guess you like

Origin www.cnblogs.com/randy-lo/p/12444091.html