Codeforces Round #107 (Div. 1) D. Mission Impassable

D. Mission Impassable
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Market stalls now have the long-awaited game The Colder Scrools V: Nvodsk. The game turned out to be difficult as hell and most students can't complete the last quest ("We don't go to Nvodsk..."). That threatened winter exams. The rector already started to wonder whether he should postpone the winter exams till April (in fact, he wanted to complete the quest himself). But all of a sudden a stranger appeared at the door of his office. "Good afternoon. My name is Chuck and I solve any problems" — he said.

And here they are sitting side by side but still they can't complete the mission. The thing is, to kill the final boss one should prove one's perfect skills in the art of managing letters. One should be a real magician to do that. And can you imagine what happens when magicians start competing...

But let's put it more formally: you are given a string and a set of integers ai. You are allowed to choose any substring that is a palindrome and delete it. At that we receive some number of points equal to ak, where k is the length of the deleted palindrome. For some kak = -1, which means that deleting palindrome strings of such length is forbidden. After a substring is deleted, the remaining part "shifts together", that is, at no moment of time the string has gaps. The process is repeated while the string has at least one palindrome substring that can be deleted. All gained points are summed up.

Determine what maximum number of points can be earned.

"Oh" — said Chuck, raising from the chair, — "I used to love deleting palindromes, just like you, but one day I took an arrow in the Knee".

Input

The first line contains an integer l (1 ≤ l ≤ 150) — the length of the string.

The second line contains exactly l integers ak ( - 1 ≤ ak ≤ 105) — the points a player gains for deleting.

The third line contains exactly l lowercase Latin letters — the original string from which a player can delete palindromes. The line contains no other characters apart from the newline character at the end of the string.

Output

Print a single number — the maximum number of points one can gain if he plays on the given string.

Sample test(s)
input
7
-1 -1 -1 -1 -1 -1 -1
abacaba
output
0
input
7
1 -1 -1 -1 -1 -1 -1
abacaba
output
7
input
7
1 5 -1 -1 -1 -1 10
abacaba
output
16
Note

In the first sample we cannot delete any substring, so the best result is 0. In the second sample we are allowed to delete only those palindromes whose length equals 1, thus, if we delete the whole string, we get 7 points. In the third sample the optimal strategy is: first we delete character c, then string aa, then bb, and the last one aa. At that we get 1 + 3 * 5 = 16 points.

题意:对于一个字符串,你可以如果它有一个长度为len 的子串,如果 a[len] != -1 那么,你就可以选择删除它,然后得到a[len]分数

,然后串会合并成新串....你又可以重复上面做法,直到不能做

思路:dp[x][y][z] 表示,对于子串 str[x~y] 删除 z 个字符以后可以得到的最大价值,

注意边界条件就好了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<map>
#include<ctime>
#include<bitset>
#define LL long long
#define ll long long
#define INF 4365899
#define maxn 152
#define eps 1e-6
#define mod 1000000007
using namespace std;

int a[maxn],dp[maxn][maxn][maxn] ;
char str[maxn] ;
int dfs(int x,int y,int z)
{
    if(dp[x][y][z] != -1) return dp[x][y][z] ;
    if(x==y&&z==1)
    {
        dp[x][y][z]=0 ;
        return 0 ;
    }
    if(x==y+1 && z==0){
        dp[x][y][z]=0;
        return dp[x][y][z];
    }
    dp[x][y][z]=-INF;
    if(z<0||z>y-x+1) return -INF ;
    if(z==0)
    {
        int Max=-INF;
        for(int i = 1 ; i <= y-x+1;i++)if(a[i]!=-1)
        {
            Max=max(Max,dfs(x,y,i)+a[i]) ;
        }
        dp[x][y][z]=Max;
        return Max;
    }
    int d;
    if(str[x]==str[y])
    {
        d = dfs(x+1,y-1,z-2);
        dp[x][y][z]=max(d,dp[x][y][z]) ;
    }
    for(int i = x ; i< y ;i++)
    {
        d = dfs(x,i,z)+dfs(i+1,y,0) ;
        dp[x][y][z]=max(d,dp[x][y][z]) ;
        d = dfs(x,i,0)+dfs(i+1,y,z) ;
        dp[x][y][z]=max(d,dp[x][y][z]) ;
    }
    return dp[x][y][z] ;
}
int ans[maxn] ;
int main()
{
    int i,j,L,R,cnt;
    int n ,m , k ;
    while(scanf("%d",&n ) != EOF )
    {
       for( i = 1 ; i <= n ;i++)
         scanf("%d",&a[i]) ;
       memset(dp,-1,sizeof(dp)) ;
       ans[0]=0;
       scanf("%s",str+1) ;
       for( i = 1 ; i <= n ;i++)
       {
           ans[i]=ans[i-1];
           for( j= 1;j<=i;j++)
            ans[i]=max(ans[i],ans[j-1]+dfs(j,i,0));
       }
       cout<<ans[n]<<endl;
    }
    return 0 ;

}
View Code
原文地址:https://www.cnblogs.com/20120125llcai/p/4060614.html