hdu 2594 Simpsons’ Hidden Talents
2016-01-25 14:26
435 查看
给出字符串S1和S2求最长字符串,使之即是S1的前缀又是S2的后缀。
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5203 Accepted Submission(s): 1883
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.
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
Source
HDU 2010-05 Programming Contest
Recommend
lcy | We have carefully selected several similar problems for you: 2595 2596 2597 2598 2599
解法一:用一次KMP即可。KMP匹配时指向缝衣针内的指针一定表示指针前面已经匹配。KMP算法已经蕴含了最长。
解法二:利用next[],将S2放在S1后面求解。
/**==========================================
* This is a solution for ACM/ICPC problem
*
* @source:hdu 2594 Simpsons’ Hidden Talents
* @type: data_structure KMP
* @author: wust_ysk
* @blog: http://blog.csdn.net/yskyskyer123 * @email: 2530094312@qq.com
*===========================================*/
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
const int INF =0x3f3f3f3f;
const int maxn=50000 ;
char N[maxn+10];
char M[2*maxn+10];
int nex[2*maxn+10];
int len_M;
int len_N;
int len;
void getnex()
{
nex[0]=nex[1]=0;
for(int i=1;i<len;i++)
{
int p=nex[i];
while(p&&M[p]!=M[i]) p=nex[p];
nex[i+1]=M[p]==M[i]?p+1:0;
}
int ans=min(len_M,nex[len]);//
ans=min(len_N,ans);//这两步很重要
if(!ans) {puts("0");return;}
for(int i=0;i<ans;i++)
{
putchar(M[i]);
}
putchar(' ');
printf("%d\n",ans);
}
int main()
{
while(~scanf("%s%s",M,N))
{
len_M=strlen(M);
len_N=strlen(N);
len =len_M+len_N;
for(int i=0;i<len_N;i++)
{
M[len_M+i]=N[i];
}
M[len]='\0';
getnex();
}
return 0;
}
Simpsons’ Hidden Talents
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5203 Accepted Submission(s): 1883
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.
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
Source
HDU 2010-05 Programming Contest
Recommend
lcy | We have carefully selected several similar problems for you: 2595 2596 2597 2598 2599
解法一:用一次KMP即可。KMP匹配时指向缝衣针内的指针一定表示指针前面已经匹配。KMP算法已经蕴含了最长。
/**========================================== * This is a solution for ACM/ICPC problem * * @source:hdu 2594 Simpsons’ Hidden Talents * @type: data_structure KMP * @author: wust_ysk * @blog: http://blog.csdn.net/yskyskyer123 * @email: 2530094312@qq.com *===========================================*/ #include<cstdio> #include<string> #include<cstring> #include<iostream> #include<cmath> #include<algorithm> using namespace std; typedef long long ll; const int INF =0x3f3f3f3f; const int maxn=50000 ; char N[maxn+10]; char M[maxn+10]; int nex[maxn+10]; int len_M; void getnex() { nex[0]=nex[1]=0; for(int i=1;i<len_M;i++) { int p=nex[i]; while(p&&M[p]!=M[i]) p=nex[p]; nex[i+1]=M[p]==M[i]?p+1:0; } } void KMP() { int len_N=strlen(N); int p=0; for(int i=0;i<len_N;i++) { while(p&&N[i]!=M[p]) p=nex[p]; if(i==len_N-1) break; if(N[i]==M[p]) p++; if(p==len_M) p=nex[p]; } int len=N[len_N-1]==M[p]?p+1:0; // int len=N[len_N-1]==M[p]?p+1:p;一样的,如果=了p,p必定为0 if(!len) {puts("0");return;} for(int i=len_N-1-len+1;i<len_N;i++) { putchar(N[i]); } printf(" %d",len); putchar('\n'); } int main() { while(~scanf("%s%s",M,N)) { len_M=strlen(M); getnex(); KMP(); } return 0; }
解法二:利用next[],将S2放在S1后面求解。
/**==========================================
* This is a solution for ACM/ICPC problem
*
* @source:hdu 2594 Simpsons’ Hidden Talents
* @type: data_structure KMP
* @author: wust_ysk
* @blog: http://blog.csdn.net/yskyskyer123 * @email: 2530094312@qq.com
*===========================================*/
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
const int INF =0x3f3f3f3f;
const int maxn=50000 ;
char N[maxn+10];
char M[2*maxn+10];
int nex[2*maxn+10];
int len_M;
int len_N;
int len;
void getnex()
{
nex[0]=nex[1]=0;
for(int i=1;i<len;i++)
{
int p=nex[i];
while(p&&M[p]!=M[i]) p=nex[p];
nex[i+1]=M[p]==M[i]?p+1:0;
}
int ans=min(len_M,nex[len]);//
ans=min(len_N,ans);//这两步很重要
if(!ans) {puts("0");return;}
for(int i=0;i<ans;i++)
{
putchar(M[i]);
}
putchar(' ');
printf("%d\n",ans);
}
int main()
{
while(~scanf("%s%s",M,N))
{
len_M=strlen(M);
len_N=strlen(N);
len =len_M+len_N;
for(int i=0;i<len_N;i++)
{
M[len_M+i]=N[i];
}
M[len]='\0';
getnex();
}
return 0;
}
相关文章推荐
- 使用系统UICollectionViewFlowLayout布局类
- 转发 正则表达式
- Spark入门实战系列--5.Hive(下)--Hive实战
- 快速对Mysql添加索引的五个方法
- 二分查找
- 二层交换机当三层交换机,使用单臂路由实现二层交换机上的VLAN互通
- JNA调用32dll和64位dll的兼容解决
- Update cocoapods
- 关于A/B测试
- 随手笔记1
- 如何运行带有pod的项目
- 查看linux静态库*.a中的函数和文件 拿appro 2a算法库开刀
- 查看linux静态库*.a中的函数和文件 拿appro 2a算法库开刀
- layer-list用法
- HttpClient+DL
- web前端面试题
- Mac - 让文本框成为第一响应者
- 用ViewFlipper实现手势滑动
- linux常用命令—— 磁盘维护(十四)
- 适配器模式