[HDU2328]Corporate Identity(后缀数组)
求 n 个串的字典序最小的最长公共子串。
和 2 个串的处理方法差不多。
把 n 个串拼接在一起,中间连上一个没有出现过的字符防止匹配过界。
求出 height 数组后二分公共子串长度给后缀数组分组。
然后 check,每一组中是否所有的字符串都包含。
直接遍历 sa 数组,第一个满足的结果就是字典序最小的。
——代码
#include <cstdio>
#include <cstring>
#include <iostream>
#define N 900005
#define M 4001 int n, len, m, start;
int buc[N], x[N], y[N], sa[N], rank[N], height[N], belong[N];
char s[N], a[M];
bool f[M]; inline void build_sa()
{
int i, k, p;
for(i = ; i < m; i++) buc[i] = ;
for(i = ; i < len; i++) buc[x[i] = s[i]]++;
for(i = ; i < m; i++) buc[i] += buc[i - ];
for(i = len - ; i >= ; i--) sa[--buc[x[i]]] = i;
for(k = ; k <= len; k <<= )
{
p = ;
for(i = len - ; i >= len - k; i--) y[p++] = i;
for(i = ; i < len; i++) if(sa[i] >= k) y[p++] = sa[i] - k;
for(i = ; i < m; i++) buc[i] = ;
for(i = ; i < len; i++) buc[x[y[i]]]++;
for(i = ; i < m; i++) buc[i] += buc[i - ];
for(i = len - ; i >= ; i--) sa[--buc[x[y[i]]]] = y[i];
std::swap(x, y);
p = , x[sa[]] = ;
for(i = ; i < len; i++)
x[sa[i]] = y[sa[i - ]] == y[sa[i]] && y[sa[i - ] + k] == y[sa[i] + k] ? p - : p++;
if(p >= len) break;
m = p;
}
} inline void build_height()
{
int i, j, k = ;
for(i = ; i < len; i++) rank[sa[i]] = i;
for(i = ; i < len; i++)
{
if(!rank[i]) continue;
if(k) k--;
j = sa[rank[i] - ];
while(s[i + k] == s[j + k] && i + k < len && j + k < len) k++;
height[rank[i]] = k;
}
} inline bool check(int k)
{
int i, cnt = ;
memset(f, , sizeof(f));
f[belong[sa[]]] = ;
for(i = ; i < len; i++)
if(height[i] >= k)
{
if(!f[belong[sa[i]]]) cnt++;
f[belong[sa[i]]] = ;
if(cnt == n)
{
start = sa[i];
return ;
}
}
else
{
memset(f, , sizeof(f));
f[belong[sa[i]]] = ;
cnt = ;
}
return ;
} int main()
{
int i, j, l, r, mid, leng;
while(scanf("%d", &n) && n)
{
len = ;
m = ;
memset(belong, , sizeof(belong));
for(i = ; i <= n; i++)
{
scanf("%s", a);
for(j = ; a[j] ^ '\0'; j++) s[len++] = a[j];
s[len++] = '#';
belong[len] = ;
}
len--;
build_sa();
build_height();
for(i = ; i < len; i++) belong[i] += belong[i - ];
l = , r = len, leng = , start = -;
while(l <= r)
{
mid = (l + r) >> ;
if(check(mid)) leng = mid, l = mid + ;
else r = mid - ;
}
if(leng && start ^ -)
{
for(i = start; i < start + leng; i++) putchar(s[i]);
putchar('\n');
}
else puts("IDENTITY LOST");
}
return ;
}
[HDU2328]Corporate Identity(后缀数组)的更多相关文章
- POJ3450 Corporate Identity —— 后缀数组 最长公共子序列
题目链接:https://vjudge.net/problem/POJ-3450 Corporate Identity Time Limit: 3000MS Memory Limit: 65536 ...
- poj 3518 Corporate Identity 后缀数组->多字符串最长相同连续子串
题目链接 题意:输入N(2 <= N <= 4000)个长度不超过200的字符串,输出字典序最小的最长公共连续子串; 思路:将所有的字符串中间加上分隔符,注:分隔符只需要和输入的字符不同, ...
- [poj3450]Corporate Identity(后缀数组)
题意:多个字符串的最长公共子串. 解题关键:字符串的任何一个子串都是这个字符串的某个后缀的前缀.求A和B的最长公共子串等价于求A的后缀和B的后缀的最长公共前缀的最大值. 后缀数组的经典例题,连接在一起 ...
- hdu2328 Corporate Identity【string库使用】【暴力】【KMP】
Corporate Identity Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- kuangbin专题十六 KMP&&扩展KMP HDU2328 Corporate Identity
Beside other services, ACM helps companies to clearly state their “corporate identity”, which includ ...
- hdu2328 Corporate Identity 扩展KMP
Beside other services, ACM helps companies to clearly state their “corporate identity”, which includ ...
- hdu2328 Corporate Identity
地址:http://acm.hdu.edu.cn/showproblem.php?pid=2328 题目: Corporate Identity Time Limit: 9000/3000 MS (J ...
- POJ-3450 Corporate Identity (KMP+后缀数组)
Description Beside other services, ACM helps companies to clearly state their “corporate identity”, ...
- POJ3450 Corporate Identity 【后缀数组】
Corporate Identity Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 7662 Accepted: 264 ...
随机推荐
- [luogu_U15118]萨塔尼亚的期末考试
https://zybuluo.com/ysner/note/1239615 题面 \(T\)次询问,求出\[\sum_{i=1}^n\frac{i}{\frac{n(n+1)}{2}}fib_i\] ...
- [luogu4735]最大异或和
https://zybuluo.com/ysner/note/1238161 题面 给定一个初始长度为\(N\)的非负整数序列\(\{a\}\). 有\(m\)个操作,操作分为两种: 在序列末尾加一个 ...
- zookeeper单机安装
安装zookeeper步骤: 1,下载zookeeper http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.14/ 2,放到合适目录,解压 ...
- Applications(模拟)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3705 题意:主要是分值计算要注意以下几点: (1) 在MOJ上解出的题,如 ...
- 拼接html 的事件转义
attach += "<div style='line-height: 10px;float: left;margin-left: 10px;' id='attach_" + ...
- ACM_Repeating Characters
Repeating Characters Time Limit: 2000/1000ms (Java/Others) Problem Description: For this problem, yo ...
- 题解报告:hdu 3790 最短路径问题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3790 Problem Description 给你n个点,m条无向边,每条边都有长度d和花费p,给你起 ...
- 未能加载文件或程序集Microsoft.SharePoint.Sandbox.dll
项目引用了MiscroSoft.SharePoint.dll程序集,编译后页面报错: 未能加载文件或程序集“Microsoft.Sharepoint.Sandbox, Version=14.0.0.0 ...
- JavaScript:常用总结
● 语法要点 ● 引用类型 ● BOM/DOM ● 事件要点 一.语法要点 1.在声明变量时,如果省去了var关键字,那么这个变量为全局变量. 2.数据类型有5种原始类型和9种引用类型: 原始类型中: ...
- iOS 从xib中加载自定义视图
想当初在学校主攻的是.NET,来到公司后,立马变成java开发,之后又跳到iOS开发,IT人这样真的好么~~ 天有不测风云,云还有变幻莫测哎,废话Over,let's go~ 新学iOS开发不久,一 ...