【分析】
参见论文
//poj 3693 Maximum repetition substring
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
#define M(a) memset(a,0,sizeof a)
#define fo(i,j,k) for(i=j;i<=k;i++)
using namespace std;
const int mxn=200005;
int n,m,T,len,ans,lef,rig;
char s[mxn];
int st[mxn][30],rank[mxn],height[mxn];
int a[mxn],b[mxn],x[mxn],y[mxn],sa[mxn];
inline bool comp(int i,int j,int l)
{
return y[i]==y[j]&&(i+l>len?-1:y[i+l])==(j+l>len?-1:y[j+l]);
}
inline void work()
{
int i,j,k,p;m=200;
fo(i,0,m) b[i]=0;
fo(i,1,len) b[x[i]=a[i]]++;
fo(i,1,m) b[i]+=b[i-1];
for(i=len;i>=1;i--) sa[b[x[i]]--]=i;
for(k=1;k<=len;k<<=1)
{
p=0;
fo(i,len-k+1,len) y[++p]=i;
fo(i,1,len) if(sa[i]>k) y[++p]=sa[i]-k;
fo(i,0,m) b[i]=0;
fo(i,1,len) b[x[y[i]]]++;
fo(i,1,m) b[i]+=b[i-1];
for(i=len;i>=1;i--) sa[b[x[y[i]]]--]=y[i];
swap(x,y),p=2,x[sa[1]]=1;
fo(i,2,len)
x[sa[i]]=comp(sa[i-1],sa[i],k)?p-1:p++;
if(p>len) break;
m=p;
}
p=k=0;
fo(i,1,len) rank[sa[i]]=i;
for(i=1;i<=len;height[rank[i++]]=k)
for(k?k--:0,j=sa[rank[i]-1];a[i+k]==a[j+k];k++);
}
inline void init()
{
int i,j;
fo(i,1,len) st[i][0]=height[i];
fo(j,1,28)
fo(i,1,len) if((i+(1<<j)-1)<=len)
st[i][j]=min(st[i][j-1],st[i+(1<<j-1)][j-1]);
}
inline int query(int x,int y)
{
if(x>y) swap(x,y);
x++;
int k=0;
while(x+(1<<k+1)<=y) k++;
return min(st[x][k],st[y-(1<<k)+1][k]);
}
int main()
{
int i,j,l,r,L;
while(1)
{
M(a);
ans=lef=rig=0;
scanf("%s",s+1);
len=strlen(s+1);
fo(i,1,len) a[i]=s[i];
if(len==1 && s[1]=='#') break;
printf("Case %d: ",++T);
work(),init();
fo(L,1,len/2)
{
for(i=1;i*L+1<=len;i++)
{
int x=(i-1)*L+1,y=i*L+1;
if(s[x]!=s[y]) continue;
r=y+query(rank[x],rank[y])-1;
fo(j,0,L-1)
{
if(x-j<1 || s[x-j]!=s[y-j]) break;
int tmp=(r-(x-j)+1)/L;
if(tmp>ans || (tmp==ans && rank[x-j]<rank[lef]))
lef=x-j,rig=lef+tmp*L-1,ans=tmp;
}
}
}
if(ans==0) printf("%c",s[sa[1]]);
else fo(i,lef,rig) printf("%c",s[i]);
printf("\n");
}
return 0;
}
/*
xbcabcab
*/