-
- 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
题目链接:https://nanti.jisuanke.com/t/17122
Q只有30000,总长也不超过2000000
上去就是一阵暴力,如果T了,说明只是需要一点小小的Hash
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define mod 73939133
#define LL long long
LL h1, h2, h3, h4, *Hash[30005], Pow[2000005] = {1};
int l1, l2, l3, l4, cnt, len[30005];
char *str[30005], temp[2000005], a[2000005], b[2000005], c[2000005], d[2000005];
LL Jud(int id, int x, int y)
{
return (Hash[id][y]-Hash[id][x-1]%mod*Pow[y-x+1]%mod+mod)%mod;
}
int main(void)
{
int T, opt, q, i, sum;
for(i=1;i<=2000005;i++)
Pow[i] = Pow[i-1]*128%mod;
scanf("%d", &T);
while(T--)
{
cnt = 0;
scanf("%d", &q);
while(q--)
{
scanf("%d", &opt);
if(opt==1)
{
scanf("%s", temp+1);
len[++cnt] = strlen(temp+1);
str[cnt] = new char[len[cnt]+3];
for(i=1;i<=len[cnt];i++)
str[cnt][i] = temp[i];
str[len[cnt]+1] = 0;
Hash[cnt] = new LL[len[cnt]+3];
Hash[cnt][0] = 0;
for(i=1;i<=len[cnt];i++)
Hash[cnt][i] = (Hash[cnt][i-1]*128+temp[i])%mod;
}
else
{
scanf("%s%s%s%s", a+1, b+1, c+1, d+1);
l1 = strlen(a+1);
l2 = strlen(b+1);
l3 = strlen(c+1);
l4 = strlen(d+1);
h1 = h2 = h3 = h4 = 0;
for(i=1;i<=l1;i++)
h1 = (h1*128+a[i])%mod;
for(i=1;i<=l2;i++)
h2 = (h2*128+b[i])%mod;
for(i=1;i<=l3;i++)
h3 = (h3*128+c[i])%mod;
for(i=1;i<=l4;i++)
h4 = (h4*128+d[i])%mod;
//printf("%lld %lld %lld %lld\n", h1, h2, h3, h4);
sum = 0;
for(i=1;i<=cnt;i++)
{
if(l1+l2>len[i]/2 || l3+l4>len[i]/2)
continue;
//printf("%lld %lld %lld\n", Hash[1][2], Hash[1][3], Hash[1][3]-Hash[1][2]*Pow[1]);
//printf("%lld %d %d\n", Jud(i, len[i]/2-l2+1, len[i]/2), len[i]/2-l2+1, len[i]/2);
if(Jud(i, 1, l1)==h1 && Jud(i, len[i]/2-l2+1, len[i]/2)==h2 && Jud(i, len[i]/2+1, len[i]/2+l3)==h3 && Jud(i, len[i]-l4+1, len[i])==h4)
sum++;
}
printf("%d\n", sum);
}
}
for(i=1;i<=cnt;i++)
{
delete []str[i];
delete []Hash[i];
}
}
return 0;
}
/*
1
10
1 abcqaq
2 a c q q
*/