Problem Description
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
2 3
2 5
1 2 5
2 2
Sample Output
3
2
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1005;
int n,q;
struct Node
{
char a[N];
int cnt;///題中的Wi
int len;///字元串a的長度
} s[N];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=1; i<=n; i++)
{
scanf("%s%d",s[i].a,&s[i].cnt);
s[i].len=strlen(s[i].a);
}
scanf("%d",&q);
int c,x,y;
for(int i=0; i<q; i++)
{
scanf("%d",&c);
if(c==1)
{
scanf("%d%d",&x,&y);
s[x].cnt=y;
// printf("%d\n",s[x].cnt);
}
else
{
scanf("%d",&x);
//printf("%d\n",s[x].cnt);
int num=1;
for(int i=1; i<=n; i++)
{
if(i==x) continue;
int flag;
if(s[i].cnt<=s[x].cnt&&s[i].len>=s[x].len)
{
flag=1;
for(int j=s[i].len-1,k=s[x].len-1; k>=0; j--,k--)
{
//printf("%c %c\n",s[i].a[j],s[x].a[k]);
if(s[i].a[j]!=s[x].a[k])
{
flag=0;
break;
}
}
if(flag) num++;
}
}
printf("%d\n",num);
}
}
}
return 0;
}
*/
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
#include<set>
#include<queue>
#define LL long long
#define P pair<int,int>
using namespace std;
const int M = 1e5 + 10;
const int N = 1e3 + 10;
char s[N][N];
int n,q;
int w[N];
int ch[M][26],tot;
int has[M * 26],id;
int len[N],ran[N],End[N];
int sum[N][N];
bool cmp(int x,int y){
return len[x] < len[y];
}
void init(){
id = tot = 0;
memset(ch[0],0,sizeof(ch[0]));
memset(sum,0,sizeof(sum));
memset(has,0,sizeof(has));
}
int lowbit(int x){
return x & (-x);
}
void up(int i,int pos,int val){
while(pos <= 1000){
sum[i][pos] += val;
pos += lowbit(pos);
}
}
int getsum(int i,int pos){
int ans = 0;
while(pos){
ans += sum[i][pos];
pos -= lowbit(pos);
}
return ans;
}
int ix(char c){
return c - 'a';
}
void Insert(char *s,int v){
int o = 0,c;
for(int i = len[v] - 1;i >= 0;i--){
c = ix(s[i]);
if(!ch[o][c]){
memset(ch[++tot],0,sizeof(ch[tot]));
ch[o][c] = tot;
}
o = ch[o][c];
if(has[o]) up(has[o],w[v],1);
}
if(!has[o]){
has[o] = ++id;
up(has[o],w[v],1);
}
End[v] = has[o];
}
void update(int v,char *s,int y){
int o = 0,c;
for(int i = len[v] - 1;i >= 0;i--){
c = ix(s[i]);
o = ch[o][c];
if(has[o]){
up(has[o],w[v],-1);
up(has[o],y,1);
}
}
w[v] = y;
}
int main()
{
int T;
cin>>T;
while(T--){
scanf("%d",&n);
for(int i = 1;i <= n;i++){
scanf("%s%d",s[i],&w[i]);
ran[i] = i;
len[i] = strlen(s[i]);
}
init();
sort(ran+1,ran+n+1,cmp);
for(int i = 1;i <= n;i++){
int v = ran[i];
Insert(s[v],v);
}
scanf("%d",&q);
int op,x,y;
while(q--){
scanf("%d%d",&op,&x);
if(op == 1){
scanf("%d",&y);
update(x,s[x],y);
}else printf("%d\n",getsum(End[x],w[x]));
}
}
return 0;
}