Reincarnationtime limit:3000msmemory limit:65536kbthis problem'll be judged onHDU. Original id:4622
64-bit integer IO format: %i64d Java class name: Main Now is Back,and has a task to do:
Given you a string s consist of lower-case 中文版 letters Only,denote F (s) as the number of distinct sub-string of S.
And you have some query,each time should calculate f (s[l ... R]), S[l ... R] means the sub-string of S start from L end at R.InputThe first line contains integer T (1<=t<=5), denote the number of the the test cases.
For each test cases,the first line contains a string s (1 <= length of S <= 2000).
Denote the length of s by N.
The second line contains an integer Q (1 <= q <= 10000), denote the number of queries.
Then Q lines Follows,each lines contains the integer l, r (1 <= l <= r <= N), denote a query.OutputFor each test cases,for each query,print the answer on one line.Sample Input
2bbaba53 4baaba53 33 41 43 55 5
Sample Output
3175813851
HintI won ' t do anything against hash because I am nice. Of course this problem have a solution that don ' t rely on hash.SourceMulti-university Training Contest 3 Problem Solving: It is said that the title of the Clj suffix automata to find the number of an interval substring
1#include <bits/stdc++.h>2 using namespacestd;3 Const intMAXN =5010;4 intret;5 structNode {6 intf,len,son[ -];7 voidinit () {8memset (son,-1,sizeofson);9Len =0;Tenf =-1; One } A }; - structSAM { -Node e[maxn<<1]; the intTot,last; - intNewNodeintLen =0) { - e[tot].init (); -E[tot].len =Len; + returntot++; - } + voidinit () { Atot = last =0; at NewNode (); - } - voidExtendintc) { - intp = LAST,NP = NewNode (E[p].len +1); - while(P! =-1&& E[p].son[c] = =-1) { -E[P].SON[C] =NP; inp =e[p].f; - } to if(p = =-1) { +E[NP].F =0; -RET + = e[np].len-e[0].len; the}Else { * intQ =E[p].son[c]; $ if(E[p].len +1==E[q].len) {Panax NotoginsengE[NP].F =Q; -RET + = E[np].len-E[q].len; the}Else { + intNQ =NewNode (); AE[NQ] =E[q]; theE[Q].F = E[NP].F =NQ; +E[nq].len = E[p].len +1; -RET + = E[np].len-E[e[np].f].len; $ while(P! =-1&& E[p].son[c] = =q) { $E[P].SON[C] =NQ; -p =e[p].f; - } the } - }WuyiLast =NP; the } - } Sam; Wu intans[ .][ .]; - CharSTR[MAXN]; About intMain () { $ intKase,q,l,r; -scanf"%d",&Kase); - while(kase--) { -scanf"%s", str); A for(inti =0; Str[i]; ++i) { + sam.init (); theRET =0; - for(intj = i; STR[J]; ++j) { $Sam.extend (Str[j]-'a'); theANS[I][J] =ret; the } the } thescanf"%d",&q); - while(q--) { inscanf"%d%d",&l,&R); theprintf"%d\n", ans[l-1][r-1]); the } About } the return 0; the}View Code
HDU 4622 Reincarnation