hihocoder1457
http://hihocoder.com/problemset/problem/1457
找不重复子串的和
topo序搞一搞,用父亲更新儿子节点的val,记得乘上节点数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
//#pragma comment(linker, "/stack:200000000") //#pragma GCC optimize("Ofast,no-stack-protector") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") //#pragma GCC optimize("unroll-loops") #include<bits/stdc++.h> #define fi first #define se second #define mp make_pair #define pb push_back #define pi acos(-1.0) #define ll long long #define mod (1000000007) #define C 0.5772156649 #define ls l,m,rt<<1 #define rs m+1,r,rt<<1|1 #define pil pair<int,ll> #define pii pair<int,int> #define ull unsigned long long #define base 1000000000000000000 #define fio ios::sync_with_stdio(false);cin.tie(0) using namespace std; const double g=10.0,eps=1e-12; const int N=2000000+10,maxn=1200000+10,inf=0x3f3f3f3f,INF=0x3f3f3f3f3f3f3f3f; inline void sub(ll &x){if(x>=mod)x-=mod;} char s[N]; int c[N],a[N]; ll val[N],sum[N]; struct SAM{ int last,cnt; int ch[N<<1][26],fa[N<<1],l[N<<1]; int mx[N<<1],tmp[N<<1]; void ins(int c) { int p=last,np=++cnt;last=np;l[np]=l[p]+1; for(;p&&!ch[p][c];p=fa[p])ch[p][c]=np; if(!p)fa[np]=1; else { int q=ch[p][c]; if(l[p]+1==l[q])fa[np]=q; else { int nq=++cnt;l[nq]=l[p]+1; memcpy(ch[nq],ch[q],sizeof(ch[q])); fa[nq]=fa[q];fa[q]=fa[np]=nq; sum[nq]=1ll*c; for(;ch[p][c]==q;p=fa[p])ch[p][c]=nq; } } sum[np]=1ll*c; } SAM() { cnt=1; } void build(){ int len=strlen(s+1); last=1; for(int i=1;i<=len;i++)ins(s[i]-'0'); // topo(); // for(int i=1;i<=cnt;i++)mx[i]=l[i],tmp[i]=0; } void topo(){ for(int i=1;i<=cnt;i++)c[l[i]]++; for(int i=1;i<=cnt;i++)c[i]+=c[i-1]; for(int i=1;i<=cnt;i++)a[c[l[i]]--]=i; } void debug() { puts(""); for(int i=1;i<=cnt;i++) { printf("%d ",i); for(int j=0;j<26;j++) printf("%d ",ch[i][j]); printf("%d %d %lld %lld\n",fa[i],l[i],val[i],sum[i]); } } void faupdate() { for(int i=cnt;i;i--) tmp[fa[a[i]]]=max(tmp[fa[a[i]]],tmp[a[i]]); } void maupdate() { for(int i=1;i<=cnt;i++) mx[i]=min(mx[i],tmp[i]),tmp[i]=0; } void match() { int len=strlen(s+1),now=1,res=0; for(int i=1;i<=len;i++) { if(ch[now][s[i]-'a']) { now=ch[now][s[i]-'a']; res++; } else { while(now&&!ch[now][s[i]-'a'])now=fa[now]; if(!now)now=1,res=0; else { res=l[now]+1; now=ch[now][s[i]-'a']; } } tmp[now]=max(tmp[now],res); } faupdate(); maupdate(); } int maxlen() { int ans=0; for(int i=1;i<=cnt;i++)ans=max(ans,mx[i]); return ans; } void cal() { topo(); for(int i=1;i<=cnt;i++) { int p=a[i]; val[p] = val[p] + 1ll*sum[p] * (l[p] - l[fa[p]])%mod; sub(val[p]); for(int j=0;j<10;j++) { if(ch[p][j]) { val[ch[p][j]]=val[ch[p][j]]+val[p]*10ll%mod; sub(val[ch[p][j]]); } } // cout << val[p] << endl; } // debug(); ll ans=0; for(int i=cnt;i;i--) { ans=ans+val[i]; sub(ans); } printf("%lld\n",ans); } }sam; int main() { int n; scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%s",s+1); sam.build(); } sam.cal(); return 0; } /******************** 1 101 ********************/
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
//#pragma comment(linker, "/stack:200000000") //#pragma GCC optimize("Ofast,no-stack-protector") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") //#pragma GCC optimize("unroll-loops") #include<bits/stdc++.h> #define fi first #define se second #define mp make_pair #define pb push_back #define pi acos(-1.0) #define ll long long #define mod (1000000007) #define C 0.5772156649 #define ls l,m,rt<<1 #define rs m+1,r,rt<<1|1 #define pil pair<int,ll> #define pii pair<int,int> #define ull unsigned long long #define base 1000000000000000000 #define fio ios::sync_with_stdio(false);cin.tie(0) using namespace std; const double g=10.0,eps=1e-12; const int N=2000000+10,maxn=1200000+10,inf=0x3f3f3f3f,INF=0x3f3f3f3f3f3f3f3f; inline void sub(ll &x){if(x>=mod)x-=mod;} char s[N]; int c[N],a[N]; ll val[N],sum[N],sz[N]; struct SAM{ int last,cnt; int ch[N<<1][26],fa[N<<1],l[N<<1]; int mx[N<<1],tmp[N<<1]; void ins(int c) { int p=last,np=++cnt;last=np;l[np]=l[p]+1; for(;p&&!ch[p][c];p=fa[p])ch[p][c]=np; if(!p)fa[np]=1; else { int q=ch[p][c]; if(l[p]+1==l[q])fa[np]=q; else { int nq=++cnt;l[nq]=l[p]+1; memcpy(ch[nq],ch[q],sizeof(ch[q])); fa[nq]=fa[q];fa[q]=fa[np]=nq; for(;ch[p][c]==q;p=fa[p])ch[p][c]=nq; } } sum[np]=1ll*c; } SAM() { cnt=1; } void build(){ int len=strlen(s+1); last=1; for(int i=1;i<=len;i++)ins(s[i]-'0'); // topo(); // for(int i=1;i<=cnt;i++)mx[i]=l[i],tmp[i]=0; } void topo(){ for(int i=1;i<=cnt;i++)c[l[i]]++; for(int i=1;i<=cnt;i++)c[i]+=c[i-1]; for(int i=1;i<=cnt;i++)a[c[l[i]]--]=i; } void debug() { puts(""); for(int i=1;i<=cnt;i++) { printf("%d ",i); for(int j=0;j<26;j++) printf("%d ",ch[i][j]); printf("%d %d %lld %lld\n",fa[i],l[i],val[i],sum[i]); } } void faupdate() { for(int i=cnt;i;i--) tmp[fa[a[i]]]=max(tmp[fa[a[i]]],tmp[a[i]]); } void maupdate() { for(int i=1;i<=cnt;i++) mx[i]=min(mx[i],tmp[i]),tmp[i]=0; } void match() { int len=strlen(s+1),now=1,res=0; for(int i=1;i<=len;i++) { if(ch[now][s[i]-'a']) { now=ch[now][s[i]-'a']; res++; } else { while(now&&!ch[now][s[i]-'a'])now=fa[now]; if(!now)now=1,res=0; else { res=l[now]+1; now=ch[now][s[i]-'a']; } } tmp[now]=max(tmp[now],res); } faupdate(); maupdate(); } int maxlen() { int ans=0; for(int i=1;i<=cnt;i++)ans=max(ans,mx[i]); return ans; } void cal() { topo(); sz[1]=1; for(int i=1;i<=cnt;i++) { int p=a[i]; // val[p] = val[p] + 1ll*sum[p] * (l[p]-l[fa[p]])%mod; // sub(val[p]); for(int j=0;j<10;j++) { if(ch[p][j]) { val[ch[p][j]]=val[ch[p][j]]+val[p]*10ll%mod+1ll*j*sz[p]%mod; sub(val[ch[p][j]]); sz[ch[p][j]]+=sz[p]; } } // cout << val[p] << endl; } // debug(); ll ans=0; for(int i=cnt;i;i--) { ans=ans+val[i]; sub(ans); } printf("%lld\n",ans); } }sam; int main() { int n; scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%s",s+1); sam.build(); // if(i!=n)sam.ins(10); } sam.cal(); return 0; } /******************** 1 987654321123456789 ********************/