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

列举几个特殊样例即可

ababab

ababab

abcab

abcabc

len1为s1的长度,len2为s2的长度,len为总长

可以发现如果Next[len]<==0 肯定为0

否则可能为0

就是循环节的长度大于s1  或者满足了不大于s1但是最终长度大于s2

还有可能就是一个很短的循环节循环了好多次。
 1 #include<stdio.h>
 2 #include<string.h>
 3 char s[100010],s1[50010];
 4 int len,lentemp1,lentemp2,Next[100010];
 5
 6 void prekmp() {
 7     int i,j;
 8     j=Next[0]=-1;
 9     i=0;
10     while(i<len) {
11         while(j!=-1&&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]>0) {
26             int i=Next[len];
27             while(i>lentemp1||i>lentemp2) {//小于s1&&小于s2  找满足条件的最大i
28                 i=Next[i];
29             }
30             if(i<=0) printf("0\n");
31             else {
32                 for(int j=0;j<i;j++) printf("%c",s[j]);
33                 printf(" %d\n",i);
34             }
35         } else printf("0\n");
36     }
37 }

转载于:https://www.cnblogs.com/ACMerszl/p/10269106.html

kuangbin专题十六 KMP扩展KMP HDU2594 Simpsons’ Hidden Talents相关推荐

  1. HDU-2594 Simpsons’ Hidden Talents

    HDU-2594 Simpsons' Hidden Talents 题目链接:HDU-2594 题目大意:给定两个字符串 问第一个字符串前缀与第二个字符串的后缀的最大的重复部分有多长 不为0的话将他们 ...

  2. HDU2594(Simpsons’ Hidden Talents)

    Simpsons' Hidden Talents Problem Description Homer: Marge, I just figured out a way to discover some ...

  3. kuangbin专题十六 KMP扩展KMP HDU3068 最长回文

    给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度. 回文就是正反读都是一样的字符串,如aba, abba等 Input输入有多组case,不超过120组,每组输入为 ...

  4. HDU2594——Simpsons’ Hidden Talents

    Problem Description Homer: Marge, I just figured out a way to discover some of the talents we weren' ...

  5. 开发指南专题十六:JEECG微云快速开发平台Excel导出

    开发指南专题十六:JEECG微云快速开发平台Excel导出 14.3.  Excel导出 导出工具类ExcelExportUtil 提供两个函数 //创建多个Sheet public static H ...

  6. kuangbin专题十二 基础DP

    kuangbin专题十二 基础DP A - HDU1024 Max Sum Plus Plus B - HDU1029 Ignatius and the Princess IV C - HDU1069 ...

  7. HDU 2594 Simpsons’ Hidden Talents (字符串-KMP)

    Simpsons' Hidden Talents Problem Description Homer: Marge, I just figured out a way to discover some ...

  8. kmp总结(相关例题1. Simpsons’ Hidden Talents 2.Oulipo)

    kmp相关及相关例题 文章目录 kmp相关及相关例题 一.kmp算法最常规使用方法 二.相关例题 1. Simpsons' Hidden Talents 2.Oulipo 一.kmp算法最常规使用方法 ...

  9. Simpsons’ Hidden Talents (HDU-2594)

    Simpsons' Hidden Talents (HDU-2594) Homer: Marge, I just figured out a way to discover some of the t ...

最新文章

  1. 自回归解码加速64倍,谷歌提出图像合成新模型MaskGIT
  2. 拼多多创始人黄峥,要彻底卸下担子,搞“科研”去了。
  3. 2021年古蔺高考成绩查询,古蔺中学2021录取分数线
  4. 处理 Maven 项目名称红色感叹号的问题
  5. 绑定MAC地址 局域网防arp ARP病毒
  6. UVa 10815 - Andy's First Dictionary
  7. 系统辨识理论及应用_液压系统故障智能诊断方法(2)
  8. Axure RP 9基础教程(2)——交互样式
  9. 浅析iOSApp签名原理及流程
  10. UVA 11991 Easy Problem from Rujia Liu?
  11. 面试——HR问你的职业规划,应该怎样回答?
  12. 获取微信好友头像,对其进行相应操作
  13. AMD锐龙R3 5400U性能怎么样?相当于什么水平级别
  14. 【正点原子MP157连载】第二十八章 A7和M4联合调试-摘自【正点原子】STM32MP1 M4裸机CubeIDE开发指南
  15. 华为交换机RRPP环协议
  16. 刷屏器!简单!快速!稳定!可控制速度!
  17. 100天精通Python(基础篇)——第7天:高级变量类型复习
  18. 使用arduino开发esp8266和esp32时首选项设置方法(附国内开发板管理网址)
  19. android 支付宝手势密码锁,Android实现支付宝手势密码功能
  20. MATLB|多微电网及分布式能源交易

热门文章

  1. activemq安装与配置_ActiveMQ(消息队列)从入门到了解
  2. 第一次连接mysql失败_MySQL 远程连接失败
  3. 用java单源最短路径问题_单源最短路径-贪心算法
  4. 2pin接口耳机_拆解报告:雷柏首款真无线耳机XS200
  5. ORA-01843:无效的月份
  6. 《操作系统知识点整理》
  7. 矩阵形状| 使用Python的线性代数
  8. krsort_PHP krsort()函数与示例
  9. vector向量容器
  10. Java——集合带All的功能演示