cf204E&bzoj3277&bzoj3473
题解:把所有串连起来做一次sa 对于每个位置的长度做一次二分,对于每个位置找到最长的合适长度,用可持久化结构在维护,时间复杂度nlogn^2;
#include <bits/stdc++.h> #define ll long long const int MAXN=2e5+10; using namespace std; typedef struct node{ int l,r,sum; }node; node d[MAXN*25]; int cnt; char str[MAXN];char str1[MAXN]; int sa[MAXN],txt[MAXN],rank1[MAXN],rank2[MAXN],td[MAXN],t1[MAXN],t2[MAXN]; int n,k; bool cmp(int f[],int w,int m,int k){return f[w]==f[m]&&f[w+k]==f[m+k];} void Sa(){ int len=strlen(str);int m=128; int *td=t1;int *rank1=t2; for(int i=0;i<m;i++)txt[i]=0; for(int i=0;i<len;i++)rank1[i]=str[i],txt[str[i]]++; for(int i=1;i<m;i++)txt[i]+=txt[i-1]; for(int i=len-1;i>=0;i--)sa[--txt[str[i]]]=i; for(int k=1;k<=len;k*=2){ int p=0; for(int i=len-k;i<len;i++)td[p++]=i; for(int i=0;i<len;i++)if(sa[i]>=k)td[p++]=sa[i]-k; for(int i=0;i<m;i++)txt[i]=0; for(int i=0;i<len;i++)txt[rank1[i]]++; for(int i=1;i<m;i++)txt[i]+=txt[i-1]; for(int i=len-1;i>=0;i--)sa[--txt[rank1[td[i]]]]=td[i]; swap(rank1,td);rank1[sa[0]]=0;p=1; for(int i=1;i<len;i++)rank1[sa[i]]=cmp(td,sa[i],sa[i-1],k)?p-1:p++; if(p==len)return ; m=p; } } int h[MAXN],H[MAXN]; void hh(){ int len=strlen(str); for(int i=0;i<len;i++)rank2[sa[i]]=i; memset(H,0,sizeof(H)); for(int i=0;i<len;i++){ if(rank2[i]==0)continue; int t=sa[rank2[i]-1];int w=i;int k; if(i==0||H[i-1]<=1)k=0; else k=H[i-1]-1,t+=k,w+=k; while(t<len&&w<len){ if(str[t]==str[w])k++; else break; t++;w++; } H[i]=k;h[rank2[i]]=k; } } int dp[MAXN][21],mu[21],ma[MAXN]; void st(int len){ mu[0]=1; for(int i=1;i<21;i++)mu[i]=mu[i-1]<<1; ma[0]=-1; for(int i=1;i<len;i++){ if((i&(i-1))==0)ma[i]=ma[i-1]+1; else ma[i]=ma[i-1]; } // for(int i=1;i<len;i++) for(int i=1;i<len;i++)dp[i][0]=h[i]; for(int j=1;mu[j]<=len;j++){ for(int i=1;i+mu[j]<=len;i++){ dp[i][j]=min(dp[i][j-1],dp[i+mu[j-1]][j-1]); } } } int rmq(int l,int r){ int k=ma[r-l+1]; // cout<<l<<" "<<r<<" "<<k<<" "<<dp[l][k]<<" "<<dp[r-mu[k]+1][k]<<endl; return min(dp[l][k],dp[r-mu[k]+1][k]); } void update(int &x,int y,int l,int r,int t,int vul){ x=++cnt;d[x]=d[y];d[x].sum+=vul; if(l==r)return ; int mid=(l+r)>>1; if(t<=mid)update(d[x].l,d[y].l,l,mid,t,vul); else update(d[x].r,d[y].r,mid+1,r,t,vul); } int ans; void querty(int x,int l,int r,int ql,int qr){ if(ql<=l&&r<=qr){ans+=d[x].sum;return ;} int mid=(l+r)>>1; if(ql<=mid)querty(d[x].l,l,mid,ql,qr); if(qr>mid)querty(d[x].r,mid+1,r,ql,qr); } int vis[MAXN],length[MAXN],pos[MAXN],rt[MAXN]; ll sum[MAXN]; bool check(int t,int p,int len){ int l=1;int r=p;int ans1=p+1; while(l<=r){ int mid=(l+r)>>1; // cout<<mid<<" "<<p<<"====="<<rmq(mid,p)<<endl; if(rmq(mid,p)>=t)ans1=mid,r=mid-1; else l=mid+1; } //if(ans1!=p+1)ans1--; ans1--; //if(ans1<0)ans1=0; l=p+1;r=len-1;int ans2=p; while(l<=r){ int mid=(l+r)>>1; // cout<<p+1<<"===="<<mid<<" "<<rmq(p+1,mid)<<endl; if(rmq(p+1,mid)>=t)ans2=mid,l=mid+1; else r=mid-1; } // cout<<ans1<<"----===="<<ans2<<" "<<t<<endl; ans=0;querty(rt[ans2],1,len,ans1,ans2); // cout<<ans<<endl; if(ans>=k)return true; return false; } void solve(){ int len=strlen(str); for(int i=1;i<len;i++){ if(!vis[sa[i]])continue; int t1=pos[sa[i]];int l=0;int r=length[vis[sa[i]]]-t1; int ans1=0; // cout<<i<<" "<<l<<" "<<r<<" "<<vis[sa[i]]<<endl; while(l<=r){ int mid=(l+r)>>1; if(check(mid,i,len))ans1=mid,l=mid+1; else r=mid-1; } // cout<<ans1<<endl; sum[vis[sa[i]]]+=1ll*ans1; } } int pre[MAXN]; int main(){ scanf("%d%d",&n,&k); int len=0; for(int i=1;i<=n;i++){ scanf("%s",str1);int len1=strlen(str1);length[i]=len1; for(int j=0;j<len1;j++)vis[len]=i,pos[len]=j,str[len++]=str1[j]; vis[len]=0,pos[len]=0;str[len++]='$'; } Sa();hh();st(len); // cout<<len<<endl; // cout<<str<<endl; // for(int i=1;i<len;i++)cout<<h[i]<<" "; // cout<<endl; for(int i=1;i<len;i++){ if(!vis[sa[i]]){rt[i]=rt[i-1];continue;} // cout<<pre[vis[sa[i]]]<<" "; update(rt[i],rt[i-1],1,len,i,1); if(pre[vis[sa[i]]])update(rt[i],rt[i],1,len,pre[vis[sa[i]]],-1); pre[vis[sa[i]]]=i; } // cout<<endl; solve(); for(int i=1;i<=n;i++)printf("%lld ",sum[i]); puts(""); }
The Little Elephant loves strings very much.
He has an array a from n strings, consisting of lowercase English letters. Let's number the elements of the array from 1 to n, then let's denote the element number i as ai. For each string ai (1 ≤ i ≤ n) the Little Elephant wants to find the number of pairs of integers l and r (1 ≤ l ≤ r ≤ |ai|) such that substring ai[l... r] is a substring to at least k strings from array a (including the i-th string).
Help the Little Elephant solve this problem.
If you are not familiar with the basic notation in string problems, you can find the corresponding definitions in the notes.
The first line contains two space-separated integers — n and k (1 ≤ n, k ≤ 105). Next n lines contain array a. The i-th line contains a non-empty string ai, consisting of lowercase English letter. The total length of all strings ai does not exceed 105.
On a single line print n space-separated integers — the i-th number is the answer for string ai.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
3 1
abc
a
ab
6 1 3
7 4
rubik
furik
abab
baba
aaabbbababa
abababababa
zero
1 0 9 9 21 30 0
Let's assume that you are given string a = a1a2... a|a|, then let's denote the string's length as |a| and the string's i-th character as ai.
A substring a[l... r] (1 ≤ l ≤ r ≤ |a|) of string a is string alal + 1... ar.
String a is a substring of string b, if there exists such pair of integers l and r (1 ≤ l ≤ r ≤ |b|), that b[l... r] = a.