Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int N = 1000;
- char cards[N + 2];
- int dp[N + 2][N + 1];
- int main(){
- int nCards;
- scanf("%d", &nCards);
- for(int i = 1; i <= nCards; ++i){
- scanf(" %c", &cards[i]);
- }
- for(int d = 3; d <= nCards; ++d){
- for(int l = 1; l <= nCards - d + 1; ++l){
- int r = l + d - 1;
- int ans = -1e9;
- for(int i = l + 2; i <= r; ++i){
- if(cards[l] == cards[i]){
- ans = max(ans, 1 + dp[l + 1][i - 1] + dp[i + 1][r]);
- }
- }
- dp[l][r] = max(ans, dp[l + 1][r]);
- }
- }
- cout << dp[1][nCards];
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement