Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- string s;
- int dp[40][40][905][2];
- int fun(int l, int r, int k,bool f) {
- if( k < 0) {
- return 0;
- }
- if( l > r ) {
- return (k == 0);
- }
- if(l == r) {
- if(k == 0) {
- return (s[l] == 'a') + 1;
- } else {
- return ((s[l] - 'a') == k);
- }
- }
- int& ans = dp[l][r][k][f];
- if(ans != -1) {
- return ans;
- }
- if(f == 0) {
- ans = fun(l + 1, r, k, 0) + fun(l, r - 1, k, 1);
- } else {
- ans = fun(l, r - 1, k, 1);
- }
- if(s[l] == s[r]) {
- ans += fun(l + 1, r - 1, k - (s[l] - 'a') * 2, 0);
- }
- return ans;
- }
- void solve()
- {
- cin >> s;
- int n, k;
- cin >> k;
- n = s.length();
- for(int i = 0; i <= n; i++) {
- for(int j = 0; j <= n; j++) {
- for(int p = 0; p <= k; p++) {
- for(int f = 0; f < 2; f++) {
- dp[i][j][p][f] = -1;
- }
- }
- }
- }
- cout << fun(0, n - 1, k, 0) << endl;
- return;
- }
- int main()
- {
- int TESTS=1;
- while(TESTS--)
- {
- solve();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment