Sample Input
clinton
homer
riemann
marjorie

Sample Output
0
rie 3

输入两个字符串 ,求最长相同前后缀
直接把两个字符串连接在一起求next就行了,唯一要注意的就是长度不能大于原来任一字符串的长度,如果长度大于了,要选择len1和len2中较小的一个输出。

 # include <cstdio>
# include <cstring>
using namespace std ; char s1[] ;
char s2[] ;
char s3[] ;
int next[] ;
int len1 , len2; void getNext()
{
int j, k;
j = ; k = -; next[] = -;
while(j < len1+len2)
if(k == - || s1[j] == s1[k])
next[++j] = ++k;
else
k = next[k];
} int main ()
{
while (scanf("%s%s" , &s1 , &s2) != EOF)
{
len1 = strlen(s1) ;
len2 = strlen(s2) ;
strcat(s1 , s2) ;
getNext() ;
int i ;
for (i = ; i < next[len1 + len2] ; i++)
{
s3[i] = s1[i] ;
}
s3[i] = '\0' ;
if (next[len1 + len2])
{
if (next[len1 + len2] > len1 ||next[len1 + len2] > len2)
{
if (len1 > len2)
printf("%s %d\n" ,s2 ,len2) ;
else
{
s3[len1] = '\0' ;
printf("%s %d\n" ,s3 ,len1) ;
}
}
else
printf("%s %d\n" ,s3 ,next[len1 + len2]) ;
}
else
printf("0\n") ;
} return ;
}

HDU 2594 最长相同前后缀的更多相关文章

  1. HDU 6186 CS Course【前后缀位运算枚举/线段树】

    [前后缀枚举] #include<cstdio> #include<string> #include<cstdlib> #include<cmath> ...

  2. KMP + 求相等前后缀--- POJ Seek the Name, Seek the Fame

    Seek the Name, Seek the Fame Problem's Link: http://poj.org/problem?id=2752 Mean: 给你一个字符串,求这个字符串中有多少 ...

  3. HDU 2594(求最长公共前后缀 kmp)

    题意是在所给的两个字符串中找最长的公共前后缀,即第一个字符串前缀和第二个字符串后缀的最长相等串. 思路是将两个字符串拼接在一起,然后直接套用 kmp 算法即可. 要注意用 next 会报编译错误,改成 ...

  4. HDU 2594 Simpsons’ Hidden Talents(KMP求s1前缀和s2后缀相同部分)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2594 题目大意:给两串字符串s1,s2,,找到最长子串满足既是s1的前缀又是s2的后缀,输出子串,及相 ...

  5. HDU 4763 Theme Section(KMP+枚举公共前后缀)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4763 题目大意: 给你一个字符串s,存在一个子串E同时出现在前缀.中间.后缀,即EAEBE这种模式,A ...

  6. HDU - 6096 :String (AC自动机,已知前后缀,匹配单词,弱数据)

    Bob has a dictionary with N words in it. Now there is a list of words in which the middle part of th ...

  7. HDU 2594 Simpsons’ Hidden Talents(KMP的Next数组应用)

    Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...

  8. HDU 2594 Simpsons’ Hidden Talents(辛普森一家的潜在天赋)

    HDU 2594 Simpsons’ Hidden Talents(辛普森一家的潜在天赋) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 3 ...

  9. 【HDU 2594 Simpsons' Hidden Talents】

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

随机推荐

  1. C#中子线程操作主线程中窗体上控件的方法

    Demo this.listView1.Visible = true; this.listView1.BeginUpdate();this.listView1.EndUpdate(); //结束数据处 ...

  2. Linux 命令详解(三)./configure、make、make install 命令

    这些都是典型的使用GNU的AUTOCONF和AUTOMAKE产生的程序的安装步骤 一.基本信息 1../configure 是用来检测你的安装平台的目标特征的.比如它会检测你是不是有CC或GCC,并不 ...

  3. Docker 入门 第四部分: Swarms

    目录 Docker 入门 第四部分: Swarms 先决条件 介绍 理解Swarm集群 部署swarm 创建一个集群 在swarm集群上部署你的app应用 为 swarm管理器配置一个docker-m ...

  4. 007、Docker 架构详解(2018-12-24 周一)

    参考https://www.cnblogs.com/CloudMan6/p/6763789.html     Docker核心组件包括:       Docker 客户端   Docker Clien ...

  5. SQL Server进阶(二)字段类型

    概述 系统数据类型详情 SqlDbType namespace System.Data { // // 摘要: // 指定要用于 System.Data.SqlClient.SqlParameter ...

  6. Python入门系列教程(一)基础

    基础知识 1.变量及类型 2.换行\n 3.输入 password = raw_input("请输入密码:") print '您刚刚输入的密码是:', password 4.格式化 ...

  7. 挖掘两个Integer对象的swap的内幕

    public class SwapTest { public static void main(String[] args) throws Exception { Integer a = 1, b=2 ...

  8. SpringBoot在IDEA中实现热部署

    gradle构建形式 添加依赖 compile("org.springframework.boot:spring-boot-devtools") 其他设置 步骤1 步骤2 按下 C ...

  9. avloadingindicatorview 使用解析

    官方文档:https://github.com/81813780/AVLoadingIndicatorView 中文文档:https://www.helplib.com/GitHub/article_ ...

  10. SRS+flv.js打造兼容性较高的直播、点播平台

    **************************************************************************************************** ...