Advertisement
esraa_syam

B. So You Think You Can Count?

Oct 23rd, 2024
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.74 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define nl "\n"
  4. #define cnl cout << nl;
  5. #define fi first
  6. #define se second
  7. #define pb push_back
  8. #define ll long long
  9. #define ull unsigned ll
  10. #define RV return void
  11. #define sz(x) int(x.size())
  12. #define all(v) v.begin(), v.end()
  13. #define rall(v) v.rbegin(), v.rend()
  14. #define fixed(n) fixed << setprecision(n)
  15. #define cin(v) for(auto&x:v) cin >> i;
  16. #define cout(v) for(auto&x:v) cout << i << " ";
  17. void files(){
  18.     #ifndef ONLINE_JUDGE
  19.        freopen("input.txt", "r", stdin);
  20.        freopen("output.txt", "w", stdout);
  21.     #endif
  22. }
  23.  
  24.  
  25. int n ;
  26. string s;
  27. vector < vector < ll > > dp;
  28. bool valid(int st , int end){
  29.     if(end - st +  1 > 10 ) return false;
  30.     int freq[10]{};
  31.     for(int i = st ; i <= end ; i++){
  32.         if(freq[s[i]-'0'] > 0) return false;
  33.         freq[s[i]-'0']++;
  34.        
  35.     }
  36.    
  37.     return true;
  38. }
  39.  
  40.  
  41. ll mod = 1e9+7;
  42. ll rec(int idx , int cnt){
  43.     if(idx >= n-1 ) {
  44.         if(idx == n -1 and valid(idx -cnt , idx )) return 1;
  45.         else return 0;
  46.     }
  47.  
  48.     ll & ret = dp[idx][cnt];
  49.     if(~ret)
  50.         return ret ;
  51.     ret =0;
  52.     if(cnt < 10 ){
  53.         ret +=  rec(idx+1 , cnt+1);
  54.         ret %= mod;
  55.     }
  56.  
  57.     if(valid(idx-cnt , idx)) {
  58.         ret += rec(idx+1 , 0);
  59.          ret %= mod;
  60.     }
  61.  
  62.     return ret ;
  63. }
  64. void solve(){
  65.  
  66.  
  67.     cin >> n >> s;
  68.     dp.assign(n+1 , vector < ll > (20 , -1));
  69.     cout << rec(0,0) << nl;
  70.    
  71.    
  72.    
  73.    
  74. }
  75.  
  76.  
  77. int main(){
  78.     ios_base::sync_with_stdio(false);
  79.     cin.tie(nullptr);
  80.     cout.tie(nullptr);
  81.                            
  82.     //  files();
  83.     int testCase=1;
  84.         // cin >> testCase ;
  85.     for(int i=1 ; i <= testCase ; i++){
  86.         solve();
  87.     }
  88.  
  89.     return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement