Zuma_Codeforces 607B

题目

 

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.

题目大意 

给你一个长度为n的字符串,每次你可以消去一段连续的回文子串,剩下的两端重新拼接成一个新的串,问最少需要消去多少次 

算法:区间dp 

https://blog.youkuaiyun.com/my_sunshine26/article/details/77141398

模板如下

//mst(dp,0) 初始化DP数组
for(int i=1;i<=n;i++)
{
    dp[i][i]=初始值
}
for(int len=2;len<=n;len++)  //区间长度
for(int i=1;i<=n;i++)        //枚举起点
{
    int j=i+len-1;           //区间终点
    if(j>n) break;           //越界结束
    for(int k=i;k<j;k++)     //枚举分割点,构造状态转移方程
    {
        dp[i][j]=max(dp[i][j],dp[i][k]+dp[k+1][j]+w[i][j]);
    }
}

 代码

 

#include<bits/stdc++.h>
#define maxn 805
#define inf 0x3f3f3f3f
using namespace std;
int dp[maxn][maxn],a[maxn];
int main()
{
    memset(dp,inf,sizeof(dp));
	int n;
	cin>>n;
    for(int i=1;i<=n;i++)
    {
    	cin>>a[i];
        dp[i][i]=1;
    }
    for(int len=2;len<=n;len++)  //区间长度
		for(int i=1;i+len-1<=n;i++)        //枚举起点
		{
		    
			int j=i+len-1;           //区间终点
			if(i+1==j)                    
		    {
		        if(a[i]==a[j]) dp[i][j]=1;	 
		        else dp[i][j]=2; 					
		    }	
		    else
		    {
		    	if(a[i]==a[j]) dp[i][j]=dp[i+1][j-1];
			    for(int k=i;k<j;k++)     //枚举分割点,构造状态转移方程
					dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]);
			}
			
		}
	cout<<dp[1][n]<<endl;	
}

 另一版本:其实思路一模一样,只不过代码不同

#include<iostream>
#include<cstring>
#define maxn 805
#define inf 1e9
using namespace std;
int dp[maxn][maxn],a[maxn],n;
int dfs(int l,int r)
{
    if(dp[l][r]!=-1)return dp[l][r]; //若果这两点被消除了直接return 
    dp[l][r]=inf;                   //先赋值两者消除时间为inf 
    if(l>r)return dp[l][r]=0;       //异常处理 
    if(l==r)return dp[l][r]=1;		//如果要消除l和l那么时间肯定是1 
    if(l+1==r)                      //如果要消除l和l+1
    {
        if(a[l]==a[r])return dp[l][r]=1;			//如果l和r元素相同,那么只需要时间1 
        else return dp[l][r]=2; 					//元素不同需要时间2 
    }
    if(a[l]==a[r])                                  //如果区间的两个端点元素值相同,那他可能是回文 
        dp[l][r]=dfs(l+1,r-1);						//那么消除l和r的时间就是消除l+1和r-1的时间 (可能不是回文也没关系) 
    for(int i=l;i<=r;i++) 							//从l开始判断 
        dp[l][r]=min(dfs(l,i)+dfs(i+1,r),dp[l][r]); //l和r的相处时间肯定是等于(消除l和r的时间)与(消除l和i+消除i+1和r的时间和) 
    return dp[l][r];                                //返回结果 
}
int main()
{
    memset(dp,-1,sizeof(dp));
	cin>>n;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    cout<<dfs(1,n)<<endl;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Cherish_lii

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值