HDU 2594 Simpsons’ Hidden Talents
来源:互联网 发布:软件导刊 质量 编辑:程序博客网 时间:2024/06/08 14:16
Simpsons’ Hidden Talents
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 10240 Accepted Submission(s): 3567
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
clintonhomerriemannmarjorie
Sample Output
0rie 3
Source
HDU 2010-05 Programming Contest
Recommend
lcy | We have carefully selected several similar problems for you: 1711 2595 2596 2597 2598
字符串hash水题
ac代码:
#include <cstdio>#include <cmath>#include <cstring>#include <string>#include <algorithm>#define ull unsigned long longusing namespace std;const int maxn=50000+10;const int base=163;char s1[maxn],s2[maxn],s3[maxn];ull hash1[maxn],hash2[maxn],p[maxn]; void INIT(){/*打一下表,方便区间取值*/ p[0]=1;for(int i=1;i<maxn;i++)p[i]=p[i-1]*base;} ull get(int l,int r,ull *g){/*标准区间取值函数,改变了一个l > r,我也不知道对不对,这里我临时加的*/ if(l > r)return 0;return g[r]-g[l-1]*p[r-l+1];}int main(){INIT();while(~scanf("%s%s",s1+1,s2+1)){hash1[0]=hash2[1]=0;int cnt1=strlen(s1+1),cnt2=strlen(s2+1);for(int i=1;i<=cnt1;i++){/*进行hash*/ hash1[i]=hash1[i-1]*base+s1[i]-'a';}for(int i=1;i<=cnt2;i++){/*进行hash*/hash2[i]=hash2[i-1]*base+s2[i]-'a';}/*ans最大取他俩之间较小的长度*/ int ans=min(cnt1,cnt2);while(ans >= 0){/*进行一个个比较*/ ull a1=get(1,ans,hash1);ull a2=get(cnt2-ans+1,cnt2,hash2);if(a1 == a2){break ;}ans--;}/*输出*/ if(ans == 0) printf("%d\n",ans);else {for(int i=1;i<=ans;i++) printf("%c",s1[i]);printf(" %d\n",ans);}}return 0;}
阅读全文
0 0
- hdu 2594 Simpsons’ Hidden Talents
- hdu 2594 Simpsons’ Hidden Talents
- hdu 2594 Simpsons’ Hidden Talents
- HDU 2594 Simpsons’ Hidden Talents
- hdu 2594 Simpsons’ Hidden Talents
- hdu 2594 Simpsons’ Hidden Talents
- hdu 2594 Simpsons’ Hidden Talents
- hdu 2594 Simpsons’ Hidden Talents
- hdu 2594 Simpsons’ Hidden Talents
- HDU 2594 Simpsons’ Hidden Talents
- hdu 2594 Simpsons’ Hidden Talents
- HDU - 2594 Simpsons’ Hidden Talents
- HDU 2594 Simpsons’ Hidden Talents
- HDU 2594 Simpsons’ Hidden Talents
- hdu 2594 Simpsons’ Hidden Talents
- hdu 2594 Simpsons’ Hidden Talents
- HDU 2594 Simpsons’ Hidden Talents
- hdu 2594 Simpsons’ Hidden Talents
- 数组操作
- 【Matrix-Tree定理】初探矩阵树小结
- std::bind与std::function
- java面试题
- 计算机系统的初步学习(持续更新)--关于replace_byte()函数
- HDU 2594 Simpsons’ Hidden Talents
- 常见的HTTP状态码(HTTP Status Code)说明
- 7-85 红色警报(25 分)
- MyBatis创建流程
- 数组和结构
- SpringMVC中的/*和/的区别
- 闭包
- heartbeat+Haproxy多VIP负载均衡高可用
- java面试题上