sacgajcvs

Untitled

Oct 22nd, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. string s;
  5. int dp[40][40][905][2];
  6.  
  7. int fun(int l, int r, int k,bool f) {
  8. if( k < 0) {
  9. return 0;
  10. }
  11. if( l > r ) {
  12. return (k == 0);
  13. }
  14. if(l == r) {
  15. if(k == 0) {
  16. return (s[l] == 'a') + 1;
  17. } else {
  18. return ((s[l] - 'a') == k);
  19. }
  20. }
  21. int& ans = dp[l][r][k][f];
  22. if(ans != -1) {
  23. return ans;
  24. }
  25. if(f == 0) {
  26. ans = fun(l + 1, r, k, 0) + fun(l, r - 1, k, 1);
  27. } else {
  28. ans = fun(l, r - 1, k, 1);
  29. }
  30. if(s[l] == s[r]) {
  31. ans += fun(l + 1, r - 1, k - (s[l] - 'a') * 2, 0);
  32. }
  33. return ans;
  34. }
  35.  
  36.  
  37. void solve()
  38. {
  39. cin >> s;
  40. int n, k;
  41. cin >> k;
  42. n = s.length();
  43. for(int i = 0; i <= n; i++) {
  44. for(int j = 0; j <= n; j++) {
  45. for(int p = 0; p <= k; p++) {
  46. for(int f = 0; f < 2; f++) {
  47. dp[i][j][p][f] = -1;
  48. }
  49. }
  50. }
  51. }
  52. cout << fun(0, n - 1, k, 0) << endl;
  53. return;
  54. }
  55. int main()
  56. {
  57. int TESTS=1;
  58. while(TESTS--)
  59. {
  60. solve();
  61. }
  62. return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment