题目链接:https://vjudge.net/problem/SPOJ-PHRASES

PHRASES - Relevant Phrases of Annihilation

no tags 

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.

Example

Input:
1
4
abbabba
dabddkababa
bacaba
baba Output:
2

(in the example above, the longest substring which fulfills the requirements is 'ba')

题意:

给出n个字符串,求出现于所有字符串中两次且不重叠的最长公共子串,输出长度。

题解:

1.将所有字符串拼接在一起,相邻两个之间用各异的分隔符隔开,得到新串。

2.求出新串的后缀数组,然后二分mid:mid将新串的后缀分成若干组,每一组对应着一个公共子串,且长度大于等于mid。在每一组中,统计公共子串出现于每个字符串中的最小和最大下标,如果最大下标-最小下标>=mid,即表明公共子串出现在该字符串内两次且不重叠。如果在同一组内,所有字符串都满足最大下标-最小下标>=mid,那么表明当前mid合法,否则不合法,因此根据此规则求出答案。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 2e5+; int id[MAXN];
int r[MAXN], sa[MAXN], Rank[MAXN], height[MAXN];
int t1[MAXN], t2[MAXN], c[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 sa[], int Rank[], int height[], int n, int m)
{
n++;
int i, j, p, *x = t1, *y = t2;
for(i = ; i<m; i++) c[i] = ;
for(i = ; i<n; i++) c[x[i] = str[i]]++;
for(i = ; i<m; i++) c[i] += c[i-];
for(i = n-; i>=; i--) sa[--c[x[i]]] = i;
for(j = ; j<=n; j <<= )
{
p = ;
for(i = n-j; i<n; i++) y[p++] = i;
for(i = ; i<n; i++) if(sa[i]>=j) y[p++] = sa[i]-j; 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(i = ; i<n; i++)
x[sa[i]] = cmp(y, sa[i-], sa[i], j)?p-:p++; if(p>=n) break;
m = p;
} int k = ;
n--;
for(i = ; i<=n; i++) Rank[sa[i]] = i;
for(i = ; i<n; i++)
{
if(k) k--;
j = sa[Rank[i]-];
while(str[i+k]==str[j+k]) k++;
height[Rank[i]] = k;
}
} //pos用于记录在当前组中,字符串i的子串出现的最小下标以及最大下标
//vis用于记录在当前组中,字符串i是否已经出现了两个不重叠的子串
int pos[][], vis[];
bool test(int n, int len, int k)
{
int cnt = ;
memset(pos, -, sizeof(pos));
memset(vis, false, sizeof(vis));
for(int i = ; i<=len; i++)
{
if(height[i]<k)
{
cnt = ;
memset(pos, -, sizeof(pos));
memset(vis, false, sizeof(vis));
}
else
{
int b1 = id[sa[i-]], b2 = id[sa[i]];
pos[b1][] = pos[b1][]==-?sa[i-]:min(pos[b1][], sa[i-]); //最小下标
pos[b1][] = pos[b1][]==-?sa[i-]:max(pos[b1][], sa[i-]); //最大下标
pos[b2][] = pos[b2][]==-?sa[i]:min(pos[b2][], sa[i]);
pos[b2][] = pos[b2][]==-?sa[i]:max(pos[b2][], sa[i]); if(!vis[b1] && pos[b1][]!=- && pos[b1][]!=- && pos[b1][]-pos[b1][]>=k)
vis[b1] = true, cnt++;
if(!vis[b2] && pos[b2][]!=- && pos[b2][]!=- && pos[b2][]-pos[b2][]>=k)
vis[b2] = true, cnt++;
if(cnt==n) return true;
}
}
return false;
} char str[MAXN];
int main()
{
int T, n;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
int len = ;
for(int i = ; i<n; i++)
{
scanf("%s", str);
int LEN = strlen(str);
for(int j = ; j<LEN; j++)
{
r[len] = str[j];
id[len++] = i;
}
r[len] = +i;
id[len++] = i;
}
r[len] = ;
DA(r,sa,Rank,height,len,); int l = , r = len;
while(l<=r)
{
int mid = (l+r)>>;
if(test(n,len,mid))
l = mid + ;
else
r = mid - ;
}
printf("%d\n", r);
}
}

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

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

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

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

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

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

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

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

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

  5. SPOJ PHRASES 每个字符串至少出现两次且不重叠的最长子串

    Description You are the King of Byteland. Your agents have just intercepted a batch of encrypted ene ...

  6. SPOJ 1811 Longest Common Substring (后缀自动机第一题,求两个串的最长公共子串)

    题目大意: 给出两个长度小于等于25W的字符串,求它们的最长公共子串. 题目链接:http://www.spoj.com/problems/LCS/ 算法讨论: 二分+哈希, 后缀数组, 后缀自动机. ...

  7. 后缀数组(模板题) - 求最长公共子串 - poj 2774 Long Long Message

    Language: Default Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 21 ...

  8. POJ 2217 (后缀数组+最长公共子串)

    题目链接: http://poj.org/problem?id=2217 题目大意: 求两个串的最长公共子串,注意子串是连续的,而子序列可以不连续. 解题思路: 后缀数组解法是这类问题的模板解法. 对 ...

  9. HDU 1403 Longest Common Substring(后缀数组,最长公共子串)

    hdu题目 poj题目 参考了 罗穗骞的论文<后缀数组——处理字符串的有力工具> 题意:求两个序列的最长公共子串 思路:后缀数组经典题目之一(模版题) //后缀数组sa:将s的n个后缀从小 ...

随机推荐

  1. java基础篇5之泛型

    1 泛型的基本应用 //反射方式 指定类型,就不用强转 Construcctor<String> constructor = String.class.getConstructor(Str ...

  2. windows8开发-关于wp7应用迁移到win8 metro风格

    虽然微软说,wp7应用移植到win8上面是比较简单,只需要修改部分API和设计原则上的细节,同时它也提供了一份比较简洁的参考文档: 而实际上这种移植的工作量还是不小的,尤其当应用引用了较多底层的API ...

  3. 转:使用Fabric自动化你的任务

    转:http://www.cnblogs.com/holbrook/archive/2012/03/05/2380398.html fabric是什么? Fabric是一个Python库,可以通过SS ...

  4. mysql 远程登陆不上

    当使用 TCP/IP 连接 mysql 时, 出现 : Can't connect to MySQL server on 'xxx.xxx.xxx.xxx.'(111) 这个错误. 经过重复折腾: 确 ...

  5. Win7如何修复开机画面

    将下面文件保存为"修复Win7开机画面.bat"双击运行即可   bcdedit /set {current} locale zh-CN    

  6. spring(16)------spring的数据源配置

    在spring中,通过XML的形式实现数据源的注入有三种形式. 一.使用spring自带的DriverManagerDataSource 使用DriverManagerDataSource配置数据源与 ...

  7. 重读金典------高质量C编程指南(林锐)-------第一章 文件结构

    第一章  文件结构       C/C++程序通常由两个文件组成,一个文件保存程序的声明,称为头文件,.h 文件.一个保存程序的实现,称为定义文件.c文件. 1.1 版权与版本的声明 版权和版本的声明 ...

  8. storm - 可靠机制

    一 可靠性简单介绍                    Storm的可靠性是指Storm会告知用户每个消息单元是否在一个指定的时间(timeout)内被全然处理. 全然处理的意思是该MessageI ...

  9. linux 安装jdk和tomcat

    安装jdk 下载相关jdk .rpm包,如:jdk-8u31-linux-i586.rpm 解压:#rpm -ivh jdk-8u31-linux-i586.rpm 配置环境变量:#vi  /etc/ ...

  10. 通俗的理解java的堆和栈

    堆 可以把堆理解为一家餐厅,里面有200张桌子,也就是最多能同时容纳200桌客人就餐,来一批客人就为他们安排一些桌子,如果某天来的客人特别多,超过200桌了,那就不能再接待超出的客人了.当然,进来吃饭 ...