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

来源:互联网 发布:java编程入门视频教程 编辑:程序博客网 时间:2024/06/11 17:12

      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.

样例输入

1101 abcqaq1 abcabcqaqqaq2 ab bc qa aq2 a c q q1 abcabcqaqqwq2 ab bc qa aq2 a c q q1 abcq2 a c q q2 a b c q

样例输出

121331


题目链接: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 longLL 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;}/*1101 abcqaq2 a c q q*/


原创粉丝点击