Codeforces 607B:Zuma DP

3778 단어 Codeforcesdp-구간dp
B. Zuma
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output
Genos recently installed the game Zuma on his phone. In Zuma there exists a line of n gemstones, the i-th of which has color ci. The goal of the game is to destroy all the gemstones in the line as quickly as possible.
In one second, Genos is able to choose exactly one continuous substring of colored gemstones that is a palindrome and remove it from the line. After the substring is removed, the remaining gemstones shift to form a solid line again. What is the minimum number of seconds needed to destroy the entire line?
Let us remind, that the string (or substring) is called palindrome, if it reads same backwards or forward. In our case this means the color of the first gemstone is equal to the color of the last one, the color of the second gemstone is equal to the color of the next to last and so on.
Input
The first line of input contains a single integer n (1 ≤ n ≤ 500) — the number of gemstones.
The second line contains n space-separated integers, the i-th of which is ci (1 ≤ ci ≤ n) — the color of the i-th gemstone in a line.
Output
Print a single integer — the minimum number of seconds needed to destroy the entire line.
Sample test(s)
input
3
1 2 1

output
1

input
3
1 2 3

output
3

input
7
1 4 4 2 3 2 1

output
2

Note
In the first sample, Genos can destroy the entire line in one second.
In the second sample, Genos can only destroy one gemstone at a time, so destroying three gemstones takes three seconds.
In the third sample, to achieve the optimal time of two seconds, destroy palindrome 4 4 first and then destroy palindrome 1 2 3 2 1.
문자열을 지정하면 매번 문자열에서 회문 문자열을 제거할 수 있으며, 그 다음에 남은 문자열이 연결되어 새로운 문자열을 구성하여 제거할 수 있습니다.문자열에 최소한 몇 번의 제거 작업이 필요한지 물어보십시오.
DP.
코드:
#pragma warning(disable:4996)  
#include   
#include   
#include   
#include   
#include   
#include 
#include 
using namespace std;
typedef long long ll;
#define INF 0x3fffffff

int n;
int val[505];
int dp[505][505];

void input()
{
    int i;
    scanf("%d", &n);

    for (i = 1; i <= n; i++)
    {
        scanf("%d", &val[i]);
    }
}

void solve()
{
    memset(dp, 63, sizeof(dp));

    int len;
    int i, j, k;

    for (len = 0; len <= n; len++)
    {
        j = 0;
        for (i = 1; i <= n&&j <= n; i++)
        {
            j = i + len;

            if (i == j)
            {
                dp[i][j] = 1;
            }
            else if (j == i + 1)
            {
                if (val[i] == val[j])
                {
                    dp[i][j] = 1;
                }
                else
                {
                    dp[i][j] = 2;
                }
            }
            else
            {
                if (val[i] == val[j])
                {
                    dp[i][j] = min(dp[i][j], dp[i + 1][j - 1]);
                }
                for (k = i; k <= j; k++)
                {
                    dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j]);
                }
            }
        }
    }
    printf("%d", dp[1][n]);
}

int main()
{
    //freopen("i.txt", "r", stdin);
    //freopen("o.txt", "w", stdout);

    input();
    solve();

    //system("pause");
    return 0;
}

좋은 웹페이지 즐겨찾기