HDU 1711 Number Sequence【kmp求子串首次出现的位置】
InputThe 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].
OutputFor each test case, you should output one line which only contain K described above. If no such K exists, output -1 instead.
Sample Input
- 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
Sample Output
- 6
- -1
- #include<stdio.h>
- int a[],b[];
- int next[];
- int n,m;
- void getNext()
- {
- int j,k;
- j=;
- k=-;
- next[]=-;
- while(j<m)
- {
- if(k==-||b[j]==b[k])
- next[++j]=++k;
- else k=next[k];
- }
- }
- //返回首次出现的位置
- int KMP_Index()
- {
- int i=,j=;
- getNext();
- while(i<n && j<m)
- {
- if(j==-||a[i]==b[j])
- {
- i++;
- j++;
- }
- else j=next[j];
- }
- if(j==m) return i-m+;
- else return -;
- }
- int main()
- {
- int T;
- scanf("%d",&T);
- while(T--)
- {
- scanf("%d%d",&n,&m);
- for(int i=;i<n;i++)
- scanf("%d",&a[i]);
- for(int i=;i<m;i++)
- scanf("%d",&b[i]);
- printf("%d\n",KMP_Index());
- }
- return ;
- }
kuangbin模板
http://www.cnblogs.com/kuangbin/archive/2012/08/14/2638803.html
HDU 1711 Number Sequence【kmp求子串首次出现的位置】的更多相关文章
- HDU 1711 Number Sequence(KMP裸题,板子题,有坑点)
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 1711 Number Sequence KMP 基础题
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 1711 Number Sequence (KMP 入门)
Number Sequence Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and ...
- HDU 1711 - Number Sequence - [KMP模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Time Limit: 10000/5000 MS (Java/Others) Memory L ...
- HDU 1711 Number Sequence KMP
题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1711 AC代码: #include <iostream> #include <cs ...
- HDU 1711 Number Sequence (字符串匹配,KMP算法)
HDU 1711 Number Sequence (字符串匹配,KMP算法) Description Given two sequences of numbers : a1, a2, ...... , ...
- HDU 1711 Number Sequence(数列)
HDU 1711 Number Sequence(数列) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- HDU 1711 Number Sequence 【KMP应用 求成功匹配子串的最小下标】
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/O ...
- hdu 1711 Number Sequence(kmp找子串第一次出现的位置)
题意:裸kmp 思路:kmp模板 #include<iostream> #include<stdio.h> #include<string.h> using nam ...
随机推荐
- 【题解】[USACO12JAN]视频游戏的连击Video Game Combos
好久没有写博客了,好惭愧啊……虽然这是一道弱题但还是写一下吧. 这道题目的思路应该说是很容易形成:字符串+最大值?自然联想到学过的AC自动机与DP.对于给定的字符串建立出AC自动机,dp状态dp[i] ...
- [bzoj2901]矩阵求和
题目大意:给出两个$n\times n$的矩阵,$m$次询问它们的积中给定子矩阵的数值和. 题解:令为$P\times Q=R$ $$\begin{align*}&\sum\limits_{i ...
- 【BZOJ 1407】[Noi2002]Savage ExGCD
我bitset+二分未遂后就来用ExGCD了,然而这道题的时间复杂度还真是玄学...... 我们枚举m然后对每一对用ExGCD判解,我们只要满足在最小的一方死亡之前无解就可以了,对于怎么用,就是ax+ ...
- 【NOIP 模拟赛】区间第K大(kth) 乱搞
biubiu~~~ 这道题就是预处理,我们就是枚举每一个数,找到左边比他大的数的个数以及其对应的区间,右边也如此,我们把左边的和右边的相乘就得到了我们的答案,我们发现这是O(n^3)的,但是实际证明他 ...
- jQuery源码分析笔记
jquery-2.0.3.js版本源码分析 (function(){ (21,94) 定义了一些变量和函数 jQuery = function(){}; (96,283) 给JQ对象,添加一些方法 ...
- [uva 1350]数位dp+二分
题目链接:https://vjudge.net/problem/38405 #include<bits/stdc++.h> using namespace std; ][]; ]; lon ...
- 安卓topbar编码实战
1.先在res->value下新建attrs.xml文件 <?xml version="1.0" encoding="utf-8"?> < ...
- JRE集成到Tomcat
将jdk集成到tomcat里面(不用客户安装JRE) 或者 tomcat使用指定的jdk_ 给客户安装软件的时候,也许客户不想你在人家机器的环境变量里设置来设置去,那么就要在tomcat里指定要使用的 ...
- 转:Spring AOP 注解方式实现的一些“坑”
使用过Spring AOP的注解方式实现,也入过不少坑,现在做一下记录,希望有同样需求的朋友可以少走弯路 使用之前还是先过一下官方文档吧,至少可以少走弯路,不用担心英文读不懂,其实只看代码例子就能理解 ...
- 按小时或天切割Nginx日志
#按小时或天切割Nginx日志到备份文件夹 LOGS_PATH=/home/www/logs/thc SAVE_PATH=/home/www/logs/thc YESTERDAY=$(date -d ...