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 ...
随机推荐
- react-native 相关问题
使用create-react-native-app时,报错,好像是npm版本不对,想问下npm怎么降低版本? npm install npm@4 -g 创建并启动项目 老方法1 创建项目 react ...
- 【剑指offer】输入一个链表,输出该链表中倒数第k个结点。
一.题目: 输入一个链表,输出该链表中倒数第k个结点. 二.思路: 用两个指针p1和p2,p2先跑k步,然后p1和p2同时跑,p2跑到头p1所在的位置就是倒数第k个节点.很简单吧?简单你也想不到,想到 ...
- Git、bower 安装
1>下载并安装nodejs .老师分享的nodejs版本“node-v8.9.4-x64” 下载页面http://nodejs.cn/download/ 一直无脑下一步操作即可安装完毕 ...
- JSP表单提交与接收
JSP表单提交与接收 在Myeclipse中新建web project,在webroot中新建userRegist1.jsp,代码如下 <%@ page contentType="te ...
- PAT Product of Polynomials[一般]
1009 Product of Polynomials (25)(25 分) This time, you are supposed to find A*B where A and B are two ...
- [LeetCode] 728. Self Dividing Numbers_Easy tag: Math
A self-dividing number is a number that is divisible by every digit it contains. For example, 128 is ...
- testNG入门详解
TestNG 的注释: @DataProvider @ExpectedExceptions @Factory @Test @Parameters <suite name="Parame ...
- C#读取Excel,Access数据库
出自:http://blog.csdn.net/limpire/article/details/2599760 使用 OpenRowSet 和 OpenDataSource 访问 Excel 97-2 ...
- Shell篇(三)TC Shell
Shell脚本的首行一般写为"#!+路径"来告诉系统,以路径所指定的程序来解释此脚本. 可以写为 #! /bin/tcsh -f (-f表示快速启动,不启动~/.tcshrc) S ...
- wordpress 修改域名后的403
wordpress的好处就是方便,不好呢,额,反正就是有. 最近,修改域名,也遇到了这个问题[修改域名后,出现403]. 网上的办法似乎有很多,但有一些并不好,比如修改数据库什么的,如果是新手,数据库 ...