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;
}
相關文章
- DreamJudge-1294-字尾子串排序排序
- 字尾陣列 SA陣列
- 字尾陣列模板陣列
- 字尾陣列,SA陣列
- 【筆記】字尾陣列筆記陣列
- 字尾陣列(後續)陣列
- 字尾陣列複習陣列
- OI loves Algorithm——字尾陣列Go陣列
- 字尾陣列 學習筆記陣列筆記
- 字尾陣列學習筆記陣列筆記
- SPOJ 1811 Longest Common Substring(字尾自動機)
- BZOJ2882: 工藝(字尾陣列)陣列
- Codeforces #123D: 字尾陣列+單調棧3D陣列
- 最長公共子串 二維陣列 Go實現陣列Go
- POJ1743 Musical Theme(字尾陣列 二分)陣列
- P10469 字尾陣列(Hash+二分)陣列
- JavaScript 陣列隨機不重複元素JavaScript陣列隨機
- 洛谷P5108 仰望半月的夜空(字尾陣列)陣列
- 洛谷P3763 [TJOI2017]DNA(字尾陣列 RMQ)陣列MQ
- 【資料結構與演算法】字串匹配(字尾陣列)資料結構演算法字串匹配陣列
- JavaScript陣列中隨機取出不重複項JavaScript陣列隨機
- python怎麼生成隨機不重複陣列Python隨機陣列
- BZOJ4698: Sdoi2008 Sandy的卡片(字尾陣列 二分)陣列
- 第五章 字串專題 ---------------- 字串匹配(三)----字尾陣列演算法字串匹配陣列演算法
- 演算法問題——判斷陣列中是否含有某一字串演算法陣列字串
- POJ 2752+KMP+利用next陣列性質求出所有相同的字首和字尾KMP陣列
- C# 輸出一個字串的字首、字尾和它的子串(資訊內容安全 實驗一)C#字串
- 堆疊的實現(1)--靜態陣列陣列
- 和為 K 的子陣列陣列
- 978 最長湍流子陣列陣列
- 子串位置
- Leetcode 陣列中和為給定值的最長子陣列LeetCode陣列
- 最短無序連續子陣列陣列
- 子陣列異或和問題陣列
- 連續子陣列的最大和陣列
- 資料結構基礎學習之(串與陣列)資料結構陣列
- ECMAScript 提案:.findLast()和.findLastIndex()從尾到頭搜尋陣列ASTIndex陣列
- 最長子串
- 子串查詢