fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. #define print(a) for(auto x : a) cout << x << " "; cout << endl
  6.  
  7.  
  8. const int M = 1000000007;
  9. const int N = 3e5+9;
  10. const int INF = 2e9+1;
  11. const int LINF = 2000000000000000001;
  12.  
  13. inline int power(int a, int b, int mod=M) {
  14. int x = 1;
  15. a %= mod;
  16. while (b) {
  17. if (b & 1) x = (x * a) % mod;
  18. a = (a * a) % mod;
  19. b >>= 1;
  20. }
  21. return x;
  22. }
  23.  
  24.  
  25. //_ ***************************** START Below *******************************
  26.  
  27.  
  28.  
  29. string str;
  30. vector<int> a;
  31.  
  32. int consistency1(int n, int k){
  33.  
  34. vector<vector<int>> dp(n+1, vector<int>(k+1, 0));
  35. dp[0][0] = 1;
  36. dp[1][1] = 1;
  37.  
  38.  
  39. for(int i=2; i<=n; i++){
  40.  
  41. for(int x=1; x<=k; x++){
  42. vector<int> f(26, 0);
  43.  
  44. f[str[i-1]-'a']++;
  45.  
  46. int j = i-1;
  47. while(j>=0){
  48. bool isValid = true;
  49.  
  50. for(int p=0; p<26; p++){
  51. if(f[p] == 0) continue;
  52. if(f[p] > a[p]){
  53. isValid = false;
  54. break;
  55. }
  56. }
  57.  
  58. if(!isValid) break;
  59.  
  60. dp[i][x] += dp[j][x-1];
  61.  
  62. if(j-1>=0) f[str[j-1]-'a']++;
  63. j--;
  64. }
  65. }
  66. }
  67.  
  68. return dp[n][k];
  69.  
  70. }
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78. int consistency2(int n, int k){
  79.  
  80. vector<vector<int>> dp(n+1, vector<int>(k+1, 0));
  81. vector<vector<int>> prefixDp(n+1, vector<int>(k+1, 0));
  82.  
  83. dp[0][0] = 1;
  84. prefixDp[0][0] = 1;
  85.  
  86. for(int i=1; i<=n; i++){
  87. prefixDp[i][0] = 1;
  88. }
  89.  
  90.  
  91. for(int x=1; x<=k; x++){
  92.  
  93. vector<int> f(26, 0);
  94.  
  95. for(int i=1, j=1; i<=n; i++){
  96. f[str[i-1]-'a']++;
  97.  
  98. while(j<=i){
  99. bool isValid = true;
  100. for(int p=0; p<26; p++){
  101. if(f[p] == 0) continue;
  102. if(f[p] > a[p]){
  103. isValid = false;
  104. break;
  105. }
  106. }
  107. if(isValid) break;
  108.  
  109. f[str[j-1]-'a']--;
  110. j++;
  111. }
  112.  
  113. if(j-2>=0){
  114. dp[i][x] = prefixDp[i-1][x-1] - prefixDp[j-2][x-1];
  115. }
  116. else{
  117. dp[i][x] = prefixDp[i-1][x-1] - 0;
  118. }
  119. prefixDp[i][x] = prefixDp[i-1][x] + dp[i][x];
  120. }
  121. }
  122.  
  123. return dp[n][k];
  124.  
  125. }
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134. int practice(int n, int k){
  135.  
  136.  
  137. return 0;
  138. }
  139.  
  140.  
  141.  
  142.  
  143.  
  144. void solve() {
  145.  
  146. int n , k;
  147. cin>> n >> k;
  148.  
  149. cin >> str;
  150.  
  151. a.resize(26);
  152. for(int i=0; i<n; i++) cin >> a[i];
  153.  
  154. cout << consistency1(n, k) << " " << consistency2(n, k) << endl;
  155.  
  156.  
  157. }
  158.  
  159.  
  160.  
  161.  
  162.  
  163. int32_t main() {
  164. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  165.  
  166. int t = 1;
  167. // cin >> t;
  168. while (t--) {
  169. solve();
  170. }
  171.  
  172. return 0;
  173. }
Success #stdin #stdout 0s 5308KB
stdin
5 3
ababc
1 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
stdout
5 5