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 ...
随机推荐
- noip2018 爆炸记
noip2018 爆炸记 day-4 ~ day-2 最后考了两套模拟题,题目好水啊,但是我还是爆炸了. 第一套最后一道题竟然时一道毒瘤打表?但是我看着插头DP可做啊..(然而我并不会插头DP)然后还 ...
- 【eclipse】点Clean后没反应
问题:点击Clean重新编译class后没反应 解决:
- UVa 10655 n次方之和(矩阵快速幂)
https://vjudge.net/problem/UVA-10655 题意: 输入非负整数p,q,n,求a^n+b^n的值,其中a和b满足a+b=p,ab=q. 思路: 递推式转化成矩阵的规律: ...
- 重器--biomart
biomart 重器 biomaRt工具包的作用在于它可以轻松地完成的在多个生物学数据库上繁琐地检索,获取相关数据在不同数据库间的关联.
- Linux网络编程--洪水攻击详解
洪水攻击详解 ①注解:洪水攻击(FLOOD ATTACK)是指利用计算机网络技术向目标主机发送大量无用的数据报文,使得目标主机忙于处理无用的数据报文而无法提供正常服务的网络行为. 主要原理:利用了网络 ...
- Internet Explorer 11.0.9600.16428 For Windows 7
官方消息http://blogs.msdn.com/b/ie/archive/2013/11/07/ie11-for-windows-7-globally-available-for-consumer ...
- WebLogic发布S2SH应用时提示ClassNotFoundException: org.hibernate.hql.ast.HqlToken异常
使用Spring+hibernate如下 <properties> <!--定义方言.fetch深度.是否显示sql--> <property name="hi ...
- Java方向如何准备BAT技术面试答案(汇总版)
这个主题的内容之前分三个篇幅分享过,导致网络上传播的比较分散,所以本篇做了一个汇总,同时对部分内容及答案做了修改,欢迎朋友们吐槽.转发.因为篇幅长度和时间的原因,部分答案我没有亲自总结.更多精彩内容请 ...
- java定时器无法自动注入的问题解析(原来Spring定时器可以这样注入service)
近些日子在做一个项目,在项目中需要用到spring的定时任务,但是在操作数据库的时候发现总是不能正确的进行数据的插入,经过查看才发现:是因为service层不能够通过普通的方法注入到action,所以 ...
- iOS-----使用AddressBookUI管理联系人
使用AddressBookUI管理联系人 iOS SDK为管理地址簿提供的视图控制器位于AddressBookUI框架内.总结来说,AddressBookUI框架提供了如下特殊的视图控制器. ABPe ...