原题链接

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

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn=1e5+;
char ch[*maxn];
char s[maxn],t[maxn];
int T,next[*maxn];
/*1.
void get_next(char *s)
{
next[1]=0;
//printf("%d\n",next[1]);
int i=1,j=0;
int slen=strlen(s);
while(i<slen){
if(j==0||s[i-1]==s[j-1]){
i++,j++;
next[i]=j;
//printf("%d\n",next[i]);
}
else j=next[j];
}
}严蔚敏数据结构P83页代码
*/
void get_next(char *s)
{
int slen=strlen(s);
next[]=;
for(int i=;i<slen;i++){
int j=next[i];
while(j&&s[i]!=s[j]) j=next[j];
next[i+]=(s[i]==s[j])?j+:;
}
}
/*3.
void get_next(char *s)
{
next[0]=0,next[1]=0;
//printf("%d\n",next[1]);
int slen=strlen(s);
for(int i=2;i<=slen;i++){
int j=next[i-1];
while(j&&s[i-2]!=s[j-1]) j=next[j];
next[i]=(s[i-2]==s[j-1])?j+1:1;
//printf("%d\n",next[i]);
}
}按照做数据结构笔试题的步骤一步一步推导
*/
int main()
{
scanf("%d",&T);
while(T--){
cin>>s>>t;
int slen=strlen(s);
int tlen=strlen(t);
for(int i=;i<tlen;i++) ch[i]=t[i];
ch[tlen]='#';
for(int i=;i<slen+tlen+;i++) ch[i+tlen+]=s[i];
int now=slen+tlen+;
ch[now]=;
int ans=;
memset(next,,sizeof(next));
get_next(ch);
while(next[now]>){
ans++;
now=next[now];
}
printf("%d\n",ans);
}
}

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. Java类的设计----关键字super

    关键字super 在Java类中使用super来引用父类的成分 super可用于访问父类中定义的属性 super可用于调用父类中定义的成员方法 super可用于在子类构造方法中调用父类的构造方法 su ...

  2. MySQL开启预编译环境

    Mysql是默认 没有开启预编译的,需要在配置中加上 jdbc:mysql://xxx.22.11.31:3306/dbname?useServerPrepStmts=true

  3. 管理开机启动:systemd

    一.CentOS7 systemd 介绍 在 CentOS7 中,使用 systemd 来管理其他服务是否开机启动,systemctl 是 systemd 服务的命令行工具 [root@localho ...

  4. C++11新特性之一——Lambda表达式

    C++11新特性总结可以参考:http://www.cnblogs.com/pzhfei/archive/2013/03/02/CPP_new_feature.html#section_6.8 C++ ...

  5. iOS 自动布局 Autolayout 优先级的使用

    一.约束的优先级 0.屏幕适配 发展历程 代码计算frame -> autoreszing(父控件和子控件的关系) -> autolayout(任何控件都可以产生关系) -> siz ...

  6. sencha touch 入门系列 (八)sencha touch类系统讲解(下)

    接着上一讲,我们通过一组代码来讲解一下st的类的一些属性: Ext.define("MyConfig",{ config:{ website:"http://127.0. ...

  7. 模拟退火算法(run away poj1379)

    http://poj.org/problem?id=1379 Run Away Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: ...

  8. Common Subsequence 最大公共子序列问题

    Problem Description A subsequence of a given sequence is the given sequence with some elements (poss ...

  9. nginx 重定向

    不带www跳转www 1.301: return 301 http://www.xx.com$request_uri; 2.(1)rewrite ^(.*)$ http://www.xx.com$1 ...

  10. nginx跟tp5无法加载控制器

    二. 另外502 bad gateway错误,可能是有PHP中的php-fpm.conf里 “ listen  fastcgi_pass /tmp/php-cgi.sock ”跟nginx的conf文 ...