HDU 2594 Simpsons’ Hidden Talents (KMP)
来源:互联网 发布:企业文件管理系统源码 编辑:程序博客网 时间:2024/05/29 07:22
Simpsons’ Hidden Talents
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4441 Accepted Submission(s): 1611
Problem Description
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.
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.
The lengths of s1 and s2 will be at most 50000.
Sample Input
clintonhomerriemannmarjorie
Sample Output
0rie 3
题目传送门:HDU 2594 Simpsons' Hidden Talents
题意:输入字符串,判断两串字符串中,即是第一个字符串的前缀,又是第二个字符串后缀的子串,输出该子串并输出长度。
思路:首先把两个字符串连接成一个字符串。那么我们想,如果第二个字符串的后缀是第一个字符串的前缀,也就是说后面某一段子串在开头的出现了。那么next数组肯定不为0.所以我们设连接后的子串s3长度为len3,如果next[len3]!=0,说明存在题意所述的子串。那么next[len3]就是子串的长度。我们只要输出第一个字符串s1的前next[len3]位就好了。
需要注意的是:如果子串的长度(即next[len3])大于给定的两个字符串s1 s2中任意一个串的长度,我们就需要输出s1 s2中比较短的那个。比如:aaaaaa aaa,我们就只能输出aaa了,总不能输出个比3长的数吧→_→ 下面是AC代码~
#include <cstdio>#include <cstring>#include <iostream>using namespace std;#define TEST cout<<"wwwwwwwwwww"<<endl#define N 50005char s1[N],s2[N],s3[N*2];int nextt[N*2];int len1,len2,len3;void getNext(char *a,int len){ int i,j; i=0; j=-1; nextt[i]=j; while(i<len) { if(j==-1 || a[i]==a[j]) { i++; j++; nextt[i]=j; } else j=nextt[j]; } return ;}int main(){ while(scanf("%s%s",s1,s2)!=EOF) { int s; strcpy(s3,s1); strcat(s3,s2); memset(nextt,0,sizeof(nextt)); len1=strlen(s1); len2=strlen(s2); len3=strlen(s3); getNext(s3,len3); if(nextt[len3]!=0) { if(nextt[len3]>len1 || nextt[len3]>len2) { if(len1>len2) s=len2; else s=len1; } else s=nextt[len3]; for(int i=0;i<s;i++) printf("%c",s1[i]); printf(" %d\n",s); } else printf("0\n"); } return 0;}
0 0
- HDU 2594 Simpsons’ Hidden Talents(KMP)
- KMP hdu-2594 Simpsons’ Hidden Talents
- HDU 2594 Simpsons’ Hidden Talents KMP
- HDU 2594Simpsons’ Hidden Talents(KMP运用)
- HDU 2594 Simpsons’ Hidden Talents KMP
- hdu 2594 Simpsons’ Hidden Talents(KMP)
- hdu 2594 Simpsons’ Hidden Talents(KMP)
- HDU 2594 Simpsons’ Hidden Talents(KMP)
- hdu 2594 Simpsons’ Hidden Talents(KMP)
- HDU 2594 Simpsons’ Hidden Talents KMP
- HDU 2594 Simpsons’ Hidden Talents (KMP)
- hdu 2594 Simpsons’ Hidden Talents KMP
- HDU 2594 Simpsons’ Hidden Talents KMP题解
- hdu 2594 Simpsons’ Hidden Talents(数据结构:KMP)
- HDU 2594 Simpsons’ Hidden Talents(KMP)
- hdu 2594 Simpsons’ Hidden Talents (kmp扩展)
- 【KMP】 HDU 2594 Simpsons’ Hidden Talents
- hdu 2594 Simpsons’ Hidden Talents ( kmp )
- python中range和xrnage的区别
- oracle列相乘
- 南邮 OJ 1221 最少硬币问题
- Android之system_server与zygote之作用
- QT国际化
- HDU 2594 Simpsons’ Hidden Talents (KMP)
- 论 f.getModifiers() & Modifier.FINAL &Modifier.STATIC 如何成立
- Tesseract-OCR引擎试用 rebuild on vs2012【testing】
- iOS开发中的测试框架
- PIC常见错误-不定期更新
- 反射例子
- 根据前序遍历和中序遍历构建二叉树
- Android 图片闪烁的效果
- Helix server 使用说明