Advertisement
RaFiN_

count how many distinct subsequence of length j of a string

Jun 26th, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.01 KB | None | 0 0
  1. /* count how many distinct subsequence of length j of a string upto position i */
  2.  
  3.  
  4.  
  5. string s;int n;
  6. ll dp[105][105];
  7.  
  8. void giveres(){
  9.     int last[300];
  10.     mem(last,-1);
  11.     for(int i = 1;i<=n;i++){
  12.        
  13.         for(int j = 1;j<=i;j++){
  14.             dp[i][j] = dp[i-1][j] + dp[i-1][j-1] ;
  15.             if(last[s[i-1]]!=-1)dp[i][j] = dp[i][j] - dp[last[s[i-1]]][j-1];
  16.         }
  17.         last[s[i-1]] = i-1;
  18.     }
  19. }
  20.  
  21. void print(){
  22.     for(int i = 0;i<=n;i++){
  23.         for(int j = 0;j<=i;j++){
  24.             cout<<i<<" "<<j<<" "<<dp[i][j]<<endl;
  25.         }
  26.     }
  27. }
  28.  
  29.  
  30. int main()
  31. {
  32.     booster()
  33.     ///read("input.txt");
  34.  
  35.     ll k;
  36.     cin>>n>>k;
  37.     cin>>s;
  38.     ll ans = 0;
  39.     for(int i = 0;i<=n;i++)
  40.     dp[i][0] = 1;
  41.     giveres();
  42.     //print();
  43.     for(int i = n;i>=0;i--){
  44.       // cout<<i<<" "<<dp[n][i]<<endl;
  45.         ans+=(min(dp[n][i],k))*(n-i);
  46.         k-=min(dp[n][i],k);
  47.         if(!k)break;
  48.     }
  49.     if(!k)cout<<ans;
  50.     else cout<<-1;
  51.  
  52.    
  53.  
  54.  
  55.     return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement