ZOJ-3894-Chessgame【区间dp】

来源:互联网 发布:港口历年数据 编辑:程序博客网 时间:2024/06/05 03:53

ZOJ-3894-Chessgame


                        Time Limit: 4 Seconds      Memory Limit: 65536 KB

Today, Bob wants to play a game on a chessboard of 1*n.

At first, n chessmen, painted in either White or Black, are placed on the chessboard. Every time, Bob can choose a chessman. If the left chessman and the right chessman of the chosen one(ignore the blank cause by previous removing) are of the same color, Bob can remove the two chessmen(except the chessman he choose) from the chessboard and get two points.

For example, in the following picture, if Bob choose the black chessman in the third cell, he can remove the two white chessmen and get two points. But the chessman he choose is still at the chessboard.
这里写图片描述

With different ways of choosing, Bob can get different points. Now, Bob wants to get as much points as possible. Can you tell him the most points he can get.

Input

The first line of input is a integer T, the number of games Bob plays. For each game he plays, the first line is the length of the chessboard n(n<=200) and the second line is a string of ‘B’ and ‘W’ which represent the color of the chessmen.

Output

For each game Bob plays, output the most points he can get in a line.

Sample Input

2
5
BWBBW
3
BBB

Sample Output

4
2

Hint

For the first game, Bob can first choose the white chessman in the second cell and remove the chessmen in the first and third cell, the chessboard become #W#BW. Then, he can choose the black chessman in the forth cell and remove the two white chessman.

题目链接:ZOJ 3894

题目思路:区间dp

以下是代码:

#include <vector>#include <map>#include <set>#include <algorithm>#include <iostream>#include <cstdio>#include <cmath>#include <cstdlib>#include <string>#include <cstring>using namespace std;int dp[205][205][205];int ans[205];int main(){    int t;    cin >> t;    while(t--)    {        int n;          cin >> n;        string s;          cin >> s;        memset(dp,0,sizeof(dp));        memset(ans,0,sizeof(ans));        for (int i = 0; i < n; i++)        {            if (s[i] == 'B')    dp[i][i][0] = 1;            else dp[i][i][1] = 1;        }        for (int j = 0; j < n; j++)        {            for (int i = j; i>=0; --i)            {                for (int u = i; u <= j; u++)                {                    for (int v = u+2; v <= j; v++)                    {                        if (dp[i][j][0] == 0)                            dp[i][j][0] = ((dp[i][u][0] && dp[v][j][0]) || (dp[i][u][1] && dp[v][j][1])) && (dp[u+1][v-1][0]);                        if (dp[i][j][1] == 0)                            dp[i][j][1] = ((dp[i][u][0] && dp[v][j][0]) || (dp[i][u][1] && dp[v][j][1])) && (dp[u+1][v-1][1]);                              }                }            }        }        for (int i = 0; i < n; i++)        {            for (int j = 0; j <= i; j++)            {                if (dp[j][i][0] || dp[j][i][1])                    ans[i] = max(ans[i],ans[j] + (i - j + 1) - 1);                else                    ans[i] = max(ans[i],ans[j]);            }        }        cout << ans[n - 1] << endl;    }    return 0;}
0 0