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.
InputThe 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].

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

  1. 2
  2. 13 5
  3. 1 2 1 2 3 1 2 3 1 3 2 1 2
  4. 1 2 3 1 3
  5. 13 5
  6. 1 2 1 2 3 1 2 3 1 3 2 1 2
  7. 1 2 3 2 1

Sample Output

  1. 6
  2. -1
  3.  
  4. 首先是预处理Next数组,初始化Next[0]=-1,j=-1 然后从i=0开始,如果j!=-1&&不匹配,j=Next[j]。否则判断p[++i]==p[++j],如果相等,直接让Next[i]=Next[j],不等的话,让Next[i]=j;
  5.  
  6. 然后在主串里匹配。i=0,j=0; j!=-1&&不匹配, j=Next[j]。否则i++,j++
  7.  
  1. #include<stdio.h>
  2. #include<string.h>
  3. int Next[],t[],p[];//模式串对应的Next
  4. int n,m,_;
  5.  
  6. void prekmp() {//预处理Next数组
  7. int i,j;
  8. j=Next[]=-;
  9. i=;
  10. while(i<m) {
  11. while(j!=-&&p[i]!=p[j]) j=Next[j];
  12. if(p[++i]==p[++j]) Next[i]=Next[j];//会快一点 博客体现了这一点
  13. else Next[i]=j;
  14. }
  15. }
  16.  
  17. int kmp() {//查找在主串的位置
  18. int i=,j=;
  19. prekmp();
  20. while(i<n&&j<m) {
  21. while(j!=-&&t[i]!=p[j]) j=Next[j];
  22. i++;j++;
  23. }
  24. if(j==m) return i-m+;
  25. else return -;
  26. }
  27.  
  28. int main() {
  29. for(scanf("%d",&_);_;_--) {
  30. scanf("%d%d",&n,&m);
  31. for(int i=;i<n;i++) {
  32. scanf("%d",&t[i]);
  33. }
  34. for(int i=;i<m;i++) {
  35. scanf("%d",&p[i]);
  36. }
  37. int ans=kmp();
  38. printf("%d\n",ans);
  39. }
  40. }
  1.  

kuangbin专题十六 KMP&&扩展KMP HDU1711 Number Sequence的更多相关文章

  1. 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 ...

  2. kuangbin专题十六 KMP&&扩展KMP HDU2328 Corporate Identity

    Beside other services, ACM helps companies to clearly state their “corporate identity”, which includ ...

  3. kuangbin专题十六 KMP&&扩展KMP HDU1238 Substrings

    You are given a number of case-sensitive strings of alphabetic characters, find the largest string X ...

  4. 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 ...

  5. kuangbin专题十六 KMP&&扩展KMP POJ3080 Blue Jeans

    The Genographic Project is a research partnership between IBM and The National Geographic Society th ...

  6. kuangbin专题十六 KMP&&扩展KMP HDU3746 Cyclic Nacklace

    CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, ...

  7. kuangbin专题十六 KMP&&扩展KMP HDU2087 剪花布条

    一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案.对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢? Input输入中含有一些数据,分别是成对出现的花布条和小 ...

  8. kuangbin专题十六 KMP&&扩展KMP HDU1686 Oulipo

    The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e ...

  9. 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 ...

随机推荐

  1. ABP系统设置

    代码分析 在abp中添加系统设置,都是在模块的预初始化方法中添加的. Configuration.Settings.Providers.Add<EmailSettingProvider>( ...

  2. 配置php的curl模块问题

    问题 checking for cURL in default path... not foundconfigure: error: Please reinstall the libcurl dist ...

  3. 计算数组arr中所有元素的和

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  4. 查看hdfs各目录分别占用多少空间

    之前在网上搜索到的全部单位好像都是byte的,看起来很麻烦,然后自己看了下 hadoop fs -help [hadoop@slave3 java]$ hadoop fs -help Usage: h ...

  5. day18-事务与连接池 5.关于不考虑事务隔离性出现的问题

  6. day35 02-Hibernate持久化对象状态及状态转换

    hibernate内置有一个c3p0,不用引入c3p0的jar包也行. 现在其实可以不用去创建表和实体类.因为hibernate可以自动帮我们生成.只要把映射建好了它就可以自动帮我们生成. 创建实体类 ...

  7. 使用Java建立聊天客户端

    ---------------siwuxie095                             关于 聊天服务器,详见本人博客的分类:来一杯Java, 里面的 使用ServerSocket ...

  8. 百度Apollo解析——2.log系统

    Apollo中的glog 在Apollo中google glog 被广泛使用,glog 是 google 的一个 c++ 开源日志系统,轻巧灵活,入门简单,而且功能也比较完善. 1. 安装 以下是官方 ...

  9. Flask框架 之 功能详解

    浏览目录 配置文件 路由系统 视图 请求相关 响应 模板渲染 session 闪现 中间件 蓝图(blueprint) 特殊装饰器 配置文件 知识点 给你一个路径 “settings.Foo”,可以找 ...

  10. POJ 2686 Traveling by Stagecoach (状压DP)

    题意:有一个人从某个城市要到另一个城市, 有n个马车票,相邻的两个城市走的话要消耗掉一个马车票.花费的时间呢,是马车票上有个速率值 ,问最后这个人花费的最短时间是多少. 析:和TSP问题差不多,dp[ ...