Barty have a computer, it can do these two things.
-
Add a new string to its memory, the length of this string is even.
-
For given 4 strings a,b,c,d, find out how many strings that can be product by a+s1+b+c+s2+d, and ∣a∣+∣s1∣+∣b∣=∣c∣+∣s2∣+∣d∣. ∣s∣ means the length of string s, s1 and s2 can be any string, including
""
.
Please help your computer to do these things.
Input Format
Test cases begins with T(T≤5).
Then T test cases follows.
Each test case begins with an integer Q(Q≤30000).
Then Q lines,
1 s: add a new string s to its memory.
2 a b c d: find how many strings satisfying the requirement above.
∑∣s∣+∣a∣+∣b∣+∣c∣+∣d∣≤2000000.
Output Format
For type 2 query. Output the answer in one line.
样例输入
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
样例输出
1 2 1 3 3 1
解:因为题目没有明确给出字符串的个数与长度 所以这里我们开一个长整型指针数组避免越界
一开始逐个字符匹配T了 所以加了hash预处理
坑点 并不是文本串的总长度大于4个字符串的总长度就可以 需要前两个 和 后两个字符串的长度总和小于等于 文本串的一半
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std;
const int N = 1e6+10;
typedef long long LL;
char str[N], a[N], b[N], c[N], d[N];
const LL mod = 1e9+7;
const LL seed = 1313;
LL *hash1[N], pos[N];
int xlen[N];
int main()
{
pos[0]=1;
for(int i=1;i<N;i++) pos[i]=pos[i-1]*seed%mod;
int t;
scanf("%d", &t);
while(t--)
{
int n;
scanf("%d",&n);
int h=0;
while(n--)
{
int xx;
scanf("%d", &xx);
if(xx==1)
{
scanf("%s",str+1);
int l=strlen(str+1);
xlen[h]=l;
hash1[h]=new LL[l+3];
hash1[h][0]=0;
for(int i=1;i<=l;i++)
hash1[h][i]=(hash1[h][i-1]*seed%mod+(str[i]-'a'+1))%mod;
h++;
}
else
{
scanf("%s %s %s %s",a+1,b+1,c+1,d+1);
int l1=strlen(a+1),l2=strlen(b+1),l3=strlen(c+1),l4=strlen(d+1);
int sum=l1+l2+l3+l4, cnt=0;
LL ans1=0, ans2=0, ans3=0, ans4=0;
for(int j=1; j<=l1; j++) ans1=(ans1*seed%mod+(a[j]-'a'+1))%mod;
for(int j=1; j<=l2; j++) ans2=(ans2*seed%mod+(b[j]-'a'+1))%mod;
for(int j=1; j<=l3; j++) ans3=(ans3*seed%mod+(c[j]-'a'+1))%mod;
for(int j=1; j<=l4; j++) ans4=(ans4*seed%mod+(d[j]-'a'+1))%mod;
for(int i=0;i<h;i++)
{
if((xlen[i]&1)||l1+l2>xlen[i]/2||l3+l4>xlen[i]/2) continue;
int cx=xlen[i]/2;
LL x=hash1[i][l1];
if(x!=ans1) continue;
x=(hash1[i][cx]-hash1[i][cx-l2]*pos[l2]%mod+mod)%mod;
if(x!=ans2) continue;
x=(hash1[i][cx+l3]-hash1[i][cx]*pos[l3]%mod+mod)%mod;
if(x!=ans3) continue;
x=(hash1[i][2*cx]-hash1[i][2*cx-l4]*pos[l4]%mod+mod)%mod;
if(x!=ans4) continue;
cnt++;
}
printf("%d\n",cnt);
}
}
}
return 0;
}