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

来源:互联网 发布:js随时检测窗口大小 编辑:程序博客网 时间:2024/05/05 20:50

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;}
0 0
原创粉丝点击