Codeforces 645E Intellectual Inquiry [贪心] [DP]

22 篇文章 0 订阅
16 篇文章 0 订阅

E. Intellectual Inquiry
time limit per test 2 seconds
memory limit per test 256 megabytes
input standard input
output standard output

After getting kicked out of her reporting job for not knowing the alphabet, Bessie has decided to attend school at the Fillet and Eggs Eater Academy. She has been making good progress with her studies and now knows the first k English letters.

Each morning, Bessie travels to school along a sidewalk consisting of m + n tiles. In order to help Bessie review, Mr. Moozing has labeled each of the first m sidewalk tiles with one of the first k lowercase English letters, spelling out a string t. Mr. Moozing, impressed by Bessie’s extensive knowledge of farm animals, plans to let her finish labeling the last n tiles of the sidewalk by herself.

Consider the resulting string s (|s| = m + n) consisting of letters labeled on tiles in order from home to school. For any sequence of indices p1 < p2 < … < pq we can define subsequence of the string s as string sp1sp2… spq. Two subsequences are considered to be distinct if they differ as strings. Bessie wants to label the remaining part of the sidewalk such that the number of distinct subsequences of tiles is maximum possible. However, since Bessie hasn’t even finished learning the alphabet, she needs your help!

Note that empty subsequence also counts.

Input
The first line of the input contains two integers n and k (0 ≤ n ≤ 1 000 000, 1 ≤ k ≤ 26).

The second line contains a string t (|t| = m, 1 ≤ m ≤ 1 000 000) consisting of only first k lowercase English letters.

Output
Determine the maximum number of distinct subsequences Bessie can form after labeling the last n sidewalk tiles each with one of the first k lowercase English letters. Since this number can be rather large, you should print it modulo 109 + 7.

Please note, that you are not asked to maximize the remainder modulo 109 + 7! The goal is to maximize the initial value and then print the remainder.

Examples
input
1 3
ac
output
8
input
0 2
aaba
output
10

Note
In the first sample, the optimal labeling gives 8 different subsequences: “” (the empty string), “a”, “c”, “b”, “ac”, “ab”, “cb”, and “acb”.

这里写图片描述
In the second sample, the entire sidewalk is already labeled. The are 10 possible different subsequences: “” (the empty string), “a”, “b”, “aa”, “ab”, “ba”, “aaa”, “aab”, “aba”, and “aaba”. Note that some strings, including “aa”, can be obtained with multiple sequences of tiles, but are only counted once.


给定一个长度为n 的字符串,你可以在字符串后再添加m 个字符,使得新字符串所包含的不同的子序列数量尽量多,求最多的不同子序列数量。
考虑当前的单个字符,该字符如果不算重复的子序列使当前可能的方案数*2,重复的就是上一个相同的字符所造成的,所以应该减掉。
为了使方案数最大,那么应该找重复的最小的dp值,然而sp取模,又dp具有单调性,所以直接转化成最小的下标即可。
注意重复的应该是dp(pos-1)的数量,因为当前这一位可以被使用多次作为当前dp(i)的一种方案。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<iomanip>
#include<ctime>
#include<climits>
#include<cctype>
#include<algorithm>
#define AUTO "%I64d"
using namespace std;
#define smax(x,tmp) x=max((x),(tmp))
#define smin(x,tmp) x=min((x),(tmp))
#define maxx(x1,x2,x3) max(max(x1,x2),x3)
#define minn(x1,x2,x3) min(min(x1,x2),x3)
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int mod = 1000000007;
const int SIGMA_SIZE = 30;
const int maxn = 2000005;
char s[maxn];
int dp[maxn];
int last[SIGMA_SIZE];
int n,k;
int main()
{
#ifndef ONLINE_JUDGE
    freopen("intel.in","r",stdin);
    freopen("intel.out","w",stdout);
#endif
    scanf("%d%d",&n,&k);
    scanf("%s",s+1);
    int lens = strlen(s+1);
    dp[0] = 1;
    for(int i=1;i<=lens;i++)
    {
        dp[i] = dp[i-1]<<1; dp[i] %= mod;
        if(last[s[i]-'a'+1]) dp[i]-=dp[last[s[i]-'a'+1]-1];
        dp[i] %= mod;
        last[s[i]-'a'+1] = i;
    }
    n += lens;
    for(int i=lens+1;i<=n;i++)
    {
        dp[i] = dp[i-1]<<1; dp[i] %= mod;
        int pos = INF , id;
        for(int p=1;p<=k;p++)
            if(pos > last[p]) pos = last[p] , id = p;
        if(pos) dp[i] -= dp[pos-1];
        dp[i] %= mod;
        last[id] = i;
    }
    printf("%d",(dp[n]%mod+mod)%mod);
    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.csdn.net/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、付费专栏及课程。

余额充值