AC日记——Number Sequence hdu 1711
Number Sequence
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 24928 Accepted Submission(s):
10551
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.
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].
only contain K described above. If no such K exists, output -1
instead.
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
-1
- #include<iostream>
- #include<cstring>
- #include<cstdlib>
- #include<cstdio>
- using namespace std;
- const int N = ;
- int nest[],n,m;
- int s[],t[];
- int slen,tlen;
- void getnest()
- {
- memset(nest,,sizeof(nest));
- int j,k;
- j=;k=-;nest[]=-;
- while(j<tlen)
- {
- if(k==-||t[j]==t[k])
- nest[++j]=++k;
- else k=nest[k];
- }
- }
- int kmp_index()
- {
- int i=;int j=;
- getnest();
- while(i<slen&&j<tlen)
- {
- if(j==-||s[i]==t[j])
- {
- i++;j++;
- }
- else j=nest[j];
- }
- if(j==tlen) return i-tlen+;
- else return -;
- }
- int kmp_count()
- {
- int ans=;
- int i,j=;
- if(slen==&&tlen==)
- {
- if(s[]==t[]) return ;
- else return ;
- }
- getnest();
- for(i=;i<slen;i++)
- {
- while(j>&&s[i]!=t[j])
- {
- j=nest[j];
- }
- if(s[i]==t[j]) j++;
- if(j==tlen)
- {
- ans++;
- j=nest[j];
- }
- }
- return ans;
- }
- int main()
- {
- int TT;
- int i,cc;
- cin>>TT;
- while(TT--)
- {
- cin>>n>>m;
- for(int i=;i<n;i++)
- cin>>s[i];
- for(int j=;j<m;j++)
- cin>>t[j];
- tlen=m,slen=n;
- cout<<kmp_index()<<endl;
- }
- return ;
- }
AC日记——Number Sequence hdu 1711的更多相关文章
- (KMP 模板)Number Sequence -- Hdu -- 1711
http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/Other ...
- Number Sequence HDU 1711(KMP)
http://acm.hdu.edu.cn/showproblem.php?pid=1711 首次接触KMP,自己都不是特别理解.在网上百度看了好几个帖子之后,对KMP也有了初步的理解. #inclu ...
- Number Sequence - HDU 1711(KMP模板题)
题意:给你一个a串和一个b串,问b串是否是a串的子串,如果是返回b在a中最早出现的位置,否则输出-1 分析:应该是最简单的模板题了吧..... 代码如下: ==================== ...
- Number Sequence HDU 1711 KMP 模板
题目大意:两个数组匹配,求子串首次出现的位置. 题目思路:数组长度,比较大,朴素算法的时间复杂度为 m*n超时.KMP的时间复杂度为m+n可行. #include<iostream> #i ...
- Number Sequence ----HDOJ 1711
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Number Sequence(HDU 1005 构造矩阵 )
Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- Number Sequence HDU - 5014
There is a special number sequence which has n+1 integers. For each number in sequence, we have two ...
- AC日记——Keywords Search hdu 2222
2222 思路: ac自动机模板题: 代码: #include <cstdio> #include <cstring> #include <iostream> #i ...
- AC日记——Super Mario hdu 4417
Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
随机推荐
- JDBC操作数据库的详细步骤
1.注册驱动 告知JVM使用的是哪一个数据库的驱动 2.创建连接 使用JDBC中的类,完成对MySQL数据库的连接 3. 得到执行sql语句的Statement对象 通过连接对象获取对SQL语句的执行 ...
- pycharm安装 suds模块报错:AttributeError: module 'pip' has no attribute 'main'
需求:安装suds模块 遇到的问题: 一.报错信息:[file][Default Settint]---Project Interpreter 点击 搜索suds安装模块报错 解决:依据上图提示找到C ...
- centos7.4系统部署nodejs前端项目
1.安装nodejs运行环境 wget命令下载Node.js安装包,该安装包是编译好的文件,解压之后,在bin文件夹中就已存在node和npm,无需重复编译 wget https://nodejs.o ...
- 面向对象之元类(metaclass)
一.前言: 要搞懂元类必须要搞清楚下面几件事: 类创建的时候,内部过程是什么样的,也就是我们定义类class 类名()的过程底层都干了些啥 类的调用即类的实例化过程的了解与分析 我们已经知道元类存在的 ...
- 欧拉函数:HDU1787-GCD Again(欧拉函数的模板)
GCD Again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- ACM Changchun 2015 J. Chip Factory
John is a manager of a CPU chip factory, the factory produces lots of chips everyday. To manage larg ...
- python基础学习笔记——类的约束
⾸先, 你要清楚. 约束是对类的约束. 用一个例子说话: 公司让小明给他们的网站完善一个支付功能,小明写了两个类,如下: class QQpay: def pay(self,money): print ...
- fortran子程序传入可变数组要在module里实现
坑死我了,我说怎么子程序传递不了可变数组 在写fortran程序的时候,要对矩阵实现特定的功能,如高斯法解线性方程组,很多时候子程序不知道矩阵的大小,如有限元程序中先要用程序得到总体刚度矩阵再把总刚传 ...
- java静态代理模式
代理模式分为动态代理和静态代理. 静态代理简述: 1.为其他对象提供一种代理,以控制对这个对象的访问. 2.代理对象会起到中介的作用,可以增加些功能,也可以去掉某些功能. 静态代理: 代理和被代理对象 ...
- FCKeditor自定义编辑区CSS样式
在网站后台使用FCKeditor编辑器的时候,见到的效果可能并不完全是”所见即所得”的,因为如果在FCKeditor编辑区中使用了前台样式表中的样式,在编辑区中并不能把这些样式显示出来.解决这个问题的 ...