题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=5558
S[a…b] means a substring of S ranging from S[a] to S[b] (0≤a≤b<N). If the first i letters have been encrypted, Alice will try to find a magic string P. Assuming P has K letters, P is the longest string which satisfies P=S[T...T+K−1] (0≤T<i,T+K≤N) and P=S[i…i+K−1](i+K≤N). In other words, P is a substring of S, of which starting address is within [0...i−1], and P is also a prefix of S[i...N−1]. If P exists, Alice will append integer Kand T to ciphertext. If T is not unique, Alice would select the minimal one. And then i is incremented by K. If P does not exist, Alice will append -1 and the ASCII code of letter S[i] to ciphertext, and then increment i by 1.
Obviously the first letter cannot be encrypted. That is to say, P does not exist when i=0. So the first integer of ciphertext must be -1, and the second integer is the ASCII code of S[0].
When i=N, all letters are encrypted, and Alice gets the final ciphertext, which consists of many pairs of integers. Please help Alice to implement this method.
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <bitset>
using namespace std;
const int N=1e5+;
char s[N];
int wa[N],wb[N],wv[N],wss[N];
int rankk[N],height[N],sa[N];
int cmp(int *r,int a,int b,int l){
return r[a]==r[b]&&r[a+l]==r[b+l];
}
void da(char *r,int *sa,int n,int m)
{
int i,j,p,*x=wa,*y=wb,*t;
for(i=;i<m;i++) wss[i]=;
for(i=;i<n;i++) wss[x[i]=r[i]]++;
for(i=;i<m;i++) wss[i]+=wss[i-];
for(i=n-;i>=;i--) sa[--wss[x[i]]]=i;
for(j=,p=;p<n;j*=,m=p)
{
for(p=,i=n-j;i<n;i++) y[p++]=i;
for(i=;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j;
for(i=;i<n;i++) wv[i]=x[y[i]];
for(i=;i<m;i++) wss[i]=;
for(i=;i<n;i++) wss[wv[i]]++;
for(i=;i<m;i++) wss[i]+=wss[i-];
for(i=n-;i>=;i--) sa[--wss[wv[i]]]=y[i];
for(t=x,x=y,y=t,p=,x[sa[]]=,i=;i<n;i++)
x[sa[i]]=cmp(y,sa[i-],sa[i],j)?p-:p++;
}
return ;
}
void calheight(char *r,int *sa,int n)
{
int i,j,k=;
for(i=;i<=n;i++) rankk[sa[i]]=i;
for(i=;i<n;height[rankk[i++]]=k)
for(k?k--:,j=sa[rankk[i]-];r[i+k]==r[j+k];k++);
return ;
} int main()
{
int T,Case=;
cin>>T;
while(T--)
{
scanf("%s",s);
int len=strlen(s);
da(s,sa,len+,);
calheight(s,sa,len);
printf("Case #%d:\n",Case++);
int i=;
while(i<len)
{
int pos,k=;
int rk=rankk[i];
int d=height[rk];
for(int j=rk;j>=&&height[j]!=;j--)
{
d=min(d,height[j]);
if(d<k) break;
if(sa[j-]<i&&((d>k)||(d==k&&sa[j-]<pos))) {
k=d; pos=sa[j-];
}
}
if(rk<len){
d=height[rk+];
for(int j=rk+; j<=len&&height[j]!=; j++)
{
d=min(d,height[j]);
if(d<k) break;
if(sa[j]<i&&((d>k)||(d==k&&sa[j]<pos))) {
k=d; pos=sa[j];
}
}
}
if(k) printf("%d %d\n",k,pos),i+=k;
else printf("%d %d\n",-,(int)s[i++]);
}
}
return ;
}
方法二:集合查找
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <cstring>
#include <cmath>
#include <queue>
#include <set>
#include <bitset>
using namespace std;
const int N=1e5+;
char s[N];
int wa[N],wb[N],wv[N],wss[N];
int cmp(int *r,int a,int b,int l){
return r[a]==r[b]&&r[a+l]==r[b+l];
}
void da(char *r,int *sa,int n,int m)
{
int i,j,p,*x=wa,*y=wb,*t;
for(i=;i<m;i++) wss[i]=;
for(i=;i<n;i++) wss[x[i]=r[i]]++;
for(i=;i<m;i++) wss[i]+=wss[i-];
for(i=n-;i>=;i--) sa[--wss[x[i]]]=i;
for(j=,p=;p<n;j*=,m=p)
{
for(p=,i=n-j;i<n;i++) y[p++]=i;
for(i=;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j;
for(i=;i<n;i++) wv[i]=x[y[i]];
for(i=;i<m;i++) wss[i]=;
for(i=;i<n;i++) wss[wv[i]]++;
for(i=;i<m;i++) wss[i]+=wss[i-];
for(i=n-;i>=;i--) sa[--wss[wv[i]]]=y[i];
for(t=x,x=y,y=t,p=,x[sa[]]=,i=;i<n;i++)
x[sa[i]]=cmp(y,sa[i-],sa[i],j)?p-:p++;
}
return ;
}
int rankk[N],height[N],sa[N],m[][N];
void calheight(char *r,int *sa,int n)
{
int i,j,k=;
for(int i=;i<=n;i++) rankk[sa[i]]=i;
for(i=;i<n;height[rankk[i++]]=k)
for(k?k--:,j=sa[rankk[i]-];r[i+k]==r[j+k];k++);
return ;
}
set<int>se;
set<int>:: iterator it1,it2; int main()
{
int T,Case=;
cin>>T;
while(T--)
{
scanf("%s",s);
int len=strlen(s);
da(s,sa,len+,);
calheight(s,sa,len);
memset(m,,sizeof(m));
for(int i=;i<=len;i++)
m[][i-]=height[i];
for(int i=;i<=(int)(log(len)/log());i++)
{
for(int j=;j+(<<i)-<=len;j++)
m[i][j]=min(height[j+(<<(i-))],min(m[i-][j],m[i-][j+(<<(i-))]));
}
printf("Case #%d:\n",Case++);
int tot=,minn=-,maxn=;
se.clear();
se.insert(minn);
se.insert(maxn);
while(tot<len)
{
int k=,p;
int pos=tot;
it1=se.upper_bound(rankk[tot]);
it2=it1;
while(*it1!=maxn)
{
int v=(int)(log(*it1-rankk[tot]+)/log());
int f=min(m[v][rankk[tot]],m[v][*it1-(<<v)+]);
if(f<k||f==) break;
if(f>k||(f==k&&sa[*it1]<p)){
k=f;
p=sa[*it1];
}
it1++;
}
it2--;
while(*it2!=minn){
int v=(int)(log(rankk[tot]-*it2+)/log());
int f=min(m[v][*it2],m[v][rankk[tot]-(<<v)+]);
if(f<k||f==) break;
if(f>k||(f==k&&sa[*it2]<p)){
k=f;
p=sa[*it2];
}
it2--;
}
if(k==) {
printf("%d %d\n",-,(int)s[tot]);
tot++;
}
else {
printf("%d %d\n",k,p);
tot+=k;
}
for(;pos<tot;pos++)
se.insert(rankk[pos]);
}
}
return ;
}