yicongli

BZ4175-DP

Apr 3rd, 2019
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.80 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define gc c=getchar()
  6. #define r(x) read(x)
  7. #define ll long long
  8.  
  9. template<typename T>
  10. inline void read(T&x){
  11.     T k=1;char gc;x=0;
  12.     while(!isdigit(c)){if(c=='-')k=-1;gc;}
  13.     while(isdigit(c))x=x*10+c-'0',gc;x*=k;
  14. }
  15.  
  16. const int p=1005060097;
  17. const int N=4e5+7;
  18.  
  19. inline int add(int a,int b){
  20.     return (a+=b)>=p?a-p:a;
  21. }
  22.  
  23. struct SAM{
  24.     int tot;
  25.     int len[N],fa[N],to[N][26],siz[N],ord[N],cnt[N];
  26.  
  27.     inline int NewNode(int l,int *tr,int su){
  28.         ++tot;
  29.         len[tot]=l;
  30.         if(tr!=NULL)memcpy(to[tot],tr,26<<2);
  31.         fa[tot]=su;
  32.         return tot;
  33.     }
  34.  
  35.     inline int Extend(int u,int c){
  36.         int t=NewNode(len[u]+1,NULL,0);
  37.         for(;u&&!to[u][c];u=fa[u])to[u][c]=t;
  38.         if(!u){
  39.             fa[t]=1;
  40.             return t;
  41.         }
  42.         int v=to[u][c];
  43.         if(len[v]==len[u]+1){
  44.             fa[t]=v;
  45.             return t;
  46.         }
  47.         int x=NewNode(len[u]+1,to[v],fa[v]);
  48.         fa[t]=fa[v]=x;
  49.         for(;u&&to[u][c]==v;u=fa[u])to[u][c]=x;
  50.         return t;
  51.     }
  52.  
  53.     inline void build(char *s,int n){
  54.         int lst=NewNode(0,NULL,0);
  55.         for(int i=1;i<=n;++i)siz[lst=Extend(lst,s[i]-'a')]=1;
  56.         for(int i=1;i<=tot;++i)cnt[len[i]]++;
  57.         for(int i=1;i<=n;++i)cnt[i]+=cnt[i-1];
  58.         for(int i=1;i<=tot;++i)ord[cnt[len[i]]--]=i;
  59.         for(int i=tot;i>1;--i)siz[fa[ord[i]]]+=siz[ord[i]];
  60.     }
  61.  
  62.     int f[2][N];
  63.  
  64.     inline void solve(int n,int m){
  65.         f[0][0]=1;
  66.         for(int i=1;i<=n;++i){
  67.             for(int j=0;j<=m;++j)f[i&1][j]=0;
  68.             for(int j=m;j;--j){
  69.                 for(int k=2;k<=tot;++k){
  70.                     if(j>=siz[k]){
  71.                         f[i&1][j]=add(f[i&1][j],(ll)f[i&1^1][j-siz[k]]*(len[k]-len[fa[k]])%p*siz[k]%p);
  72.                     }
  73.                 }
  74.             }
  75.         }
  76.         printf("%d\n",f[n&1][m]);
  77.         return ;
  78.     }
  79. }sam;
  80.  
  81. char s[N];
  82.  
  83. int main(){
  84.     freopen("tele.in","r",stdin);
  85.     freopen("tele.out","w",stdout);
  86.     int k,m;r(k),r(m);
  87.     scanf("%s",s+1);
  88.     int n=strlen(s+1);
  89.     sam.build(s,n);
  90.     sam.solve(k,m);
  91. }
Advertisement
Add Comment
Please, Sign In to add comment