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 ...
随机推荐
- C语言 cgi(3)
1cs3157 – Advanced ProgrammingSummer 2014, Project 1, 150 pointsJune 17, 2014Follow these step-by-st ...
- Path相关评论的方法(一)
以前的主要是关于Canvas的translate(平移) .scale(缩放) .rotate(旋转) .skew(错切).接下来几篇主要讲下android里的Path(封装了贝塞尔曲线)& ...
- DB2 “The transaction log for the database is full” 存在的问题及解决方案
DB2在执行一个大的insert/update操作的时候报"The transaction log for the database is full.. "错误,查了一下文档是DB ...
- C++面试宝典2011版
1.new.delete.malloc.free关系 delete会调用对象的析构函数,和new相应free仅仅会释放内存,new调用构造函数.malloc与free是C++/C语言的标准库函数,ne ...
- 十大经典数据挖掘算法(9) 朴素贝叶斯分类器 Naive Bayes
贝叶斯分类器 贝叶斯分类分类原则是一个对象的通过先验概率.贝叶斯后验概率公式后计算,也就是说,该对象属于一类的概率.选择具有最大后验概率的类作为对象的类属.现在更多的研究贝叶斯分类器,有四个,每间:N ...
- Microsoft Fakes进行单元测试
使用Microsoft Fakes进行单元测试(1) 一:什么是单元测试 单元测试是对软件进行准确性验证的步骤.单元测试并不进行整个软件功能的测试,仅仅是对于最小工作单元的测试.一般最小工作单元就 ...
- 【C语言的日常实践(十四)】constkeyword详细解释
const是C语言keyword,它定义一个变量不同意变更.使用const在一定程度上,可以提高节目的安全性和可靠性.其他.解const的作用,在看别人的代码时,对理解对方的程序有一定帮助. 1.co ...
- 2.大约QT数据库操作,简单的数据库连接操作,增删改查数据库,QSqlTableModel和QTableView,事务性操作,大约QItemDelegate 代理
Linux下的qt安装,命令时:sudoapt-get install qt-sdk 安装mysql数据库,安装方法參考博客:http://blog.csdn.net/tototuzuoquan ...
- [LeetCode299]Bulls and Cows
题目: You are playing the following Bulls and Cows game with your friend: You write down a number and ...
- 【原创】构建高性能ASP.NET站点之二 优化HTTP请求(前端)
原文:[原创]构建高性能ASP.NET站点之二 优化HTTP请求(前端) 构建高性能ASP.NET站点之二 优化HTTP请求(前端) 前言: 这段时间比较的忙,文章写不是很勤,希望大家谅解. 上一篇文 ...