原题链接

注意: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. Effective C++ Item 16 Use the same form in corresponding uses of new and delete

    1. When you created an array and want to return the memory to system. You need to explicitly add [] ...

  2. TestNG入门教程

    阅读目录 TestNG介绍 在Eclipse中在线安装TestNG 在Eclipse中离线安装Testng TestNG最简单的测试 TestNG的基本注解 TestNG中如何执行测试 使用testt ...

  3. Windows7安装Mongodb

    1.安装mongodb-win32-x86_64-3.0.4-signed.msi 2.安装kb2731284 安装补丁:Windows6.1-KB2731284-v3-x64.msu 3.创建数据库 ...

  4. 查看系统资源使用情况:vmstat

    vmstat命令可以动态地查看系统资源的使用情况,如内存/交换分区/CPU的使用情况,通过使用该命令可以判断系统的瓶颈在哪里: [root@localhost ~]$ vmstat 1 5 # 表示每 ...

  5. Linux 下安装 Python3

    Linux CentOS 7 安装 Python3: [root@localhost ~]$ yum install -y epel-release [root@localhost ~]$ yum i ...

  6. Python SQLAlchemy 模块

    SQLAlchemy 简介: SQLAlchemy 是用于实现 ORM(Object Relational Mapping,对象关系映射)的一个模块,即把数据库的表结构映射到对象上在 Python 中 ...

  7. 通过Nagios监控weblogic服务

    1.前言      前段时间搭建了一套Nagios监控服务,心血来潮想自己写一个脚本,拓展Nagios插件来监控公司的weblogic服务. 环境:weblogic10.3.3.0 . CentOS6 ...

  8. mysql concat

    CONCAT_WS() 代表 CONCAT With Separator ,是CONCAT()的特殊形式. 第一个参数是其它参数的分隔符.分隔符的位置放在要连接的两个字符串之间. 分隔符可以是一个字符 ...

  9. kerberos认证协议分析

    Kerberos认证协议分析 Kerberos认证协议流程 如上图: * 第一步:client和认证服务器(AS)通信完成认证过程,如果认证成功AS返回给client一个TGT(用来向TGS获取tic ...

  10. shell中特殊变量$0 $1 $# $$ $! $?的涵义

    $0: 执行脚本的名字 $*和$@: 将所有参数返回 $#: 参数的个数 $_: 代表上一个命令的最后一个参数 $$: 代表所在命令的PID $!: 代表最后执行的后台命令的PID $?: 代表上一个 ...