啦啦啦,第三道强行算作今天(19号)完成,flag get,开心
这道看到它各种RE心都碎了…于是查出了两个错
1、关于字符串rank最后补0,我栽在了多组数据上…平时单组我就强行当做自己补过0了,结果吃了个亏,折腾了半个小时才发现
2、关于分隔字符也是人…23333333我开了2w+的数组RE了,交了好几遍改了好几次都没发现,最后看题解发现人家开了3w
最近好像懒了很多,题解越来越短啦…不过今天太困了【强行辩解
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#define LL long long
using namespace std;
const int maxn=300005;
const int maxm=maxn;
char str[maxn];
LL ans;
int T,t,n,m,len,l,tot,temp,pos[maxn];
int num[maxn],sa[maxn],rk[maxn],height[maxn];
int wa[maxn],wb[maxn],wv[maxn],cnt[maxm];
void DA(int *r,int n,int m)
{
int *x=wa,*y=wb,p;
for(int i=0;i<m;i++)cnt[i]=0;
for(int i=0;i<n;i++)cnt[x[i]=r[i]]++;
for(int i=1;i<m;i++)cnt[i]+=cnt[i-1];
for(int i=n-1;i>=0;i--)sa[--cnt[x[i]]]=i;
for(int j=1;j<n;j<<=1)
{
p=0;
for(int i=n-j;i<n;i++)y[p++]=i;
for(int i=0;i<n;i++)if(sa[i]-j>=0)y[p++]=sa[i]-j;
for(int i=0;i<n;i++)wv[i]=x[y[i]];
for(int i=0;i<m;i++)cnt[i]=0;
for(int i=0;i<n;i++)cnt[wv[i]]++;
for(int i=1;i<m;i++)cnt[i]+=cnt[i-1];
for(int i=n-1;i>=0;i--)sa[--cnt[wv[i]]]=y[i];
swap(x,y);
p=1;x[sa[0]]=0;
for(int i=1;i<n;i++)
x[sa[i]]=(y[sa[i]]==y[sa[i-1]] && y[sa[i]+j]==y[sa[i-1]+j])?p-1:p++;
if(p>=n)break;
m=p;
}
for(int i=1;i<n;i++)rk[sa[i]]=i;
}
void calheight(int *r,int n)
{
int j=0,k=0;
for(int i=0;i<n;height[rk[i]]=k,i++)
for(k?k--:k=0,j=sa[rk[i]-1];r[i+k]==r[j+k];k++);
}
int main(void)
{
scanf("%d",&T);
while(T--)
{
m=0,tot=27;
scanf("%d",&n);
scanf("%s",str);
len=strlen(str);
for(int i=0;i<len;i++)num[m++]=str[i]-'a'+1;
for(int i=0;i<n;i++)
{
num[m++]=tot++;
scanf("%s",str);
l=strlen(str);
for(int j=0;j<l;j++)
num[m++]=str[j]-'a'+1;
}
num[m]=0;
DA(num,m+1,tot+1);
calheight(num,m);
memset(pos,0,sizeof(pos));
temp=maxn;
for(int i=1;i<=m;i++)
if(sa[i]<len)
{
if(height[i]<temp) temp=height[i];
if(pos[sa[i]]<temp) pos[sa[i]]=temp;
}
else temp=maxn;
temp=maxn;
for(int i=m;i>=1;i--)
if(sa[i-1]<len)
{
if(height[i]<temp) temp=height[i];
if(pos[sa[i-1]]<temp) pos[sa[i-1]]=temp;
}
else temp=maxn;
for(int i=1;i<=m;i++)
if(sa[i]<len&& sa[i-1]<len)
if(pos[sa[i-1]]<height[i])
pos[sa[i-1]]=height[i];
ans=(LL)len*(LL)(len+1)/(LL)2;
for(int i=0;i<len;i++)ans-=pos[i];
cout<<"Case "<<++t<<": "<<ans<<endl;
}
return 0;
}