CROC 2016 - Elimination Round (Rated Unofficial Edition) E. Intellectual Inquiry 贪心 构造 dp
E. Intellectual Inquiry
题目连接:
http://www.codeforces.com/contest/655/problem/E
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.
Sample Input
1 3
ac
Sample Output
8
Hint
题意
现在给你n,k和长度为m的串
你需要构造一个长度为n+m的串,使得其中不同的子序列最多,输出数量。
题解:
构造题
假设我们构造出来了,我们怎么去统计呢?
dp[i]表示以i结尾的不同子序列数量,显然dp[i]=sigma(dp[j])+1,i!=j
观察可以知道其实dp[i]=前面所有不同子串数量-以i结尾的子串
统计知道了,我们怎么去构造呢?
贪心去构造就好了,我们可以简单的发现前面所有的不同的子串数量这个是相同的,我们取最少的以i结尾的那个字母就好了
这个显然就是最前面的那个字符,取最早出现的那个字符就好了。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e6+7;
const int mod = 1e9+7;
string s;
int n,k,m,last[1005],dp[1005];
int main()
{
cin>>n>>k>>s;m=s.size();
for(int i=0;i<s.size();i++)
{
int x = s[i];
for(int j='a';j<'a'+k;j++)
{
if(x==j)continue;
dp[x]=(dp[x]+dp[j])%mod;
}
dp[x]=(dp[x]+1)%mod;
last[x]=i+1;
}
for(int i=1;i<=n;i++)
{
int x = 'a';
for(int j='a';j<'a'+k;j++)
if(last[j]<last[x])x=j;
for(int j='a';j<'a'+k;j++)
{
if(j==x)continue;
dp[x]=(dp[x]+dp[j])%mod;
}
dp[x]=(dp[x]+1)%mod;
last[x]=i+m;
}
long long ans = 0;
for(int i='a';i<'a'+k;i++)ans=(ans+dp[i])%mod;
cout<<(ans+1)%mod<<endl;
}
CROC 2016 - Elimination Round (Rated Unofficial Edition) E. Intellectual Inquiry 贪心 构造 dp的更多相关文章
- CROC 2016 - Elimination Round (Rated Unofficial Edition) E - Intellectual Inquiry dp
E - Intellectual Inquiry 思路:我自己YY了一个算本质不同子序列的方法, 发现和网上都不一样. 我们从每个点出发向其后面第一个a, b, c, d ...连一条边,那么总的不同 ...
- CROC 2016 - Elimination Round (Rated Unofficial Edition) D. Robot Rapping Results Report 二分+拓扑排序
D. Robot Rapping Results Report 题目连接: http://www.codeforces.com/contest/655/problem/D Description Wh ...
- CROC 2016 - Elimination Round (Rated Unofficial Edition) D. Robot Rapping Results Report 拓扑排序+二分
题目链接: http://www.codeforces.com/contest/655/problem/D 题意: 题目是要求前k个场次就能确定唯一的拓扑序,求满足条件的最小k. 题解: 二分k的取值 ...
- CROC 2016 - Elimination Round (Rated Unofficial Edition) F - Cowslip Collections 数论 + 容斥
F - Cowslip Collections http://codeforces.com/blog/entry/43868 这个题解讲的很好... #include<bits/stdc++.h ...
- CROC 2016 - Elimination Round (Rated Unofficial Edition) C. Enduring Exodus 二分
C. Enduring Exodus 题目连接: http://www.codeforces.com/contest/655/problem/C Description In an attempt t ...
- CROC 2016 - Elimination Round (Rated Unofficial Edition) B. Mischievous Mess Makers 贪心
B. Mischievous Mess Makers 题目连接: http://www.codeforces.com/contest/655/problem/B Description It is a ...
- CROC 2016 - Elimination Round (Rated Unofficial Edition) A. Amity Assessment 水题
A. Amity Assessment 题目连接: http://www.codeforces.com/contest/655/problem/A Description Bessie the cow ...
- CF #CROC 2016 - Elimination Round D. Robot Rapping Results Report 二分+拓扑排序
题目链接:http://codeforces.com/contest/655/problem/D 大意是给若干对偏序,问最少需要前多少对关系,可以确定所有的大小关系. 解法是二分答案,利用拓扑排序看是 ...
- 8VC Venture Cup 2016 - Elimination Round D. Jerry's Protest 暴力
D. Jerry's Protest 题目连接: http://www.codeforces.com/contest/626/problem/D Description Andrew and Jerr ...
随机推荐
- Feather包实现数据框快速读写,你值得拥有
什么是Feather? Feature是一种文件格式,支持R语言和Python的交互式存储,速度更快.目前支持R语言的data.frame和Python pandas 的DataFrame. Feat ...
- OSCP考试回顾
技术背景 从2011年开始接触学习渗透测试,全职做过的职位有渗透测试,Python后端研发,DevOps,甲方安全打杂. 学习过程 首先考试报名,交钱.买了价值800美元的一个月lab,包含Pente ...
- 004_Gradle 笔记——Java构建入门
Gradle是一个通用的构建工具,通过它的构建脚本你可以构建任何你想要实现的东西,不过前提是你需要先写好构建脚本的代码.而大部分的项目,它 们的构建流程基本是一样的,我们不必为每一个工程都编写它的构建 ...
- 提高spring boot jpa性能(译)
Spring Data JPA为Spring应用程序提供了数据访问层的实现.这是一个非常方便的组件,因为它不会重新发明每个新应用程序的数据访问方式,因此您可以花更多时间来实现业务逻辑.使用Spring ...
- apache 软件历史版本查询
http://archive.apache.org/dist/ http://archive.apache.org/dist/struts/binaries/
- 认识hasLayout——IE浏览器css bug的一大罪恶根源 (转)
认识hasLayout--IE浏览器css bug的一大罪恶根源 转 什么是hasLayout?hasLayout是IE特有的一个属性.很多的ie下的css bug都与其息息相关.在ie中,一个元素要 ...
- MVC – 14.ajax异步请求
14.1.配置文件 14.2.AjaxHelper – 异步链接按钮 14.3.AjaxHelper – 异步表单 AjaxOptions常见属性: 14.4.AjaxOptions对象生成[对应]触 ...
- MYSQL-----IFNULL()函数的用法
Mysql查询中可以使用IFNULL()函数,IFNULL函数中有两个参数,它的作用是测试如果ifnull第一个参数不为null,就会显示出查询的结果,如果第一个参数为null,则会返回第二个参数的字 ...
- beego离线安装及运行
官网: https://beego.me/ 由于公司上不了网,啥都得下载到本地来弄. go的安装不多说了,GOPATH要设置好的. 先离线下载好https://github.com/astaxie/b ...
- 一个简单的AboutMe页面
Web2.0程序设计的小练习. Firefox下的效果 Chrome下的效果,套上了Helvetica,不知道为什么FF没有 (其实好像应该再优先加个Helvetica Neue的……呃……) 代码和 ...