PerfectPan
;

Codeforces 444C DZY Loves Colors

March 04, 2018

题目链接http://acm.hdu.edu.cn/showproblem.php?pid=2457

题意:给你若干个模式串,再给你一个文本串,问你最少替换掉多少个字符能使得文本串里不含这些模式串。

思路:对于给定的模式串建立好 Trie 图后,进行 dp。设 dp[i][j] 为走到 i 节点且已经走了 j 长度的文本串的时候与文本串前 j 个不同的有 x 个,则可以列出状态转移方程 dp[i][j]=dp[k][j-1]+v,其中 k 是可以走到 i 的节点,当 s[i] 与当前结点所代表的字符相同的时候 v 为 0 否则为 1。初始边界:dp[i][j]=-1 except dp[0][0]=0。

#pragma comment(linker, "/STACK:102400000,102400000")
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
#define Key_Value ch[ch[root][1]][0]
#define DBN1(a)           cerr<<#a<<"="<<(a)<<"\n"
#define DBN2(a,b)         cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<"\n"
#define DBN3(a,b,c)       cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<"\n"
#define DBN4(a,b,c,d)     cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<"\n"
#define DBN5(a,b,c,d,e)   cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<"\n"
#define DBN6(a,b,c,d,e,f) cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<", "<<#f<<"="<<(f)<<"\n"
#define clr(a,x) memset(a,x,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn=500000+5;
const int INF=0x3f3f3f3f;
const int P=1000000007;
const double PI=acos(-1.0);
template<typename T>
inline T read(T&x){
    x=0;int _f=0;char ch=getchar();
    while(ch<'0'||ch>'9')_f|=(ch=='-'),ch=getchar();
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return x=_f?-x:x;
}
inline int add(int a,int b){return (a+=b)>=P?a-P:a;}
inline int sub(int a,int b){return (a-=b)<0?a+P:a;}
inline int mul(int a,int b){return 1LL*a*b%P;}
int T,n,V,dp[5000],val[2100];
int main(){
    for (scanf("%d",&T);T--;){
       scanf("%d",&n);
       for (int i=1;i<n;i++) scanf("%d",val+i);
       memset(dp,-1,sizeof(dp));
       dp[0]=n*val[1];
       for (int i=2;i<n;i++) val[i]-=val[1];
       V=n-2;
       for (int i=2;i<n;i++){
            for (int j=i-1;j<=V;j++)if(dp[j-i+1]!=-1){
                if (dp[j]!=-1) dp[j]=max(dp[j],dp[j-i+1]+val[i]);
                else dp[j]=dp[j-i+1]+val[i];
            }
       }
       printf("%d\n",dp[V]);
    }
    return 0;
}

Yiming Pan

Written by Yiming Pan who lives and works in Hangzhou China. Welcome follow me on Github

Cannot load comments. Please check you network.