后缀数组(重复次数最多的最长重复子串)—— POJ 3693

来源:互联网 发布:c语言结束符 编辑:程序博客网 时间:2024/05/17 03:28

对应POJ题目:点击打开链接

Maximum repetition substring
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 7546 Accepted: 2267

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

ccabababcdaabbccaa#

Sample Output

Case 1: abababCase 2: aa

题意:求一个字符串中重复次数最多的最长连续重复子串,如果有多个解, 输出字典序最小的那个。


思路:看了论文后不是太懂,看此题解后豁然开朗。以下题解引自:http://blog.csdn.net/acm_cxlove/article/details/7941205

比较容易理解的部分就是枚举长度为L,然后看长度为L的字符串最多连续出现几次。

既然长度为L的串重复出现,那么str[0],str[l],str[2*l]……中肯定有两个连续的出现在字符串中。

那么就枚举连续的两个,然后从这两个字符前后匹配,看最多能匹配多远。

即以str[i*l],str[i*l+l]前后匹配,这里是通过查询suffix(i*l),suffix(i*l+l)的最长公共前缀

通过rank值能找到i*l,与i*l+l的排名,我们要查询的是这段区间的height的最小值,通过RMQ预处理

达到查询为0(1)的复杂度

 设LCP长度为M, 则答案显然为M / L + 1, 但这不一定是最好的, 因为答案的首尾不一定再我们枚举的位置上. 我的解决方法是, 我们考虑M % L的值的意义, 我们可以认为是后面多了M % L个字符, 但是我们更可以想成前面少了(L - M % L)个字符! 所以我们求后缀j * L - (L - M % L)与后缀(j + 1) * L - (L - M % L)的最长公共前缀。

即把之前的区间前缀L-M%L即可。

然后把可能取到最大重复次数ans和长度L保存,由于 题目要求字典序最小,通过sa数组进行枚举,取到的第一组,肯定是字典序最小的。


#include <stdio.h>  #include <stdlib.h>  #include <string.h>  #include <math.h>  #define MS(x, y) memset(x, y, sizeof(x))  #define MIN(x, y) ((x)<(y)?(x):(y))#define MAX(x, y) ((x)>(y)?(x):(y))const int MAXN = 100000+10;  const int INF = 1<<30;    int dp[MAXN][20];int wa[MAXN],wb[MAXN],wv[MAXN],ws[MAXN];  int rank[MAXN],r[MAXN],sa[MAXN],height[MAXN];  char str[MAXN];  int a[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, *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<<=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++) 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;  }    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-1; height[rank[i++]] = k)          for(k ? k-- : 0,j=sa[rank[i]-1]; r[i+k] == r[j+k]; k++);      return;  }  void caldp(int n)  {  int i,j;    for(i=1; i<n; i++)        dp[i][0]=height[i];      for(j=1; j<=20; j++){ //注意这个要在外层          for(i=1; i<n; i++){              if(i+(1<<j)-1 < n){                  dp[i][j]=MIN(dp[i][j-1], dp[i+(1<<(j-1))][j-1]);              }          }      }  }    int rmq(int l, int r)  {  if(l > r) {int tmp = l; l = r + 1; r = tmp;}else l++;    int k = int(log(double(r-l+1))/log(2.0));      return MIN(dp[l][k], dp[r-(1<<k)+1][k]);  }  int main(){#if 1freopen("in.txt", "r", stdin);#endifint w = 0;while(~scanf("%s", str), str[0] != '#'){printf("Case %d: ", ++w);MS(dp, 0);MS(r, 0);MS(sa, 0);MS(rank, 0);MS(height, 0);MS(ws, 0);MS(wv, 0);MS(wa, 0);MS(wb, 0);int i,j;int len = strlen(str);for(i=0; i<len; i++)r[i] = str[i] - 'a' + 1;r[len++] = 0;da(r, sa, len, 30);calheight(r, sa, len);caldp(len);int L, ans = 0;int M, per, tmp, cnt = 0, beg = 0;for(L=1; L<len; L++){for(i=0; i<len - 1; i+=L){if(r[i] != r[i + L]) continue;M = rmq(rank[i], rank[i + L]);tmp = M / L + 1;per = i - (L - M % L);if(per >= 0 && M % L){//检查是否可以往前拼凑if(rmq(rank[per], rank[per + L]) > M) tmp++;}if(tmp == ans) a[cnt++] = L;else if(tmp > ans){cnt = 0;a[cnt++] = L;ans = tmp;}}}for(i=1; i<len; i++){for(j=0; j<cnt; j++){L = a[j];if(rmq(i, rank[sa[i] + L]) >= (ans-1) * L){//第一组符合条件的配对beg = sa[i];goto A;}}}A:for(i=beg; i<beg + L * ans; i++)printf("%c", str[i]);printf("\n");}return 0;}




0 0