HDU 1711 Number Sequence (数字KMP,变形)
题意:
在一个序列中找到一个连续的子序列,返回其开始位置。
思路:
每个数字当成1个字符,长的序列是原串,短的序列是模式串,求next数组后进行匹配。
#include <iostream>
#include <cmath>
#include <cstdio>
using namespace std;
const int N=;
int a[N];
int nextt[];
int b[];
int n, m; void get_next()
{
nextt[]=-;
int i=, j=-;
while(i<m)
{
if(j==- || b[i]==b[j])
{
nextt[i+]=++j;
++i;
}
else
j=nextt[j];
}
} int cal()
{ get_next();
/*
for(int i=0; i<=m; i++)
cout<<next[i]<<" ";
cout<<endl;*/
if(m>n) return -;
int i=, j=, cnt=;
while(i<n)
{
if(j==-||a[i]==b[j])
i++,j++;
else
j=nextt[j];
if(j==m) return i-m+;
}
return -;
} int main()
{
//freopen("input.txt", "r", stdin);
int t;
cin>>t;
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=; i<n; i++)
scanf("%d",&a[i]);
for(int i=; i<m; i++)
scanf("%d",&b[i]);
printf("%d\n",cal());
} return ;
}
AC代码
HDU 1711 Number Sequence (数字KMP,变形)的更多相关文章
- 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) ...
- HDU 1711 Number Sequence(KMP匹配数字串)
这个就是kmp的数组形式,不用来处理字符串还真有点不习惯呢... #include<iostream> using namespace std; ,MAXM = ; int T[MAXN] ...
- HDU 1711 Number Sequence(kmp)
Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], .... ...
- HDU 1711 Number Sequence(KMP模板)
http://acm.hdu.edu.cn/showproblem.php?pid=1711 这道题就是一个KMP模板. #include<iostream> #include<cs ...
- HDU 1711 Number Sequence (KMP)
白书说这个是MP,没有对f 数组优化过,所以说KMP有点不准确 #include <stdio.h> int a,b; int T[1000010],P[10010];//从0开始存 in ...
- hdu 1711 Number Sequence(KMP模板题)
我的第一道KMP. 把两个数列分别当成KMP算法中的模式串和目标串,这道题就变成了一个KMP算法模板题. #include<stdio.h> #include<string.h> ...
- HDU 1711 Number Sequence【kmp求子串首次出现的位置】
Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= ...
随机推荐
- (转)Tips for Optimizing C/C++ Code
本来要自己翻译的,随手搜索了一下,发现五天前已经有人翻译了,我就不重复发明轮子了. 转自:http://blog.csdn.net/yutianzuijin/article/details/26289 ...
- git的安装使用和代码自动部署
1.安装 http://www.cnblogs.com/sunada2005/archive/2013/06/06/3121098.html http://www.cnblogs.com/zhcncn ...
- iOSpush过后返回多级界面
有导航控制器push过后pop可以反回上一个界面,然而我们需要返回多级界面有下面两种方法 调用API - (NSArray *)popToViewController:(UIViewControlle ...
- UVA 562 Dividing coins
题目描述:给出一些不同面值的硬币,每个硬币只有一个.将这些硬币分成两堆,并且两堆硬币的面值和尽可能接近. 分析:将所有能够取到的面值数标记出来,然后选择最接近sum/2的两个面值 状态表示:d[j]表 ...
- POJ 3525 Most Distant Point from the Sea (半平面交向内推进+二分半径)
题目链接 题意 : 给你一个多边形,问你里边能够盛的下的最大的圆的半径是多少. 思路 :先二分半径r,半平面交向内推进r.模板题 #include <stdio.h> #include & ...
- UVA 1362 Exploring Pyramids 区间DP
Archaeologists have discovered a new set of hidden caves in one of the Egyptian pyramids. The decryp ...
- java如何追加写入txt文件
java中,对文件进行追加内容操作的三种方法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 import java.io.BufferedWriter; import ...
- QScrollArea可以帮助我们实现让一个widget的内容带有滚动条(QWidget里内置QScrollArea,QScrollArea里再内置其它QWidget)
使用QScrollArea可以帮助我们实现让一个widget的内容带有滚动条,用户可以通过拖动滚动条来查看更多内容, 代码示例如下: 1.带有滚动条的widget列表 #include "w ...
- TPaintBox的前世今生
TPaintBox是一个图形控件,继承于TGraphicControl,并且只有聊聊几个函数和属性,主要就是Canvas和Paint函数,都在这里了: TPaintBox = class(TGraph ...
- 286. Walls and Gates
题目: You are given a m x n 2D grid initialized with these three possible values. -1 - A wall or an ob ...