题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=2594
题目:
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
思路:渣渣我一开始写的是kmp的,后来嫌麻烦,就直接写了个暴力,,然后1a了,还是390MS。。(还是暴力大法好,虽然很可能会T,但在比赛时你没其他思路时,不妨来一发,万一AC了呢?缘分这种事谁说的清呢?手动 脸红.jpg)
弱弱的说句:数据真弱
等下补上kmp的做法==
#include <bits/stdc++.h> #define PB push_back #define MP make_pair using namespace std; typedef long long LL; typedef pair<int,int> PII; #define PI acos((double)-1) #define E exp(double(1)) #define K 1000000+9 int nt[K]; string sa,sb; int solve(void) { int mx=0; for(int i=0,len=min(sa.length(),sb.length());i<len;i++) if(sa.compare(0,i+1,sb,sb.length()-i-1,i+1)==0) mx=max(mx,i+1); return mx; } int main(void) { while(cin>>sa>>sb) { int k=solve(); if(k) { for(int i=0;i<k;i++) printf("%c",sa[i]); printf(" %d ",k); } else printf("0 "); } return 0; }
kmp:看到前缀和后缀的匹配肯定是kmp啊,把sb加在sa后面求个next数组就可以了==
不过当next[sa.length()-1]>min(sa.length(),sb.length())时(第一个sa是拼接后的字符串,后两个是原字符串)。
要不短回退至适配位置.
具体见代码:
#include <bits/stdc++.h> #define PB push_back #define MP make_pair using namespace std; typedef long long LL; typedef pair<int,int> PII; #define PI acos((double)-1) #define E exp(double(1)) #define K 1000000+9 int nt[K]; string sa,sb; void kmp_next(void) { nt[0]=0; for(int i=1,j=0,len=sa.length();i<len;i++) { while(j&&sa[i]!=sa[j])j=nt[j-1]; if(sa[i]==sa[j])j++; nt[i]=j; } } int main(void) { while(cin>>sa>>sb) { int len=min(sa.length(),sb.length()); sa+=sb; kmp_next(); int k=nt[sa.length()-1]; while(k>len)k=nt[k-1]; if(k) { for(int i=0;i<k;i++) printf("%c",sa[i]); printf(" %d ",k); } else printf("0 "); } return 0; }