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. JAVA记录-基础常识

    1.==与equals区别 1)==用于基本数据类型的比较,判断引用是否指向堆内存的同一地址.---引用地址 2)equals用于判断两个变量是否是对同一对象的引用,即堆中的内容是否相同,返回值为布尔 ...

  2. 让div固定在顶部不随滚动条滚动【转】

    让div固定在顶部不随滚动条滚动 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "h ...

  3. MQ问题

    大部分的处理方案: 1.重试 2.定时任务 3.幂等

  4. 调用write()写

    一.在POSIX中的定义 #include <unistd.h> ssize_t write(int fd, const void *buf, size_t count); 二.返回值 ( ...

  5. C# 密封类使用sealed修饰

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace @sea ...

  6. js设置div透明度

    原生js设置透明度 为了兼容IE与其他浏览器对透明度的设置,我们需要对以上两种样式分别进行设置: 复制代码代码如下: var alpha = 30; //透明度值变量 var oDiv = docum ...

  7. UML 类图 说明

    继承关系用空心三角形+实线来表示 关联:就是属性 聚合: 合成:组成 依赖:作为参数存在

  8. Maven聚合工程的使用

    创建一个service模块 接下来,在该项目中创建一个接口 创建一个实现类,并实现接口 在sm1234-web项目中,调用service的方法,需要在该项目的pom.xml中引入依赖Service模块 ...

  9. NSOperation 代码,阐述NSOperation一般功能和重要功能

    // // ViewController.m // 05-NSOperation // // Created by jerry on 15/9/5. // Copyright (c) 2015年 je ...

  10. SpringBoot修改Servlet相关配置

    第一种方式在配置文件中进行修改 server.port=8081 server.servlet.context-path=/springboot server.tomcat.uri-encoding= ...