kuangbin专题十六 KMP&&扩展KMP HDU1711 Number Sequence
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].
OutputFor 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
- 首先是预处理Next数组,初始化Next[0]=-1,j=-1 然后从i=0开始,如果j!=-1&&不匹配,j=Next[j]。否则判断p[++i]==p[++j],如果相等,直接让Next[i]=Next[j],不等的话,让Next[i]=j;
- 然后在主串里匹配。i=0,j=0; j!=-1&&不匹配, j=Next[j]。否则i++,j++
- #include<stdio.h>
- #include<string.h>
- int Next[],t[],p[];//模式串对应的Next
- int n,m,_;
- void prekmp() {//预处理Next数组
- int i,j;
- j=Next[]=-;
- i=;
- while(i<m) {
- while(j!=-&&p[i]!=p[j]) j=Next[j];
- if(p[++i]==p[++j]) Next[i]=Next[j];//会快一点 博客体现了这一点
- else Next[i]=j;
- }
- }
- int kmp() {//查找在主串的位置
- int i=,j=;
- prekmp();
- while(i<n&&j<m) {
- while(j!=-&&t[i]!=p[j]) j=Next[j];
- i++;j++;
- }
- if(j==m) return i-m+;
- else return -;
- }
- int main() {
- for(scanf("%d",&_);_;_--) {
- scanf("%d%d",&n,&m);
- for(int i=;i<n;i++) {
- scanf("%d",&t[i]);
- }
- for(int i=;i<m;i++) {
- scanf("%d",&p[i]);
- }
- int ans=kmp();
- printf("%d\n",ans);
- }
- }
kuangbin专题十六 KMP&&扩展KMP HDU1711 Number Sequence的更多相关文章
- kuangbin专题十六 KMP&&扩展KMP HDU2609 How many (最小字符串表示法)
Give you n ( n < 10000) necklaces ,the length of necklace will not large than 100,tell me How man ...
- kuangbin专题十六 KMP&&扩展KMP HDU2328 Corporate Identity
Beside other services, ACM helps companies to clearly state their “corporate identity”, which includ ...
- kuangbin专题十六 KMP&&扩展KMP HDU1238 Substrings
You are given a number of case-sensitive strings of alphabetic characters, find the largest string X ...
- kuangbin专题十六 KMP&&扩展KMP HDU3336 Count the string
It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...
- kuangbin专题十六 KMP&&扩展KMP POJ3080 Blue Jeans
The Genographic Project is a research partnership between IBM and The National Geographic Society th ...
- kuangbin专题十六 KMP&&扩展KMP HDU3746 Cyclic Nacklace
CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, ...
- kuangbin专题十六 KMP&&扩展KMP HDU2087 剪花布条
一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案.对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢? Input输入中含有一些数据,分别是成对出现的花布条和小 ...
- kuangbin专题十六 KMP&&扩展KMP HDU1686 Oulipo
The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e ...
- kuangbin专题十六 KMP&&扩展KMP HDU3613 Best Reward(前缀和+manacher or ekmp)
After an uphill battle, General Li won a great victory. Now the head of state decide to reward him w ...
随机推荐
- ABP系统设置
代码分析 在abp中添加系统设置,都是在模块的预初始化方法中添加的. Configuration.Settings.Providers.Add<EmailSettingProvider>( ...
- 配置php的curl模块问题
问题 checking for cURL in default path... not foundconfigure: error: Please reinstall the libcurl dist ...
- 计算数组arr中所有元素的和
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- 查看hdfs各目录分别占用多少空间
之前在网上搜索到的全部单位好像都是byte的,看起来很麻烦,然后自己看了下 hadoop fs -help [hadoop@slave3 java]$ hadoop fs -help Usage: h ...
- day18-事务与连接池 5.关于不考虑事务隔离性出现的问题
- day35 02-Hibernate持久化对象状态及状态转换
hibernate内置有一个c3p0,不用引入c3p0的jar包也行. 现在其实可以不用去创建表和实体类.因为hibernate可以自动帮我们生成.只要把映射建好了它就可以自动帮我们生成. 创建实体类 ...
- 使用Java建立聊天客户端
---------------siwuxie095 关于 聊天服务器,详见本人博客的分类:来一杯Java, 里面的 使用ServerSocket ...
- 百度Apollo解析——2.log系统
Apollo中的glog 在Apollo中google glog 被广泛使用,glog 是 google 的一个 c++ 开源日志系统,轻巧灵活,入门简单,而且功能也比较完善. 1. 安装 以下是官方 ...
- Flask框架 之 功能详解
浏览目录 配置文件 路由系统 视图 请求相关 响应 模板渲染 session 闪现 中间件 蓝图(blueprint) 特殊装饰器 配置文件 知识点 给你一个路径 “settings.Foo”,可以找 ...
- POJ 2686 Traveling by Stagecoach (状压DP)
题意:有一个人从某个城市要到另一个城市, 有n个马车票,相邻的两个城市走的话要消耗掉一个马车票.花费的时间呢,是马车票上有个速率值 ,问最后这个人花费的最短时间是多少. 析:和TSP问题差不多,dp[ ...