HDU 1711 Number Sequence (KMP)
白书说这个是MP,没有对f 数组优化过,所以说KMP有点不准确
#include <stdio.h>
int a,b;
int T[1000010],P[10010];//从0开始存
int f[10010];//记录P的自我匹配
void getFail(){
int m=b;
f[0]=f[1]=0;
for(int i=1;i<m;i++){
int j=f[i];
while(j&&P[i]!=P[j])j=f[j];
f[i+1]= P[i]==P[j] ? j+1 : 0;
}
} bool find(){
int len1=a,len2=b;
getFail();
int j=0;
for(int i=0;i<a;i++)
{
while(j&&P[j]!=T[i])j=f[j];
if(P[j]==T[i])j++;
if(j==b){printf("%d\n",i-b+2);return true;}
}
return false;
} int main(){
int cas,i;scanf("%d",&cas);
while(cas--){ scanf("%d%d",&a,&b);
for(i=0;i<a;i++)scanf("%d",&T[i]);
for(i=0;i<b;i++)scanf("%d",&P[i]);
bool ans=find();
if(ans==false)printf("-1\n");
}
return 0;
}
HDU 1711 Number Sequence (KMP)的更多相关文章
- 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)
Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], .... ...
- 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简单题)
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 1711 Number Sequence(kmp)
Number Sequence Time Limit : 10000/5000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) To ...
- HDU 1711 Number Sequence(KMP模板)
http://acm.hdu.edu.cn/showproblem.php?pid=1711 这道题就是一个KMP模板. #include<iostream> #include<cs ...
- hdu 1711 Number Sequence(kmp找子串第一次出现的位置)
题意:裸kmp 思路:kmp模板 #include<iostream> #include<stdio.h> #include<string.h> using nam ...
- hdu 1711 Number Sequence(KMP模板题)
我的第一道KMP. 把两个数列分别当成KMP算法中的模式串和目标串,这道题就变成了一个KMP算法模板题. #include<stdio.h> #include<string.h> ...
- HDU 1711 Number Sequence(KMP匹配数字串)
这个就是kmp的数组形式,不用来处理字符串还真有点不习惯呢... #include<iostream> using namespace std; ,MAXM = ; int T[MAXN] ...
随机推荐
- 小甲鱼python视频第七讲(课后习题)
1.assert的作用. assert用来判断语句的真假,如果为假的话将触发AssertionError错误. 如果为真则继续执行. 2.变量互换(注意顺序) 3.成员资格运算符(in) 4.分数的划 ...
- C++通过模板实现多态
#include <iostream> using namespace std; template<typename T> class Phone { public: void ...
- Java复数的四则运算
import java.util.Scanner; import com.sun.jndi.url.iiopname.iiopnameURLContextFactory; public cla ...
- windows下关闭80端口被system占用的情况
用管理员运行cmd然后用net stop http 停止pid 为4的进程
- EditText 焦点
让EditText不自动获取焦点,只需要在EditText的父控件加上 android:focusable="true"android:focusableInTouchMode=& ...
- 在CentOS安装cobbler自动化部署软件
#!/bin/bash##cobbler server addressip=192.168.119.133#DHCP server net and address fanweinet=192.168. ...
- android中掩码的使用
掩码是一串二进制代码对目标字段进行位与运算,屏蔽当前的输入位,所以有时又称为屏蔽码. 在Android中常使用这种技巧设置flag来判断标记,具体实现可参考framework层的WindowManag ...
- 5.11-5.15javascript制作动态表格
制作动态表格的主要是运用js中表格类的insertRow.insertCell简易添加行和列的代码,不过要注意每行添加的表格是有位置行编号的,每行的编号为rows.length-1,增加的表格内的标签 ...
- JS中window.showModalDialog()详解
window.showModalDialog()方法用来创建一个显示HTML内容的模态对话框. window.showModelessDialog()方法用来创建一个显示HTML内容的非模态对话框. ...
- 《Automatic Face Classification of Cushing’s Syndrome in Women – A Novel Screening Approach》学习笔记
<针对女性库欣综合征患者的自动面部分类-一种新颖的筛查方法> Abstract 目的:库兴氏综合征对身体造成相当大的伤害如果不及时治疗,还经常是诊断的时间太长.在这项研究中,我们旨在测试面 ...