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

  1. clinton
  2. homer
  3. riemann
  4. marjorie

Sample Output

  1. 0
  2. rie 3
  3.  
  4. 思路就是直接拼接,然后处理即可。刚开始什么都没考虑,直接交了。后来一点点发现了问题。8Y
  5.  
  6. 列举几个特殊样例即可
  7.  
  8. ab
    abab
  9.  
  10. abab
    ab
  11.  
  12. abc
    ab
  13.  
  14. ab
    cabc
  15.  
  16. len1s1的长度,len2s2的长度,len为总长
  17.  
  18. 可以发现如果Next[len]<==0 肯定为0
  19.  
  20. 否则可能为0
  21.  
  22. 就是循环节的长度大于s1 或者满足了不大于s1但是最终长度大于s2
  23.  
  24. 还有可能就是一个很短的循环节循环了好多次。
  25.  
  1. #include<stdio.h>
  2. #include<string.h>
  3. char s[],s1[];
  4. int len,lentemp1,lentemp2,Next[];
  5.  
  6. void prekmp() {
  7. int i,j;
  8. j=Next[]=-;
  9. i=;
  10. while(i<len) {
  11. while(j!=-&&s[i]!=s[j]) j=Next[j];
  12. Next[++i]=++j;
  13. }
  14. }
  15.  
  16. int main() {
  17. //freopen("in","r",stdin);
  18. while(~scanf("%s",s)) {
  19. lentemp1=strlen(s);
  20. scanf("%s",s1);
  21. lentemp2=strlen(s1);
  22. strcat(s,s1);
  23. len=strlen(s);
  24. prekmp();
  25. if(Next[len]>) {
  26. int i=Next[len];
  27. while(i>lentemp1||i>lentemp2) {//小于s1&&小于s2 找满足条件的最大i
  28. i=Next[i];
  29. }
  30. if(i<=) printf("0\n");
  31. else {
  32. for(int j=;j<i;j++) printf("%c",s[j]);
  33. printf(" %d\n",i);
  34. }
  35. } else printf("0\n");
  36. }
  37. }
  1.  

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. [Python]python CGI脚本在apache服务器上运行时出现“Premature end of script headers”错误

    在测试自己的python CGI脚本时, 当html网页中的表单form内容传送到服务器python脚本时, 总是出现Premature end of script headers错误, 网页显示是服 ...

  2. 问题:iis配置json;结果:如何配置iis支持.json格式的文件

    如何配置iis支持.json格式的文件 | 浏览:1357 | 更新:2015-04-05 11:00 | 标签:软件 1 2 3 4 5 6 7 分步阅读 现在大家在制作HTM5的一些小场景,小游戏 ...

  3. docker 笔记(2) base镜像

    base 镜像有两层含义: 不依赖其他镜像,从 scratch 构建. 其他镜像可以之为基础进行扩展. 所以,能称作 base 镜像的通常都是各种 Linux 发行版的 Docker 镜像,比如 Ub ...

  4. C语言学习笔记--动态内存分配

    1. 动态内存分配的意义 (1)C 语言中的一切操作都是基于内存的. (2)变量和数组都是内存的别名. ①内存分配由编译器在编译期间决定 ②定义数组的时候必须指定数组长度 ③数组长度是在编译期就必须确 ...

  5. DAY11-MYSQL补充之SQL逻辑查询语句执行顺序

    一 SELECT语句关键字的定义顺序 SELECT DISTINCT <select_list> FROM <left_table> <join_type> JOI ...

  6. 关于大数据领域各个组件打包部署到集群运行的总结(含手动和maven)(博主推荐)

    对于这里的打包,总结: (1)     最简单的,也是为了适应公司里,还是要用maven,当然spark那边sbt,maven都可以.但是maven居多. Eclipse/MyEclipse下如何Ma ...

  7. cocos2d-js 热更新模块 使用AssetsManager

    原帖子地址:http://cn.cocos2d-x.org/tutorial/show?id=1186 在这个文章中原作者已经说的很清楚,我在这个其他改动一些适用我项目中需求 1.满足Web和Nati ...

  8. Maven 导包后,在Maven Dependencies 里面却没有相应的包

    1 问题描述 在1处显示成功,但是在2处却没有相应的包 2 问题原因 查看pom.xml的源码,看你的依赖是否和我的方框中标签是一样的,有的会多出一个  xxxupdate 的标签(我这里给出的是正确 ...

  9. nginx显示目录下面的文件

    location / { root /data/source; index index.html index.htm; autoindex on;     #autoindex指令,显示文件目录下面的 ...

  10. PCL—关键点检测(iss&Trajkovic)低层次点云处理

    博客转载自:http://www.cnblogs.com/ironstark/p/5069311.html 关键点检测往往需要和特征提取联合在一起,关键点检测的一个重要性质就是旋转不变性,也就是说,物 ...