[HDOJ1711]Number Sequence
2015-09-03 19:34
459 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711
Total Submission(s): 15683 Accepted Submission(s): 6898
[align=left]Problem Description[/align]
Given two sequences of numbers : a[1], a[2], ...... , a
, and b[1], b[2], ...... , b[M] (1 <= M <= 10000, 1 <= N <= 1000000). Your task is to find a number K which make a[K] = b[1], a[K + 1] = b[2], ...... , a[K + M - 1] = b[M]. If there are more than one K exist, output the smallest one.
[align=left]Input[/align]
The first line of input is a number T which indicate the number of cases. Each case contains three lines. The first line is two numbers N and M (1 <= M <= 10000, 1 <= N <= 1000000). The second line contains N integers which indicate a[1], a[2], ...... , a
. The third line contains M integers which indicate b[1], b[2], ...... , b[M]. All integers are in the range of [-1000000, 1000000].
[align=left]Output[/align]
For each test case, you should output one line which only contain K described above. If no such K exists, output -1 instead.
[align=left]Sample Input[/align]
2
13 5
1 2 1 2 3 1 2 3 1 3 2 1 2
1 2 3 1 3
13 5
1 2 1 2 3 1 2 3 1 3 2 1 2
1 2 3 2 1
[align=left]Sample Output[/align]
6
-1
初学KMP,水一发模版题
本题还可以用hash做,效率与kmp差距不大。
Number Sequence
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 15683 Accepted Submission(s): 6898
[align=left]Problem Description[/align]
Given two sequences of numbers : a[1], a[2], ...... , a
, and b[1], b[2], ...... , b[M] (1 <= M <= 10000, 1 <= N <= 1000000). Your task is to find a number K which make a[K] = b[1], a[K + 1] = b[2], ...... , a[K + M - 1] = b[M]. If there are more than one K exist, output the smallest one.
[align=left]Input[/align]
The first line of input is a number T which indicate the number of cases. Each case contains three lines. The first line is two numbers N and M (1 <= M <= 10000, 1 <= N <= 1000000). The second line contains N integers which indicate a[1], a[2], ...... , a
. The third line contains M integers which indicate b[1], b[2], ...... , b[M]. All integers are in the range of [-1000000, 1000000].
[align=left]Output[/align]
For each test case, you should output one line which only contain K described above. If no such K exists, output -1 instead.
[align=left]Sample Input[/align]
2
13 5
1 2 1 2 3 1 2 3 1 3 2 1 2
1 2 3 1 3
13 5
1 2 1 2 3 1 2 3 1 3 2 1 2
1 2 3 2 1
[align=left]Sample Output[/align]
6
-1
初学KMP,水一发模版题
#include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <iostream> #include <cmath> #include <queue> #include <map> #include <stack> #include <list> #include <vector> using namespace std; const int maxn = 1000010; int na, nb; int a[maxn]; int b[maxn]; int pre[maxn]; void getpre(int *b, int *pre) { int j, k; pre[0] = -1; j = 0; k = -1; while(j < nb - 1) { if(k == -1 || b[j] == b[k]) {//匹配 j++; k++; pre[j] = k; } else { //b[j] != b[k] k = pre[k]; } } } int kmp() { int i = 0; int j = 0; getpre(b, pre); while(i < na) { if(j == -1 || a[i] == b[j]) { i++; j++; } else { j = pre[j]; } if(j == nb) { return i - nb + 1; } } return -1; } int main() { int T; scanf("%d", &T); while(T--) { scanf("%d %d", &na, &nb); for(int i = 0; i < na; i++) { scanf("%d", &a[i]); } for(int i = 0; i < nb; i++) { scanf("%d", &b[i]); } printf("%d\n", kmp()); } return 0; }
本题还可以用hash做,效率与kmp差距不大。
#include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <iostream> #include <cmath> #include <queue> #include <map> #include <set> #include <stack> #include <list> #include <vector> using namespace std; typedef unsigned long long ull; const int B = 100007; const int maxn = 1000010; int a[maxn], b[maxn]; int na, nb; ull quickmul(int x, int n) { ull ans = 1; ull t = x; while(n) { if(n & 1) { ans = (ans * t); } t = t * t; n >>= 1; } return ans; } int contain() { if(na > nb) { return false; } ull t = quickmul(B, na); ull ah = 0, bh = 0; for(int i = 0; i < na; i++) { ah = ah * B + a[i]; } for(int i = 0; i < na; i++) { bh = bh * B + b[i]; } for(int i = 0; i + na <= nb; i++) { if(ah == bh) { return i + 1; } if(i + na < nb) { bh = bh * B + b[i+na] - b[i] * t; } } return -1; } int main() { // freopen("in", "r", stdin); int T; scanf("%d", &T); while(T--) { scanf("%d %d", &nb, &na); for(int i = 0; i < nb; i++) { scanf("%d", &b[i]); } for(int i = 0; i < na; i++) { scanf("%d", &a[i]); } printf("%d\n", contain()); } return 0; }
相关文章推荐
- gui界面开发演示
- Codeforces GYM 100114 C. Sequence 打表
- Access restriction: The type * is not accessible due to restrict,报错问题
- 在Closing事件中,将e.Cancle设置成true,则Windows无法关机和重启系统的解决办法
- poj 2299 Ultra-QuickSort(树状数组)
- 2012-2013 ACM-ICPC, NEERC, Central Subregional Contest C Sequence (打表)
- 一篇讲得非常好的build模式的解释文章
- maven -- 问题解决(二)解决“Could not calculate build plan”问题
- STL vector, deque, list对比
- [刷题]Implement Queue by Two Stacks
- 九度OJ 题目1119:Integer Inquiry
- 让UITextView文字垂直居中
- UITextFiledView页面之间的相互传值
- Android UI设计(引导界面):ViewPager之三PagerTabStrip与PagerTitleStrip添加标题栏的区别
- jquey.fullpage详解
- UINavigaionController中的传值(协议)
- Android UI设计——ViewPage的无限循环(五)
- Arduino LM35温度检测
- RYU的GUI安装
- 黑马程序员—————Java基础--------GUI