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的后缀,输出子串,及相 ...
随机推荐
- c# winform导出Excel
//需要注意添加引用Microsoft.Office.Interop.Excel.dll string fileName =DateTime.Now.Year+ DateTime.Now.Month+ ...
- windows 内建环境变量
PS C:\Windows> ls env: Name Value ---- ----- _NT_SYMBOL_PATH srv*C:\Users\vv\Documents\symbols AL ...
- mysql [assword expired
mysql 5.6 在使用Navicat在其他机器上进行远程登录数据库时 会出现 password expired ,需要重新设置一下密码. SET PASSWORD FOR 'root'@'%' = ...
- w3c JS测试
到W3c的js测试里面溜达了一圈: 做错了几道题: 外部脚本必须包含<script>标签吗? 否!! 这里的外部脚本是指xx.js这个文件,在文件中写js代码是不需要包含script标签的 ...
- 微信小程序自定义组件
要做自定义组件,我们先定一个小目标,比如说我们在小程序中实现一下 WEUI 中的弹窗组件,基本效果图如下. Step1 我们初始化一个小程序(本示例基础版本库为 1.7 ),删掉里面的示例代码,并新建 ...
- centOS 7下无法启动网络(service network start)错误解决办法
今天在centOS 7下更改完静态ip后发现network服务重启不了,翻遍了网络,尝试了各种方法,终于解决了. 现把各种解决方法归纳整理,希望能让后面的同学少走点歪路... 首先看问题:执行serv ...
- 如何在MAC上运行exe程序
1. 首先下载并运行CrossOver 运行CrossOver需要收费,试用期为14天,运行CrossOver 2. 选择exe应用程序,新建容器,安装exe程序 3.安装成功后,运行exe应用程序启 ...
- python设计模式第十七天【解释器模式】
1.应用场景 (1)解释预先定义的文法 2.代码实现 #!/usr/bin/env python #!_*_ coding:UTF-8 _*_ from abc import ABCMeta, abs ...
- pip 升级
pip install --upgrade qrcode pip install --upgrade qrcode==5.3
- 前后端进行数据交互时候 要优先考虑json格式即简直对形式,[{}] 列表形式 等便于操作的数据结构
前后端进行数据交互时候 要优先考虑json格式即简直对形式,[{}] 列表形式 等便于操作的数据结构