Magic FZU

Kim is a magician, he can use n kinds of magic, number from 1 to n. We use string Si to describe magic i. Magic Si will make Wi points of damage. Note that Wi may change over time.

Kim obey the following rules to use magic:

Each turn, he picks out one magic, suppose that is magic Sk, then Kim will use all the magic i satisfying the following condition:

1. Wi<=Wk

2. Sk is a suffix of Si.

Now Kim wondering how many magic will he use each turn.

Note that all the strings are considered as a suffix of itself.

Input

First line the number of test case T. (T<=6)

For each case, first line an integer n (1<=n<=1000) stand for the number of magic.

Next n lines, each line a string Si (Length of Si<=1000) and an integer Wi (1<=Wi<=1000), stand for magic i and it’s damage Wi.

Next line an integer Q (1<=Q<=80000), stand for there are Q operations. There are two kinds of operation.

“1 x y” means Wx is changed to y.

“2 x” means Kim has picked out magic x, and you should tell him how many magic he will use in this turn.

Note that different Si can be the same.

Output

For each query, output the answer.

Sample Input

1
5
abracadabra 2
adbra 1
bra 3
abr 3
br 2
5
2 3
2 5
1 2 5
2 3
2 2

Sample Output

3
1
2
1

题意:

给n个长度<=1000的字符串S[i](仅由小写字母构成),每个字符串有权值w[i]
有两种操作
2 k 选中第k个字符串,问你有多少个字符串满足w[i]<=w[k]而且S[k]是S[i]的后缀
1 k y 把第k个字符串的权值修改为

暴力HASH 预处理下后缀

然后暴力查询

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <queue>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <set>
 7 #include <iostream>
 8 #include <map>
 9 #include <stack>
10 #include <string>
11 #include <vector>
12 #define  pi acos(-1.0)
13 #define  eps 1e-6
14 #define  fi first
15 #define  se second
16 #define  lson l,m,rt<<1
17 #define  rson m+1,r,rt<<1|1
18 #define  bug         printf("******
")
19 #define  mem(a,b)    memset(a,b,sizeof(a))
20 #define  fuck(x)     cout<<"["<<x<<"]"<<endl
21 #define  f(a)        a*a
22 #define  sf(n)       scanf("%d", &n)
23 #define  sff(a,b)    scanf("%d %d", &a, &b)
24 #define  sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
25 #define  sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)
26 #define  pf          printf
27 #define  FRE(i,a,b)  for(i = a; i <= b; i++)
28 #define  FREE(i,a,b) for(i = a; i >= b; i--)
29 #define  FRL(i,a,b)  for(i = a; i < b; i++)
30 #define  FRLL(i,a,b) for(i = a; i > b; i--)
31 #define  FIN         freopen("DATA.txt","r",stdin)
32 #define  gcd(a,b)    __gcd(a,b)
33 #define  lowbit(x)   x&-x
34 #pragma  comment (linker,"/STACK:102400000,102400000")
35 using namespace std;
36 typedef long long  LL;
37 typedef unsigned long long ULL;
38 const int INF = 0x7fffffff;
39 const int mod = 1e9 + 7;
40 const int maxn = 1e3 + 10;
41 int t, n, m, val[maxn], len[maxn];
42 char mp[maxn][maxn];
43 vector<int>cnt[maxn];
44 ULL HA[maxn][maxn], p[maxn], seed = 99959;
45 ULL getcnt(int id, int l, int r) {
46     return HA[id][r] - HA[id][l - 1] * p[r - l + 1];
47 }
48 void init() {
49     p[0] = 1;
50     for (int i = 1 ; i < maxn ; i++) p[i] = p[i - 1] * seed;
51     for (int i = 1 ; i <= n ; i++) {
52         HA[i][0] = 0;
53         for (int j = 1 ; j <= len[i] ; j++) {
54             HA[i][j] = HA[i][j - 1] * seed + mp[i][len[i] - j + 1];
55         }
56     }
57     for (int i = 1 ; i <= n ; i++) {
58         cnt[i].push_back(i);
59         for (int j = i + 1 ; j <= n ; j++ ) {
60             if (len[i] == len[j] && HA[i][len[i]] == HA[j][len[j]]) cnt[i].push_back(j), cnt[j].push_back(i);
61             else if (len[i] > len[j] && HA[j][len[j]] == getcnt(i, 1, len[j] )) cnt[j].push_back(i);
62             else if (len[i] < len[j] && HA[i][len[i]] == getcnt(j, 1, len[i] )) cnt[i].push_back(j);
63         }
64     }
65 }
66 int main() {
67     //FIN;
68     sf(t);
69     while(t--) {
70         sf(n);
71         for (int i = 1 ; i <= n ; i++) {
72             cnt[i].clear();
73             scanf("%s%d", mp[i] + 1, &val[i]);
74             len[i] = strlen(mp[i] + 1);
75         }
76         init();
77         sf(m);
78         while(m--) {
79             int op, x, y;
80             sf(op);
81             if (op == 1) {
82                 sff(x, y);
83                 val[x] = y;
84             } else {
85                 sf(x);
86                 int ans = 0;
87                 for (int i = 0 ; i < cnt[x].size() ; i++)
88                     if (val[cnt[x][i]] <= val[x]) ans++;
89                 printf("%d
", ans);
90             }
91         }
92     }
93     return 0;
94 }

原文地址:https://www.cnblogs.com/qldabiaoge/p/9515687.html