Advertisement
Mirbek

Субпалиндромы (Рекурсивый метод)

Feb 22nd, 2022
730
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.47 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int n;
  6. string s;
  7.  
  8. int get(int l, int r) {
  9.     if (l > r) return 0;
  10.     if (l == r) return 1;
  11.     int ans = 0;
  12.     int a1 = get(l, r - 1);
  13.     int a2 = get(l + 1, r);
  14.     int a3 = get(l + 1, r - 1);
  15.     ans += a1 + a2 - a3;
  16.     if (s[l] == s[r]) {
  17.         ans += get(l + 1, r - 1) + 1;
  18.     }
  19.     return ans;
  20. }
  21.  
  22. int main(){
  23.     cin >> s;
  24.     n = s.size();
  25.     s = ' ' + s;
  26.     cout << get(1, n) << endl;
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement