【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
acOutput1
8
Input2
0 2
aabaOutput2
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的前缀和
然后
公式可以简单证明,很有意思,可以自己去证
#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;}
大概就是这个样子,如果有什么问题,或错误,请在评论区提出,谢谢。
- Codeforces 645E Intellectual Inquiry (贪心+DP)
- Codeforces 645E Intellectual Inquiry [贪心] [DP]
- 【Codeforces 645E】【贪心】Intellectual Inquiry
- CodeForces 645E Intellectual Inquiry(构造+贪心+dp)
- CodeForces 645 E.Intellectual Inquiry(dp+贪心)
- Codeforces 645E Intellectual Inquiry (dp不同子序列计数)
- CF645E-Intellectual Inquiry 贪心
- CROC 2016 - Elimination Round (Rated Unofficial Edition) E. Intellectual Inquiry 贪心 构造 dp
- Codeforces645E Intellectual Inquiry
- codeforces 732E (贪心)
- Codeforces 777E 贪心
- codeforces 777E 贪心
- 【贪心】Codeforces 353E Antichain
- Codeforces 611E 贪心 STL
- Codeforces 732E Sockets【贪心】
- Mishap in Club--codeForces 245E--贪心
- Codeforces 432E Square Tiling(构造+贪心)
- codeforces 496e Distributing Parts 贪心
- Redis(3.2.3)集群部署实战
- 13.Java的Volatile关键字
- GIMP 恢复默认界面
- JAVA与SQL 中的null与NULL
- activiti5.20简单介绍(十) -- spring和业务关联
- 【Codeforces 645E】【贪心】Intellectual Inquiry
- 页面置换算法
- YAML 语言教程
- lua4.0数值定义
- android studio eclipse转as
- stage3d 限制
- android2
- 详细图解如何注册 Navicat for Mysql 11.0.17 企业版(Win7 64bit)
- MySQL C API programming tutorial