HDU 1711 Number Sequence(KMP模板)
http://acm.hdu.edu.cn/showproblem.php?pid=1711
这道题就是一个KMP模板。
#include<iostream>
#include<cstring>
using namespace std; const int maxn = +; int n,m; int next[maxn];
int a[maxn], b[maxn]; void get_next()
{
int i = -, j = ;
::next[] = -;
while (j < m)
{
if (i == - || b[i] == b[j])
::next[++j] = ++i;
else
i = ::next[i];
}
} int kmp()
{
int i = , j = ;
while (i < n)
{
if (j == - || a[i] == b[j])
{
i++;
j++;
}
else
j = ::next[j];
if (j == m)
return i;
}
return -;
} int main()
{
//freopen("D:\\txt.txt", "r", stdin);
int t;
cin >> t;
while (t--)
{
cin >> n >> m;
for (int i = ; i < n; i++)
cin >> a[i];
for (int i = ; i < m; i++)
cin >> b[i];
get_next();
int k=kmp();
if (k!=-) cout << k-m+ << endl;
else cout << "-1" << endl;
}
return ;
}
HDU 1711 Number Sequence(KMP模板)的更多相关文章
- 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裸题,板子题,有坑点)
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 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)附带KMP的详解
题目代号:HDU 1711 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/ ...
- HDU 1711 Number Sequence 【KMP应用 求成功匹配子串的最小下标】
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/O ...
随机推荐
- mathType插入公式编号,及对公式编号的字体进行修改。调整公式上下间距。
一:插入 公式编号. 1:首先设置公式格式.点击 mathtype>insert number >format 2:有简单格式和 高级格式: https://we ...
- [py]约瑟夫问题-循环队列
约瑟夫问题(历史战争问题) 直观理解 老外视频讲解 模拟器演示 约瑟夫问题 数学姥公众号 讲的最清楚 背景及,推倒过程讲解得很清晰,旨在提高人们对数据的兴趣 简单说下: 几个人围成一圈(循环队列), ...
- JS的二维数组
今天,记录一下JS的二位数组,并附上例题. 一.二维数组的本质:数组中的元素又是数组. 其实,我们都见过这样的二维数组,只不过没在意罢了,例如: var arr = [[1,2,4,6],[2,4,7 ...
- Git 全局设置
Git 全局设置: git config --global user.name "ASxx" git config --global user.email "123456 ...
- 怎样在div中添加图片或设置颜色
1.插入图片<div><img src="图片地址" /></div>2.图片做背景<div style="background ...
- sql 查询不存在左表的数据
select * from zyz_mgr_wlcyiduifu a left join WLCInformation b ona.wlcId=b.WLCInvestorApplyID where b ...
- 人生苦短之我用Python篇(socket编程)
socket模块 实现简单的远程连接: 客户端: import socket client = socket.socket()#声明socket类型,同时生成socke连接t对象 client.con ...
- python管道pipe,两个进程,使用管道的两端分别执行写文件动作,带锁(lock)
#coding=utf-8import multiprocessing as mp def write_file(content,lock): lock.acquire() with op ...
- IO(基础知识)
IO流类的构造方法决定是输入流还是输出流.输入流连接一个文件,它会将文件中的内容读到流里面,read方法是将流里面的内容 往外读.输出流连接一个文件,它的write方法,是将内存中的内 ...
- Secure CRT 自动记录日志log配置
SecureCRT8.0的下载地址下载地址: 链接:https://pan.baidu.com/s/1i5q09qH 密码:4pa2 配置自动log操作如下: 1.options ---> Se ...