POJ 3693 Maximum repetition substring(最多重复次数的子串)

Maximum repetition substring

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10461   Accepted: 3234

Description

The repetition number of a string is defined as the maximum number R such that the string can be partitioned into R same consecutive substrings. For example, the repetition number of "ababab" is 3 and "ababa" is 1.

Given a string containing lowercase letters, you are to find a substring of it with maximum repetition number.

Input

The input consists of multiple test cases. Each test case contains exactly one line, which
gives a non-empty string consisting of lowercase letters. The length of the string will not be greater than 100,000.

The last test case is followed by a line containing a ‘#‘.

Output

For each test case, print a line containing the test case number( beginning with 1) followed by the substring of maximum repetition number. If there are multiple substrings of maximum repetition number, print the lexicographically smallest one.

Sample Input

ccabababc
daabbccaa
#

Sample Output

Case 1: ababab
Case 2: aa

题目链接:POJ 3693

把所有可能构成的最多重复次数的子串所对应的循环节大小存下来,然后枚举$SA[i]$与循环节大小,如果刚好枚举到了这个$SA[i]$对应的就是这个循环节大小,那么就是最小的字典序解了,因为$SA[]$是按照字典序排的,当然要注意单个字符这种字符串,因此一开始要把长度定为1。

另外这题数据非常弱,实际上枚举的时候需要加一些边界防止越界问题。可以试一下这些数据:

kabhvlkba
slgnaebbga
lajnbabab
kabkbakbvkab
akbakabka
akjbakjbajkba
akjbakbaiajklbna
kljdfnbisn
akbvkab

答案应该是

a

b

abab

a

a

akjbakjb

a

b

a

代码:

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <bitset>
#include <string>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define fin(name) freopen(name,"r",stdin)
#define fout(name) freopen(name,"w",stdout)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 100010;
int wa[N], wb[N], cnt[N], sa[N];
int ran[N], height[N];
char s[N];
int pos[N];

inline int cmp(int r[], int a, int b, int d)
{
    return r[a] == r[b] && r[a + d] == r[b + d];
}
void DA(int n, int m)
{
    int i;
    int *x = wa, *y = wb;
    for (i = 0; i < m; ++i)
        cnt[i] = 0;
    for (i = 0; i < n; ++i)
        ++cnt[x[i] = s[i]];
    for (i = 1; i < m; ++i)
        cnt[i] += cnt[i - 1];
    for (i = n - 1; i >= 0; --i)
        sa[--cnt[x[i]]] = i;
    for (int k = 1; k <= n; k <<= 1)
    {
        int p = 0;
        for (i = n - k; i < n; ++i)
            y[p++] = i;
        for (i = 0; i < n; ++i)
            if (sa[i] >= k)
                y[p++] = sa[i] - k;
        for (i = 0; i < m; ++i)
            cnt[i] = 0;
        for (i = 0; i < n; ++i)
            ++cnt[x[y[i]]];
        for (i = 1; i < m; ++i)
            cnt[i] += cnt[i - 1];
        for (i = n - 1; i >= 0; --i)
            sa[--cnt[x[y[i]]]] = y[i];
        swap(x, y);
        x[sa[0]] = 0;
        p = 1;
        for (i = 1; i < n; ++i)
            x[sa[i]] = cmp(y, sa[i - 1], sa[i], k) ? p - 1 : p++;
        m = p;
        if (m >= n)
            break;
    }
}
void gethgt(int n)
{
    int i, k = 0;
    for (i = 1; i <= n; ++i)
        ran[sa[i]] = i;
    for (i = 0; i < n; ++i)
    {
        if (k)
            --k;
        int j = sa[ran[i] - 1];
        while (s[j + k] == s[i + k])
            ++k;
        height[ran[i]] = k;
    }
}
namespace SG
{
    int dp[N][17];
    void init(int l, int r)
    {
        int i, j;
        for (i = l; i <= r; ++i)
            dp[i][0] = height[i];
        for (j = 1; l + (1 << j) - 1 <= r; ++j)
        {
            for (i = l; i + (1 << j) - 1 <= r; ++i)
                dp[i][j] = min(dp[i][j - 1], dp[i + (1 << (j - 1))][j - 1]);
        }
    }
    int ask(int l, int r)
    {
        int len = r - l + 1;
        int k = 0;
        while (1 << (k + 1) <= len)
            ++k;
        return min(dp[l][k], dp[r - (1 << k) + 1][k]);
    }
    int LCP(int l, int r, int len)
    {
        l = ran[l], r = ran[r];
        if (l > r)
            swap(l, r);
        if (l == r)
            return len - sa[l];
        return ask(l + 1, r);
    }
}
int main(void)
{
    int T = 0, len, i, j;
    while (~scanf("%s", s) && s[0] != ‘#‘)
    {
        len = strlen(s);
        DA(len + 1, 130);
        gethgt(len);
        SG::init(1, len);
        int ans = 1;
        int sz = 0;
        for (int L = 1; L < len; ++L)
        {
            for (i = 0; i + L < len; i += L)
            {
                int lcp = SG::LCP(i, i + L, len);
                int cnt = lcp / L + 1;
                int j = i - (L - lcp % L);
                if (j >= 0 && lcp % L != 0 && SG::LCP(j , j + L, len) / L + 1 > cnt)
                    ++cnt;
                if (cnt > ans)
                {
                    ans = cnt;
                    sz = 0;
                    pos[sz++] = L;
                }
                else if (cnt == ans)
                    pos[sz++] = L;
            }
        }
        int length = 1;
        int st = 0;
        int flag = 0;
        for (i = 1; i <= len && !flag; ++i)
        {
            for (j = 0; j < sz; ++j)
            {
                int unit = pos[j];
                if (sa[i] + unit < len && SG::LCP(sa[i], sa[i] + unit, len) >= (ans - 1) * unit)
                {
                    length = ans * unit;
                    st = sa[i];
                    flag = 1;
                    break;
                }
            }
        }
        printf("Case %d: ", ++T);
        for (i = st; i < st + length; ++i)
            putchar(s[i]);
        puts("");
    }
    return 0;
}
时间: 2024-10-08 02:52:37

POJ 3693 Maximum repetition substring(最多重复次数的子串)的相关文章

POJ 3693 Maximum repetition substring (寻找重复次数最多的连续子串)

Maximum repetition substring Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9083   Accepted: 2782 Description The repetition number of a string is defined as the maximum number R such that the string can be partitioned into R same conse

POJ 3693 Maximum repetition substring (后缀数组)

题目大意: 求出字典序最小,重复次数最多,的子串. 思路分析: RMQ + height 数组可以求出任意两个后缀的lcp 我们枚举答案字符串的重复的长度. 如果这个字符串的长度为 l ,而且这个字符串出现过两次或两次以上 那么你会发现在原串中  str[0] str[l] str[2*l] ....肯定有相邻的两个被包含在重复的串中. 我们求出这两个相邻的后缀的lcp 我们上面仅仅说的是被包含在重复的串中,但并不一定就是以 str[0], str[l],str[2*l]....为起点的. 那我

POJ 3693 Maximum repetition substring(后缀数组神题)

POJ 3693 Maximum repetition substring 题目链接 题意:给定一个字符串,求出其子串中,重复次数最多的串,如果有相同的,输出字典序最小的 思路:枚举长度l,把字符串按l分段,这样对于长度为l的字符串,肯定会包含一个分段位置,这样一来就可以在每个分段位置,往后做一次lcp,求出最大匹配长度,然后如果匹配长度有剩余,看剩余多少,就往前多少位置再做一次lcp,如果匹配出来长度更长,匹配次数就加1,这样就可以枚举过程中保存下答案了 这样问题还有字典序的问题,这个完全可以

poj 3693 Maximum repetition substring(后缀数组)

题目链接:poj 3693 Maximum repetition substring 题目大意:求一个字符串中循环子串次数最多的子串. 解题思路:对字符串构建后缀数组,然后枚举循环长度,分区间确定.对于一个长度l,每次求出i和i+l的LCP,那么以i为起点,循环子串长度为l的子串的循环次数为LCP/l+1,然后再考虑一下从i-l+1~i之间有没有存在增长的可能性. #include <cstdio> #include <cstring> #include <vector>

POJ 3693 Maximum repetition substring(后缀数组求最长重复子串)

题目大意:和spoj687类似,就是当长度相同是需要输出一个最小的字典序的序列. 解体思路:这次需要枚举所有的从i到d = i-L/i (d = i-L%i)的位置,然后记录保证最大值的同时,求出来字典序最小的. Maximum repetition substring Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7418   Accepted: 2217 Description The repetition numb

POJ 3693 Maximum repetition substring(后缀数组+RMQ)

Maximum repetition substring The repetition number of a string is defined as the maximum number R such that the string can be partitioned into R same consecutive substrings. For example, the repetition number of "ababab" is 3 and "ababa&quo

poj 3693 Maximum repetition substring(有点麻烦的后缀数组)

Maximum repetition substring Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6638   Accepted: 2007 Description The repetition number of a string is defined as the maximum number R such that the string can be partitioned into R same conse

POJ - 3693 Maximum repetition substring(后缀数组求重复次数最多的连续重复子串)

Description The repetition number of a string is defined as the maximum number R such that the string can be partitioned into R same consecutive substrings. For example, the repetition number of "ababab" is 3 and "ababa" is 1. Given a

poj 3693 Maximum repetition substring 重复次数最多的连续子串

题目链接 题意 对于任意的字符串,定义它的 重复次数 为:它最多可被划分成的完全相同的子串个数.例如:ababab 的重复次数为3,ababa 的重复次数为1. 现给定一字符串,求它的一个子串,其重复次数取到最大值,且字典序取到最小值. 思路 参考 hzwer. 首先,重复次数显然至少为\(1\),所以下面只考虑重复次数\(\geq 2\)的情况. 首先枚举子串长度\(L\).对于长度为\(L\)的子串,其重复次数至少为\(2\),意味着它的其中两个重复部分必为\(s[0],s[L],s[2L]