hdu 2594 Simpsons’ Hidden Talents(扩展kmp)
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.
The lengths of s1 and s2 will be at most 50000.
#include <cstdio>
#include <map>
#include <iostream>
#include<cstring>
#include<bits/stdc++.h>
#define ll long long int
#define M 6
using namespace std;
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
int moth[]={,,,,,,,,,,,,};
int dir[][]={, ,, ,-, ,,-};
int dirs[][]={, ,, ,-, ,,-, -,- ,-, ,,- ,,};
const int inf=0x3f3f3f3f;
const ll mod=1e9+;
int nextt[];
int extend[];
void getnext(string s){
int po; int len=s.length();
nextt[]=len; //初始化next[0]
int pos=;
while(s[pos]==s[pos+]&&pos<len-) pos++;
nextt[]=pos; //计算next[1]
po=; //初始化po的位置
for(int i=;i<len;i++){
if(nextt[i-po]+i<po+nextt[po]) //第一种情况,可以直接得到next[i]的值
nextt[i]=nextt[i-po];
else{ //第二种情况,要继续匹配才能得到next[i]的值
int j=po+nextt[po]-i;
if(j<) j=;
while(i+j<len&&s[j]==s[i+j]) j++;
nextt[i]=j;
po=i;
}
//cout<<nextt[i]<<endl;
}
}
void exkmp(string a,string b){
int len1=a.length(); int len2=b.length();
getnext(a);
int po; int pos=;
while(a[pos]==b[pos]&&pos<len1&&pos<len2) pos++;
extend[]=pos;
po=;
for(int i=;i<len2;i++){
if(nextt[i-po]+i<extend[po]+po)
extend[i]=nextt[i-po];
else{
int j=po+extend[po]-i;
if(j<) j=;
while(i+j<len2&&a[j]==b[i+j]&&j<len1) j++;
extend[i]=j;
po=i;
}
}
}
int main(){
ios::sync_with_stdio(false);
string s1,s2;
while(cin>>s1>>s2){
exkmp(s1,s2);
int len1=s1.length(); int len2=s2.length();
int maxn=-inf;
int maxn_i;
for(int i=;i<len2;i++){
//cout<<extend[i]<<endl;
if(extend[i]+i==len2&&maxn<extend[i]){
maxn=extend[i];
maxn_i=i;
}
}
if(maxn==-inf) cout<<""<<endl;
else{ for(int i=maxn_i;i<maxn_i+maxn;i++)
cout<<s2[i];
cout<<" "<<maxn;
cout<<endl;
}
}
return ;
}
hdu 2594 Simpsons’ Hidden Talents(扩展kmp)的更多相关文章
- HDU 2594 Simpsons’ Hidden Talents(KMP的Next数组应用)
Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
- HDU 2594 Simpsons’ Hidden Talents (KMP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2594 这题直接用KMP算法就能够做出来,只是我还尝试了用扩展的kmp,这题用扩展的KMP效率没那么高. ...
- hdu 2594 Simpsons’ Hidden Talents 【KMP】
题目链接:http://acm.acmcoder.com/showproblem.php?pid=2594 题意:求最长的串 同一时候是s1的前缀又是s2的后缀.输出子串和长度. 思路:kmp 代码: ...
- HDU 2594 Simpsons’ Hidden Talents(辛普森一家的潜在天赋)
HDU 2594 Simpsons’ Hidden Talents(辛普森一家的潜在天赋) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 3 ...
- hdu 2594 Simpsons’ Hidden Talents KMP
Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
- hdu 2594 Simpsons’ Hidden Talents KMP应用
Simpsons’ Hidden Talents Problem Description Write a program that, when given strings s1 and s2, fin ...
- hdu 2594 Simpsons’ Hidden Talents(KMP入门)
Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
- hdoj 2594 Simpsons’ Hidden Talents 【KMP】【求串的最长公共前缀后缀】
Simpsons' Hidden Talents Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
- HDU 2594 Simpsons’ Hidden Talents(KMP求s1前缀和s2后缀相同部分)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2594 题目大意:给两串字符串s1,s2,,找到最长子串满足既是s1的前缀又是s2的后缀,输出子串,及相 ...
随机推荐
- vue 动态插入组件
HTML代码: <div id="app"> <p>{{ message }}</p> <button @click="add( ...
- 连接mysql 出现 1005 error(150) , error(121)的错误
1.显示不能创建表 出现150错误 将检查是因为 我的user 表示拷贝过来的所以它设置的编码格式是utf-8 而我又新创建的表没有添加编码格式,所以它认为这两个关联的表之间的编码格式不匹配. 2.出 ...
- Keras和tensorflow的区别
参考: https://blog.csdn.net/zhangbaoanhadoop/article/details/82111056
- mysql数据库的备份和还原的总结
mysql数据库的备份和还原的总结 (来自一运维同事的总结) 1. 备份方式: 热备:数据库在线进行备份,不影响读和写的在线备份方式! 温备:数据库在线进行备份,对表备份时先锁定写操作,仅可以执行读操 ...
- jdbc工具类1.0
package cn.zhouzhou; import java.sql.Connection; import java.sql.Driver; import java.sql.DriverManag ...
- CSS 的三种样式 内联 内部 外部
CSS:层叠样式表的缩写 就是 Cascading Style Sheets Cascading Style Sheets : 层叠样式表 优先级问题 :遵守就近原则 内联> 内部>外部 ...
- 解决Safari页面缓存的问题
在开发一个移动应用的过程中,遇到问题:在订单确认页,用户点击 收货地址链接,跳转到地址选择页面,咋选一个地址,跳转回订单确认页,发现收货地址没有改变,还是最开始的地址. 用Android手机发现地址有 ...
- Lodop打印旋转180度 倒着打
方法1:打印出来后,直接把纸张倒过来.如果本身是白纸,打印机出纸内容是倒着的,可以打出来后手动倒着把纸张正过来.如果本身不是白纸,需要打印的纸张上有背景,调整进纸方向.(如果是卷纸,卷纸背景是反的,查 ...
- Spring Boot 构建电商基础秒杀项目 (二) 使用 Spring MVC 方式获取用户信息
SpringBoot构建电商基础秒杀项目 学习笔记 修改 DOMapper 在 UserPasswordDOMapper.xml 添加: <select id="selectByUse ...
- Asp.Net Core 输出 Word
In one of the ASP.NET Core projects we did in the last year, we created an OutputFormatter to provid ...