【 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.?
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 marjorieSample Output
0 rie 3題目大意:
? 多組輸入,每組輸入給你兩個字符串,問你第一個字符串的前綴串和第二個字符串的公共串最長是多長。
? ?換種講法:給你兩個字符串s1、s2,讓你找出最長的字符串 使得它既是s1的前綴又是s2的后綴。
解題報告:
? ? 這題很顯然是要用KMP去求解的。首先把第一個串和第二個串嫁接到一起,然后對這個ss串求解失配函數next。如果沒有重疊,那么這個值即為所求,但是有可能出現交叉的情況,比如aaa? 和? aaaa,這樣next[len] = 6了。。顯然不是我們要的答案,所以我們不斷的進行next迭代,直到這個值小于len1和len2。因為通過KMP的證明我們知道每一次求的一定是最優值,所以最終的k即為所求,最后輸出就可以了。注意所有的迭代啊等等,都是對ss字符串求解的,這時候已經拋開了s1和s2了,也不管他倆到底誰長誰短,也不用分情況討論,只用管ss數組就可以,只是最后的答案需要用len1和len2去約束一下。
?
錯誤代碼:處理不了abca bcabc? 這樣的情況、
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; char s1[100100], s2[50100]; char ss[50100]; int f[100100];//注意數組大小 void getfail(char *P) {int len = strlen(P);f[0] = f[1] = 0;for(int i = 1; i < len; i++) {int j = f[i];while(j && P[i] != P[j])j = f[j];f[i+1] = P[i]==P[j] ? j+1 : 0;} } int main() {while(scanf("%s%s", s1, s2) != EOF) {int l1 = strlen(s1);int l2 = strlen(s2);strcpy(ss, s1);strcat(s1, s2);getfail(s1);int len = strlen(s1);if(f[len]) {if(f[len] > min(l1, l2)) { //特殊情況if(l1 < l2)//選取長度較小的串printf("%s %d\n", ss ,l1);elseprintf("%s %d\n", s2, l2);} else {for(int i = 0; i < f[len]; i++)printf("%c", s1[i]);printf(" %d\n", f[len]);}} elseprintf("0\n");}return 0; }AC代碼:
#include<iostream> #include<cstring> #include<string> #include<cstdio> using namespace std; char s1[100005],s2[50005],ss[1000005]; int Next[100005],len,len1,len2;void getNext() {int k = -1,j = 0;Next[0] = -1;while(j < len) {if(k == -1 || ss[j] == ss[k]) {j++,k++;Next[j] = k;}else k = Next[k];} } int main() {while(~scanf("%s%s",s1,s2)) {len1=strlen(s1),len2=strlen(s2),len=len1+len2;strcpy(ss,s1);strcat(ss,s2);//加長了 getNext();int k=Next[len];if(k == 0 || k == -1) {puts("0");continue;}while(k>len1||k>len2) k=Next[k]; //防止aaa aaaa這種情況,或者abca bcabc這樣 for(int i = 0; i<k; i++)printf("%c",s1[i]);printf(" %d\n",k);} }?
總結
以上是生活随笔為你收集整理的【 HDU - 2594 】Simpsons’ Hidden Talents(KMP应用,求最长前缀后缀公共子串)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 加拿大发现3.5万年前猛犸象幼崽:亡时1
- 下一篇: 联想拯救者Y9000K升级压感键盘:游戏