POJ 3693 后缀数组

 

题目链接:http://poj.org/problem?id=3693

题意:首先定义了一个字符串的重复度。即一个字符串由一个子串重复k次构成。那么最大的k即是该字符串的重复度。现在给定一个长度为n的字符串,求最大重复次数的子串,有多解时输出字典序最小解。

思路:与SPOJ的题意差不多,可以点击这里看<<SPOJ REPEATS 后缀数组>>

说下字典序的问题,想记录size=最大重复次数,把所有满足条件的长度L都记录起来,因为求的是字典序最小,那么就可以按照sa数组记录的后缀位置来判断,如果index=sa[i]和index+L的LCP>=(size-1)*L,那么index开始长度为(size*L)的子串就是所要的答案,因为是sa数组的顺序来求的,所以第一个解肯定是最优解。

#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<time.h>
#include<cmath>
using namespace std;
typedef long long int LL;
const int MAXN = 100000 + 5;
int cmp(int *r, int a, int b, int l){
    return r[a] == r[b] && r[a + l] == r[b + l];
}
int wa[MAXN], wb[MAXN], wv[MAXN], WS[MAXN];
void da(int *r, int *sa, int n, int m){
    int i, j, p, *x = wa, *y = wb, *t;
    for (i = 0; i < m; i++) { WS[i] = 0; }
    for (i = 0; i < n; i++) { WS[x[i] = r[i]]++; }
    for (i = 1; i < m; i++) { WS[i] += WS[i - 1]; }
    for (i = n - 1; i >= 0; i--) { sa[--WS[x[i]]] = i; }
    for (j = 1, p = 1; p<n; j *= 2, 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++) { 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--) { sa[--WS[wv[i]]] = y[i]; }
        for (t = x, x = y, y = t, 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],sa[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;
}
int RMQ[MAXN],mm[MAXN],best[20][MAXN];
void initRMQ(int n){
    int i, j, a, b;
    for (mm[0] = -1, i = 1; i <= n; i++)
        mm[i] = ((i&(i - 1)) == 0) ? mm[i - 1] + 1 : mm[i - 1];
    for (i = 1; i <= n; i++) best[0][i] = i;
    for (i = 1; i <= mm[n]; i++)
        for (j = 1; j <= n + 1 - (1 << i); j++)
        {
        a = best[i - 1][j];
        b = best[i - 1][j + (1 << (i - 1))];
        if (RMQ[a]<RMQ[b]) best[i][j] = a;
        else best[i][j] = b;
        }
    return;
}
int askRMQ(int a, int b){
    int t;
    t = mm[b - a + 1]; b -= (1 << t) - 1;
    a = best[t][a]; b = best[t][b];
    return RMQ[a]<RMQ[b] ? a : b;
}
int lcp(int a, int b){
    int t;
    a = Rank[a]; b = Rank[b];
    if (a>b) { t = a; a = b; b = t; }
    return(height[askRMQ(a + 1, b)]);
}
int r[MAXN], Ca=1, len; char str[MAXN];
void solve(){
    int ansSize=1;
    vector<int>tmpL; //保存对于相同的重复次数的情况下,所以可能的长度
    for (int L = 1; L <= len; L++){
        for (int i = 0; i + 2*L<=len; i+=L){
            int lcpLen = lcp(i, i + L); 
            int tmp = lcpLen / L + 1; 
            int sur = (L - lcpLen%L); 
            int prei = i - sur; 
            if (prei >= 0 && prei + L < len&&lcp(prei,prei+L)>=L){
                tmp++;
            }
            if (tmp > ansSize){ //更优解
                ansSize = tmp; //更新解
                tmpL.clear(); //清除原来的长度
                tmpL.push_back(L); //保存新的长度
            }
            if (tmp == ansSize){ //用于相同的循环次数
                tmpL.push_back(L); //记录长度
            }
        }
    }
    tmpL.erase(unique(tmpL.begin(), tmpL.end()),tmpL.end());//去重
    int index;//记录答案的起点
    for (int i = 1,flag=0;i <= len&&!flag; i++){ //按后缀字典序找答案
        for (int j = 0; j < tmpL.size(); j++){
            int L = tmpL[j]; //长度
            if (sa[i]+L<=len&&lcp(sa[i], sa[i] + L) >= (ansSize - 1)*L){
                index = sa[i]; flag = 1;  //找到答案
                str[sa[i] + ansSize*L] = '\0'; 
                break;
            }
        }
    }
    printf("Case %d: %s\n", Ca++, str + index);
}
int main(){
//#ifdef kirito
//    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);
//#endif
//    int start = clock();
    while (scanf("%s",str)&&str[0]!='#'){
        len = strlen(str);
        for (int i = 0; i < len; i++){
            r[i] = str[i]-'a'+1;
        }
        r[len] = 0;
        da(r, sa, len+1, 30); 
        calheight(r, sa, len);
        for (int i = 1; i <= len; i++){ RMQ[i] = height[i]; }
        initRMQ(len);
        solve();
    }
//#ifdef LOCAL_TIME
//    cout << "[Finished in " << clock() - start << " ms]" << endl;
//#endif
    return 0;
}

 

posted @ 2016-08-12 15:59  キリト  阅读(225)  评论(0编辑  收藏  举报