Codeforces607B

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:dp[i][j]表示删除序列[i,j]的最少次数,但是难点在如何更新状态,找到两个状态之间的关系

考虑dp[i][j],这里用位置i作为参考点:

如果i作为一个单独的回文串删除,则dp[i][j]=min(dp[i][j], 1+dp[i+1][j])

如果i和[i+1,j]内某个位置k可以构成回文串,则dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j])

因为可以将位置i和位置k看成一个回文串删去(如果i+1和k-1也相同,也没有影响,删除的次数都是相同的),所以上式又可以改 成:dp[i][j]=min(dp[i][j],dp[i+1][k-1]+dp[k+1][j])

这样找到了状态转移方程,外层的循环时字符串的长度,中间那层是i的起始位置,内层则是查找与i比较的字符(可以看代码的注释)

#include<iostream>
#include<string.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
using namespace std;
const int INF = 1<<29;
const int MAX = 550;
int N;
int num[MAX];
int dp[MAX][MAX];//dp[i][j]表示删除序列[i,j]的最少次数


void solve()
{
    for (int i = 1; i <= N; i++)
        dp[i][i] = 1;

    for (int len = 2; len <= N; len++)
    {
        for (int i = 1; i <= N - len + 1; i++)
        {
            //把位置i当作一个单独的回文串消去的情况
            dp[i][i+len-1] = min(dp[i][i+len-1], 1+dp[i+1][i+len-1]);

            //在区间[i+1,i+len-1)中查找是否有与左端点相同的字符
            for (int j = i + 1; j <= len + i - 2; j++)
                if (num[i] == num[j])
                {
                    if (j - 1 >= i + 1)
                        dp[i][i+len-1] = min(dp[i][i+len-1], dp[i+1][j-1] + dp[j+1][i+len-1]);
                    else    //j-1<i+1
                        dp[i][i+len-1] = min(dp[i][i+len-1], 1 + dp[j+1][i+len-1]);
                }
            //判断最后一个字符(第i+len-1个)和第i个字符是否相同
            if (num[i] == num[len+i-1]) //还得判断 i+1和i+len-2的关系
            {
                if (len != 2)
                    dp[i][i+len-1] = min(dp[i][i+len-1], dp[i+1][i+len-2]);
                else
                    dp[i][i+len-1] = 1;
            }

        }
    }
}

int main()
{
    cin >> N;
    for (int i = 1; i <= N; i++)
        cin >> num[i];

    for (int i = 1; i <= N; i++)
        for (int j = 1; j <= N; j++)
            dp[i][j] = INF;

    solve();
    cout << dp[1][N] << endl;

    return 0;
}





 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值