SPOJ PHRASES Relevant Phrases of Annihilation(后缀数组 + 二分)题解
题意:
给\(n\)个串,要你求出一个最长子串\(A\),\(A\)在每个字串至少都出现\(2\)次且不覆盖,问\(A\)最长长度是多少
思路:
后缀数组处理完之后,二分这个长度,可以\(O(n)\)验证可行性,注意是“不覆盖”(英文不好看不懂),随便搞一下就好了。
代码:
#include<map>
#include<set>
#include<queue>
#include<cmath>
#include<stack>
#include<ctime>
#include<string>
#include<vector>
#include<cstdio>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 2e5 + 10;
const int INF = 0x3f3f3f3f;
const ull seed = 11;
const int MOD = 1e9 + 7;
using namespace std;
int str[maxn];
int t1[maxn], t2[maxn], c[maxn];
int sa[maxn];
int rk[maxn];
int height[maxn];
bool cmp(int *r, int a, int b, int l){
return r[a] == r[b] && r[a + l] == r[b + l];
}
void da(int *str, int n, int m){
n++;
int i, j, p, *x = t1, *y = t2;
for(i = 0; i < m; i++) c[i] = 0;
for(i = 0; i < n; i++) c[x[i] = str[i]]++;
for(i = 1; i < m; i++) c[i] += c[i - 1];
for(i = n - 1; i >= 0; i--) sa[--c[x[i]]] = i;
for(j = 1; j <= n; j <<= 1){
p = 0;
for(i = n - j; i < n; i++) y[p++] = i;
for(i = 0; i < n; i++) if(sa[i] >= j) y[p++] = sa[i] - j;
for(i = 0; i < m; i++) c[i] = 0;
for(i = 0; i < n; i++) c[x[y[i]]]++;
for(i = 1; i < m; i++) c[i] += c[i - 1];
for(i = n - 1; i >= 0; i--) sa[--c[x[y[i]]]] = y[i];
swap(x, y);
p = 1; x[sa[0]] = 0;
for(i = 1; i < n; i++)
x[sa[i]] = cmp(y, sa[i - 1], sa[i], j)? p - 1 : p++;
if(p >= n) break;
m = p;
}
int k = 0;
n--;
for(i = 0; i <= n; i++) rk[sa[i]] = i;
for(i = 0; i < n; i++){
if(k) k--;
j = sa[rk[i] - 1];
while(str[i + k] == str[j + k]) k++;
height[rk[i]] = k;
}
}
char s[maxn];
int belong[maxn];
int num[15], pre[15];
int tot;
bool judge(int n){
for(int i = 1; i <= n; i++){
if(num[i] < 2) return false;
}
return true;
}
bool check(int len, int cnt, int n){
memset(num, 0, sizeof(num));
memset(pre, -1, sizeof(pre));
for(int i = 2; i <= cnt; i++){
if(height[i] >= len){
if(pre[belong[sa[i - 1]]] == -1){
num[belong[sa[i - 1]]]++;
pre[belong[sa[i - 1]]] = sa[i - 1];
}
else if(abs(pre[belong[sa[i - 1]]] - sa[i - 1]) >= len){
num[belong[sa[i - 1]]]++;
}
if(pre[belong[sa[i]]] == -1){
num[belong[sa[i]]]++;
pre[belong[sa[i]]] = sa[i];
}
else if(abs(pre[belong[sa[i]]] - sa[i]) >= len){
num[belong[sa[i]]]++;
}
}
else{
if(judge(n)) return true;
memset(num, 0, sizeof(num));
memset(pre, -1, sizeof(pre));
}
}
return judge(n);
}
int main(){
int n;
int T;
scanf("%d", &T);
while(T--){
scanf("%d", &n);
int cnt = 0;
for(int i = 1; i <= n; i++){
scanf("%s", s);
int len = strlen(s);
for(int j = 0; j < len; j++){
belong[cnt] = i;
str[cnt++] = s[j];
}
belong[cnt] = -i;
str[cnt++] = i;
}
cnt--;
str[cnt] = 0;
da(str, cnt, 130);
int l = 1, r = 10000;
int Max = 0;
while(l <= r){
int m = (l + r) >> 1;
if(check(m, cnt, n)){
Max = m;
l = m + 1;
}
else{
r = m - 1;
}
}
printf("%d\n", Max);
}
return 0;
}
SPOJ PHRASES Relevant Phrases of Annihilation(后缀数组 + 二分)题解的更多相关文章
- SPOJ - PHRASES Relevant Phrases of Annihilation —— 后缀数组 出现于所有字符串中两次且不重叠的最长公共子串
题目链接:https://vjudge.net/problem/SPOJ-PHRASES PHRASES - Relevant Phrases of Annihilation no tags You ...
- SPOJ - PHRASES Relevant Phrases of Annihilation
传送门:SPOJ - PHRASES(后缀数组+二分) 题意:给你n个字符串,找出一个最长的子串,他必须在每次字符串中都出现至少两次. 题解:被自己蠢哭...记录一下自己憨憨的操作,还一度质疑评测鸡( ...
- 【SPOJ 220】 PHRASES - Relevant Phrases of Annihilation
[链接]h在这里写链接 [题意] 给你n(n<=10)个字符串. 每个字符串长度最大为1e4; 问你能不能找到一个子串. 使得这个子串,在每个字符串里面都不想交出 ...
- BZOJ 3230: 相似子串( RMQ + 后缀数组 + 二分 )
二分查找求出k大串, 然后正反做后缀数组, RMQ求LCP, 时间复杂度O(NlogN+logN) -------------------------------------------------- ...
- BZOJ_2946_[Poi2000]公共串_后缀数组+二分答案
BZOJ_2946_[Poi2000]公共串_后缀数组+二分答案 Description 给出几个由小写字母构成的单词,求它们最长的公共子串的长度. 任务: l 读入单 ...
- 【bzoj4310】跳蚤 后缀数组+二分
题目描述 很久很久以前,森林里住着一群跳蚤.一天,跳蚤国王得到了一个神秘的字符串,它想进行研究. 首先,他会把串分成不超过 k 个子串,然后对于每个子串 S,他会从S的所有子串中选择字典序最大的那一个 ...
- BZOJ 1717 [USACO06DEC] Milk Patterns (后缀数组+二分)
题目大意:求可重叠的相同子串数量至少是K的子串最长长度 洛谷传送门 依然是后缀数组+二分,先用后缀数组处理出height 每次二分出一个长度x,然后去验证,在排序的后缀串集合里,有没有连续数量多于K个 ...
- POJ 1743 [USACO5.1] Musical Theme (后缀数组+二分)
洛谷P2743传送门 题目大意:给你一个序列,求其中最长的一对相似等长子串 一对合法的相似子串被定义为: 1.任意一个子串长度都大于等于5 2.不能有重叠部分 3.其中一个子串可以在全部+/-某个值后 ...
- Poj 1743 Musical Theme(后缀数组+二分答案)
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 28435 Accepted: 9604 Descri ...
- Poj 3261 Milk Patterns(后缀数组+二分答案)
Milk Patterns Case Time Limit: 2000MS Description Farmer John has noticed that the quality of milk g ...
随机推荐
- Linux下载并安装JDK1.8
https://blog.csdn.net/Future_LL/article/details/84667634
- 迈凯伦765LT/600LT/720S/650S/570S维修手册电路图Mclaren车间手册接线图
全套迈凯伦维修手册电路图Mclaren车间手册线路图:语言:English,German,French,Spanish,Chinese,Japanese.McLaren迈凯伦新GT维修手册电路图零配件 ...
- Py其他内置函数,文件修改
其他内置函数 1.abs函数,取绝对值 print(abs(-1)) 2.all函数,判断可迭代对象是否全为真,有假直接假 假:0,'',None print(all([1,2,'1'])) prin ...
- 【AtCoder Beginner Contest 181】A~F题解
越学越菜系列 于2020.11.2,我绿了(错乱) A - Heavy Rotation 签到题,奇数Black,偶数White. code: #include<bits/stdc++.h> ...
- .net core 不同地区时间相互转换
.net core 不同地区时间相互转换 //韩国时间转换成当前时间 //value=需要转换的时间 //Korea Standard Tim 韩国时间 //China Standard Time 中 ...
- 7. A typical stream socket session
http://publibfp.dhe.ibm.com/epubs/pdf/f1a2d400.pdf Read and write data on socket s, using the send() ...
- Linux 文件搜索神器 find 实战详解,建议收藏!
大家好,我是肖邦,这是我的第 10 篇原创文章. 在 Linux 系统使用中,作为一个管理员,我希望能查找系统中所有的大小超过 200M 文件,查看近 7 天系统中哪些文件被修改过,找出所有子目录中的 ...
- SpringMVC听课笔记(九:数据转换 & 数据格式化 & 数据校验)
1.数据绑定流程 --1). Spring MVC主框架将ServletRequest对象及目标方法入参实例传递给WebDataBinderFactory实例,以创建DataBinder实例对象. - ...
- Oracle数据库误删除数据恢复(Oracle闪回功能)
一.启用行移动功能 alter table table_name enable row movement ; 二.可查询之前某一个时间点的数据(默认是1440分钟,即24小时内的数据) select ...
- Ajax(form表单文件上传、请求头之contentType、Ajax传递json数据、Ajax文件上传)
form表单文件上传 上菜 file_put.html <form action="" method="post" enctype="multi ...