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 ...
随机推荐
- 24V转5V稳压芯片,高效率的同步降压DC-DC变换器3A输出电流
PW2330开发了一种高效率的同步降压DC-DC变换器3A输出电流.PW2330在4.5V到30V的宽输入电压范围内工作集成主开关和同步开关,具有非常低的RDS(ON)以最小化传导损失.PW2330采 ...
- 关于java并发场景下,HttpServletRequst中session丢失问题
使用场景: 在list数据进来之后使用安全数组 Lists.newCopyOnWriteArrayList() 进行了 parallelStream 并行处理,在接口中进行了登录者信息接口 ...
- 庐山真面目之十一微服务架构手把手教你搭建基于Jenkins的企业级CI/CD环境
庐山真面目之十一微服务架构手把手教你搭建基于Jenkins的企业级CI/CD环境 一.介绍 说起微服务架构来,有一个环节是少不了的,那就是CI/CD持续集成的环境.当然,搭建CI/CD环境的工具很多, ...
- jmeter跳过验证码登录配置:通过手动添加 Cookie 跳过带验证码的登录接口
目录 一.基本配置 二.HTTP请求默认值 三.HTTP信息头管理器 四.HTTP Cookie管理器 五.线程组下接口设置 一.基本配置 二.HTTP请求默认值 (1)jmeter的设置: (2)设 ...
- tcp的3次握手4次挥手
- ByteDance 2019 春招题目
牛客网字节跳动笔试真题:https://www.nowcoder.com/test/16516564/summary 分了 2 次做,磕磕碰碰才写完,弱鸡悲鸣. 1. 聪明的编辑 题目:Link . ...
- Spring MVC—模型数据,转发重定向,静态资源处理方式
Spring MVC处理模型数据 添加模型数据的方法 ModelAndView Map及Model SessionAttribute ModelAttribute Spring MVC转发和重定向 S ...
- Java 8新特性(Lambda,Stream API)
由于最近总监要求学习Java 8的一些知识,就去网上找了 一套教程来学习学习,将学习结果做一个小的总结记录,方便以后使用: 1.Java 8的优点 2.Lambda表达式优点 2.1Lambda实例 ...
- Docker及其使用思维导图
学习Docker的使用. 包括Docker的一些命令,Docker容器的使用,Docker镜像的使用,Docker容器连接等内容. 各种思维导图下载地址 Docker使用思维导图及各各种命令 Dock ...
- CentOS 安装TFTP
1.当然是使用yum安装最直接,一共会安装3个东东tftp.i386tftp-server.i386xinetd.i386[root@localhost CentOS]# yum -y install ...