You are the King of Byteland. Your agents have just intercepted a batch of encrypted enemy messages concerning the date of the planned attack on your island. You immedietaly send for the Bytelandian Cryptographer, but he is currently busy eating popcorn and claims that he may only decrypt the most important part of the text (since the rest would be a waste of his time). You decide to select the fragment of the text which the enemy has strongly emphasised, evidently regarding it as the most important. So, you are looking for a fragment of text which appears in all the messages disjointly at least twice. Since you are not overfond of the cryptographer, try to make this fragment as long as possible.

Input

The first line of input contains a single positive integer t<=10, the number of test cases. t test cases follow. Each test case begins with integer n (n<=10), the number of messages. The next n lines contain the messages, consisting only of between 2 and 10000 characters 'a'-'z', possibly with some additional trailing white space which should be ignored.

Output

For each test case output the length of longest string which appears disjointly at least twice in all of the messages.

题目大意:给n个字符串,求在每个字符串中出现至少两次且不重叠的最长子串的长度。

思路:每个字符串用不相同的字符连起来。求后缀数组和height[]数组。

二分长度L,检查长度。

检查的时候从前往后扫一遍,看有没有连起来的一部分,公共前缀大于等于L且在每个字符串中都出现了且不重叠。

复杂度为O(nlogn)

代码(0.34s):

 #include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std; const int MAXN = ; char s[MAXN];
int id[MAXN];
int sa[MAXN], rank[MAXN], height[MAXN], c[MAXN], tmp[MAXN];
int n, m, T; void makesa(int m) {
memset(c, , m * sizeof(int));
for(int i = ; i < n; ++i) ++c[rank[i] = s[i]];
for(int i = ; i < m; ++i) c[i] += c[i - ];
for(int i = ; i < n; ++i) sa[--c[rank[i]]] = i;
for(int k = ; k < n; k <<= ) {
for(int i = ; i < n; ++i) {
int j = sa[i] - k;
if(j < ) j += n;
tmp[c[rank[j]]++] = j;
}
int j = c[] = sa[tmp[]] = ;
for(int i = ; i < n; ++i) {
if(rank[tmp[i]] != rank[tmp[i - ]] || rank[tmp[i] + k] != rank[tmp[i - ] + k])
c[++j] = i;
sa[tmp[i]] = j;
}
memcpy(rank, sa, n * sizeof(int));
memcpy(sa, tmp, n * sizeof(int));
}
} void calheight() {
for(int i = , k = ; i < n; height[rank[i++]] = k) {
k -= (k > );
int j = sa[rank[i] - ];
while(s[i + k] == s[j + k]) ++k;
}
} int mx[MAXN], mn[MAXN];
int stk[MAXN]; void update_max(int &a, int b) {
if(a == - || a < b) a = b;
} void update_min(int &a, int b) {
if(a == - || a > b) a = b;
} bool check(int L) {
int sum = , top = ;
memset(mx, -, m * sizeof(int));
memset(mn, -, m * sizeof(int));
memset(c, , m * sizeof(int));
for(int i = ; i < n; ++i) {
if(height[i] >= L) {
update_max(mx[id[sa[i]]], sa[i]);
update_min(mn[id[sa[i]]], sa[i]);
stk[++top] = id[sa[i]];
if(mx[id[sa[i]]] - mn[id[sa[i]]] >= L) {
if(!c[id[sa[i]]]) ++sum;
c[id[sa[i]]] = true;
if(sum >= m) return true;
}
} else {
sum = ;
while(top) {
int t = stk[top--];
mx[t] = mn[t] = -;
c[t] = false;
}
update_max(mx[id[sa[i]]], sa[i]);
update_min(mn[id[sa[i]]], sa[i]);
stk[++top] = id[sa[i]];
}
}
return false;
} int solve() {
int l = , r = ;
while(l < r) {
int mid = (l + r) >> ;
if(check(mid)) l = mid + ;
else r = mid;
}
return l - ;
} int main() {
scanf("%d", &T);
while(T--) {
scanf("%d", &m);
n = ;
for(int i = ; i < m; ++i) {
scanf("%s", s + n);
while(s[n]) id[n++] = i;
s[n++] = i + ;
}
s[n - ] = ;
makesa();
calheight();
printf("%d\n", solve());
}
}

SPOJ 220 Relevant Phrases of Annihilation(后缀数组)的更多相关文章

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

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

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

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

  3. SPOJ220 Relevant Phrases of Annihilation(后缀数组)

    引用罗穗骞论文中的话: 先将n 个字符串连起来,中间用不相同的且没有出现在字符串中的字符隔开,求后缀数组.然后二分答案,再将后缀分组.判断的时候,要看是否有一组后缀在每个原来的字符串中至少出现两次,并 ...

  4. 【SPOJ 220】Relevant Phrases of Annihilation

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

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

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

  6. SPOJ - PHRASES K - Relevant Phrases of Annihilation

    K - Relevant Phrases of Annihilation 题目大意:给你 n 个串,问你最长的在每个字符串中出现两次且不重叠的子串的长度. 思路:二分长度,然后将height分块,看是 ...

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

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

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

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

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

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

随机推荐

  1. phpcms标签整理_当前栏目调用

    phpcms标签整理_当前栏目调用 转载 **//SQL语句调用: {pc:get sql="select * from phpcms_category where catid in($ca ...

  2. MyBatis-Generator 最佳实践

    为数据库中的表A生成A.java, A.java, A.xml 由于该插件生成的A.java, A.xml会带有example, 不希望生成example 数据库中的字段写有注释, 希望注释能自动生成 ...

  3. 图解Activity与Fragment

    文字待补充.. 注:由于图片太大,显示不清晰请谅解.大家可以右键图片-->在新窗口打开,或者保存到本地查看.

  4. Object的属性property详细解释(自动生成成员变量)

    类Class中的属性property: 在ios第一版中,我们为输出口同时声明了属性和底层实例变量,那时,属性是oc语言的一个新的机制,并且要求你必须声明与之对应的实例变量,例如: @interfac ...

  5. How to disable and clear query ranges in sysquery form

    query = new query('Query name'); queryBuildDataSource = query.dataSourceTable(tableNum('table name') ...

  6. Android开发笔记:打包数据库

    对于数据比较多的控制一般会加入SQLite数据库进行数据存储,在打包时这些数据库是不自动打包到apk中的,如何创建数据库呢 方法1:将创建数据库的sql语句在SQLiteHelper继承类中实现,在第 ...

  7. 面向对象分析方法(I)

    找出最关键的一些业务场景:一般通过动词来寻找,比如招聘系统中,一个应聘人投递一个职位就是一次应聘,应聘就是一个业务场景:一个学生参加某门课的考试,那么考试就是一个业务场景:一个学生去图书馆借书,那么借 ...

  8. Selenium2学习-015-WebUI自动化实战实例-013-通过 URL 关闭多余的已开浏览器窗口

    在日常的 WebUI 自动化测试脚本执行的过程中,经常会打开不同的网页,进行相应的操作,此时可能会打开很多的网页,当打开的网页过多时,无效的网页资源对运行脚本的机器造成了过多无效的资源浪费,因而在日常 ...

  9. ASP.NET MVC 中将数据从View传递到控制器中的表单提交法

    本方法以搜索功能为例,在view中输入要搜索的关键字,提交到相应controller中进行处理. view中代码: <div class="searchBox"> @u ...

  10. Tomcat 处理请求时的中文乱码问题

    利用Tomcat8作为服务器,采用servlet接收前端请求后进行处理的过程中,前台请求中有中文时,中文信息变成了乱码. 经过调试和查阅,发现Tomcat在处理get请求和post请求是有区别的.参照 ...