CodeForces 73C LionAge II Dp

本文介绍了一种算法,通过更改角色名称中最多k个字母来最大化其音韵美感。该算法考虑了不同字母组合所带来的加分效果,并通过动态规划求解最大音韵值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

Vasya plays the LionAge II. He was bored of playing with a stupid computer, so he installed this popular MMORPG, to fight with his friends. Vasya came up with the name of his character — non-empty string s, consisting of a lowercase Latin letters. However, in order not to put up a front of friends, Vasya has decided to change no more than k letters of the character name so that the new name sounded as good as possible. Euphony of the line is defined as follows: for each pair of adjacent letters x and y (x immediately precedes y) the bonusc(x, y) is added to the result. Your task is to determine what the greatest Euphony can be obtained by changing at most k letters in the name of the Vasya's character.

Input

The first line contains character's name s and an integer number k (0 ≤ k ≤ 100). The length of the nonempty string s does not exceed100. The second line contains an integer number n (0 ≤ n ≤ 676) — amount of pairs of letters, giving bonus to the euphony. The next nlines contain description of these pairs «x y c», which means that sequence xy gives bonus c (x, y — lowercase Latin letters,  - 1000 ≤ c ≤ 1000). It is guaranteed that no pair x y mentioned twice in the input data.

Output

Output the only number — maximum possible euphony оf the new character's name.

Sample Input

Input
winner 4
4
s e 7
o s 8
l o 13
o o 8
Output
36
Input
abcdef 1
5
a b -10
b c 5
c d 5
d e 5
e f 5
Output
20

Hint

In the first example the most euphony name will be looser. It is easy to calculate that its euphony is 36.


-----------------------

注意初始化的问题。

----------------------

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;

const int OO=1e9;

int f[111][1111][33];
int a[33][33];
char s[1111];
bool v[111][1111][33];
int k;

int main()
{
    int n;
    char x,y;
    int c;
    memset(a,0,sizeof(a));
    memset(f,0,sizeof(f));
    memset(v,0,sizeof(v));
    cin>>(s+1)>>k;
    cin>>n;
    for (int i=1; i<=n; i++)
    {
        cin>>x>>y>>c;
        a[x-'a'][y-'a']=c;
    }
    int len=strlen(s+1);

    for (int i=0;i<=len;i++)
    {
        for (int j=0;j<26;j++)
        {
            for (int l=0;l<=k;l++)
            {
                f[l][i][j]=-OO;
            }
        }
    }

    for (int i=0;i<26;i++)
    {
        if (s[1]-'a'==i)
        {
            f[0][1][i]=0;
        }
        else
        {
            f[1][1][i]=0;
        }
    }

    for (int i=2; i<=len; i++)
    {
        for (int j=0; j<26; j++)
        {
            if (j==s[i]-'a')
            {
                for (int l=0; l<=k; l++)
                    for (int t=0; t<26; t++)
                    {
                        f[l][i][j]=max( f[l][i][j], f[l][i-1][t]+a[t][j] );
                    }

            }
            else
            {
                for (int l=1; l<=k; l++)
                    for (int t=0; t<26; t++)
                    {
                        f[l][i][j]=max( f[l][i][j], f[l-1][i-1][t]+a[t][j] );
                    }
            }
        }
    }

    int ans=-OO;
    for (int l=0; l<=k; l++)
    {
        for (int j=0; j<26; j++)
        {
            if (f[l][len][j]>ans) ans=f[l][len][j];
        }
    }
    cout<<ans<<endl;

    return 0;
}



引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.youkuaiyun.com/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值