2017西安邀请赛: I. Barty's Computer(暴力+Hash)

    • Barty have a computer, it can do these two things.

  1. Add a new string to its memory, the length of this string is even.

  2. For given 44 strings a,b,c,da,b,c,d, find out how many strings that can be product by a+s1+b+c+s2+da+s1+b+c+s2+d, and |a| + |s1| + |b| = |c| + |s2| + |d|a+s1+b=c+s2+d|s|s means the length of string sss1s1 and s2s2 can be any string, including "".

Please help your computer to do these things.

Input Format

Test cases begins with T(T \le 5)T(T5).

Then TT test cases follows.

Each test case begins with an integer Q(Q \le 30000)Q(Q30000).

Then QQ lines,

1 s: add a new string ss to its memory.

2 a b c d: find how many strings satisfying the requirement above.

\sum |s| + |a| + |b| + |c| + |d| \le 2000000s+a+b+c+d2000000.

Output Format

For type 22 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
*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值