1711 Number Sequence(kmp)
Number Sequence
Time Limit : 10000/5000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 90 Accepted Submission(s) : 57
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
Input
Output
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
Source
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
using namespace std;
int a[];
int b[];
int nxt[];
int n, m;
int kmp()
{
int i, j;
j = ;
for (i = ; i <= n; i++)
{
while (j&&b[j + ] != a[i]) j = nxt[j];
if (a[i] == b[j+])
{
j++;
}
if (j == m)
{
return i - m + ;
}
}
return -;
}
int main()
{
int t;
cin >> t;
while (t--)
{
cin >> n >> m;
int i, j;
for (i = ; i <= n; i++)
{
cin >> a[i];
}
for (i = ; i <= m; i++)
{
cin >> b[i];
}
j = ;
for (i = ; i <= m; i++)
{
while (j&&b[j + ] != b[i]) j = nxt[j];
if (b[j + ] == b[i]) j++;
nxt[i] = j;
}
int f = kmp();
cout << f << endl;
}
return ;
}
#include <iostream>#include <algorithm>#include <cstring>#include <string>using namespace std;int a[1000005];int b[10005];int nxt[10005];int n, m;int kmp(){int i, j;j = 0;for (i = 1; i <= n; i++){while (j&&b[j + 1] != a[i]) j = nxt[j];if (a[i] == b[j+1]){j++;}if (j == m){return i - m + 1;}}return -1;}int main(){int t;cin >> t;while (t--){cin >> n >> m;int i, j;for (i = 1; i <= n; i++){cin >> a[i];}for (i = 1; i <= m; i++){cin >> b[i];}j = 0;for (i = 2; i <= m; i++){while (j&&b[j + 1] != b[i]) j = nxt[j];if (b[j + 1] == b[i]) j++;nxt[i] = j;}int f = kmp();cout << f << endl;}return 0;}
1711 Number Sequence(kmp)的更多相关文章
- HDU 1711 Number Sequence(KMP)附带KMP的详解
题目代号:HDU 1711 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/ ...
- HDU 1711 Number Sequence(kmp)
Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], .... ...
- HDU 1711 Number Sequence (KMP)
白书说这个是MP,没有对f 数组优化过,所以说KMP有点不准确 #include <stdio.h> int a,b; int T[1000010],P[10010];//从0开始存 in ...
- 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简单题)
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) ...
- Number Sequence(kmp)
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- hdu 1711 Number Sequence(kmp找子串第一次出现的位置)
题意:裸kmp 思路:kmp模板 #include<iostream> #include<stdio.h> #include<string.h> using nam ...
- HDU 1711 Number Sequence(KMP模板)
http://acm.hdu.edu.cn/showproblem.php?pid=1711 这道题就是一个KMP模板. #include<iostream> #include<cs ...
随机推荐
- 前端工程化 - bower
什么是bower bower是前端的包管理工具,类似于php的composer,python的pip,虽然先如今bower已经停止了更新(主要是因为Browserify和Webpack等包管理工具的兴 ...
- requestWindowFeature()的应用
android开发中经常会在setContentView(R.layout.XXX); 前设置requestWindowFeature(XXXX). 他的意思是需要软件全屏显示.自定义标题(使用按钮等 ...
- /etc/apt/sources.list.d/ros-latest.list' permission denied
换为英文的' sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu trusty main" > /etc/apt ...
- python 判断列表字符串元素首尾字符是否相同
def match_words(words): ctr = for word in words: and word[] == word[-]: ctr += return ctr print(matc ...
- [转]Cryengine渲染引擎剖析
转篇Napoleon314 大牛的分析,排版好乱,见谅,耐心读,这是个好东西,注意看他自己的实现,是个技术狂人啊,Ogre焕发次时代的光芒啊~~~努力 ------------------------ ...
- Java回顾之多线程同步
在这篇文章里,我们关注线程同步的话题.这是比多线程更复杂,稍不留意,我们就会“掉到坑里”,而且和单线程程序不同,多线程的错误是否每次都出现,也是不固定的,这给调试也带来了很大的挑战. 在这篇文章里,我 ...
- 雷林鹏分享:Ruby XML, XSLT 和 XPath 教程
Ruby XML, XSLT 和 XPath 教程 什么是 XML ? XML 指可扩展标记语言(eXtensible Markup Language). 可扩展标记语言,标准通用标记语言的子集,一种 ...
- LeetCode 48
这种方法首先对原数组取其转置矩阵,然后把每行的数字翻转可得到结果,如下所示(其中蓝色数字表示翻转轴): 1 2 3 1 4 7 7 4 1 4 5 6 --> 2 5 ...
- HTML 5之meta标签viewport应用
关于viewport的概念: 先了解移动设备的屏幕尺寸和设备尺寸: iPhone3 设备尺寸 320*480 ; 屏幕尺寸 320*480 iPhone4 设备尺寸 320*480 ; 屏幕尺寸 ...
- IE6开发调试插件:IE Developer Toolbar
下载地址:http://www.microsoft.com/en-us/download/details.aspx?id=18359 1.下载后点击安装 2.安装后重启IE6