poj 3518 Corporate Identity 后缀数组->多字符串最长相同连续子串
题意:输入N(2 <= N <= 4000)个长度不超过200的字符串,输出字典序最小的最长公共连续子串;
思路:将所有的字符串中间加上分隔符,注:分隔符只需要和输入的字符不同,且各自不同即可,没有必要是最小的字符;
连接后缀数组求解出height之后二分长度,由于height是根据sa数组建立的,所以前面符合的就是字典序最小的,直接找到就停止即可;
ps: 把之前的模板简化了下,A题才是关键;
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<time.h>
using namespace std;
#define MS0(a) memset(a,0,sizeof(a))
typedef long long ll;
const int MAXN = ;
int sa[MAXN],t[MAXN],t2[MAXN],c[MAXN],wv[MAXN];
int cmp(int *r, int a, int b, int l){
return r[a] == r[b] && r[a+l] == r[b+l];
}
void build_sa(int *r, int n, int m){ // 倍增算法 r为待匹配数组 n为总长度 m为字符范围
int i, j, p, *x = t, *y = t2;
for(i = ; i < m; i ++) c[i] = ;
for(i = ; i < n; i ++) c[x[i] = r[i]] ++;
for(i = ; i < m; i ++) c[i] += c[i-];
for(i = n-; i >= ; i --) sa[--c[x[i]]] = i;
for(j = , p = ; p < n; j <<= , m = p){
for(p = , i = n-j; i < n; i ++) y[p++] = i;
for(i = ; i < n; i ++) if(sa[i] >= j) y[p++] = sa[i] - j;
for(i = ; i < n; i ++) wv[i] = x[y[i]];
for(i = ; i < m; i ++) c[i] = ;
for(i = ; i < n; i ++) c[wv[i]] ++;
for(i = ; i < m; i ++) c[i] += c[i-];
for(i = n-; i >= ; i --) sa[--c[wv[i]]] = y[i];
for(swap(x,y), p = , x[sa[]] = , i = ; i < n; i++){
x[sa[i]] = cmp(y, sa[i-], sa[i], j) ? p - : p++;
}
}
}
int rk[MAXN],height[MAXN];
void getHeight(int *r,int n)
{
for(int i = ;i <= n;i++) rk[sa[i]] = i; // rk[i]:后缀i在sa[]中的下标
for(int i = ,j,k = ; i < n; height[rk[i++]] = k){
for(k? k--: ,j = sa[rk[i] - ];r[i+k] == r[j+k];k++);
}
}
int d[MAXN],num[MAXN],vs[],T,len,tot;
char tt[];
bool check(int L)
{
MS0(vs);
int cnt = ;
for(int i = ;i <= tot;i++){
if(height[i] < L){
MS0(vs);
cnt = ;
continue;
}
if(!vs[d[sa[i]]]){
vs[d[sa[i]]] = ;cnt++;
}
if(!vs[d[sa[i-]]]){
vs[d[sa[i-]]] = ;cnt++;
}
if(cnt == T){ //对于同一个长度,我们只取第一次出现的符合条件的字符串;
for(int j = ; j<L; j++)
tt[j] = num[sa[i]+j];
tt[L] = '\0';
return true;
}
}
return false;
}
char str[];
int main()
{
while(scanf("%d",&T) == && T){
tot = ;
for(int i = ;i <= T;i++){
scanf("%s",str);
len = strlen(str);
for(int j = ;j < len;j++)
num[tot] = str[j],d[tot++] = i;
num[tot] = 'z'+i,d[tot++] = 'z'+i;// '#' + i竟然WA了
}
num[tot] = ;//最后添加一个最小字符;
build_sa(num,tot+,);
getHeight(num,tot);
int ans = ,l = ,r = len,mid,id;
while(l <= r){
mid = l + r >> ;
if(check(mid)){ans = mid,l = mid + ;}
else r = mid - ;
}
if(ans){
printf("%s\n",tt);
}
else puts("IDENTITY LOST");
}
return ;
}
poj 3518 Corporate Identity 后缀数组->多字符串最长相同连续子串的更多相关文章
- POJ3450 Corporate Identity —— 后缀数组 最长公共子序列
题目链接:https://vjudge.net/problem/POJ-3450 Corporate Identity Time Limit: 3000MS Memory Limit: 65536 ...
- [poj3450]Corporate Identity(后缀数组)
题意:多个字符串的最长公共子串. 解题关键:字符串的任何一个子串都是这个字符串的某个后缀的前缀.求A和B的最长公共子串等价于求A的后缀和B的后缀的最长公共前缀的最大值. 后缀数组的经典例题,连接在一起 ...
- POJ2774 Long Long Message —— 后缀数组 两字符串的最长公共子串
题目链接:https://vjudge.net/problem/POJ-2774 Long Long Message Time Limit: 4000MS Memory Limit: 131072 ...
- [poj 1743] Musical Theme 后缀数组 or hash
Musical Theme 题意 给出n个1-88组成的音符,让找出一个最长的连续子序列,满足以下条件: 长度大于5 不重叠的出现两次(这里的出现可以经过变调,即这个序列的每个数字全都加上一个整数x) ...
- 利用后缀数组(suffix array)求最长公共子串(longest common substring)
摘要:本文讨论了最长公共子串的的相关算法的时间复杂度,然后在后缀数组的基础上提出了一个时间复杂度为o(n^2*logn),空间复杂度为o(n)的算法.该算法虽然不及动态规划和后缀树算法的复杂度低,但其 ...
- poj 3294 后缀数组 多字符串中不小于 k 个字符串中的最长子串
Life Forms Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 16223 Accepted: 4763 Descr ...
- 后缀数组(模板题) - 求最长公共子串 - poj 2774 Long Long Message
Language: Default Long Long Message Time Limit: 4000MS Memory Limit: 131072K Total Submissions: 21 ...
- POJ 3450 Corporate Identity(KMP)
[题目链接] http://poj.org/problem?id=3450 [题目大意] 求k个字符串的最长公共子串,如果有多个答案,则输出字典序最小的. [题解] 我们对第一个串的每一个后缀和其余所 ...
- POJ 1226 Substrings(后缀数组+二分答案)
[题目链接] http://poj.org/problem?id=1226 [题目大意] 求在每个给出字符串中出现的最长子串的长度,字符串在出现的时候可以是倒置的. [题解] 我们将每个字符串倒置,用 ...
随机推荐
- Emacs 配置 Python 编程环境
python编程环境设置涉及到:自动完成.语法检查.虚拟环境. 为了不把系统搞乱,在python的虚拟环境中安装相关的插件. 一.安装python虚拟环境 virtualenvwrapper sudo ...
- SQL Server :事务和锁
1.事务 事务概念:全部执行或全部不执行的一条或者多条语句的组合 例子说明:到银行里转账,将一个账户(Tom)里的100元钱转到另一个账户(Jake) update table money=money ...
- [转]Web性能监控自动化探索之路–初识WebPageTest
本文转自:http://www.webryan.net/2013/01/use-webpagetest-to-analyze-web-performance/ 无论是从Velocity 2012还是在 ...
- Win10开机小键盘不亮解决办法
1.首先修改注册表打开[HKEY_USERS\.DEFAULT\Control Panel\Keyboard]项,将"InitialKeyboardIndicators"的键值从& ...
- 原生js 实现的瀑布流
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- 设计模式——设计模式之禅day2
接口隔离原则 接口分为两种: ● 实例接口( Object Interface) , 在Java中声明一个类, 然后用new关键字产生一个实例, 它是对一个类型的事物的描述, 这是一种接口. 比如你定 ...
- SQL自动补充其他月份为0
,) ), Sales int,Dates datetime) insert into ProductSale ,'2014-01-05' UNION ALL ,'2014-02-05' UNION ...
- CF下Split的使用
在Compact Framework 下 String的Split不能正确的处理 字符串 分割字符串 如对字符串 "abcfdef12abcedef23" 以"ef&qu ...
- Xcode7主题路径
// Xcode7主题路径~/Library/Developer/Xcode/UserData/FontAndColorThemes
- IOS-UI-UIDynamic(一)
UIDynamic是从iOS7开始引入的技术 属于UIkit框架 可以模拟显示生活中的物理现象 如碰撞 抖动 摆动等 一. 使用UIDynamic步骤: 1.创建一个动力效果器UIDynamicAni ...