SPOJ - PHRASES K - Relevant Phrases of Annihilation
K - Relevant Phrases of Annihilation
题目大意:给你 n 个串,问你最长的在每个字符串中出现两次且不重叠的子串的长度。
思路:二分长度,然后将height分块,看是否存在一个块里面 每个串都符合条件。
#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define pii pair<int, int>
#define y1 skldjfskldjg
#define y2 skldfjsklejg using namespace std; const int N = 1e5 + ;
const int M = 1e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f; char s[N], str[N];
int n, m, b[N];
int sa[N], t[N], t2[N], c[N], rk[N], height[N];
int L[], R[]; void buildSa(char *s, int n, int m) {
int i, j = , k = , *x = t, *y = t2;
for(i = ; i < m; i++) c[i] = ;
for(i = ; i < n; i++) c[x[i] = s[i]]++;
for(i = ; i < m; i++) c[i] += c[i - ];
for(i = n - ; i >= ; i--) sa[--c[x[i]]] = i;
for(int k = ; k <= n; k <<= ) {
int p = ;
for(i = n - k; i < n; i++) y[p++] = i;
for(i = ; i < n; i++) if(sa[i] >= k) y[p++] = sa[i] - k;
for(i = ; i < m; i++) c[i] = ;
for(i = ; i < n; i++) c[x[y[i]]]++;
for(i = ; i < m; i++) c[i] += c[i - ];
for(i = n - ; i >= ; i--) sa[--c[x[y[i]]]] = y[i];
swap(x, y);
p = ; x[sa[]] = ;
for(int i = ; i < n; i++) {
if(y[sa[i - ]] == y[sa[i]] && y[sa[i - ] + k] == y[sa[i] + k])
x[sa[i]] = p - ;
else x[sa[i]] = p++;
}
if(p >= n) break;
m = p;
} for(i = ; i < n; i++) rk[sa[i]] = i;
for(i = ; i < n - ; i++) {
if(k) k--;
j = sa[rk[i] - ];
while(s[i + k] == s[j + k]) k++;
height[rk[i]] = k;
}
} bool check(int len, int n) {
int l = , r;
while(l <= n) {
for(int i = ; i <= m; i++) L[i] = inf, R[i] = -inf;
r = l;
L[b[sa[l]]] = min(L[b[sa[l]]], sa[l]);
R[b[sa[l]]] = max(R[b[sa[l]]], sa[l]);
while(r < n && height[r + ] >= len) {
r++;
L[b[sa[r]]] = min(L[b[sa[r]]], sa[r]);
R[b[sa[r]]] = max(R[b[sa[r]]], sa[r]);
} bool flag = true; for(int i = ; i <= m; i++) {
if(L[i] + len > R[i]) {
flag = false;
break;
}
} if(flag) return true;
l = r + ;
}
return false;
} int main() {
int T; scanf("%d", &T);
while(T--) {
scanf("%d", &m); char ch = 'A'; int tot = ;
for(int i = ; i <= m; i++) {
scanf("%s", str);
int len = strlen(str);
for(int j = ; j < len; j++) {
s[tot] = str[j];
b[tot++] = i;
}
s[tot++] = ch++;
}
s[tot] = '\0'; buildSa(s, tot + , ); int l = , r = tot, mid, ans = ; while(l <= r) {
mid = l + r >> ;
if(check(mid, tot)) ans = mid, l = mid + ;
else r = mid - ;
} printf("%d\n", ans);
}
return ;
} /*
1
4
abbabba
dabddkababa
bacaba
baba
*/
SPOJ - PHRASES K - Relevant Phrases of Annihilation的更多相关文章
- 【SPOJ 220】Relevant Phrases of Annihilation
http://www.spoj.com/problems/PHRASES/ 求出后缀数组然后二分. 因为有多组数据,所以倍增求后缀数组时要特判是否越界. 二分答案时的判断要注意优化! 时间复杂度\(O ...
- POJ - 3294~Relevant Phrases of Annihilation SPOJ - PHRASES~Substrings POJ - 1226~POJ - 3450 ~ POJ - 3080 (后缀数组求解多个串的公共字串问题)
多个字符串的相关问题 这类问题的一个常用做法是,先将所有的字符串连接起来, 然后求后缀数组 和 height 数组,再利用 height 数组进行求解. 这中间可能需要二分答案. POJ - 3294 ...
- SPOJ - PHRASES Relevant Phrases of Annihilation —— 后缀数组 出现于所有字符串中两次且不重叠的最长公共子串
题目链接:https://vjudge.net/problem/SPOJ-PHRASES PHRASES - Relevant Phrases of Annihilation no tags You ...
- SPOJ 220 Relevant Phrases of Annihilation(后缀数组+二分答案)
[题目链接] http://www.spoj.pl/problems/PHRASES/ [题目大意] 求在每个字符串中出现至少两次的最长的子串 [题解] 注意到这么几个关键点:最长,至少两次,每个字符 ...
- SPOJ - PHRASES Relevant Phrases of Annihilation
传送门:SPOJ - PHRASES(后缀数组+二分) 题意:给你n个字符串,找出一个最长的子串,他必须在每次字符串中都出现至少两次. 题解:被自己蠢哭...记录一下自己憨憨的操作,还一度质疑评测鸡( ...
- SPOJ 220 Relevant Phrases of Annihilation(后缀数组)
You are the King of Byteland. Your agents have just intercepted a batch of encrypted enemy messages ...
- 【SPOJ 220】 PHRASES - Relevant Phrases of Annihilation
[链接]h在这里写链接 [题意] 给你n(n<=10)个字符串. 每个字符串长度最大为1e4; 问你能不能找到一个子串. 使得这个子串,在每个字符串里面都不想交出 ...
- SPOJ - PHRASES Relevant Phrases of Annihilation (后缀数组)
You are the King of Byteland. Your agents have just intercepted a batch of encrypted enemy messages ...
- SPOJ PHRASES Relevant Phrases of Annihilation(后缀数组 + 二分)题解
题意: 给\(n\)个串,要你求出一个最长子串\(A\),\(A\)在每个字串至少都出现\(2\)次且不覆盖,问\(A\)最长长度是多少 思路: 后缀数组处理完之后,二分这个长度,可以\(O(n)\) ...
随机推荐
- CCPC-Winter Camp div2 day8 A
---恢复内容开始--- 题目: 题解: 我们考虑第i个叶子节点的情况,根据题目给的输入条件,我们可以知道,深度大的节点的序号一定是大于深度浅的节点的序号的 如图 题目要求我们找出每一个叶子节点距离编 ...
- mysql的IFNULL()函数FLOOR(),ROUND()函数
用法说明 1 IFNULL(expr1,expr2) 如果 expr1 不是 NULL,IFNULL() 返回 expr1,否则它返回 expr2. IFNULL()返回一个数字或字符串值,取决于它被 ...
- Android数据库资料
一.联系人和通话记录: 数据库文件/data/data/com.android.providers.contacts/databases/contacts2.db 通话记录的数据存在calls表中; ...
- bzoj2428 [HAOI2006]均分数据 模拟退火
[HAOI2006]均分数据 Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 3434 Solved: 1091[Submit][Status][Dis ...
- Java中x=x+1 与x+=1 的一点区别
转载自:http://www.cnblogs.com/heshan664754022/archive/2013/04/01/2994028.html 作者:十年半山 今天同悦姐学到了关于Java的复合 ...
- java web中resources路径
UserBean.class.getClassLoader().getResource(filePath).getPath() 或者 Thread.currentThread().getContext ...
- redis linux下的环境搭建
系统 CentOS7 Redis 官网下载 https://redis.io/download 1.下载解压 [root@TestServer-DFJR programs]# /usr/loca ...
- Maven-Dependency Mechanism
依赖管理是maven的一个广为人知的特性, 这也是maven擅长的一个领域. 为单一的工程管理依赖不是很难, 但当你着手处理多模块工程和包含数十或数百个模块的应用时, maven可以帮助你很好地保持高 ...
- IIS 搭建
1. 在打开程序功能里面,点击IIS安装.注意要选择适当的各种有用的服务.例如默认文档就需要安装非IIS下面的选项. 2. IIS部署网站可以参考网上的步骤.会遇到500处理程序“Extensionl ...
- 【BZOJ】4530: [Bjoi2014]大融合
[题意]给定n个点的树,从无到有加边,过程中动态询问当前图某条边两端连通点数的乘积,n<=10^5. [算法]线段树合并+并查集 (||LCT(LCT维护子树信息 LCT维护子树信息(+启发式合 ...