fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int fun(string x){
  4. int n = x.size();
  5. vector<vector<int>> dp(n,vector<int>(n,0));
  6. for(int i=0;i<n;i++){
  7. dp[i][i] = 1;
  8. }
  9. for(int k=1;k<n;k++){
  10. for(int i=0;i<n-k;i++){
  11. int j = i + k;
  12. if(x[i]==x[j]){
  13. dp[i][j] = 2 + dp[i+1][j-1];
  14. } else{
  15. dp[i][j] = max(dp[i][j-1],dp[i+1][j]);
  16. }
  17. }
  18. }
  19. int maxProd = 0;
  20. for(int i=0;i<n;i++){
  21. for(int j=0;j<n-1;j++){
  22. maxProd = max(maxProd,dp[i][j]*dp[j+1][n-1]);
  23. }
  24. }
  25. return maxProd;
  26. }
  27. int main() {
  28. // your code goes here
  29. cout<<fun("accbcac");
  30. return 0;
  31. }
Success #stdin #stdout 0s 4320KB
stdin
Standard input is empty
stdout
6