Magic FZU - 2280 hash+ 暴力

给定n个字符串,每个字符串一个价值wi,询问

1 更改某个串的价值

2 查询以第x个串为后缀且价值小等于x串的字符串个数

字符串个数 n<=1000

查询数 q<=80000

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
const int x = 131;
const int mx = 1005;
typedef unsigned long long ul;
ul h[mx][mx];
ul xp[mx];
int len[mx];
char s[mx][mx];
int a[mx];
int n,q;
void geth(){
    xp[0] = 1;
    for(int i = 1; i <= 1000; i++)
        xp[i] = xp[i-1]*x;
    for(int i = 1; i <= n; i++){
        h[i][len[i]] = 0;
        for(int j = len[i]-1; j >= 0; j--)
            h[i][j] = h[i][j+1]*x + (s[i][j]-'a');
    }
}
ul HASH(int i,int j,int l){
    return h[i][j]-h[i][j+l]*xp[l];
}
int search(int x){
    int ans = 0;
    for(int i = 1; i <= n; i++){
        if(a[i]>a[x])
            continue;
        if(len[x] == len[i])
            ans += HASH(x,0,len[x])==HASH(i,0,len[x]);
        else if(len[x] < len[i])
            ans += HASH(x,0,len[x])==HASH(i,len[i]-len[x],len[x]);
    }
    return ans;
}
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[i]);
            len[i] = strlen(s[i]);
        }
        geth();
        scanf("%d",&q);
        while(q--){
            int casei,x,y;
            scanf("%d",&casei);
            if(casei == 2){
                scanf("%d",&x);
                printf("%d\n",search(x));
            }
            else{
                scanf("%d%d",&x,&y);
                a[x] = y;
            }
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值