HDU 2594 Simpsons’ Hidden Talents (kmp)
2016-07-24 19:13
399 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2594
Total Submission(s): 6451 Accepted Submission(s): 2321
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
Input consists of two lines. The first line contains s1 and the second line contains s2. You may assume all letters are in lowercase.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
clinton
homer
riemann
marjorie
[align=left]Sample Output[/align]
0
rie 3
思路:kmp
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
int ne[50010];
char st1[50010],st2[50010];
void getnex(char s[],int m)
{
int k = -1,j = 0;
ne[0] = -1;
while (j < m )
{
if (k == -1 || s[j] == s[k])
{
++k;
++j;
ne[j] = k;
}
else
{
k = ne[k];
}
}
}
int main()
{
while (cin >> st1 >> st2)
{
int len1 = strlen(st1);
getnex(st1,len1);
int len2 = strlen(st2);
int j = 0,i = 0;
if ( len2 > len1 )
{
i = len2 - len1;
}
while ( i < len2 && j < len1)
{
if ( j == -1 || st1[j] == st2[i])
{
i++;
j++;
}
else
{
j = ne[j];
}
}
if ( j == 0 || j == -1)
{
printf("0\n");
}
else
{
for (int i = 0 ; i < j; i++ )
{
printf("%c",st1[i]);
}
printf(" %d\n",j);
}
}
return 0;
}
Simpsons’ Hidden Talents
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6451 Accepted Submission(s): 2321
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
Input consists of two lines. The first line contains s1 and the second line contains s2. You may assume all letters are in lowercase.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
clinton
homer
riemann
marjorie
[align=left]Sample Output[/align]
0
rie 3
思路:kmp
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
int ne[50010];
char st1[50010],st2[50010];
void getnex(char s[],int m)
{
int k = -1,j = 0;
ne[0] = -1;
while (j < m )
{
if (k == -1 || s[j] == s[k])
{
++k;
++j;
ne[j] = k;
}
else
{
k = ne[k];
}
}
}
int main()
{
while (cin >> st1 >> st2)
{
int len1 = strlen(st1);
getnex(st1,len1);
int len2 = strlen(st2);
int j = 0,i = 0;
if ( len2 > len1 )
{
i = len2 - len1;
}
while ( i < len2 && j < len1)
{
if ( j == -1 || st1[j] == st2[i])
{
i++;
j++;
}
else
{
j = ne[j];
}
}
if ( j == 0 || j == -1)
{
printf("0\n");
}
else
{
for (int i = 0 ; i < j; i++ )
{
printf("%c",st1[i]);
}
printf(" %d\n",j);
}
}
return 0;
}
相关文章推荐
- 网页中Span和Div的区别
- Span元素的width属性无效果原因及解决方案
- 如何让div span等元素能响应键盘事件操作指南
- javascript给span标签赋值的方法
- 深入理解DIV和SPAN的区别
- SPAN与RSPAN
- 快来看啊-cisco端口镜象详解(span,vspan,rspan )
- SPAN端口镜像
- p标签中有span标签,垂直居中时pc与移动的坑
- 学习yii的进步时。。。。
- Android中Span研究
- 如何设置span的width
- html5 span设置宽度无效的问题
- html中DIV与SPAN标签的区别
- [SDIBT-OJ]27页
- Div和Span的区别
- span无法确定宽度的解决方案
- css html span 块状不换行
- 多个span中间会有间隙
- pan与a元素的键盘聚焦性以及键盘点击性研究