HDU 2594 Simpsons’ Hidden Talents

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.
 

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
 

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 long
using 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
该资源内项目源码是个人的课程设计、毕业设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 该资源内项目源码是个人的课程设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值