SPOJ 687. Repeats(字尾陣列求最長重複子串)

畫船聽雨發表於2015-01-26

題目大意:給你一個串讓你求出重複次數最多的連續重複子串的重複次數。

解題思路:論文上給出的解答是:


這還沒完,因為經過這兩個點的情況還不完備,應還可以假設起點在 [ i*j-i+1, i*j-d],其中 d = i-L/i (d = i-L%i)其意義為根據已知的匹配長度,可以將起點往前移動的範圍,太靠後將不能夠構造出比之前更好的解。如果要求出某個最多的連續重複子串的最小字典序子需要列舉所有起點,但如果只是要的到最多的重複次數或者任意最多的連續重複子串,那麼只需要列舉i*j-d處的起點即可,因為後面的起點若能夠得到最優的結果,那麼i*j-d處也一定能得到,且答案一樣均為L/i+2。(這樣保證了第0倍不是從起點開始的解的情況。)

SPOJ Problem Set (classical)

687. Repeats

Problem code: REPEATS

A string s is called an (k,l)-repeat if s is obtained by concatenating k>=1 times some seed string t with length l>=1. For example, the string

s = abaabaabaaba

is a (4,3)-repeat with t = aba as its seed string. That is, the seed string t is 3 characters long, and the whole string s is obtained by repeating t 4 times.

Write a program for the following task: Your program is given a long string u consisting of characters ‘a’ and/or ‘b’ as input. Your program must find some (k,l)-repeat that occurs as substring within u with k as large as possible. For example, the input string

u = babbabaabaabaabab

contains the underlined (4,3)-repeat s starting at position 5. Since u contains no other contiguous substring with more than 4 repeats, your program must output the maximum k.

Input

In the first line of the input contains H- the number of test cases (H <= 20). H test cases follow. First line of each test cases is n - length of the input string (n <= 50000), The next n lines contain the input string, one character (either ‘a’ or ‘b’) per line, in order.

Output

For each test cases, you should write exactly one interger k in a line - the repeat count that is maximized.

Example

Input:
1
17
b
a
b
b
a
b
a
a
b
a
a
b
a
a
b
a
b

Output:
4
since a (4, 3)-repeat is found starting at the 5th character of the input string.
Added by: Hoang Hong Quan
Date: 2006-01-05
Time limit: 1.985s
Source limit: 50000B
Memory limit: 1536MB
Cluster: Cube (Intel Pentium G860 3GHz)
Languages: All except: NODEJS PERL 6 SCM chicken VB.net
Resource: BOI 2004








#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 = 50050;

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++;
    }
}


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(int 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 dp[maxn][30];


void RMQ(int len)
{
    for(int i = 1; i <= len; i++)
        dp[i][0] = height[i];
    for(int j = 1; 1<<j <= maxn; j++)
    {
        for(int i = 1; i+(1<<j)-1 <= len; i++)
            dp[i][j] = min(dp[i][j-1], dp[i+(1<<(j-1))][j-1]);
    }
}
int lg[maxn];

int querry(int l, int r)
{
    int k = lg[r-l+1];
    return min(dp[l][k], dp[r-(1<<k)+1][k]);
}


void init()
{
    lg[0] = -1;
    for (int i = 1; i < maxn; ++i)
        lg[i] = lg[i>>1] + 1;
}
int seq[maxn];

void Del1(int n)
{
    int Max = 0;
    int pos = 0;
    for(int i = 0; i < n; i++)
    {
        int l = rank[i];
        int r = rank[2*n-i+1];
        if(l > r) swap(l, r);
        int tmp = querry(l+1, r);
        if(tmp*2 > Max)
        {
            Max = tmp*2;
            pos = i;
        }
        l = rank[i];
        r = rank[2*n-i];
        if(l > r) swap(l, r);
        tmp = querry(l+1, r);
        if(tmp*2-1 > Max)
        {
            Max = tmp*2-1;
            pos = i;
        }
    }
    for (int i = pos-Max/2; Max--; ++i)
        putchar(seq[i]);
    puts("");
    return ;
}

void Del(int n)
{
    int Max = 0;
    for(int i = 1; i <= n/2; i++)
    {
        for(int j = 0; i*j+i < n; j++)
        {
            int l = rank[i*j];
            int r = rank[i*j+i];
            if(l > r) swap(l, r);
            int x = querry(l+1, r);
            int y = 0;
            int xp = i - x%i;
            if(xp && j)
            {
                l = rank[i*j-xp];
                r = rank[i*j+i-xp];
                if(l > r) swap(l, r);
                y = querry(l+1, r);
            }
            Max = max(Max, max(x/i, y/i)+1);
        }
    }
    cout<<Max<<endl;
}

char str[10];

int main()
{
    init();
    int T;
    cin >>T;
    while(T--)
    {
        int n;
        scanf("%d",&n);
        for(int i = 0; i < n; i++)
        {
             scanf("%s", str);
             seq[i] = str[0]-'a'+1;
        }
        seq[n] = 0;
        da(seq, sa, n+1, 5);
        calheight(seq, sa, n);
        RMQ(n);
        Del(n);
    }
    return 0;
}



相關文章