SPOJ 220. Relevant Phrases of Annihilation(字尾陣列多次不重疊子串)
題目大意:給定N個串,求每個串至少出現兩次的最長子串。
解題思路:每個字串至少出現兩次且不可重疊的最長子串:二分列舉長度後在同一分組中對每一個字串保留一個最小的位置和一個最大的位置,最後檢視是否每個串在同一組中都有至少兩個字尾,並且字尾的座標差大於列舉的長度。
POJ Problem Set (classical)
220. Relevant Phrases of Annihilation
Problem code: PHRASES
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')
Added by: | Adrian Kosowski |
Date: | 2004-10-11 |
Time limit: | 9s |
Source limit: | 50000B |
Memory limit: | 1536MB |
Cluster: | Cube (Intel Pentium G860 3GHz) |
Languages: | All except: NODEJS PERL 6 SCM chicken VB.net |
Resource: | DASM Programming League 2004 (problemset 1) |
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <ctime>
#include <map>
#include <set>
#define eps 1e-9
///#define M 1000100
///#define LL __int64
#define LL long long
///#define INF 0x7ffffff
#define INF 0x3f3f3f3f
#define PI 3.1415926535898
#define zero(x) ((fabs(x)<eps)?0:x)
#define mod 1000000007
#define Read() freopen("autocomplete.in","r",stdin)
#define Write() freopen("autocomplete.out","w",stdout)
#define Cin() ios::sync_with_stdio(false)
using namespace std;
inline int read()
{
char ch;
bool flag = false;
int a = 0;
while(!((((ch = getchar()) >= '0') && (ch <= '9')) || (ch == '-')));
if(ch != '-')
{
a *= 10;
a += ch - '0';
}
else
{
flag = true;
}
while(((ch = getchar()) >= '0') && (ch <= '9'))
{
a *= 10;
a += ch - '0';
}
if(flag)
{
a = -a;
}
return a;
}
void write(int a)
{
if(a < 0)
{
putchar('-');
a = -a;
}
if(a >= 10)
{
write(a / 10);
}
putchar(a % 10 + '0');
}
const int maxn = 200010;
int wa[maxn], wb[maxn], wv[maxn], ws1[maxn];
int sa[maxn];
int cmp(int *r, int a, int b, int l)
{
return r[a] == r[b] && r[a+l] == r[b+l];
}
void da(int *r, int *sa, int n, int m)
{
int i, j, p, *x = wa, *y = wb;
for(i = 0; i < m; i++) ws1[i] = 0;
for(i = 0; i < n; i++) ws1[x[i] = r[i]]++;
for(i = 1; i < m; i++) ws1[i] += ws1[i-1];
for(i = n-1; i >= 0; i--) sa[--ws1[x[i]]] = i;
for(j = 1, p = 1; p < n; j <<= 1, m = p)
{
for(p = 0, 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 < n; i++) wv[i] = x[y[i]];
for(i = 0; i < m; i++) ws1[i] = 0;
for(i = 0; i < n; i++) ws1[wv[i]]++;
for(i = 1; i < m; i++) ws1[i] += ws1[i-1];
for(i = n-1; i >= 0; i--) sa[--ws1[wv[i]]] = y[i];
for(swap(x, y), p = 1, x[sa[0]] = 0, i = 1; i < n; i++)
x[sa[i]] = cmp(y, sa[i-1], sa[i], j)?p-1:p++;
}
return ;
}
int rank[maxn], height[maxn];
void calheight(int *r, int *sa, int n)
{
int i, j, k = 0;
for(i = 1; i <= n; i++) rank[sa[i]] = i;
for(i = 0; i < n; height[rank[i++]] = k)
for(k?k--:0, j = sa[rank[i]-1]; r[i+k] == r[j+k]; k++);
return;
}
char str1[maxn], str2[maxn];
int seq[maxn];
int hash[maxn];
char str[110][1010];
int vis[110];
int Find(int x)
{
int s = hash[x];
for(int i = 0; i < s; i++)
{
int len = strlen(str[i]);
x -= len;
}
x -= s;
return x;
}
int pos[20][2];
bool judge(int mid, int n, int m)
{
for(int i = 2; i <= n; i++)
{
memset(vis, 0, sizeof(vis));
vis[hash[sa[i-1]]]++;
pos[hash[sa[i-1]]][0] = pos[hash[sa[i-1]]][1] = sa[i-1];
while(mid <= height[i])
{
int xp = hash[sa[i]];
if(!vis[xp])
{
vis[xp]++;
pos[xp][1] = pos[xp][0] = sa[i];
i++;
continue;
}
vis[xp]++;
pos[xp][0] = max(pos[xp][0], sa[i]);
pos[xp][1] = min(pos[xp][1], sa[i]);
i++;
}
int k;
for(k = 0; k < m; k++)
{
if(vis[k] < 2 || pos[k][0]-pos[k][1] < mid)
break;
}
if(k == m) return true;
}
return false;
}
void Del(int n, int len, int m)
{
int l = 1;
int r = len/2;
int xans = 0;
while(l <= r)
{
int mid = (l+r)>>1;
if(judge(mid, n, m))
{
l = mid+1;
xans = mid;
}
else r = mid-1;
}
cout<<xans<<endl;
}
int main()
{
int T;
cin >>T;
int n;
while(T--)
{
scanf("%d", &n);
memset(hash, -1, sizeof(hash));
int ans = 0;
int Min = maxn;
for(int i = 0; i < n; i++)
{
scanf("%s",str[i]);
int len = strlen(str[i]);
Min = min(Min, len);
for(int j = 0; j < len; j++)
{
seq[ans] = str[i][j];
hash[ans++] = i;
}
seq[ans++] = 200+i;
}
seq[ans] = 0;
da(seq, sa, ans+1, 310);
calheight(seq, sa, ans);
Del(ans, Min, n);
}
return 0;
}
相關文章
- POJ 1743 Musical Theme (字尾陣列,求最長不重疊重複子串)陣列
- SPOJ 687. Repeats(字尾陣列求最長重複子串)陣列
- SPOJ 694 求一個字串有多少子串 字尾陣列字串陣列
- POJ 3294 Life Forms(字尾陣列求k個串的最長子串)ORM陣列
- ural 1297 最長迴文子串 字尾陣列陣列
- POJ 2217-Secretary(字尾陣列+高度陣列-最大公共子串長度)陣列
- poj 2774 求兩字串的最長公共子串 字尾陣列字串陣列
- POJ 3693 Maximum repetition substring(字尾陣列求最長重複子串)陣列
- POJ 2774-Long Long Message(字尾陣列+高度陣列-最大公共子串長度)陣列
- SPOJ 694. Distinct Substrings,705. New Distinct Substrings(字尾陣列)陣列
- HDU 5769-Substring(字尾陣列-不相同的子串的個數)陣列
- HDU 4622 Reincarnation( 任意區間子串的長度, 字尾陣列+RMQ)陣列MQ
- 字尾陣列模板陣列
- 字尾陣列 SA陣列
- 字尾陣列複習陣列
- 字尾陣列(後續)陣列
- 字尾陣列詳解陣列
- 【筆記】字尾陣列筆記陣列
- POJ 3415-Common Substrings(字尾陣列+單調棧-公共子串的長度)陣列
- URAL 1297. Palindrome(字尾陣列求最大回文串)陣列
- 字尾陣列學習筆記陣列筆記
- 學習筆記----字尾陣列筆記陣列
- 字尾陣列 學習筆記陣列筆記
- BZOJ2882: 工藝(字尾陣列)陣列
- POJ 3581-Sequence(字尾陣列)陣列
- POJ 1743 Musical Theme(字尾陣列)陣列
- js生成不重複數字陣列程式碼例項JS陣列
- ES6字串轉換為陣列字串陣列
- 最長公共子串 二維陣列 Go實現陣列Go
- poj 3415 Common Substrings(長度大於k的相同子串對數xian 字尾陣列+單調桟統計)陣列
- 【Kmp求既是字首又是字尾的子串】POJ - 2752 Seek the Name, Seek the FameKMP
- SPOJ DQUERY (離線數狀陣列||線上主席樹)陣列
- POJ1743 Musical Theme(字尾陣列 二分)陣列
- POJ2774Long Long Message(字尾陣列模板)陣列
- JavaScript 陣列隨機不重複元素JavaScript陣列隨機
- 【資料結構與演算法】字串匹配(字尾陣列)資料結構演算法字串匹配陣列
- Codeforces #123D: 字尾陣列+單調棧3D陣列
- poj3080-kmp+列舉子串 求最長公共子串KMP