【Codeforces 645E】【贪心】Intellectual Inquiry

来源:互联网 发布:淘宝商品链接怎么做 编辑:程序博客网 时间:2024/05/19 03:17

Intellectual Inquiry

Time Limit: 2000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u

Description

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.

Samples

Input1

1 3
ac

Output1

8

Input2

0 2
aaba

Output2

10

Hint

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.

Source

CROC 2016 - Elimination Round

网上很多人说这个是dp,但我觉得这个只是很想dp,实际上还是贪心。
为什么这么说呢?因为我们每次去的时候是鼠目寸光的看见当前位置的last最小的来取,而没有一个从子问题里面选取最优的过程,所以我说是贪心

然后就是怎么贪心
我们设置一个last数组来记录上一次每个字幕出现的位置,然后因为这里的字串不是连续的,我们先建立一个f数组,表示当前的字母会增加多少个字串,g是f的前缀和
然后F[i]=g[i1]+g[last[i]1]就可以推出来了
公式可以简单证明,很有意思,可以自己去证

#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<set>#include<map>#include<queue>#include<algorithm>#include<vector>#include<cstdlib>#include<cmath>#include<ctime>#include<stack>#define INF 2100000000#define ll long long#define clr(x)  memset(x,0,sizeof(x))#define clrmax(x)  memset(x,127,sizeof(x))using namespace std;inline int read(){    char c;    int ret=0;    while(!(c>='0'&&c<='9'))        c=getchar();    while(c>='0'&&c<='9')    {        ret=(c-'0')+(ret<<1)+(ret<<3);        c=getchar();    }    return ret;}#define M 2000005#define P 1000000007ll f[M],g[M],last[30],n,m,k;char s[M];int get_min(){    int ret=1;    for(int i=2;i<=k;i++)        if(last[i]<last[ret])ret=i;    return ret;}int main(){    freopen("in.txt","r",stdin);    freopen("out.txt","w",stdout);    m=read();k=read();    scanf("%s",s);    n=strlen(s);    f[0]=g[0]=1;    for(int i=1;i<=n+m;i++)    {        int x=(i>n?get_min():s[i-1]-'a'+1);        int l=last[x];        last[x]=i;        f[i]=g[i-1]-(l==0?0:g[l-1])%P;        if(f[i]<0)f[i]+=P;        g[i]=f[i]+g[i-1];        g[i]%=P;    }    cout<<g[n+m];    return 0;}

大概就是这个样子,如果有什么问题,或错误,请在评论区提出,谢谢。

0 0
原创粉丝点击