题干:

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

题目大意:

  多组输入,每组输入给你两个字符串,问你第一个字符串的前缀串和第二个字符串的公共串最长是多长。

   换种讲法:给你两个字符串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);
				else
					printf("%s %d\n", s2, l2);
			} else {
				for(int i = 0; i < f[len]; i++)
					printf("%c", s1[i]);
				printf(" %d\n", f[len]);
			}
		} else
			printf("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);
	}
}