hdu 1711 Number Sequence(KMP模板题)
我的第一道KMP。
把两个数列分别当成KMP算法中的模式串和目标串,这道题就变成了一个KMP算法模板题。
#include<stdio.h>
#include<string.h>
#define N 1000005
#define M 10005
int a[N],b[M];
int next[M];
int n,m;
void setNext()
{
int i,j;
i=0;
j=-1;
next[i]=j;
while(i<m)
{
if(j==-1||b[i]==b[j])
{
i++;
j++;
next[i]=j;
}
else
j=next[j];
}
return ;
}
int KMP()
{
int i,j;
i=j=0;
setNext();
while(i<n)
{
if(j==-1||a[i]==b[j])
{
i++;
j++;
if(j==m)
return i-m+1;
}
else
j=next[j];
}
return -1;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
int i;
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<m;i++)
scanf("%d",&b[i]);
int temp;
temp=KMP();
printf("%d\n",temp);
}
return 0;
}
hdu 1711 Number Sequence(KMP模板题)的更多相关文章
- 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裸题,板子题,有坑点)
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 入门)
Number Sequence Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and ...
- 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)附带KMP的详解
题目代号:HDU 1711 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/ ...
- HDU 1711 Number Sequence 【KMP应用 求成功匹配子串的最小下标】
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/O ...
随机推荐
- ucenter 通信原理个人总结
用户登陆discuz,调用on_login() , on_login ()里调用了userlogin() 验证用户信息,正确的话,调用uc_user_synlogin(),然后调用uc_api_pos ...
- CallableStatement执行存储过程
/** * 使用CablleStatement调用存储过程 * @author APPle * */ public class Demo1 { /** * 调用带有输入参数的存储过程 * CALL p ...
- CSS 3层嵌套居中布局
<html> <head> <style type="text/css"> .root{ background-color: red; widt ...
- Swift—Core Foundation框架-备
Core Foundation框架是苹果公司提供一套概念来源于Foundation框架,编程接口面向C语言风格的API.虽然在Swift中调用这种C语言风格的API比较麻烦,但是在OS X和iOS开发 ...
- JAVA构造方法,继承关系和SUPER关键字
SUPER可调用父类的构造方法,但要注意默认调用和参数调用. 同时,在继承类时,可以用SUPER调用其它非构造方法哟. class Test extends Object{ public Test() ...
- Getting Text Metrics in Firemonkey(delphiscience的博客)
Firemonkey’s abstract TCanvas class has been providing the dimensions of the bounding rectangle of s ...
- SignalR 的跨域支持
How to establish a cross-domain connection Typically if the browser loads a page from http://contoso ...
- BZOJ3314: [Usaco2013 Nov]Crowded Cows
3314: [Usaco2013 Nov]Crowded Cows Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 86 Solved: 61[Subm ...
- RSA实例破解
Description: Decode the message. You intercept the following message, which you know has been encode ...
- 写两个线程,一个对n每次加一,一个对n每次减一
public class A{ private static Integer n = 0; } public class B extends A implements Runnable ...