HDU 1711 Number Sequence (KMP 入门)
Number Sequence
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string> using namespace std;
const int maxn = ;
int rs[][], nex[maxn], a[maxn], b[maxn/ + ], n, m;
void cal_next(int *str, int len)
{
nex[] = -; int k = -;
for (int i = ; i <= len - ; i++) {
while (k > - && str[k + ] != str[i])
k = nex[k];
if (str[k + ] == str[i])k = k + ;
nex[i] = k;
}
}
int KMP(int *a, int la, int *b, int lb)
{
cal_next(b , lb);
int k = -;
for (int i = ; i < la; i++) {
while (k > - && b[k + ] != a[i])
k = nex[k];
if (b[k + ] == a[i])k = k + ;
if (k == lb - )return i - lb + ;
}
return -;
}
int main()
{
ios::sync_with_stdio(false);
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];
int ans = KMP(a, n, b, m);
if (ans != -)cout << ans + << endl;
else cout << "-1" << endl;
}
return ;
}
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模板题]
题目链接: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的详解
题目代号:HDU 1711 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/ ...
- HDU 1711 Number Sequence (KMP简单题)
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
随机推荐
- 【JZOJ4788】【NOIP2016提高A组模拟9.17】序列
题目描述 输入 输出 样例输入 1 5 2 1 3 0 3 2 2 0 1 0 样例输出 1 数据范围 解法 考虑没有模的情况,问题就仅仅只是简单的差分问题(广告铺设): 设r[i]是第i位需要加的次 ...
- python 通过.pth文件修改搜索路径
- 跟我一起认识axure(三)
交互设置,添加链接 点击预览
- MongoDB sharding 集合不分片性能更高?
最近云上用户用户遇到一个 sharding 集群性能问题的疑惑,比较有代表性,简单分享一下 测试配置 mongos x 2.shard x 3 测试1:集合不开启分片,批量 insert 导入数据,每 ...
- 比较全面的一个PHP缓存类解析
转自:http://www.blhere.com/1164.html 一.引论 PHP,一门最近几年兴起的web设计脚本语言,由于它的强大和可伸缩性,近几年来得到长足的发展,php相比传统的asp网站 ...
- Project configuration is not up-to-date with pom.xml. Run Maven->Update Project or use Quick Fix
版权声明:本文为 testcs_dn(微wx笑) 原创文章,非商用自由转载-保持署名-注明出处.谢谢. https://blog.csdn.net/testcs_dn/article/details/ ...
- apply( )与 call( ) 的区别
JavaScript中的每一个Function对象都有一个apply()方法和一个call()方法 语法 /*apply()方法*/ function.apply(thisObj[, argArray ...
- mysql列转行 行转列
列转行 SELECT flag ,substring_index(substring_index(t.context,), ) as result FROM ( select 'aa' as flag ...
- c++ 模板和traits
#define TEST(ITEMNAME) AddItem(ITEMNAME, #ITEMNAME); template <typename T> void AddItem(T& ...
- Git 的两种忽略文件方式 gitignore 和 exclude
Git 的两种忽略文件方式 gitignore 和 exclude .gitignore 不用说了,大家都知道. 有一个 exclude 可能接触比较少. 知道这个功能后发现,用在服务器上非常方便,因 ...