【37.68%】【hdu 5918】Sequence I
来源:互联网 发布:我知主掌管明天 编辑:程序博客网 时间:2024/04/25 17:56
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 706 Accepted Submission(s): 266
Problem Description
Mr. Frog has two sequences a1,a2,⋯,an and b1,b2,⋯,bm and a number p. He wants to know the number of positions q such that sequence b1,b2,⋯,bm is exactly the sequence aq,aq+p,aq+2p,⋯,aq+(m−1)p where q+(m−1)p≤n and q≥1.
Input
The first line contains only one integer T≤100, which indicates the number of test cases.
Each test case contains three lines.
The first line contains three space-separated integers 1≤n≤106,1≤m≤106 and 1≤p≤106.
The second line contains n integers a1,a2,⋯,an(1≤ai≤109).
the third line contains m integers b1,b2,⋯,bm(1≤bi≤109).
Output
For each test case, output one line “Case #x: y”, where x is the case number (starting from 1) and y is the number of valid q’s.
Sample Input
2
6 3 1
1 2 3 1 2 3
1 2 3
6 3 2
1 3 2 2 3 1
1 2 3
Sample Output
Case #1: 2
Case #2: 1
Source
2016中国大学生程序设计竞赛(长春)-重现赛
【题解】
意思是让你在
a1,a1+p,a1+2p,a1+3p…
a2,a2+p,a2+2p,a2 + 3p..
a3,a3+p,a3+2p,a3 +3p
…
ap,ap+p,ap+2p,ap+3p..
(a右边的东西都是下标;)
这p个序列里面找b数组的匹配数目;
用vector类处理出这个p个数列就好。
剩下的用KMP算法解决。
找完一个匹配之后,j==m。
这个时候让j= f[j];
就能继续找匹配了。
记住就好。不然每次都想好烦。
#include <cstdio>#include <iostream>#include <vector>const int MAXN = 2e6;const int MAXM = 2e6;using namespace std;int p;vector <int> a[MAXN];int b[MAXM];int f[MAXM],ans,n,m;void input(int &r){ int t = getchar(); while (!isdigit(t)) t = getchar(); r = 0; while (isdigit(t)) r = r * 10+t-'0', t = getchar();}int main(){ //freopen("F:\\rush.txt", "r", stdin); int t; input(t); for (int q = 1; q <= t; q++) { for (int i = 0; i <= 1000000; i++)//a[0]也要clear! a[i].clear(); ans = 0; input(n); input(m); input(p); for (int i = 1; i <= n; i++) { int x; input(x); a[(i%p)].push_back(x); } for (int j = 0; j <= m - 1; j++) input(b[j]); f[0] = 0; f[1] = 0; for (int i = 1; i <= m - 1; i++)//获取失配函数,b数组下表是从0开始的。 { int j = f[i]; while (j && b[j] != b[i]) j = f[j]; f[i + 1] = b[j] == b[i] ? j + 1 : 0; } for (int i = 0; i <= p - 1; i++)//给p个数列找匹配数目 { int j = 0, len = a[i].size(); for (int k = 0; k <= len - 1; k++) { while (j && a[i][k] != b[j]) j = f[j]; if (a[i][k] == b[j]) j++; if (j == m) { ans++; j = f[j]; } } } printf("Case #%d: %d\n", q, ans); } return 0;}
- 【37.68%】【hdu 5918】Sequence I
- HDU 5918 Sequence I
- hdu 5918 Sequence I (kmp)
- hdu 5918 Sequence I kmp
- HDU 5918 Sequence I (kmp)
- [HDU 5918] Sequence I (kmp)
- hdu 5918 Sequence I kmp算法
- HDU-5918:Sequence I(KMP)
- 2016 ccpc长春现场赛I Sequence I(hdu 5918)
- HDU-5918 Sequence I(暴力)(KMP)
- HDU 5918 Sequence I CCPC 长春 暴力求解
- hdu 5918 Sequence I (CCPC长春,KMP)
- HDU 5918 Sequence I (水题+字符串匹配)
- HDU 5918 Sequence I (2016-ccpc-长春)
- HDU 5918 Sequence I (2016长春区域赛, KMP)
- HDOJ 5918 Sequence I(KMP)
- HDOj 5918 Sequence I【KMP】
- HDU 5918 Sequence I kmp算法+虽然暴力也能过
- android手机无法用adb connect 10.58.106.158 指令
- 遍历map的几种方法
- 【HDU5932 2016CCPC东北地区大学生程序设计竞赛 - 重现赛 K】【树上背包 贪心乱搞】Backpack on Tree 物品成本只有12345下的树上背包
- 做朋友圈的界面 cell的高度的动态调节2
- 求最小公倍数
- 【37.68%】【hdu 5918】Sequence I
- AllAngleExpandableButton
- display:flex 布局教程
- java代码实现二叉树的遍历
- Python 数据处理扩展包: pandas 模块的DataFrame介绍(创建和基本操作)
- 58. Length of Last Word
- 国庆节 班级管理系统
- Uva1625 -Color Length(DP)
- 链表实践—多项式ADT(数组实现)