HDU 1711 Number Sequence【数字KMP】
来源:互联网 发布:tvp动画软件 编辑:程序博客网 时间:2024/06/05 15:43
Number Sequence
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 22886 Accepted Submission(s): 9781
Problem Description
Given two sequences of numbers : a[1], a[2], ...... , a[N], 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.
Input
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[N]. The third line contains M integers which indicate b[1], b[2], ...... , b[M]. All integers are in the range of [-1000000, 1000000].
Output
For each test case, you should output one line which only contain K described above. If no such K exists, output -1 instead.
Sample Input
213 51 2 1 2 3 1 2 3 1 3 2 1 21 2 3 1 313 51 2 1 2 3 1 2 3 1 3 2 1 21 2 3 2 1
Sample Output
6-1
Source
HDU 2007-Spring Programming Contest
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711
数字的KMP。
第二个数组中的数字在第一个数组中出现的位置
AC代码:
#include <iostream>#include <cstring>#include <cstdio>using namespace std;const int maxn= 1000000+5;const int maxm= 10000+5;int lena,lenb;int a[maxn];int b[maxm];int nextt[maxm];void getNext(){ int i=0,j=-1; nextt[0]=-1;//不要忘记呀!!!! while(i<lenb) { if(j==-1||b[i]==b[j]) { i++,j++; nextt[i]=j; } else j=nextt[j]; }}int KMP(){ int i=0,j=0; getNext(); while(i<lena) { if(j==-1||a[i]==b[j]) i++,j++; else j=nextt[j]; if(j==lenb) return i-j+1; } return -1;}int main(){ int T; //freopen("data/1711.txt","r",stdin); cin>>T; while(T--) { cin>>lena>>lenb; for(int i=0;i<lena;i++) cin>>a[i]; for(int i=0;i<lenb;i++) cin>>b[i]; memset(nextt,0,sizeof(nextt)); cout<<KMP()<<endl; } return 0;}
0 0
- HDU 1711 Number Sequence (数字KMP)
- HDU 1711 Number Sequence【数字KMP】
- HDU 1711 Number Sequence(kmp)
- HDU 1711 Number Sequence (kmp)
- hdu 1711 Number Sequence--KMP
- hdu kmp 1711 Number Sequence
- HDU 1711 Number Sequence (KMP)
- HDU 1711Number Sequence(KMP)
- Hdu 1711 KMP Number Sequence
- HDU 1711 Number Sequence KMP
- [HDU 1711]Number Sequence[kmp]
- 【KMP】hdu 1711 Number Sequence
- HDU 1711 Number Sequence(KMP)
- hdu 1711 Number Sequence(KMP)
- 【HDU】1711 Number Sequence KMP
- HDU 1711 Number Sequence (KMP)
- hdu 1711 Number Sequence(KMP)
- KMP & hdu 1711 Number Sequence
- 关于 ZJCPC2004第二题
- Java VisualVM 多线程监控分析工具
- autoload之composer分析
- 我的第一次vi经历
- 详解Java中native关键字
- HDU 1711 Number Sequence【数字KMP】
- 统一会话
- php单例模式
- hdu 1285 拓扑排序
- 服务器开发之图形验证码
- Windows程序内部运行原理
- 2016京胜杯-吃在工大
- Instance Segmentation Semantic Segmentation
- com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK META-INF/LICENSE