HDU4622 Reincarnation 字符串哈希

Reincarnation

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 4571    Accepted Submission(s): 1868


Problem Description
Now you are back,and have a task to do:
Given you a string s consist of lower-case English letters only,denote f(s) as the number of distinct sub-string of s.
And you have some query,each time you should calculate f(s[l...r]), s[l...r] means the sub-string of s start from l end at r.
 

 

Input
The first line contains integer T(1<=T<=5), denote the number of 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 two integer l, r(1 <= l <= r <= n), denote a query.
 

 

Output
For each test cases,for each query,print the answer in one line.
 

 

Sample Input
2 bbaba 5 3 4 2 2 2 5 2 4 1 4 baaba 5 3 3 3 4 1 4 3 5 5 5
 

 

Sample Output
3 1 7 5 8 1 3 8 5 1
Hint
I won't do anything against hash because I am nice.Of course this problem has a solution that don't rely on hash.
 

 直接用的kuangbin的板子

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int HASH = 10007;
 4 const int MAXN = 2010;
 5 struct HASHMAP
 6 {
 7         int head[HASH],next[MAXN],size;
 8         unsigned long long state[MAXN];
 9         int f[MAXN];
10         void init()
11         {
12                 size = 0;
13                 memset(head,-1,sizeof(head));
14         }
15         int insert(unsigned long long val,int _id)
16         {
17                 int h = val%HASH;
18                 for (int i = head[h]; i != -1; i = next[i])
19                 {
20                         if (val == state[i])
21                         {
22                                 int tmp = f[i];
23                                 f[i] = _id;
24                                 return tmp;
25                         }
26                 }
27                 f[size] = _id;
28                 state[size] = val;
29                 next[size] = head[h];
30                 head[h] = size++;
31                 return 0;
32         }
33 } H;
34 const int SEED = 13331;
35 unsigned long long P[MAXN];
36 unsigned long long S[MAXN];
37 char str[MAXN];
38 int ans[MAXN][MAXN];
39 int main()
40 {
41         P[0] = 1;
42         for(int i = 1; i < MAXN; i++)
43                 P[i] = P[i - 1] * SEED;
44         int T;
45         scanf("%d",&T);
46         while(T -- )
47         {
48                 scanf("%s",str);
49                 int n = strlen(str);
50                 S[0] = 0;
51                 for(int i = 1; i <= n; i++)
52                         S[i] = S[i - 1]*SEED + str[i - 1];
53                 memset(ans,0,sizeof(ans));
54                 for(int L = 1; L <= n; L++)
55                 {
56                         H.init();
57                         for(int i = 1; i + L - 1 <= n; i++)
58                         {
59                                 int l = H.insert(S[i+L - 1] - S[i - 1]*P[L],i);
60                                 ans[i][i+L - 1] ++;
61                                 ans[l][i+L - 1] -- ;
62                         }
63                 }
64                 for(int i = n; i >= 0; i -- )
65                         for(int j = i; j <= n; j++)
66                                 ans[i][j] += ans[i+1][j] + ans[i][j - 1] - ans[i+1][j
67                                                 - 1];
68                 int m,u,v;
69                 scanf("%d",&m);
70                 while(m -- )
71                 {
72                         scanf("%d%d",&u,&v);
73                         printf("%d\n",ans[u][v]);
74                 }
75         }
76         return 0;
77 }

 

posted @ 2018-08-28 10:56  mizersy  阅读(215)  评论(0编辑  收藏  举报