SPOJ PHRASES 后缀数组

题目链接:http://www.spoj.com/problems/PHRASES/en/

题意:给定n个字符串,求一个最长的子串至少在每个串中的不重叠出现次数都不小于2。输出满足条件的最长子串长度

思路:根据<<后缀数组——处理字符串的有力工具>>的思路,先将 n个字符串连起来, 中间用不相同的且没有出现在字符串中的字符隔开, 求后缀数组。 然后二分答案, 再将后缀分组。判断的时候, 要看是否有一组后缀在每个原来的字符串中至少出现两次, 并且在每个原来的字符串中, 后缀的起始位置的最大值与最小值之差是否不小于当前答案(判断能否做到不重叠, 如果題目中没有不重叠的要求, 那么不用做此判断) 。这个做法的时间复杂度为 0(nlogn) 。

#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<time.h>
#include<cmath>
#include<set>
using namespace std;
typedef long long int LL;
const int MAXN = * * ;
int wa[MAXN], wb[MAXN], wv[MAXN], WS[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, *t;
for (i = ; i < m; i++) WS[i] = ;
for (i = ; i < n; i++) WS[x[i] = r[i]]++;
for (i = ; i < m; i++) WS[i] += WS[i - ];
for (i = n - ; i >= ; i--) sa[--WS[x[i]]] = i;
for (j = , p = ; p < n; j *= , m = p)
{
for (p = , 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 < n; i++) wv[i] = x[y[i]];
for (i = ; i < m; i++) WS[i] = ;
for (i = ; i < n; i++) WS[wv[i]]++;
for (i = ; i < m; i++) WS[i] += WS[i - ];
for (i = n - ; i >= ; i--) sa[--WS[wv[i]]] = y[i];
for (t = x, x = y, y = t, p = , x[sa[]] = , i = ; i < n; i++)
x[sa[i]] = cmp(y, sa[i - ], sa[i], j) ? p - : p++;
}
return;
}
int Rank[MAXN], height[MAXN], sa[MAXN];
void calheight(int *r, int *sa, int n){
int i, j, k = ;
for (i = ; i <= n; i++) { Rank[sa[i]] = i; }
for (i = ; i < n; height[Rank[i++]] = k){
for (k ? k-- : , j = sa[Rank[i] - ]; r[i + k] == r[j + k]; k++);
}
return;
}
int r[MAXN], len, n, t, Index[MAXN],vis[MAXN];
char sub[+];
struct Node{
int cnt,maxsa, minsa;
void init(){cnt = , maxsa = -, minsa = MAXN;}
}node[];
bool check(int x){
int tot = ,idx,Lidx;
for (int i = ; i <= n; i++){
node[i].init();
}
memset(vis, , sizeof(vis));
for (int i = ; i < len; i++){
//heigth[i]是sa[i]和sa[i-1]的LCP
idx = Index[sa[i]], Lidx = Index[sa[i - ]];
if (i == len - ){
for (int k = ; k <= n; k++){
//判断每个字符串的出现次数和后缀的起始位置的最大值和最小值的差是否不小于x
if (node[k].cnt >= && node[k].maxsa - node[k].minsa >= x){
tot++;
}
node[k].init();
}
if (tot == n){ return true; }//n个串都满足要求,说明长度x存在
tot = ;
break;
}
if (height[i] >= x){
if (!vis[i]){//每个后缀只算一次
vis[i] = ; node[idx].cnt++; //记录后缀在该组出现的次数
node[idx].maxsa = max(node[idx].maxsa, sa[i]);//最大值
node[idx].minsa = min(node[idx].minsa, sa[i]);//最小值
}
if (!vis[i-]){
vis[i - ] = ; node[Lidx].cnt++;
node[Lidx].maxsa = max(node[Lidx].maxsa, sa[i-]);
node[Lidx].minsa = min(node[Lidx].minsa, sa[i-]);
}
}
else{
for (int k = ; k <= n; k++){
//判断每个字符串的出现次数和后缀的起始位置的最大值和最小值的差是否不小于x
if (node[k].cnt >= &&node[k].maxsa-node[k].minsa>=x){
tot++;
}
node[k].init();
}
if (tot == n){ return true;} //n个串都满足要求,说明长度x存在
tot = ;
}
}
return false;
}
void solve(){
int L = , R = /, mid, ans = ;
while (R >= L){
mid = (L + R) / ;
if (check(mid)){
ans = mid;
L = mid + ;
}
else{
R = mid - ;
}
}
printf("%d\n", ans);
}
int main(){
//#ifdef kirito
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
//#endif
// int start = clock();
scanf("%d", &t);
while (t--){
scanf("%d", &n); len = ;
for (int i = , val = ; i <= n; i++, val++){
scanf("%s", &sub);
for (int j = ; j < strlen(sub); j++){
Index[len] = i; //记录每个拼接后每个位置属于原输入的哪个
r[len++] = (sub[j] - 'a' + n + );
}
Index[len] = i;
r[len++] = val;
}
da(r, sa, len, );
calheight(r, sa, len - );
solve();
}
//#ifdef LOCAL_TIME
// cout << "[Finished in " << clock() - start << " ms]" << endl;
//#endif
return ;
}
上一篇:AngularJS 模块及provide


下一篇:如果js设置移动端有两种方式 大家可以参考