File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed
Dynamic Programming/Count Distinct Subsequences Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change 1+ #include < iostream>
2+ #include < set>
3+ #include < iterator>
4+ #include < bitset>
5+ #include < bits/stdc++.h>
6+ using namespace std ;
7+ typedef long long int ll;
8+ ll mod=1000000007 ;
9+
10+
11+ int main () {
12+ ios_base::sync_with_stdio (false );
13+ cin.tie (0 );
14+ cout.tie (0 );
15+ int t;
16+ cin>>t;
17+ while (t--) {
18+ string s;
19+ cin>>s;
20+ int l[26 ] = {0 };
21+ int n = s.length ();
22+ ll sum[n+1 ]={0 } ;
23+ ll dp[n+1 ]={0 };
24+ dp[0 ] = 1 ;
25+ dp[1 ] = 1 ;
26+ sum[0 ] =1 ;
27+ sum[1 ] = 2 ;
28+ l[s[0 ]-' A' ] = 1 ;
29+ for (int i=2 ;i<=n;i++){
30+ dp[i] = sum[i-1 ];
31+ if (l[s[i-1 ]-' A' ]!=0 ) dp[i] = (dp[i] - sum[l[s[i-1 ]-' A' ]-1 ] +mod) %mod;
32+ sum[i] = (sum[i-1 ] + dp[i] )%mod;
33+ l[s[i-1 ]-' A' ] = i;
34+ }
35+ ll ans =0 ;
36+ for (int i=0 ;i<=n;i++){
37+ ans = (ans + dp[i] )%mod;
38+ }
39+ cout<<ans;
40+ }
41+ return 0 ;
42+ }
You can’t perform that action at this time.
0 commit comments