首页 > 其他 > 详细

POJ 3693 求重复次数最多的子串

时间:2014-01-30 03:49:56      阅读:490      评论:0      收藏:0      [点我收藏+]
Maximum repetition substring
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6169   Accepted: 1857

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


题意:求重复次数最多的子串,如果重复次数相同,输出字典序最小。

解题思路;后缀数组rmq预处理,枚举长度,然后枚举每一段,根据lcp扩展,跟新答案。

代码:

/* ***********************************************
Author :xianxingwuguan
Created Time :2014-1-29 20:43:51
File Name :4.cpp
************************************************ */

#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int maxn=300300;
int height[maxn],sa[maxn],rank[maxn],c[maxn],t1[maxn],t2[maxn];  
void da(int *str,int n,int m)  
{  
      int i,j,k,p,*x=t1,*y=t2;  
      for(i=0;i<m;i++)c[i]=0;  
      for(i=0;i<n;i++)c[x[i]=str[i]]++;  
      for(i=1;i<m;i++)c[i]+=c[i-1];  
      for(i=n-1;i>=0;i--)sa[--c[x[i]]]=i;  
      for(k=1;k<=n;k<<=1)  
      {  
         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++)c[i]=0;  
         for(i=0;i<n;i++)c[x[y[i]]]++;  
         for(i=1;i<m;i++)c[i]+=c[i-1];  
         for(i=n-1;i>=0;i--)sa[--c[x[y[i]]]]=y[i];  
         swap(x,y);  
         p=1;x[sa[0]]=0;  
         for(i=1;i<n;i++)  
         x[sa[i]]=y[sa[i]]==y[sa[i-1]]&&y[sa[i]+k]==y[sa[i-1]+k]?p-1:p++;  
         if(p>=n)break;  
         m=p;  
      }  
}  
void calheight(int *str,int n)  
{  
      int i,j,k=0;  
      for(i=0;i<=n;i++)rank[sa[i]]=i;  
      for(i=0;i<n;i++)  
      {  
         if(k)k--;  
         j=sa[rank[i]-1];  
         while(str[i+k]==str[j+k])k++;  
         height[rank[i]]=k;  
      }  
         // printf("sa:");for(i=0;i<=n;i++)printf("%d ",sa[i]);puts("");  
       //   printf("rank:");for(i=0;i<=n;i++)printf("%d ",rank[i]);puts("");  
         // printf("height:");for(i=0;i<=n;i++)printf("%d ",height[i]);puts("");  
      
}  
int str[maxn];  
char ss[maxn];  
int Log[maxn];  
int best[23][maxn];  
void init(int n)  
{  
      int i,j;  
      Log[0]=-1;  
      for(i=1;i<=n;i++)  
         Log[i]=(i&(i-1))?Log[i-1]:Log[i-1]+1;  
      for(i=1;i<=n;i++)best[0][i]=height[i];  
      for(i=1;i<=Log[n];i++)  
         for(j=1;j<=n;j++)  
            best[i][j]=min(best[i-1][j],best[i-1][j+(1<<(i-1))]);  
}  
int lcp(int a,int b)  
{  
      a=rank[a];  
      b=rank[b];  
      if(a>b)swap(a,b);  
      a++;  
      int t=Log[b-a+1];  
      return min(best[t][a],best[t][b-(1<<t)+1]);  
}  
int main()
{
      //freopen("data.in","r",stdin);
      //freopen("data.out","w",stdout);
      int T=0;
      while(~scanf("%s",ss)&&ss[0]!=‘#‘)
      {
	     int n=strlen(ss);
	     for(int i=0;i<n;i++)
		    str[i]=ss[i];
	     str[n]=0;
	     da(str,n+1,300);
	     calheight(str,n);
	     init(n);
	     int m1=1,be=sa[1],l1=1;
	     for(int L=1;L<=n;L++)
	     {
		    for(int i=0;i+L<n;i+=L)
		    {
			   int p=lcp(i,i+L);
			   if(p/L+2>=m1)
			   {
				  for(int j=0;j<=i&&j<L&&str[i-j]==str[i-j+L];j++,p++)
				  if(p/L+1>m1||(p/L+1==m1&&rank[be]>rank[i-j]))
					 m1=p/L+1,be=i-j,l1=L;
			   }
		    }
	     }
	     ss[be+m1*l1]=‘\0‘;
            printf("Case %d: %s\n", ++T, ss+be);
      }
      return 0;
}


POJ 3693 求重复次数最多的子串

原文:http://blog.csdn.net/xianxingwuguan1/article/details/18867897

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!