zoukankan      html  css  js  c++  java
  • POJ3693 Maximum repetition substring —— 后缀数组 重复次数最多的连续重复子串

    题目链接:https://vjudge.net/problem/POJ-3693

    Maximum repetition substring
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 11250   Accepted: 3465

    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

    Source

    题意:

    给出一个字符串,求该字符串的重复次数最多的连续重复子串,输出该子串,如果有多个答案,输出字典序最小的那个。

    题解:

    SPOJ - REPEATS的加强版,需要输出目标串。

    代码如下:

      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <algorithm>
      5 #include <vector>
      6 #include <cmath>
      7 #include <queue>
      8 #include <stack>
      9 #include <map>
     10 #include <string>
     11 #include <set>
     12 using namespace std;
     13 typedef long long LL;
     14 const int INF = 2e9;
     15 const LL LNF = 9e18;
     16 const int MOD = 1e9+7;
     17 const int MAXN = 1e6+100;
     18 
     19 bool cmp(int *r, int a, int b, int l)
     20 {
     21     return r[a]==r[b] && r[a+l]==r[b+l];
     22 }
     23 
     24 int r[MAXN], sa[MAXN], Rank[MAXN], height[MAXN];
     25 int t1[MAXN], t2[MAXN], c[MAXN];
     26 void DA(int str[], int sa[], int Rank[], int height[], int n, int m)
     27 {
     28     n++;
     29     int i, j, p, *x = t1, *y = t2;
     30     for(i = 0; i<m; i++) c[i] = 0;
     31     for(i = 0; i<n; i++) c[x[i] = str[i]]++;
     32     for(i = 1; i<m; i++) c[i] += c[i-1];
     33     for(i = n-1; i>=0; i--) sa[--c[x[i]]] = i;
     34     for(j = 1; j<=n; j <<= 1)
     35     {
     36         p = 0;
     37         for(i = n-j; i<n; i++) y[p++] = i;
     38         for(i = 0; i<n; i++) if(sa[i]>=j) y[p++] = sa[i]-j;
     39 
     40         for(i = 0; i<m; i++) c[i] = 0;
     41         for(i = 0; i<n; i++) c[x[y[i]]]++;
     42         for(i = 1; i<m; i++) c[i] += c[i-1];
     43         for(i = n-1; i>=0; i--) sa[--c[x[y[i]]]] = y[i];
     44 
     45         swap(x, y);
     46         p = 1; x[sa[0]] = 0;
     47         for(i = 1; i<n; i++)
     48             x[sa[i]] = cmp(y, sa[i-1], sa[i], j)?p-1:p++;
     49 
     50         if(p>=n) break;
     51         m = p;
     52     }
     53 
     54     int k = 0;
     55     n--;
     56     for(i = 0; i<=n; i++) Rank[sa[i]] = i;
     57     for(i = 0; i<n; i++)
     58     {
     59         if(k) k--;
     60         j = sa[Rank[i]-1];
     61         while(str[i+k]==str[j+k]) k++;
     62         height[Rank[i]] = k;
     63     }
     64 }
     65 
     66 int dp[MAXN][20], mm[MAXN];
     67 void initRMQ(int n, int b[])
     68 {
     69     mm[0] = -1;
     70     for(int i = 1; i<=n; i++)
     71         dp[i][0] = b[i], mm[i] = ((i&(i-1))==0)?mm[i-1]+1:mm[i-1];
     72     for(int j = 1; j<=mm[n]; j++)
     73     for(int i = 1; i+(1<<j)-1<=n; i++)
     74         dp[i][j] = min(dp[i][j-1], dp[i+(1<<(j-1))][j-1]);
     75 }
     76 
     77 int RMQ(int x, int y)
     78 {
     79     if(x>y) swap(x, y);
     80     x++;
     81     int k = mm[y-x+1];
     82     return min(dp[x][k], dp[y-(1<<k)+1][k]);
     83 }
     84 
     85 char str[MAXN];
     86 int Len[MAXN];
     87 int main()
     88 {
     89     int kase = 0;
     90     while(scanf("%s", str) && str[0]!='#')
     91     {
     92         int n = strlen(str);
     93         for(int i = 0; i<n; i++)
     94             r[i] = str[i];
     95         r[n] = 0;
     96         DA(r, sa, Rank, height, n, 128);
     97         initRMQ(n, height);
     98 
     99         int times = 0, cnt = 0;
    100         for(int len = 1; len<=n; len++)
    101         for(int pos = 0; pos+len<n; pos += len)
    102         {
    103             int LCP = RMQ(Rank[pos], Rank[pos+len]);
    104             int supplement = len - LCP%len;
    105             int k = pos - supplement;
    106             if(k>=0 && LCP%len && RMQ(Rank[k],Rank[k+len])>=supplement)
    107                 LCP += supplement;
    108             /*
    109                 当不能加上supplement时,以pos为起点的子串不一定是字典序最小,
    110                 而应该在[pos, pos+LCP%len]里面取最小,所以为了取得字典序最小,
    111                 先不记录位置,而只记录出现次数最大的情况下有多少种循环节,等
    112                 统计完之后,再按排名从前到后,为每个sa[i]匹配循环节,匹配成功
    113                 即为答案。
    114             */
    115             int tmp = LCP/len+1;
    116             if(tmp>times) times = tmp, Len[cnt=1] = len;
    117             else if(tmp==times) Len[++cnt] = len;
    118         }
    119         int L, R, flag = true;
    120         for(int i = 1; i<=n && flag; i++)
    121         for(int j = 1; j<=cnt; i++)
    122         {
    123             int len = Len[j];
    124             int LCP = RMQ(i, Rank[sa[i]+len]);
    125             if(LCP>=len*(times-1))
    126             {
    127                 L = sa[i];
    128                 R = sa[i]+len*times-1;
    129                 flag = false;
    130                 break;
    131             }
    132         }
    133         printf("Case %d: ", ++kase);
    134         for(int i = L; i<=R; i++)
    135             putchar(str[i]);
    136         putchar('
    ');
    137     }
    138 }
    View Code
  • 相关阅读:
    leetcode刷题笔记一百四十一题与一百四十二题 环形链表与环形链表2
    leetcode刷题笔记一百三十九题与一百四十题 单词拆分与单词拆分II
    leetcode刷题笔记一百三十八题 复制带随机指针的链表
    leetcode刷题笔记一百三十六题与一百三十七题 只出现一次的数字与只出现一次的数字II
    sklearn.ensemble.RandomForestClassifier 随机深林参数详解
    Python国内镜像地址
    机器学习from(zhouxun-old leader)
    np.argsort()元素从小到大排序后,提取对应的索引index,可以一行搞定排序
    df.mask() 和df.where() 替换方法区别
    sklearn.feature_selection.VarianceThreshold 方差阈值法(过滤法的一种)
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/8470172.html
Copyright © 2011-2022 走看看