fork(3) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. string s;
  5. vector<vector<int>> DP;
  6.  
  7. int solve(int l, int r)
  8. {
  9. if (l >= r) {
  10. return 1;
  11. }
  12. if (DP[l][r] > -1) {
  13. return DP[l][r];
  14. }
  15. DP[l][r] = r - l + 1;
  16. for (int m = l; m < r; ++m) {
  17. DP[l][r] = min(DP[l][r], solve(l, m) + solve(m + 1, r));
  18. }
  19. if (s[l] == s[r]) {
  20. DP[l][r] = min(DP[l][r], solve(l + 1, r - 1));
  21. }
  22. return DP[l][r];
  23. }
  24.  
  25. int main()
  26. {
  27. cin >> s;
  28. DP.assign(s.size(), vector<int>(s.size(), -1));
  29. cout << solve(0, s.size() - 1) << endl;
  30. return 0;
  31. }
Success #stdin #stdout 0s 15240KB
stdin
addbcba
stdout
2