题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711

题意就是求b数组在a数组出现的位置;就是kmp模板;

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std; const int N = 1e6+; int a[N], b[N], Next[N], n, m; void GetNext()
{
int i=, j=-;
Next[] = -;
while(i<n)
{
if(j==- || b[i] == b[j])
Next[++i] = ++j;
else
j=Next[j];
}
}
int kmp()
{
int i=, j=;
while(i<m)
{
if(j==- || a[i] == b[j])
{
i++;j++;
}
else
j=Next[j];
if(j==n)///如果j==n就返回下标;
return i-n+;
}
return -;
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &m, &n);
for(int i=; i<m; i++)
scanf("%d", &a[i]);
for(int i=; i<n; i++)
scanf("%d", &b[i]);
GetNext();
int ans = kmp();
printf("%d\n", ans);
}
return ;
}

Number Sequence---hdu1711(kmp)的更多相关文章

  1. 【hdu 5918】Sequence I(KMP)

    给定两个数字序列,求a序列中每隔p个构成的p+1个序列中共能匹配多少个b序列. 例如1 1 2 2 3 3 每隔1个的序列有两个1 2 3 kmp,匹配时每次主串往前p个,枚举1到p为起点. 题目 # ...

  2. hdu-1711(kmp)

    题意:给你两串数字,问你第二串数字第一次出现在第一串数字的位置,没有输出-1: 解题思路:其是就是字符串匹配,就是这里是数字匹配,把char数组改成int型就可以了: 代码: #include< ...

  3. 【LeetCode】306. Additive Number 解题报告(Python)

    [LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...

  4. poj2406 Power Strings(kmp)

    poj2406 Power Strings(kmp) 给出一个字符串,问这个字符串是一个字符串重复几次.要求最大化重复次数. 若当前字符串为S,用kmp匹配'\0'+S和S即可. #include & ...

  5. HDU 1711 Number Sequence(KMP)附带KMP的详解

    题目代号:HDU 1711 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/ ...

  6. Number Sequence(kmp)

        Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  7. 1711 Number Sequence(kmp)

    Number Sequence Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) To ...

  8. HDU 1711:Number Sequence(KMP)

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  9. hdoj--1171--Number Sequence(KMP)

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  10. URAL 1732 Ministry of Truth(KMP)

    Description In whiteblack on blackwhite is written the utterance that has been censored by the Minis ...

随机推荐

  1. cocos2dx场景切换的坑

    有一个类可以使用不同的数据源,每个数据源对应一个对象. 我在类里保存了对象的实例,由于要在其它地方使用所以做成了静态,并在每次初始化时 重新设置,析构时删除. 现在我打开了A,切换到B,结果这个静态的 ...

  2. 存档格式选择--JSON

    游戏里存档可以直接用lua,但是lua需要有一定编程基础:另外可以用ini,不过ini又太简单了,复杂的 格式无法用ini描述:还可以用xml,它的表达能力非常丰富,甚至有限数据库都用xml来作存储结 ...

  3. 简单实现Spring中BeanFactory原理

    上一篇文章介绍了Java反射机制在Spring IOC中的应用,知道了BeanFactory底层的实现原理. 原理搞懂了,对Spring IOC理解起来也很容易. 先来看看Java代码获取Spring ...

  4. Tomcat Ajax跨域访问

    http://ibleave60.blog.51cto.com/2669415/1208652 http://enable-cors.org/server.html 下载cors-filter-1.7 ...

  5. per-cpu

    What is percpu data? percpu data 是内核为smp系统中不同CPU之间的数据保护方式,系统为每个CPU维护一段私有的空间,在这段空间中的数据只有这个CPU能访问.但是这种 ...

  6. SpringBoot配置使用jsp页面技术

    SpringBoot配置使用jsp页面技术 1.pom配置 package配置必须为war类型 添加依赖 <packaging>war</packaging> <depe ...

  7. cs108 03 ( 调试, java通用性)

    Debuger Great questions These questions will solve most bugs: what method shows the symptom ? what l ...

  8. C++ 运算符重载二(一元运算符重载)

    //一元运算符重载 #include<iostream> using namespace std; class Point { public: Point(int x,int y){ th ...

  9. JS关于浏览器尺寸的方法

    document.body.clientWidth BODY对象宽度.通配符未清零margin的时候,小于页面可见区域宽度document.body.clientHeight BODY对象高度.doc ...

  10. json剥离

    String json=get("http://www.weather.com.cn/data/cityinfo/101010100.html"); JSONObject json ...