2017西安邀请赛: I. Barty's Computer(暴力+Hash)
来源:互联网 发布:java编程入门视频教程 编辑:程序博客网 时间:2024/06/11 17:12
- 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.
样例输入
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*/
- 2017西安邀请赛: I. Barty's Computer(暴力+Hash)
- 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 Barty's Computer(暴力+hash)
- 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 I. Barty's Computer(哈希||字典树)
- 2017 ACM-ICPC西安赛区网络赛 Barty's Computer【哈希】
- 计蒜客 Barty's Computer hash求字符串前缀和后缀
- 2017西安邀请赛总结
- 2017西安全国邀请赛总结
- 2017西安邀请赛problem A
- 西安邀请赛
- 2017 ACM ICPC 西安邀请赛总结
- 2014西安邀请赛 (hdu4847-4856)
- 青岛理工大学第五届邀请赛 I: 字符串的匹配(暴力)
- 2014 北京、西安邀请赛
- 2014西安邀请赛总结
- 2014 西安赛区邀请赛
- 西安邀请赛 233 Matrix
- poj1840(暴力+hash)
- hash(2014北京邀请赛)bnu34990
- Calico 的网络结构是什么?- 每天5分钟玩转 Docker 容器技术(68)
- git详细教程
- opencv相关---IplImage* 的头文件
- 番茄工作法——中断(笔记)
- java基础语法
- 2017西安邀请赛: I. Barty's Computer(暴力+Hash)
- JavaScript最佳新手入门系列 (初识JavaScript)
- js 获取昨天上周 上个月 上个季度的开始时间和结束时间
- JS——Ajax
- DroneKit教程(六):继承和自定义Vehicle类
- <c语言经典100例>c14 最大公倍数和最小公倍数
- 【BJOI2010】次小生成树
- SGISTL源码探究-STL中的红黑树(下)
- c# struct