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的更多相关文章

  1. 【SPOJ 220】Relevant Phrases of Annihilation

    http://www.spoj.com/problems/PHRASES/ 求出后缀数组然后二分. 因为有多组数据,所以倍增求后缀数组时要特判是否越界. 二分答案时的判断要注意优化! 时间复杂度\(O ...

  2. POJ - 3294~Relevant Phrases of Annihilation SPOJ - PHRASES~Substrings POJ - 1226~POJ - 3450 ~ POJ - 3080 (后缀数组求解多个串的公共字串问题)

    多个字符串的相关问题 这类问题的一个常用做法是,先将所有的字符串连接起来, 然后求后缀数组 和 height 数组,再利用 height 数组进行求解. 这中间可能需要二分答案. POJ - 3294 ...

  3. SPOJ - PHRASES Relevant Phrases of Annihilation —— 后缀数组 出现于所有字符串中两次且不重叠的最长公共子串

    题目链接:https://vjudge.net/problem/SPOJ-PHRASES PHRASES - Relevant Phrases of Annihilation no tags  You ...

  4. SPOJ 220 Relevant Phrases of Annihilation(后缀数组+二分答案)

    [题目链接] http://www.spoj.pl/problems/PHRASES/ [题目大意] 求在每个字符串中出现至少两次的最长的子串 [题解] 注意到这么几个关键点:最长,至少两次,每个字符 ...

  5. SPOJ - PHRASES Relevant Phrases of Annihilation

    传送门:SPOJ - PHRASES(后缀数组+二分) 题意:给你n个字符串,找出一个最长的子串,他必须在每次字符串中都出现至少两次. 题解:被自己蠢哭...记录一下自己憨憨的操作,还一度质疑评测鸡( ...

  6. SPOJ 220 Relevant Phrases of Annihilation(后缀数组)

    You are the King of Byteland. Your agents have just intercepted a batch of encrypted enemy messages ...

  7. 【SPOJ 220】 PHRASES - Relevant Phrases of Annihilation

    [链接]h在这里写链接 [题意]     给你n(n<=10)个字符串.     每个字符串长度最大为1e4;     问你能不能找到一个子串.     使得这个子串,在每个字符串里面都不想交出 ...

  8. SPOJ - PHRASES Relevant Phrases of Annihilation (后缀数组)

    You are the King of Byteland. Your agents have just intercepted a batch of encrypted enemy messages ...

  9. SPOJ PHRASES Relevant Phrases of Annihilation(后缀数组 + 二分)题解

    题意: 给\(n\)个串,要你求出一个最长子串\(A\),\(A\)在每个字串至少都出现\(2\)次且不覆盖,问\(A\)最长长度是多少 思路: 后缀数组处理完之后,二分这个长度,可以\(O(n)\) ...

随机推荐

  1. RabbitMQ基础概念(消息、队列、交换机)

    1.消息的确认 RabbitMQ需要对每一条发送的消息进行确认.消费者必须通过AMQP的basic.ack命令显式地向RabbitMQ发送一个确认,或者在订阅到队列的时候就将auto_ack参数设置为 ...

  2. php中相关函数

    1.php标准风格 <?php //这是标准风格 echo '推荐标准风格'; ?> 2.php中文乱码 .html:<meta http-equiv="Content-T ...

  3. [LeetCode] 21. Merge Two Sorted Lists ☆

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  4. 耗子学Python了(1)___Python环境配置

    一:写在开始的开始 最近我们团对的老大问我想看什么书,我让老大买了一本<Python 3面向对象编程>,所以在看的时候边敲边实践,决定在自己电脑配置下相应的环境,然后开始Python的学习 ...

  5. java主线程捕获子线程中的异常

    本文主要参考:<think in java> 好,下面上货. 正常情况下,如果不做特殊的处理,在主线程中是不能够捕获到子线程中的异常的. 例如下面的情况. package com.xuey ...

  6. CentOS查看系统版本号

    命令:cat /etc/redhat-release [elsearch@localhost data]$ cat /etc/redhat-release Red Hat Enterprise Lin ...

  7. 命令行访问远程mysql数据库

    命令: mysql -u用户名 -p密码 -h远程数据库IP地址 -P端口 -D数据库名 例子: 使用用root帐号,密码为123456,登录ip地址为192.168.1.110,端口为3306,名称 ...

  8. 使用Apache Curator监控Zookeeper的Node和Path的状态

    1.Zookeeper经常被我们用来做配置管理,配置的管理在分布式应用环境中很常见,例如同一个应用系统需要多台 PC Server 运行,但是它们运行的应用系统的某些配置项是相同的,如果要修改这些相同 ...

  9. Mayor's posters(线段树+离散化+区间染色)

    题目链接:http://poj.org/problem?id=2528 题目: 题意:将n个区间进行染色(对于同一个区间,后一次染色会覆盖上一次的染色),问最后可见的颜色有多少种. 思路:由于区间长度 ...

  10. python初步学习-pycharm使用 (二)

    pycharm调试模式 假设我们的程序在运行过程中命中了一个错误,那我们如何定位错误发生的位置?这就需要进行调试. 在Pycharm中我们可以在其中直接对程序进行调试,唯一需要做的准备工作就是在程序必 ...