题意
对于字符串 S S S 的每个前缀,求其所以回文子串的前缀个数和,本质相同的前缀不重复计数。 N ≤ 3 × 1 0 5 N\leq3\times 10^5 N≤3×105。
乱搞
考虑在末尾新加入一个字符时会产生多少贡献。
假如新出现前缀,它肯定是新产生的回文串(记为 c u r cur cur)的前缀,也就是 PAM 上新加的结点的前缀。
这些前缀有一些是在之前的回文串里面出现过的,因此我们尝试找到 c u r cur cur 与之前任意一个回文串的 LCP 的最大值,则不长于这个 LCP(记为 t t t)的前缀都是出现过的。
t t t 至少有 c u r . f a i l cur.fail cur.fail 那么长。以 t t t 为前缀的回文串要么就是 c u r . f a i l cur.fail cur.fail;要么比 c u r . f a i l cur.fail cur.fail 长,那么这个回文串(记为 u u u)一定是 c u r . f a i l cur.fail cur.fail 在 fail 树上的后代。
实际上第三种情况只用枚举 u . f a i l u.fail u.fail 等于 c u r . f a i l cur.fail cur.fail 的 u u u。原因:假设 u u u 有一个长于 c u r . f a i l cur.fail cur.fail 的回文前缀 v = u . f a i l v=u.fail v=u.fail。若 v v v 是 t t t 的前缀,则 v v v 也应该是 c u r cur cur 的前缀, c u r . f a i l cur.fail cur.fail 应当是 v v v,矛盾;因此 t t t 是 v v v 的前缀,则枚举 v v v 就能统计到 t t t。
因此每次在 PAM 中新加入节点 c u r cur cur 时,暴力枚举 c u r cur cur 在 fail 树上的兄弟 u u u,并判断 c u r cur cur 与 u u u 的最长公共前缀。答案的增量即为 c u r . l e n − max LCP cur.len-\max \text{LCP} cur.len−maxLCP。
时间复杂度: O ( 没被卡 ) O(\text{没被卡}) O(没被卡)
代码(代码后还有内容):
#include<bits/stdc++.h>
using namespace std;
int getint(){
int ans=0,f=1;
char c=getchar();
while(c<'0'||c>'9'){
if(c=='-')f=-1;
c=getchar();
}
while(c>='0'&&c<='9'){
ans=ans*10+c-'0';
c=getchar();
}
return ans*f;
}
const int N=3e5+10;
char s[N];
int n;
namespace PAM{
int ch[N][26],fail[N],len[N],cnt,lst,po[N];
vector<int>fch[N];
string str[N];
long long ans=0;
void init(){
cnt=2;
fail[0]=1;len[0]=0;fch[1].push_back(0);
fail[1]=1;len[1]=-1;
}
void extend(char c,int pos){
int cur=lst;
while(s[pos-len[cur]-1]!=c)cur=fail[cur];
if(!ch[cur][c-'a']){
lst=cnt++;
len[lst]=len[cur]+2;
po[lst]=pos-len[lst]+1;
fail[lst]=fail[cur];
while(s[pos-len[fail[lst]]-1]!=c)fail[lst]=fail[fail[lst]];
fail[lst]=ch[fail[lst]][c-'a'];
fch[fail[lst]].push_back(lst);
ch[cur][c-'a']=lst;
if(0);else{
int fa=fail[lst];
int l=len[fa];
for(int i=0;i<fch[fa].size();i++){
if(fch[fa][i]==lst)continue;
int ll=len[fa];
while(s[po[lst]+ll]==s[po[fch[fa][i]]+ll])++ll;//脑抽没有用 SA 判 LCP
l=max(l,ll);
}
ans+=max(0,len[lst]-l);
}
}
lst=ch[cur][c-'a'];
}
void print(){
for(int i=0;i<cnt;i++){
for(int j=0;j<26;j++){
if(ch[i][j])cerr<<i<<" "<<ch[i][j]<<" "<<char('a'+j)<<endl;
}
cerr<<"fail "<<i<<" "<<fail[i]<<endl;
}
}
}//namespace PAM
int main(){
freopen("string.in","r",stdin);
freopen("string.out","w",stdout);
n=getint();
scanf("%s",s+1);
PAM::init();
for(int i=1;i<=n;i++){
PAM::extend(s[i],i);
printf("%d\n",PAM::ans);
}
//print();
return 0;
}
Hack
想到斐波那契字符串很强,于是乱 Hack 一波。
f=open("string.in","w")
f.write("300000\n")
sb="mnbvcfdssdfcvbnm"
sa="qwertyuioplkjhgazxxzaghjklpoiuytrewq"
while len(sa)<=300000:
sc=sa+sb
sa=sb
sb=sc
# f.write(sa[0:300000])
f.write('a'*300000)
f.close()
能将上述乱搞卡到 2s 左右。
Anti-Hack
除了像我这样没有脑子的人以外,大家应该都想得到用 SA 来求 LCP,于是上述 Hack 只能把它卡到 0.5s 左右。
代码(在出题人的数据上跑得更慢了)(代码后还有内容):
#include<bits/stdc++.h>
using namespace std;
int getint(){
int ans=0,f=1;
char c=getchar();
while(c<'0'||c>'9'){
if(c=='-')f=-1;
c=getchar();
}
while(c>='0'&&c<='9'){
ans=ans*10+c-'0';
c=getchar();
}
return ans*f;
}
const int N=3e5+10;
char s[N];
int n;
namespace SA{
int sa[N],rk[N],tp[N],h[N];
int c[N];
void rsort(int m){
for(int i=1;i<=m;i++)c[i]=0;
for(int i=1;i<=n;i++)c[rk[tp[i]]]++;
for(int i=1;i<=m;i++)c[i]+=c[i-1];
for(int i=n;i>=1;i--)sa[c[rk[tp[i]]]--]=tp[i];
}
bool cmp(int x,int y,int k){
return tp[x]==tp[y]&&tp[x+k]==tp[y+k];
}
void get_sa(){
for(int i=1;i<=n;i++)rk[i]=s[i],tp[i]=i;
int m='z';
rsort(m);
for(int p=0,k=1;p<n;k<<=1,m=p){
p=0;
for(int i=n-k+1;i<=n;i++)tp[++p]=i;
for(int i=1;i<=n;i++)
if(sa[i]>k)tp[++p]=sa[i]-k;
rsort(m);
memcpy(tp,rk,sizeof(tp));
rk[sa[1]]=p=1;
for(int i=2;i<=n;i++)
rk[sa[i]]=cmp(sa[i],sa[i-1],k)?p:++p;
}
//for(int i=1;i<=n;i++)cerr<<sa[i]<<" ";cerr<<endl;
//for(int i=1;i<=n;i++)cerr<<rk[i]<<" ";cerr<<endl;
int t=0;
for(int i=1;i<=n;i++){
if(t)--t;
while(s[sa[rk[i]-1]+t]==s[i+t])++t;
h[rk[i]]=t;
}
//for(int i=1;i<=n;i++)cerr<<h[i]<<" ";cerr<<endl;
}
const int L=20;
int st[L][N],l2[N];
void init_st(){
l2[0]=-1;for(int i=1;i<=n;i++)st[0][i]=h[i+1],l2[i]=l2[i>>1]+1;
for(int i=1;i<L;i++){
for(int j=1;j<=n-(1<<i-1);j++){
st[i][j]=min(st[i-1][j],st[i-1][j+(1<<i-1)]);
}
}
}
int get_min(int l,int r){
if(l>r)swap(l,r);
if(l==r)return n-sa[l]+1;
int t=l2[r-l];
return min(st[t][l],st[t][r-(1<<t)]);
}
}
namespace PAM{
int ch[N][26],fail[N],len[N],cnt,lst,po[N];
vector<int>fch[N];
string str[N];
long long ans=0;
void init(){
cnt=2;
fail[0]=1;len[0]=0;fch[1].push_back(0);
fail[1]=1;len[1]=-1;
}
long long qaq=0;
void extend(char c,int pos){
int cur=lst;
while(s[pos-len[cur]-1]!=c)cur=fail[cur];
if(!ch[cur][c-'a']){
lst=cnt++;
len[lst]=len[cur]+2;
po[lst]=pos-len[lst]+1;
fail[lst]=fail[cur];
while(s[pos-len[fail[lst]]-1]!=c)fail[lst]=fail[fail[lst]];
fail[lst]=ch[fail[lst]][c-'a'];
fch[fail[lst]].push_back(lst);
ch[cur][c-'a']=lst;
if(0);else{
int fa=fail[lst];
int l=len[fa];
for(int i=0;i<fch[fa].size();i++){
if(fch[fa][i]==lst)continue;
if(s[po[lst]+len[fa]]!=s[po[fch[fa][i]]+len[fa]])continue;
//int ll=len[fa];
//while(s[po[lst]+ll]==s[po[fch[fa][i]]+ll])++ll;
int ll=SA::get_min(SA::rk[po[lst]],SA::rk[po[fch[fa][i]]]);
l=max(l,ll);
//++qaq;
}
ans+=max(0,len[lst]-l);
}
}
lst=ch[cur][c-'a'];
}
void print(){
for(int i=0;i<cnt;i++){
for(int j=0;j<26;j++){
if(ch[i][j])cerr<<i<<" "<<ch[i][j]<<" "<<char('a'+j)<<endl;
}
cerr<<"fail "<<i<<" "<<fail[i]<<endl;
}
}
}//namespace PAM
int main(){
freopen("string.in","r",stdin);
freopen("string.out","w",stdout);
n=getint();
scanf("%s",s+1);
PAM::init();
SA::get_sa();
SA::init_st();
for(int i=1;i<=n;i++){
PAM::extend(s[i],i);
printf("%d\n",PAM::ans);
}
//print();
//cerr<<PAM::qaq;
return 0;
}
Anti-Anti-Hack
尽可能让足够多的回文串的 f a i l fail fail 指到一个节点上。于是把它 Hack 掉了。(3~4s)
f=open("string.in","w")
f.write("300000\n")
a="qwertyuiopasdfghjklzxcvbnm"
s=""
i=0
while len(s)<=300000:
i=(i+23333)%300000
c0=a[i%26]
c1=a[i//26%26]
c2=a[i//26//26%26]
c3=a[i//26//26//26%26]
s=s+'a'+c0+c1+c2+c3+c2+c1+c0+'a'
f.write(s[0:300000])
f.close()
Anti-Anti-Anti-Hack(正确的复杂度)
见 某 SCOI 模拟赛 T3 串(string)【PAM】(在出题人的数据上跑得更慢了)