zoukankan      html  css  js  c++  java
  • CodeForces 607B zuma

    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.

    Examples

    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。若区间i~j为回文串,那么i+1~j-1也必然为回文串,dp[i][j]=dp[i+1][j-1],

    如果不是,这就枚举中间点,区间DP裸题;

    参考代码为:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 
     4 int num[550], dp[550][550];
     5 int main()
     6 {
     7     int n;
     8     while (~scanf("%d", &n))
     9     {
    10         memset(num, 0, sizeof(num));
    11         memset(dp,0x3f,sizeof(dp));
    12         for (int i = 1; i <= n; i++)
    13             scanf("%d", &num[i]);
    14         for (int i = 1; i <= n; i++)
    15             dp[i][i] = 1;
    16         for (int i = n; i >= 1; i--)
    17             for (int j = i + 1; j <= n; j++)
    18             {
    19                 if (num[i] == num[j])
    20                 {
    21                     if (abs(j - i) != 1)
    22                         dp[i][j] = dp[i + 1][j - 1];
    23                     else
    24                         dp[i][j] = 1;
    25                 }
    26                 
    27                 for (int k = i; k<j; k++)
    28                 {
    29                     dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j]);
    30                 }
    31             }
    32         printf("%d
    ", dp[1][n]);
    33     }
    34     return 0;
    35 }
    View Code
     
  • 相关阅读:
    sql server 2008 不允许保存更改,您所做的更改要求删除并重新创建以下表 的解决办法
    给 textbox TextMode="password" 赋值后显示出来
    mysql limit分页优化方法分享
    jquery iframe取得元素与自适应高度
    js动态改变iframe的高度
    完全解读 margin 标签
    Page.ClientScript、ClientScript、ScriptManager、ClientScriptManager等的详细解说
    Asp.net C# 使用Newtonsoft.Json 实现DataTable转Json格式数据
    Newtonsoft.Json(Json.net)的基本用法
    利用navicat创建存储过程、触发器和使用游标的简单实例
  • 原文地址:https://www.cnblogs.com/csushl/p/9415954.html
Copyright © 2011-2022 走看看