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) ...
随机推荐
- 【转载】【技巧总结】PyCharm怎么克隆github上开源的项目
PyCharm怎么clone github上开源的项目 一.先要确保PyCharm正确的配置了Git 如果你已经在PyCharm中配置好了Git,可以跳过此步骤,直接看下一步. 那么怎么在Py ...
- LeetCode Top 100 Liked Questions in Golang(updating...)
leetcode go语言版本,主要为了熟悉下语言 1. Two Sum 双指针版本, O(NlogN) func twoSum(nums []int, target int) []int { val ...
- SPSS分析技术:二阶聚类分析;为什么出现大学生“裸贷”业务,因为放贷者知道贷款者还不起
SPSS分析技术:二阶聚类分析:为什么出现大学生"裸贷"业务,因为放贷者知道贷款者还不起 今天将介绍一种智能聚类法,二阶聚类法,在开始介绍之前,先解答很多人在后台提出的一个疑问:那 ...
- nth-child和nth-of-type的使用案列
HTML: <div id="footer-f"> <ul class="trajectory"> <li> <div ...
- Directx教程(27) 简单的光照模型(6)
原文:Directx教程(27) 简单的光照模型(6) 从myTutorialD3D11_15到myTutorialD3D11_19的工程中,我们都只有一个光源,光源的位置在LightCla ...
- bzoj3732 Network(NOIP2013 货车运输)
Description 给你N个点的无向图 (1 <= N <= 15,000),记为:1…N. 图中有M条边 (1 <= M <= 30,000) ,第j条边的长度为: d_ ...
- Java练习 SDUT-2053_整理音乐
整理音乐 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 请用链表完成下面题目要求. xiaobai 很喜欢音乐,几年来 ...
- codeblocs的安装使用
安装后,上面菜单栏 点击“Setting --> Compiler” "Creat a new project"
- 2019-3-25-win10-uwp-如何将像素数组转-png-文件
title author date CreateTime categories win10 uwp 如何将像素数组转 png 文件 lindexi 2019-3-25 8:53:1 +0800 201 ...
- Data Flow-File Read-网络距离