Simpsons’ Hidden Talents (HDU-2594)

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.

Input

Input consists of two lines. The first line contains s1 and the second line contains s2. You may assume all letters are in lowercase.

Output

Output 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

题意:给你两个字符串,在第一个字符串中找到一个最长的前缀,同时也是第二个字符串的后缀;找不到的话输出0

思路:把两个串连起来,那么next[len]就是要求的前缀长度了。但是连起来之后因为next[len]很有可能大于两个原来的串的最小的长度,因此,需要再处理一步

AC代码:

#include <bits/stdc++.h>
const int maxx=100010;
const int inf=0x3f3f3f3f;
using namespace std;
char s1[maxx];
char s2[maxx];
int nex[maxx];
int s1len;
void Getnextval()
{int i=0,j=-1;nex[0]=-1;while(i<s1len){while(j!=-1 && s1[i]!=s1[j])j=nex[j];nex[++i]=++j;}
}
int main()
{while(~scanf("%s%s",s1,s2)){strcat(s1,s2);//字符串连接函数s1len=strlen(s1);int s2len=strlen(s2);Getnextval();if(nex[s1len]==0)printf("0\n");else{int cnt=s1len-s2len>s2len?s2len:s1len-s2len;while(cnt<nex[s1len])nex[s1len]=nex[nex[s1len]];printf("%s %d\n",s1+s1len-nex[s1len],nex[s1len]);}}return 0;
}

Simpsons’ Hidden Talents (HDU-2594)相关推荐

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

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

  2. Simpsons’ Hidden Talents(扩展KMP)

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

  3. Simpsons’ Hidden Talents(辛普森一家的隐藏天赋 )(kmp经典模板题) HDU - 2594

    题目:Simpsons' Hidden Talents(辛普森一家的隐藏天赋 ) 中文大意 Homer: Marge, I just figured out a way to discover som ...

  4. 【 HDU - 2594 】Simpsons’ Hidden Talents(KMP应用,求最长前缀后缀公共子串)

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

  5. Simpsons’ Hidden Talents(HDU-2594)

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

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

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

  7. HDU-2594-Simpsons’ Hidden Talents (kmp)

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

  8. Simpsons’ Hidden Talents(KMP ,两个串的前后缀匹配)

    Simpsons' Hidden Talents 题目 给两个串,求S1的前缀和S2的后缀的最大匹配 思路 拼接两个串,处理出nxt数组,nxt[k] 即为所求,因为它们的最大匹配不能超过原串的长度, ...

  9. HDU2594(Simpsons’ Hidden Talents)

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

最新文章

  1. vue中的浏览量_vue中前进刷新、后退缓存用户浏览数据和浏览位置的实践
  2. EOS生产区块:解析插件producer_plugin
  3. python制作文本编辑器_Python小实战:制作文本编辑器
  4. 一步步创建第一个Docker App —— 4. 部署应用
  5. Oracle数据库之三
  6. php ip操作,ip操作 · PHP 个人常用知识总结 · 看云
  7. WinRAR注册+去广告教程
  8. 音乐播放器——SoundManager API介绍
  9. 200元组群晖—HP惠普T610瘦客户端:直接硬盘启动黑群晖
  10. C/C++ 模拟键盘操作(一)
  11. iOS_数据库3_sqlite3基本操作
  12. MSF之persistence权限维持
  13. 社会力量如何参与长城保护管理?国家文物局这样说
  14. 计算机页面优盘页面分开,如何使用老毛桃winpe从电脑分区中拆分出一个空闲分区?...
  15. EditPluss有效激活码
  16. 青少年软件编程等级考试 python-青少年软件编程等级考试Python(一级)
  17. “基于医疗知识图谱的问答系统”代码解析(一)
  18. D* Lite 介绍
  19. 和12岁小同志搞创客开发:遥控舵机
  20. 《layui宇宙版教程》:字体图标

热门文章

  1. 手游后台PVP系统网络同步方案总结
  2. android swf webview,android webview播放swf文件
  3. 《疯狂Java讲义》读书笔记7
  4. 使用PicGo+Gitee做图床
  5. 各国制度、文化距离-原始数据+测算数据(2003-2018年)
  6. 西门子S7-1200 移位指令SHL
  7. 36_ue4[UI]05_3DUI显示与展示
  8. 【GNN报告】GNN-LOGS部分报告汇总
  9. 搜狗搜索推广Api在postman中的使用
  10. SfxMCP基于rtsp协议H5无插件直播方案中RTSP设备的接入方法 - sfxlab