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. Docker 入门 第四部分: Swarms

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

  2. 在Eclipse中利用maven整合搭建ssm框架

    首先说明用到的框架: spring  +  springMVC  +  mybatis 构建工具:maven 开发工具:eclipse 开发环境:win10      java版本:jdk1.8    ...

  3. js scroll函数

    $(function () { $(".sticky").hide(); var top = $(window).scrollTop(); if (top >= 100) { ...

  4. Python GUI工具Tkinter以及拖拉工具Page安装

    如果使用Tkinter作为Python GUI工具,我们需要安装Tkinter,这个使用conda或者pip即可: conda install -c anaconda tk 为了提高界面编写效率,可以 ...

  5. mysql 8.0 ~ 安装

    1 环境配置   wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.13-linux-glibc2.12-x86_64.tar. ...

  6. proxysql 系列~审核功能

    一 简介:今天我们来探讨下具体的审核功能 二 平台审计功能 一 proxysql 设置  set mysql-eventslog_filename = '/data/ProxySQL/log/sql. ...

  7. Struts防止表单重复提交

    1.什么是表单重复提交 > 在不刷新表单页面的前提下:         >> 多次点击提交按钮        >> 已经提交成功, 按 "回退" 之后 ...

  8. Jetson tk1 刷机教程

    前期准备: 1.  Jetson TK1开发板. 2.  安装有ubuntu系统的PC(或者ubuntu虚拟机)切记:不管是PC还是虚拟机,务必确保有大于5G的存储空间,之后安装过程会作详细解释. 3 ...

  9. 【转】CentOS 7.X 系统安装及优化

    [转]CentOS 7.X 系统安装及优化 centos的演变 启动流程sysvinit 串行启动:一次一个,一个一个启动 并行启动:全部的一起启动 init优点 运行非常良好.主要依赖于shell脚 ...

  10. T_RegionNDS表创建及值

    -- Table structure for t_regionnds -- ---------------------------- DROP TABLE IF EXISTS t_regionnds; ...