计蒜客 17122 Barty's Computer(hash)

Description

有一个字符串组成的集合,初始为空集,有 q q 个操作,操作分两种:

1 s:在集合中加入字符串 s s s长度为偶数

2 a b c d: 2   a   b   c   d : 问集合中有多少字符串可以被表示成 a+s1+b+c+s2+d a + s 1 + b + c + s 2 + d 的形式且 |a|+|s1|+|b|=|c|+|s2|+|d| | a | + | s 1 | + | b | = | c | + | s 2 | + | d | ,其中 s1,s2 s 1 , s 2 为任意字符串,也可以是空串

Input

第一行一整数 T T 表示用例组数,每组用例首先输入一整数q表示操作数,之后 q q 行每行输入一个操作(T5,q30000,|s|+|a|+|b|+|c|+|d|2106)

Output

对于每个查询操作输出结果

Sample Input

1
10
1 abcqaq
1 abcabcqaqqaq
2 ab bc qa aq
2 a c q q
1 abcabcqaqqwq
2 ab bc qa aq
2 a c q q
1 abcq
2 a c q q
2 a b c q

Sample Output

1
2
1
3
3
1

Solution

把集合中每个字符串分成长度相同的两部分,把这两部分字符串的前缀和后缀都 hash h a s h 后存起来,对于输入的 a,b,c,d a , b , c , d ,只要该字符串的前半部分的前缀后缀 hash h a s h 值分别与 a,b a , b hash h a s h 值相同,后半部分的前缀后缀 hash h a s h 值分别与 c,d c , d hash h a s h 值相同则该字符串满足条件

Code

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f,maxn=30005,maxm=2000005;
#define mod 1000000007
vector<int>al[maxn],ar[maxn],bl[maxn],br[maxn];
int T,q,len[maxn],res;
char s[maxm],a[maxm],b[maxm],c[maxm],d[maxm];
int lhash(char *s,int n)
{
    int ans=0;
    for(int i=0;i<n;i++)ans=(26ll*ans+s[i]-'a')%mod;
    return ans;
}
int rhash(char *s,int n)
{
    int ans=0;
    for(int i=n-1;i>=0;i--)ans=(26ll*ans+s[i]-'a')%mod;
    return ans;
}
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        for(int i=0;i<res;i++)al[i].clear(),ar[i].clear(),bl[i].clear(),br[i].clear();
        res=0;
        scanf("%d",&q);
        while(q--)
        {
            int op;
            scanf("%d",&op);
            if(op==1)
            {
                scanf("%s",s);
                int n=strlen(s),temp=0;
                for(int i=0;i<n/2;i++)
                {
                    temp=(26ll*temp+s[i]-'a')%mod;
                    al[res].push_back(temp);
                }
                temp=0;
                for(int i=n/2-1;i>0;i--)
                {
                    temp=(26ll*temp+s[i]-'a')%mod;
                    ar[res].push_back(temp);
                }
                temp=0;
                for(int i=n/2;i<n;i++)
                {
                    temp=(26ll*temp+s[i]-'a')%mod;
                    bl[res].push_back(temp);
                }
                temp=0;
                for(int i=n-1;i>=n/2;i--)
                {
                    temp=(26ll*temp+s[i]-'a')%mod;
                    br[res].push_back(temp);
                }
                len[res]=n/2;
                res++;
            }
            else
            {
                scanf("%s%s%s%s",a,b,c,d);
                int la=strlen(a),lb=strlen(b),lc=strlen(c),ld=strlen(d);
                int ans=0;
                for(int i=0;i<res;i++)
                {
                    if(la+lb>len[i]||lc+ld>len[i])continue;
                    if(lhash(a,la)==al[i][la-1]&&rhash(b,lb)==ar[i][lb-1]&&lhash(c,lc)==bl[i][lc-1]&&rhash(d,ld)==br[i][ld-1])ans++;
                }
                printf("%d\n",ans);
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值