Homer: Marge, I just figured out a way to discover some of the talents we weren’t aware we had.
Marge: Yeah, what is it?

Homer: Take me for example. I want to find out if I have a talent in politics, OK?

Marge: OK.

Homer: So I take some politician’s name, say Clinton, and try to find the length of the longest prefix

in Clinton’s name that is a suffix in my name. That’s how close I am to being a politician like Clinton

Marge: Why on earth choose the longest prefix that is a suffix???

Homer: Well, our talents are deeply hidden within ourselves, Marge.

Marge: So how close are you?

Homer: 0!

Marge: I’m not surprised.

Homer: But you know, you must have some real math talent hidden deep in you.

Marge: How come?

Homer: Riemann and Marjorie gives 3!!!

Marge: Who the heck is Riemann?

Homer: Never mind.

Write a program that, when given strings s1 and s2, finds the longest prefix of s1 that is a suffix of s2.
InputInput consists of two lines. The first line contains s1 and
the second line contains s2. You may assume all letters are in
lowercase.OutputOutput consists of a single line that contains the longest
string that is a prefix of s1 and a suffix of s2, followed by the length
of that prefix. If the longest such string is the empty string, then
the output should be 0.

The lengths of s1 and s2 will be at most 50000.Sample Input

clinton
homer
riemann
marjorie

Sample Output

0
rie 3 思路就是直接拼接,然后处理即可。刚开始什么都没考虑,直接交了。后来一点点发现了问题。8Y 列举几个特殊样例即可 ab
abab abab
ab abc
ab ab
cabc len1为s1的长度,len2为s2的长度,len为总长 可以发现如果Next[len]<==0 肯定为0 否则可能为0 就是循环节的长度大于s1 或者满足了不大于s1但是最终长度大于s2 还有可能就是一个很短的循环节循环了好多次。
 #include<stdio.h>
#include<string.h>
char s[],s1[];
int len,lentemp1,lentemp2,Next[]; void prekmp() {
int i,j;
j=Next[]=-;
i=;
while(i<len) {
while(j!=-&&s[i]!=s[j]) j=Next[j];
Next[++i]=++j;
}
} int main() {
//freopen("in","r",stdin);
while(~scanf("%s",s)) {
lentemp1=strlen(s);
scanf("%s",s1);
lentemp2=strlen(s1);
strcat(s,s1);
len=strlen(s);
prekmp();
if(Next[len]>) {
int i=Next[len];
while(i>lentemp1||i>lentemp2) {//小于s1&&小于s2 找满足条件的最大i
i=Next[i];
}
if(i<=) printf("0\n");
else {
for(int j=;j<i;j++) printf("%c",s[j]);
printf(" %d\n",i);
}
} else printf("0\n");
}
}

kuangbin专题十六 KMP&&扩展KMP HDU2594 Simpsons’ Hidden Talents的更多相关文章

  1. HDU2594 Simpsons’ Hidden Talents —— KMP next数组

    题目链接:https://vjudge.net/problem/HDU-2594 Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Oth ...

  2. hdu2594 Simpsons’ Hidden Talents kmp

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

  3. hdu2594 Simpsons' Hidden Talents【next数组应用】

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

  4. kuangbin专题十六 KMP&&扩展KMP HDU2609 How many (最小字符串表示法)

    Give you n ( n < 10000) necklaces ,the length of necklace will not large than 100,tell me How man ...

  5. hdu2594 Simpsons’ Hidden Talents LCS--扩展KMP

    Homer: Marge, I just figured out a way to discover some of the talents we weren’t aware we had.Marge ...

  6. kuangbin专题十六 KMP&&扩展KMP HDU2328 Corporate Identity

    Beside other services, ACM helps companies to clearly state their “corporate identity”, which includ ...

  7. kuangbin专题十六 KMP&&扩展KMP HDU1238 Substrings

    You are given a number of case-sensitive strings of alphabetic characters, find the largest string X ...

  8. kuangbin专题十六 KMP&&扩展KMP HDU3336 Count the string

    It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...

  9. kuangbin专题十六 KMP&&扩展KMP POJ3080 Blue Jeans

    The Genographic Project is a research partnership between IBM and The National Geographic Society th ...

随机推荐

  1. appium-unittest框架中的断言

    1.首先unittest本身是一个python的测试框架,他有他自己的使用规则: 2.如果用其中的方法,需要引入,方法: import unittest class Login(unittest.Te ...

  2. c#的日志插件NLog基本使用

    本文介绍c#的日志插件NLog 安装插件 创建logger 日志级别 书写日志信息 配置 包装器 布局 安装插件 直接下载插件包 Install-Package NLog.Config 创建logge ...

  3. php学习之try catch

    PHP 5 添加了类似于其它语言的异常处理模块.在 PHP 代码中所产生的异常可被 throw语句抛出并被 catch 语句捕获.(注:一定要先抛才能获取) 需要进行异常处理的代码都必须放入 try ...

  4. css水平居中(一)

    第一种方法:使用text-align属性. 看到一篇博客,也不知道是不是我理解的问题,博客上说text-align可以是内联元素水平居中,我感觉这样的说法是不是有些不准确. text-align属性规 ...

  5. String/StringBuilder 类 判断QQ号码

    1.1. 训练描述:[方法.String类]  一.需求说明:请用户输入一个“QQ号码”,我们来判断这个QQ号码是否正确. 要求:使用方法来完成判断功能. 1.2. 操作步骤描述 建立MainApp类 ...

  6. idea 修改Recent projects

    idea用了一段时间了,打开的项目多了,导致Open Recent列表中的项目也非常多,在找一个项目时很不方便. 后来查询,在~/Library/Preferences/IntelliJIdea目录/ ...

  7. 【转】PEAR安装、管理及使用

    PEAR安装   linux下只要你安装的是PHP 4.3.0以上的版本,默认安装都是支持PEAR的,除非你使用了”--WITHOUT-PEAR”选项,修改PHP.INI文件,在INCLUDE_PAT ...

  8. 《Spring实战》-2

    装配Bean 1.装配wiring,即创建应用对象之间的协作关系的行为,者也是依赖注入的本质. 2.创建Spring配置 从Sring3.0开始,Spring容器提供了两种配置Bean的方式: XML ...

  9. C++11新标准:auto关键字

    一.auto意义 编程时常常需要把表达式的值赋给变量,这就要求在声明变量的时候清楚地知道表达式的类型,然后要做到这一点并非那么容易.为了解决这个问题,C++11新标准引入了auto类型说明符,用它就能 ...

  10. MacBook Pro (13 英寸, 2017 年)安装win10系统

    准备: windows10系统镜像 16G或更大容量U盘(存放驱动,必须格式化为FAT) 官方提供的驱动软件 详细步骤: 1.打开Bootcamp,选择镜像文件 2.点击下一步,选择Bootcamp分 ...