题意:给出N个字符串,求出最长的子串,满足在每个字符串中出现至少有两次不重叠的位置。
思路:将N个字符串用没有出现且不相同的字符连接。二分答案,用答案进行分组。因为要不重叠,所以我们要记录每个字符串中该子串出现的位置,最后判断是否有重叠。
注意:非常要注意的地方就是考虑height的关系。当开始一个组的时候,出现height >= ans,其实会添加两个合法位置。但是组在增长的时候,只能添加一个合法位置。这点要切记切记。
代码如下:
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <set>
using namespace std;
static const int maxn =1001000;//three times of length
int rk[maxn];//0 - n-1
int sa[maxn];//1 - n
int height[maxn];//1 - n
int wa[maxn],wb[maxn],wv[maxn],ws[maxn];
#define F(x) ((x)/3 + ((x)%3 == 1 ? 0:tb))
#define G(x) ((x) < tb ? (x)*3+1 : ((x)-tb)*3 + 2)
int c0(int *r, int a, int b){
return r[a] == r[b] && r[a+1] == r[b+1] && r[a+2] == r[b+2];
}
int c12(int k, int *r, int a,int b){
if (k == 2)
return r[a] < r[b] || r[a] == r[b] && c12(1,r,a+1,b+1);
else
return r[a] < r[b] || r[a] == r[b] && wv[a+1] < wv[b+1];
}
void radix_sort(int *r, int *a,int *b,int n,int m) {
int i;
for (i = 0; i < n; i++) wv[i] = r[a[i]];
for (i = 0; i < m; i++) ws[i] = 0;
for (i = 0; i < n; i++) ws[wv[i]]++;
for (i = 1; i < m; i++) ws[i] += ws[i-1];
for (i = n-1; i >= 0; i--) b[--ws[wv[i]]] = a[i];
return ;
}
void dc3(int *r,int *sa,int n, int m){
int i,j,*rn = r + n, *san = sa + n;
int ta = 0, tb = (n+1)/3,tbc = 0, p;
r[n] = r[n+1] = 0;
for(i = 0; i < n; i++)
if(i%3 != 0) wa[tbc++] = i;
radix_sort(r+2, wa, wb, tbc, m);
radix_sort(r+1, wb, wa, tbc, m);
radix_sort( r, wa, wb, tbc, m);
for (p = 1, rn[F(wb[0])] = 0, i = 1; i < tbc; i++)
rn[F(wb[i])] = c0(r,wb[i-1],wb[i]) ? p-1 : p++;
if(p < tbc) dc3(rn, san, tbc, p);
else
for (i = 0; i < tbc; i++) san[rn[i]] = i;
for(i = 0; i < tbc; i++)
if (san[i] < tb) wb[ta++] = san[i]*3;
if(n%3 == 1) wb[ta++] = n-1;
radix_sort(r, wb, wa, ta, m);
for(i = 0; i < tbc; i++)
wv[wb[i]=G(san[i])] = i;
for(i = 0,j = 0,p = 0; i < ta && j < tbc; p++)
sa[p] = c12(wb[j]%3,r,wa[i],wb[j]) ? wa[i++] : wb[j++];
for( ; i < ta; p++) sa[p] = wa[i++];
for ( ; j < tbc; p++) sa[p] = wb[j++];
return ;
}
void calc_sa(int *r, int n, int m){//attention: 1 <= r[i] <= m
r[n] = 0;//add zero, length : n + 1
dc3(r,sa,n+1,m);
}
void calc_height(int *r,int n){
int i,j,k = 0;
for (i = 0; i < n+1; i++)
rk[sa[i]] = i;
for (i = 0; i < n; height[rk[i++]] = k)//according to rank, only n times
for (k ? k-- : 0, j = sa[rk[i]-1]; r[i+k] == r[j+k]; k++)
;
return;
}
void print(int * r, int n){
for(int i = 1; i <= n; ++i){
for(int j = sa[i]; j < n; ++j)
putchar(r[j]);
putchar('\n');
}
}
// static const int MAX = 200100;
// int p[MAX];
// int d[MAX][20];
// void rmq_init(int n){
// p[0] = -1;
// for(int i = 1; i <= n; ++i)
// p[i] = i & (i-1)?p[i-1]:p[i-1]+1;
// for(int i = 1; i <= n; ++i) d[i][0] = height[i];
// for(int j = 1; j <= p[n]; ++j)
// for(int i = 1; i + (1 << j) - 1 <= n; ++i)
// d[i][j] = min(d[i][j-1],d[i+(1<<j-1)][j-1]);
// }
// int rmp_query(int l, int r){
// int k = p[r - l + 1];
// return min(d[l][k],d[r - (1<<k) + 1][k]);
// }
// int lcp(int l, int r){//l,r is the start postion of two suffix
// l = rank[l], r = rank[r];//we should turn them to the index in sa
// if(l > r) swap(l,r);l++;
// return rmp_query(l,r);
// }
int r[200010];
char str[10010];
int p[200010];
int n,N;
set<int> records[20];
bool sat(int mid)
{
// for(int j = 0; j < N; ++j)
// printf("%d%c",records[j].size(), j == N - 1?'\n':' ');
for(int i = 0; i < N; ++i){
if(records[i].size() < 2) return false;
set<int>::iterator it1 = records[i].begin(), it2 = records[i].end();
it2--;
if(*it2 - *it1 < mid) return false;
}
return true;
}
bool judge(int mid)
{
for(int i = 1; i <= n; ++i){
if(height[i] < mid){
for(int j = 0; j < N; ++j)
records[j].clear();
continue;
}
records[p[sa[i]]].insert(sa[i]);
records[p[sa[i-1]]].insert(sa[i-1]);
if(sat(mid)) return true;
}
return false;
}
int main(void)
{
//freopen("input.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T;
scanf("%d",&T);
while(T--){
scanf("%d",&N);
n = 0;
int maxlen = 0;
for(int i = 0; i < N; ++i){
scanf("%s",str);
int len = strlen(str);
copy(str,str+len,r+n);
fill(p+n,p+n+len,i);
n += len;
maxlen = max(maxlen,len);
r[n++] = '$'+i;
}
calc_sa(r,n,256);
calc_height(r,n);
// print(r,n);
//printf("%d\n",judge(2));
int lb = 0, ub = maxlen + 1;
while(lb + 1 < ub){
int mid = (lb + ub) >> 1;
//printf("%d\n",mid);
if(judge(mid)) lb = mid;
else ub = mid;
}
printf("%d\n",lb);
}
return 0;
}