poj3693 Maximum repetition substring

Maximum repetition substring
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5907 Accepted: 1787

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
这题也就是上一题的升级版,保存下来所有的最多重复的长度,再用sa数组从小到大一个一个的找,sa数组刚好保证了是字典序,这样就可以得出答案了!
 
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
using namespace std;
#define maxn 1005000
int wa[maxn], wb[maxn], ws[maxn], wv[maxn], wsd[maxn], r[maxn], sa[maxn];
char str[maxn];
#define M 1000100
int dp[M][18];
#define F(x) ((x)/3+((x)%3==1?0:tb))
#define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)
int c0(int *r, int a, int b) {
  return r[a] == r[b] && r[a + 1] == r[b + 1] && r[a + 2] == r[b + 2];
}
int c12(int k, int *r, int a, int b) {
  if (k == 2) return r[a] < r[b] || r[a] == r[b] && c12(1, r, a + 1, b + 1);
  else return r[a] < r[b] || r[a] == r[b] && wv[a + 1] < wv[b + 1];
}
void sort(int *r, int *a, int *b, int n, int m) {
  int i;
  for (i = 0; i < n; i++) wv[i] = r[a[i]];
  for (i = 0; i < m; i++) wsd[i] = 0;
  for (i = 0; i < n; i++) wsd[wv[i]]++;
  for (i = 1; i < m; i++) wsd[i] += wsd[i - 1];
  for (i = n - 1; i >= 0; i--) b[--wsd[wv[i]]] = a[i];
  return;
}
void dc3(int *r, int *sa, int n, int m) {
  int i, j, *rn = r + n, *san = sa + n, ta = 0, tb = (n + 1) / 3, tbc = 0, p;
  r[n] = r[n + 1] = 0;
  for (i = 0; i < n; i++) if (i % 3 != 0) wa[tbc++] = i;
  sort(r + 2, wa, wb, tbc, m);
  sort(r + 1, wb, wa, tbc, m);
  sort(r, wa, wb, tbc, m);
  for (p = 1, rn[F(wb[0])] = 0, i = 1; i < tbc; i++)
    rn[F(wb[i])] = c0(r, wb[i - 1], wb[i]) ? p - 1 : p++;
  if (p < tbc) dc3(rn, san, tbc, p);
  else for (i = 0; i < tbc; i++) san[rn[i]] = i;
  for (i = 0; i < tbc; i++) if (san[i] < tb) wb[ta++] = san[i] * 3;
  if (n % 3 == 1) wb[ta++] = n - 1;
  sort(r, wb, wa, ta, m);
  for (i = 0; i < tbc; i++) wv[wb[i] = G(san[i])] = i;
  for (i = 0, j = 0, p = 0; i < ta && j < tbc; p++)
    sa[p] = c12(wb[j] % 3, r, wa[i], wb[j]) ? wa[i++] : wb[j++];
  for (; i < ta; p++) sa[p] = wa[i++];
  for (; j < tbc; p++) sa[p] = wb[j++];
  return;
}
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 = 0; i < m; i++) wsd[i] = 0;
  for (i = 0; i < n; i++) wsd[x[i] = r[i]]++;
  for (i = 1; i < m; i++) wsd[i] += wsd[i - 1];
  for (i = n - 1; i >= 0; i--) sa[--wsd[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++) wsd[i] = 0;
    for (i = 0; i < n; i++) wsd[wv[i]]++;
    for (i = 1; i < m; i++) wsd[i] += wsd[i - 1];
    for (i = n - 1; i >= 0; i--) sa[--wsd[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 rank1[maxn], height[maxn];
void calheight(int *r, int *sa, int n) {
  int i, j, k = 0;
  for (i = 1; i <= n; i++) rank1[sa[i]] = i;
  for (i = 0; i < n; height[rank1[i++]] = k)
    for (k ? k-- : 0, j = sa[rank1[i] - 1]; r[i + k] == r[j + k]; k++);
  return;
}
void makermq(int n, int b[]) {
  int i, j;
  for (i = 1; i <= n; i++)
    dp[i][0] = b[i];
  for (j = 1; (1 << j) <= n; j++)
    for (i = 1; i + (1 << j) - 1 <= n; i++)
      dp[i][j] = min(dp[i][j - 1], dp[i + (1 << (j - 1))][j - 1]);
}
int rmq(int s, int v) {
  s = rank1[s], v = rank1[v];
  if (s > v)swap(s, v);
  s++;
  int k = (int) (log((v - s + 1) * 1.0) / log(2.0));
  return min(dp[s][k], dp[v - (1 << k) + 1][k]);
}
int q[M], cnt;
int solve(int n) {
  int i, k, tmp, j, maxx = 1, l, t, jj;
  cnt = 0;
  for (l = 1; l < n; l++)
    for (i = 0; i + l < n; i = i + l) {
      k = rmq(i, i + l);
      t = k / l + 1;
      tmp = i - (l - k % l);
      if (tmp >= 0 && (k % l != 0) && rmq(tmp, tmp + l) >= k)
        t++;
      if (t > maxx) {
        maxx = t;
        cnt = 0;
        q[cnt++] = l;
      }
      if (t == maxx) {
        q[cnt++] = l;
      }
    }
  bool flag = true;
  for (i = 1; i <= n && flag; i++)
    for (j = 0; j < cnt; j++) {
      t = q[j];
      tmp = rmq(sa[i], sa[i] + t);
      if (tmp >= (maxx - 1) * t) {
        l = maxx * t;
        for (k = sa[i], jj = 0; jj < l; jj++) {
          printf("%c", str[k + jj]);
        }
        printf("\n");
        flag = false;
        return 1;
      }
    }
  printf("%c\n", str[0]);
  return 1;
}
int main() {
  int n, n1, i, tcase, t = 1;
  while (scanf("%s", str) != EOF) {
    n = strlen(str);
    if (n == 1 && str[0] == '#')
      break;
    printf("Case %d: ", t++);
    for (i = 0; i < n; i++) {
      r[i] = (int) str[i] + 1;
      rank1[i] = height[i] = 0;
      str[n + i] = str[i];
    }
    int l = n;
    r[n] = 0;
    int m = 300;
    //da(r,sa,n+1,m);
    dc3(r, sa, n + 1, m);
    calheight(r, sa, n);
    makermq(n, height);
    int maxx = solve(n);
  }
  return 0;
}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值