Problem Description
Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= M <= 10000, 1 <= N <= 1000000). Your task is to find a number K which make a[K] = b[1], a[K + 1] = b[2], ...... , a[K + M - 1] = b[M]. If there are more than one K exist, output the smallest one.
 
Input
The first line of input is a number T which indicate the number of cases. Each case contains three lines. The first line is two numbers N and M (1 <= M <= 10000, 1 <= N <= 1000000). The second line contains N integers which indicate a[1], a[2], ...... , a[N]. The third line contains M integers which indicate b[1], b[2], ...... , b[M]. All integers are in the range of [-1000000, 1000000].
 
Output
For each test case, you should output one line which only contain K described above. If no such K exists, output -1 instead.
 
Sample Input
2
13 5
1 2 1 2 3 1 2 3 1 3 2 1 2
1 2 3 1 3
13 5
1 2 1 2 3 1 2 3 1 3 2 1 2
1 2 3 2 1
 
Sample Output
6
-1
 
第一道kmp  是一道模板题
不过还是wa了两发
1.不能开next数组   和系统自带名字重名
2.一开始我是把一个个数据转化成一条字符串来进行kmp    但是显然是错的QWQ
改成数组形式就行了
 
#include<bits/stdc++.h>
using namespace std;
//input
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i>=(b);i--)
#define RI(n) scanf("%d",&(n))
#define RII(n,m) scanf("%d%d",&n,&m);
#define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define RS(s) scanf("%s",s);
#define ll long long
#define inf 0x3f3f3f3f
#define REP(i,N) for(int i=0;i<(N);i++)
#define CLR(A,v) memset(A,v,sizeof A)
//////////////////////////////////
int nex[+];
int lenp,lens;
int p[+];//可以是char 也可以是string
int s[+];
void getnext()
{
nex[]=-;
int k=-,j=;
while(j<lenp-)
{
if(k==-||p[j]==p[k])
nex[++j]=++k;
else k=nex[k];
}
}
int kmp()
{
//lens=
//lenp=
int j=;
int i=;
while(i<lens&&j<lenp)
{
if(s[i]==p[j]||j==-)
{
i++;
j++;
}
else
j=nex[j]; if(j==lenp)
{
return i-j+;
}
}
return -;
}
int main()
{
int cas;
RI(cas);
while(cas--)
{
RII(lens,lenp);
rep(i,,lens-)
RI(s[i]);
rep(i,,lenp-)
RI(p[i]); getnext();
cout<<kmp()<<endl;
}
return ;
}
 
 
 

Number Sequence kmp的更多相关文章

  1. HDU 1711 Number Sequence(KMP裸题,板子题,有坑点)

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

  2. hdu 1711 Number Sequence KMP 基础题

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

  3. HDU 1711 Number Sequence (KMP 入门)

    Number Sequence Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and ...

  4. 1711 Number Sequence(kmp)

    Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], .... ...

  5. HDU 1711 - Number Sequence - [KMP模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Time Limit: 10000/5000 MS (Java/Others) Memory L ...

  6. hdu1711 Number Sequence kmp应用

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1711 题目: Problem Description Given two sequences of n ...

  7. Number Sequence (KMP的应用)

    个人心得:朴素代码绝对超时,所以要用到KMP算法,特意了解了,还是比较抽象,要多体会 Given two sequences of numbers : a11, a22, ...... , aNN, ...

  8. Number Sequence(KMP,判断子串 模板)

    题意: 给两数组,求一个是否是另一个的子数组,若是返回匹配的首位置 分析: KMP 入门 //扫描字符串A,并更新可以匹配到B的什么位置. #include <map> #include ...

  9. HDU 1711 Number Sequence KMP

    题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1711 AC代码: #include <iostream> #include <cs ...

随机推荐

  1. [加密算法]为什么说RSA难以被破解

    RSA算法运用了数学“两个大的质数相乘,难以在短时间内将其因式分解”的这么一套看似简单事实上真的是很困难的一个数学难题...... 以前也接触过RSA加密算法,感觉这个东西太神秘了,是数学家的事,和我 ...

  2. 异步io模块

    '''客户端''' import socket,select #####进行封装,将socket对象和要返回的url,和回调函数进行一下封装 class Httprequest(): def __in ...

  3. 31)django-序列化

    目录 1)序列化 2)为什么不用json序列化 3)django序列化QuerySet,ErrorDict数据 一:序列化 序列化是将对象状态转换为可保持或传输的格式的过程 反序列化是指将存储在存储媒 ...

  4. ORACLE透明加密

    --官网文档:https://www.oracle.com/technetwork/cn/tutorials/tde-096009-zhs.html#t概述Oracle 数据库 10g 第 2 版透明 ...

  5. 手机端上点击input框软键盘出现时把input框不被覆盖,显示在屏幕中间(转)

    转载地址:https://www.cnblogs.com/xzzzys/p/7526761.html 1  用定位为题来解决var oHeight = $(document).height(); // ...

  6. vue打包项目后使用-webkit-line-clamp: 2;这个属性不生效?

    在项目中要实现多行省略,-webkit-line-clamp: 2;打包后不生效,使用下面的方法: word-break: break-all; text-overflow: ellipsis; di ...

  7. Confluence 6 通过 SSL 或 HTTPS 运行 - 创建或请求一个 SSL 证书

    在启用 HTTPS 之前,你需要一个有效的证书,如果你已经有了一个有效的证书,你可以直接跳过这个步骤,进入 step 2. 你可以创建一个自签名的证书,或者从信任的 Certificate Autho ...

  8. bitset用法详解

    参见此博客: https://www.cnblogs.com/magisk/p/8809922.html

  9. RESTful架构解读

    什么是REST REST与技术无关,代表的是一种软件架构风格.REST全称是Representational State Tranfer, 表征性状态转移. REST从资源的角度类审视整个网络,它将分 ...

  10. AI-跨域、垃圾回收、content_type组见、接口处理

    AI-跨域.垃圾回收.content_type组见.接口处理 跨域 为什么有跨域?什么时候遇见的?答:由于浏览器的同源策略 阻止ajax请求 不阻止src请求:在测试时,项目上线后不会遇见跨域.源:协 ...