Number Sequence---hdu1711(kmp)
2015-09-24 12:49
567 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711
题意就是求b数组在a数组出现的位置;就是kmp模板;
View Code
题意就是求b数组在a数组出现的位置;就是kmp模板;
#include<stdio.h> #include<iostream> #include<algorithm> #include<string.h> using namespace std; const int N = 1e6+7; int a , b , Next , n, m; void GetNext() { int i=0, j=-1; Next[0] = -1; while(i<n) { if(j==-1 || b[i] == b[j]) Next[++i] = ++j; else j=Next[j]; } } int kmp() { int i=0, j=0; while(i<m) { if(j==-1 || a[i] == b[j]) { i++;j++; } else j=Next[j]; if(j==n)///如果j==n就返回下标; return i-n+1; } return -1; } int main() { int T; scanf("%d", &T); while(T--) { scanf("%d%d", &m, &n); for(int i=0; i<m; i++) scanf("%d", &a[i]); for(int i=0; i<n; i++) scanf("%d", &b[i]); GetNext(); int ans = kmp(); printf("%d\n", ans); } return 0; }
View Code
相关文章推荐
- UILabel UiButton 文字下面加下划线
- UILabel,UITextField 以及UIButton应用
- Liferay中request
- leetcode 95-96:Unique Binary Search Trees
- <s:checkbox> fieldValue取值问题
- Introduction to Java Programming编程题8.37<Guess the capitals>
- White spaces are required between publicId and systemId
- Jenkins-svn: E175002: OPTIONS request failed on URL
- easyui tabs中打开新的tabs关闭当前tabs
- easyUI之Form(表单)组件
- iOS开发UI篇—Quartz2D使用(图形上下文栈)
- GPUImage
- quicksort+binarySearch
- 【HDOJ】5288 OO’s Sequence
- UILabel对齐方式 距上 居左 对齐
- What’s the Difference between a Hue, Tint, Shade and Tone ?
- easyui框架session失效问题
- JSP中request解决中文乱码问题(绝对经典)
- ios8 UITableView设置 setSeparatorInset:UIEdgeInsetsZero不起作用的解决办法
- Java中的枚举与values()方法