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
clinton
homer
riemann
marjorie
Sample Output
0
rie 3
Source
Recommend
给出两个串,求第一个前缀和第二个后缀的最大匹配长度。
扩展kmp:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <queue> #define MAX 50010 using namespace std; char s[MAX],t[MAX]; int Next[MAX],Prefix[MAX]; void getNext(char *str) {///获得Next数组 int p = 1;///匹配最远的开始匹配位置 比如从i开始和0位置依次往后匹配相等能到最远的位置 p就是i 即i + Next[i] - 1最大时的i为p Next[0] = strlen(str);///显然自己和自己匹配 Next[1] = 0; while(str[Next[1] + 1] && str[Next[1] + 1] == str[Next[1]]) Next[1] ++;///匹配1位置作为基础 for(int i = 2;str[i];i ++) {///2位置开始 if(i + Next[i - p] < p + Next[p]) Next[i] = Next[i - p];///如果不超过最远位置 else {///超过最远位置 if(p + Next[p] - 1 >= i) Next[i] = p + Next[p] - i;///i点没超过最远位置 实际是 p + Next[p] - 1 - i + 1 else Next[i] = 0;///否则从头开始匹配啊 while(str[Next[i] + i] && str[Next[i]] == str[Next[i] + i]) Next[i] ++;///继续匹配 p = i; } } } void ExKmp(char *str1,char *str2) { getNext(str2); int p = 0; Prefix[0] = 0; while(str1[Prefix[0]] && str2[Prefix[0]] && str1[Prefix[0]] == str2[Prefix[0]]) Prefix[0] ++; for(int i = 1;str1[i];i ++) { if(i + Next[i - p] < p + Prefix[p]) Prefix[i] = Next[i - p];///i加上i-p前缀长度 没超过最远位置 else { if(i <= p + Prefix[p] - 1) Prefix[i] = p + Prefix[p] - i;///i没超过最远位置 else Prefix[i] = 0; while(str1[i + Prefix[i]] && str1[i + Prefix[i]] == str2[Prefix[i]]) Prefix[i] ++;///继续匹配 p = i;///更新最远位置下标 } } } int main() { while(~scanf("%s%s",s,t)) { ExKmp(t,s); int flag = 0,len = strlen(t); for(int i = 0;t[i];i ++) { if(i + Prefix[i] == len) { flag = 1; printf("%s %d ",t + i,Prefix[i]); break; } } if(!flag) puts("0"); } }
拼接后可用kmp但是要保证长度不能比两串各自的长度还大。
kmp:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <queue> #define MAX 50010 using namespace std; char s[MAX * 2],t[MAX]; int Next[MAX * 2]; int getNext(char *str,int m) { Next[0] = -1; int i = 0,j = -1,len = strlen(s); while(str[i]) { if(j == -1 || str[i] == str[j]) { Next[++ i] = ++ j; } else j = Next[j]; } while(Next[len] > m) len = Next[len]; return Next[len]; } int main() { while(~scanf("%s%s",s,t)) { int len = min(strlen(s),strlen(t)); strcat(s,t); int ans = getNext(s,len); if(!ans) puts("0"); else printf("%s %d ",s + strlen(s) - ans,ans); } }