POJ 2774 后缀数组:查找最长公共子
思考:其实很easy。就在两个串在一起。通过一个特殊字符,中间分隔,然后找到后缀数组的最长的公共前缀。然后在两个不同的串,最长是最长的公共子串。
注意的是:用第一个字符串来推断是不是在同一个字符中,刚開始用了第二个字符的长度来推断WA了2发才发现。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<cmath>
#include<bitset>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson i<<1,l,mid
#define rson i<<1|1,mid+1,r
#define llson j<<1,l,mid
#define rrson j<<1|1,mid+1,r
#define INF 0x7fffffff
#define maxn 200010
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
void radix(int *str,int *a,int *b,int n,int m)
{
static int count[maxn];
mem(count,0);
for(int i=0; i<n; i++) ++count[str[a[i]]];
for(int i=1; i<=m; i++) count[i]+=count[i-1];
for(int i=n-1; i>=0; i--) b[--count[str[a[i]]]]=a[i];
}
void suffix(int *str,int *sa,int n,int m) //倍增算法计算出后缀数组sa
{
static int rank[maxn],a[maxn],b[maxn];
for(int i=0; i<n; i++) rank[i]=i;
radix(str,rank,sa,n,m);
rank[sa[0]]=0;
for(int i=1; i<n; i++)
rank[sa[i]]=rank[sa[i-1]]+(str[sa[i]]!=str[sa[i-1]]);
for(int i=0; 1<<i<n; i++)
{
for(int j=0; j<n; j++)
{
a[j]=rank[j]+1;
b[j]=j+(1<<i)>=n?0:rank[j+(1<<i)]+1;
sa[j]=j;
}
radix(b,sa,rank,n,n);
radix(a,rank,sa,n,n);
rank[sa[0]]=0;
for(int j=1; j<n; j++)
rank[sa[j]]=rank[sa[j-1]]+(a[sa[j-1]]!=a[sa[j]]||b[sa[j-1]]!=b[sa[j]]);
}
}
void calcHeight(int *str,int *sa,int *h,int *rank,int n) //求出最长公共前缀数组h
{
int k=0;
h[0]=0;
for(int i=0; i<n; i++) rank[sa[i]]=i;
for(int i=0; i<n; i++)
{
k=k==0?0:k-1;
if(rank[i])
while(str[i+k]==str[sa[rank[i]-1]+k]) k++;
else k=0;
h[rank[i]]=k;
}
}
int a[maxn],sa[maxn],height[maxn],rank[maxn];
string s,ss;
int main()
{
//freopen("1.txt","r",stdin);
while(cin>>s>>ss)
{
ss=s+"#"+ss;
copy(ss.begin(),ss.end(),a);
int n=ss.size(),len=0;
suffix(a,sa,n,256);
calcHeight(a,sa,height,rank,n);
for(int i=1; i<n; i++)
if(height[i]>len&&((sa[i]<s.size())!=sa[i-1]<s.size()))
len=height[i];
cout<<len<<endl;
}
return 0;
}
/*
jworerrrrr
rrreeeeeeeee
abcd
stedste
*/
版权声明:本文博主原创文章,博客,未经同意不得转载。
POJ 2774 后缀数组:查找最长公共子的更多相关文章
- 使用后缀数组寻找最长公共子字符串JavaScript版
后缀数组很久很久以前就出现了,具体的概念读者自行搜索,小菜仅略知一二,不便讨论. 本文通过寻找两个字符串的最长公共子字符串,演示了后缀数组的经典应用. 首先需要说明,小菜实现的这个后缀数组算法,并非标 ...
- POJ 2774 Long Long Message [ 最长公共子串 后缀数组]
题目:http://poj.org/problem?id=2774 Long Long Message Time Limit: 4000MS Memory Limit: 131072K Total ...
- poj 2774 后缀数组 两个字符串的最长公共子串
Long Long Message Time Limit: 4000MS Memory Limit: 131072K Total Submissions: 31904 Accepted: 12 ...
- POJ 2774 后缀数组
题目链接:http://poj.org/problem?id=2774 题意:给定两个只含小写字母的字符串,求字符串的最长公共子串长度. 思路:根据<<后缀数组——处理字符串的有力工具&g ...
- poj2774 Long Long Message 后缀数组求最长公共子串
题目链接:http://poj.org/problem?id=2774 这是一道很好的后缀数组的入门题目 题意:给你两个字符串,然后求这两个的字符串的最长连续的公共子串 一般用后缀数组解决的两个字符串 ...
- HDU 1403 Longest Common Substring(后缀数组,最长公共子串)
hdu题目 poj题目 参考了 罗穗骞的论文<后缀数组——处理字符串的有力工具> 题意:求两个序列的最长公共子串 思路:后缀数组经典题目之一(模版题) //后缀数组sa:将s的n个后缀从小 ...
- Long Long Message POJ - 2774 后缀数组
The little cat is majoring in physics in the capital of Byterland. A piece of sad news comes to him ...
- poj 1743 后缀数组 求最长不重叠重复子串
题意:有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1..88范围内的整数,现在要找一个重复的主题. “主题”是整个音符序列的一个子串,它需要满足如下条件:1 ...
- POJ 2774 后缀数组 || 二分+哈希
Long Long Message Time Limit: 4000MS Memory Limit: 131072K Total Submissions: 35607 Accepted: 14 ...
随机推荐
- POJ 3264-Balanced Lineup(段树:单点更新,间隔查询)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 34522 Accepted: 16224 ...
- 怎么样MyEclipse配置Tomcat?
1.下载tomcat免安装版.tomcat路径不包含空格 http://download.csdn.net/detail/u014112584/7549191 2.windows -preferenc ...
- 如何设置一个activity透明(转)
1.在AndroidManifest.xml文件中设置: ? 1 android:theme="@android:style/Theme.Translucent 此代码固定为全背景透明. 2 ...
- IIS的WebGarden、WebFarm和StateServer
开启IIS的WebGarden.WebFarm和StateServer之旅 前言 公司系统虽然配置有1台NLB后拖4台App Server最后搭一台强劲无比的DB Server,但每天下午4点左右总被 ...
- libpomelo 增加编译静态库cocos2d-x xcode 工程
离 https://github.com/NetEase/libpomelo 下载最新版本.拉开拉链,静态库 ./pomelo_gyp -DTO=ios ./build_ios ./build_ios ...
- 小米2S 中文和英文支持TWRP,真实双系统支持
经过我几天的努力小米2S的TWRP 的功能已经完美了. 支持功能 : 中文和英文显示能相互切换 真实双系统功能已经完成95%. 刷入手机方法.由于时间原因我只制作了img文件.没有制作成卡刷包格式. ...
- SSH证书登录方式(无password验证登录)
经常在工作中须要在各个Linux机间进行跳转,每次password的输入成了麻烦,并且也不安全.在实际使用中,在windows下常使用secureCRT工具或teraterm工具进行SSH登录.以及实 ...
- cocos2dx环境配置和打包
安装软件准备就绪: vs2012 cocos2d-x-2.2.1 adt-bundle-windows-x86_64-20121030 android-ndk-r9c-windows-x86_64 j ...
- javascript中的三角学
三角学主要研究三角形和它们的边角关系,包含一个90度角的三角形被称为直角三角形.在这里主要研究直角三角形相关的知识. 1. 角度和弧度 360(角度) = 2*Math.PI(弧度) degrees ...
- LatinIME输入法分析
输入法的设置在res/xml/method.xml的<input-method>标签中,主要设置两个属性: android:settingsActivity,输入法的设置程序入口. and ...