原题链接

注意:2号和3号get_next()函数中next[i]赋值时的区别,一个是0,一个是1,且不能互换

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<iostream>
  4. using namespace std;
  5. const int maxn=1e5+;
  6. char ch[*maxn];
  7. char s[maxn],t[maxn];
  8. int T,next[*maxn];
  9. /*1.
  10. void get_next(char *s)
  11. {
  12. next[1]=0;
  13. //printf("%d\n",next[1]);
  14. int i=1,j=0;
  15. int slen=strlen(s);
  16. while(i<slen){
  17. if(j==0||s[i-1]==s[j-1]){
  18. i++,j++;
  19. next[i]=j;
  20. //printf("%d\n",next[i]);
  21. }
  22. else j=next[j];
  23. }
  24. }严蔚敏数据结构P83页代码
  25. */
  26. void get_next(char *s)
  27. {
  28. int slen=strlen(s);
  29. next[]=;
  30. for(int i=;i<slen;i++){
  31. int j=next[i];
  32. while(j&&s[i]!=s[j]) j=next[j];
  33. next[i+]=(s[i]==s[j])?j+:;
  34. }
  35. }
  36. /*3.
  37. void get_next(char *s)
  38. {
  39. next[0]=0,next[1]=0;
  40. //printf("%d\n",next[1]);
  41. int slen=strlen(s);
  42. for(int i=2;i<=slen;i++){
  43. int j=next[i-1];
  44. while(j&&s[i-2]!=s[j-1]) j=next[j];
  45. next[i]=(s[i-2]==s[j-1])?j+1:1;
  46. //printf("%d\n",next[i]);
  47. }
  48. }按照做数据结构笔试题的步骤一步一步推导
  49. */
  50. int main()
  51. {
  52. scanf("%d",&T);
  53. while(T--){
  54. cin>>s>>t;
  55. int slen=strlen(s);
  56. int tlen=strlen(t);
  57. for(int i=;i<tlen;i++) ch[i]=t[i];
  58. ch[tlen]='#';
  59. for(int i=;i<slen+tlen+;i++) ch[i+tlen+]=s[i];
  60. int now=slen+tlen+;
  61. ch[now]=;
  62. int ans=;
  63. memset(next,,sizeof(next));
  64. get_next(ch);
  65. while(next[now]>){
  66. ans++;
  67. now=next[now];
  68. }
  69. printf("%d\n",ans);
  70. }
  71. }

A题:Common Substrings(KMP应用)的更多相关文章

  1. dutacm.club_1087_Common Substrings_(KMP)_(结合此题通俗理解kmp的next数组)

    1087: Common Substrings Time Limit:3000/1000 MS (Java/Others)   Memory Limit:163840/131072 KB (Java/ ...

  2. poj 3415 Common Substrings(后缀数组+单调栈)

    http://poj.org/problem?id=3415 Common Substrings Time Limit: 5000MS   Memory Limit: 65536K Total Sub ...

  3. 【POJ3415】 Common Substrings(后缀数组|SAM)

    Common Substrings Description A substring of a string T is defined as: T(i, k)=TiTi+1...Ti+k-1, 1≤i≤ ...

  4. 字符串(后缀数组):POJ 3415 Common Substrings

    Common Substrings   Description A substring of a string T is defined as: T(i, k)=TiTi+1...Ti+k-1, 1≤ ...

  5. 1087: Common Substrings (哈希)

    1087: Common Substrings Time Limit:3000/1000 MS (Java/Others)   Memory Limit:163840/131072 KB (Java/ ...

  6. POJ3415 Common Substrings —— 后缀数组 + 单调栈 公共子串个数

    题目链接:https://vjudge.net/problem/POJ-3415 Common Substrings Time Limit: 5000MS   Memory Limit: 65536K ...

  7. Common Substrings POJ - 3415 (后缀自动机)

    Common Substrings \[ Time Limit: 5000 ms\quad Memory Limit: 65536 kB \] 题意 给出两个字符串,要求两个字符串公共子串长度不小于 ...

  8. POJ 3415 Common Substrings(后缀数组 + 单调栈)题解

    题意: 给两个串\(A.B\),问你长度\(>=k\)的有几对公共子串 思路: 先想一个朴素算法: 把\(B\)接在\(A\)后面,然后去跑后缀数组,得到\(height\)数组,那么直接\(r ...

  9. hdu 1238 Substrings(kmp+暴力枚举)

    Problem Description You are given a number of case-sensitive strings of alphabetic characters, find ...

随机推荐

  1. python2.0_s12_day9_mysql操作

    mysql的基本语法: 1.数据库操作 show databases; create database 数据库名;如果想允许数据库可以写中文create database 数据库名 charset u ...

  2. Unity版本与虚拟现实头盔Deepoon大朋版本测试

    一.看这里 Unity官方与OC runtime版本兼容性说明: https://developer.oculus.com/documentation/game-engines/latest/conc ...

  3. 用示例详解php连接数据库操作

    首先数据库mydb有三个表: 1  info表 2  users表 3  sname表 首先先做一个登录主页面 login_1.php <!DOCTYPE html PUBLIC "- ...

  4. Vue基础---->vue-router的使用(一)

    用 Vue.js + vue-router 创建单页应用,是非常简单的.使用 Vue.js 时,我们就已经把组件组合成一个应用了,当你要把 vue-router 加进来,只需要配置组件和路由映射,然后 ...

  5. Fluent Nhibernate Mapping for Sql Views

    Views are mapped the same way tables are mapped except that you should put Readonly() in the mapping ...

  6. 优雅的go语言--入门篇

    1.特点 1.静态类型,编译型的开源语言 2.脚本华的语法,支持多种编程范式(函数式&面向对象) 3.原生,给力的并发编程的支持 2.优势 1.脚本化的语法 2.静态类型+编译型,程序运行速度 ...

  7. ajax解决跨域方法(适用于自己写接口解决跨域)

    原因是这样的:最近用PHP开发了一个网站,这个网站需要提供接口,接口开发完成之后,在本地进行请求,跨域测试. jsonp处理跨域和用PHP函数来处理跨域就不说了. 现在说的使用用 header 这个来 ...

  8. OneThink后台模型怎么玩?

    OneThink 后台模型有个模型类型: 模型下——>设计——>表单显示分组(怎么玩?) 这个将会显示在:内——>发布文章内容的时候: 单选按钮: 内容模块显示: 枚举类型可以这样玩 ...

  9. 二进制状态压缩dp(旅行商TSP)POJ3311

    http://poj.org/problem?id=3311 Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Subm ...

  10. javascript飞机大战-----001分析

    1.游戏引擎 首先要做飞机大战要考虑的是这个游戏被分成了哪几大部分?这样我们一块一块去做,特别清晰明了.那么接下来我们就简单的分析下飞机大战分成了哪几大部分 1.游戏引擎 2.英雄机 3.敌机 4.子 ...