HDU 1711 Number Sequence【KMP】【模板题】【水题】(返回匹配到的第一个字母的位置)
来源:互联网 发布:税收征管数据质量方案 编辑:程序博客网 时间:2024/06/05 05:39
Number Sequence
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 29634 Accepted Submission(s): 12464
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
返回匹配到的第一个字母的位置#include <iostream>#include <cmath>#include <algorithm>#include <cstdio>#include <stdlib.h>#include <string>#include <cstring>#include <map>#include <set>#include <queue>#include <stack>#define INF 0x3f3f3f3f#define ms(x,y) memset(x,y,sizeof(x))using namespace std;typedef long long ll;const double pi = acos(-1.0);const int mod = 1e9 + 7;const int maxn = 1e5 + 10;int nextval[1000010];int s[1000010], p[10010];int slen, plen;//p为模式串void getnext(int p[], int nextval[]) //朴素kmp,nextval[i]即为1~i-1的最长前后缀长度{ int len = plen; int i = 0, j = -1; nextval[0] = -1; while (i < len) { if (j == -1 || p[i] == p[j]) { nextval[++i] = ++j; } else j = nextval[j]; }}//在s中找p出现的位置int KMP(int s[], int p[], int nextval[]){ getnext(p, nextval); int ans = 0; int i = 0; //s下标 int j = 0; //p下标 int s_len = slen; int p_len = plen; while (i < s_len && j < p_len) { if (j == -1 || s[i] == p[j]) { i++; j++; } else j = nextval[j]; if (j == p_len) { return i - j + 1; } } return -1;}int main(){ //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int t; scanf("%d", &t); while (t--) { scanf("%d%d", &slen, &plen); for (int i = 0; i < slen; i++) scanf(" %d", &s[i]); for (int i = 0; i < plen ; i++) scanf(" %d", &p[i]); printf("%d\n", KMP(s, p, nextval)); } return 0;}
阅读全文
0 0
- 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
- hdu 1711 Number Sequence KMP模板题~~~
- hdu 1711 Number Sequence KMP模板题
- hdu 1711 Number Sequence(kmp模板题)
- hdu 1711Number Sequence(kmp模板题)
- 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 1711 Number Sequence(KMP模板)
- KMP算法的定义及KMP练手题 HDU 1711 Number Sequence (我的模板代码)
- hdu 1711 Number Sequence(KMP模板题)
- hdu 1711 Number Sequence(KMP入门模板题)
- 如何防止ISE综合时信号不被优化掉
- 年中总结
- Neo4j CQL -(4)- MATCH & RETURN匹配和返回
- shell 脚本替换文件中某个字符串
- vc2010创建dll和使用
- HDU 1711 Number Sequence【KMP】【模板题】【水题】(返回匹配到的第一个字母的位置)
- 数据库
- 【JVM】——垃圾回收机制
- BootStrap
- ios重签名教程
- 《西瓜书》笔记04:决策树
- python爬虫-Requests库
- Map集合HashMap,TreeMap
- Spring源码之AOP