HDU1711 Number Sequence 题解 KMP算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711
题目大意:最基础的字符串匹配,只不过这里用整数数组代替了字符串。
给你两个数组 \(a[1..N]\) 和 \(b[1..M]\) ,找到最小的 \(K\) 使得 \(a[K] = b[1], a[K + 1] = b[2], ...... , a[K + M - 1] = b[M]\) 。
题目分析:就是最基础的KMP字符串匹配,改一下数据类型。
实现代码如下:
#include <cstdio>
#include <string>
using namespace std;
const int maxn = 1001000;
int T, n, m, nxt[maxn], cas = 1;
int s[maxn], t[maxn];
void cal_next() {
for (int i = 0, j = -1; i < m; i ++) {
while (j != -1 && t[j+1] != t[i]) j = nxt[j];
nxt[i] = (j+1 < i && t[j+1] == t[i]) ? ++j : -1;
}
}
int find_first_s_has_t_position() {
cal_next();
for (int i = 0, j = -1; i < n; i ++) {
while (j != -1 && t[j+1] != s[i]) j = nxt[j];
if (t[j+1] == s[i]) {
j ++;
if (j >= m-1) {
return i - j + 1;
}
}
}
return -1;
}
int main() {
scanf("%d", &T);
while (T--) {
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i ++) scanf("%d", s+i);
for (int i = 0; i < m; i ++) scanf("%d", t+i);
int pos = find_first_s_has_t_position();
printf("%d\n", pos);
}
return 0;
}
作者:zifeiy
HDU1711 Number Sequence 题解 KMP算法的更多相关文章
- HDU1711 Number Sequence(KMP模板题)
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu_1711: Number Sequence【KMP算法】
题目链接 此次插播点笔记 hdu中点击蓝色的"Compilation Error"可以查看自己是为什么CE的 hdu中提交的话,语言选择G++可以使用<bits/stdc++ ...
- hdu1711 Number Sequence kmp模板
题目传送门 学习博客 学习了kmp算法,理解了算法思想,但还没有到能把这个思想用语言来描述出来. #include<bits/stdc++.h> #define clr(a,b) mems ...
- Number Sequence(kmp)
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- HDU 1711:Number Sequence(KMP)
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU - 1711 A - Number Sequence(kmp
HDU - 1711 A - Number Sequence Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1 ...
- HDU 1711 Number Sequence(KMP)附带KMP的详解
题目代号:HDU 1711 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/ ...
- 1711 Number Sequence(kmp)
Number Sequence Time Limit : 10000/5000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) To ...
- HDU 1711 Number Sequence (KMP简单题)
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
随机推荐
- The content of element type must match解决方法
当我在mybatis的核心配置文件SqlMapConfig.xml中配置别名的时候,老是提示错误. 把鼠标移到上去就可以看到详细的内容 如下图所示: 问题原因: 通过错误的提示信息,原来这个xml文件 ...
- Vue Router 相关
1. 路由传参: 编程式的导航 router.push this.$router.push("home"); this.$router.push({ name: 'news', p ...
- ML面试1000题系列(41-50)
本文总结ML面试常见的问题集 转载来源:https://blog.csdn.net/v_july_v/article/details/78121924 41. #include和#include“fi ...
- Vue--过滤器(私有和公有)
一.过滤器的基本使用 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...
- System.getProperty()和getenv()
System.getproperty(String name) 获取系统属性 System.getProperties() 获取所有系统属性 System.getenv(String name) 获取 ...
- 杨柳絮-Info:春天将不再漫天飞“雪”,济源治理杨柳絮在行动
ylbtech-杨柳絮-Info:春天将不再漫天飞“雪”,济源治理杨柳絮在行动 1.返回顶部 1. 天气暖和了,连心情都是阳光的.然而,在这美好的时刻,漫天飞舞的杨柳絮,甚是煞风景.<ignor ...
- 2018-12-27-WPF-从文件创建图片的方法
title author date CreateTime categories WPF 从文件创建图片的方法 lindexi 2018-12-27 11:37:46 +0800 2018-12-27 ...
- Python 使用BeautifulSoup模块抽取数据
- word Stock Market Indices
Stock Market Indices USA Africa Asia and Pacific Canada Europe Middle East South America Internation ...
- C++中数字转换成字符串
头文件:<string> 转换函数:to_string(); 例如:int n=10; string str=to_string(n) ;