Codeforces 645E. Intellectual Inquiry



题意:给定一串字符,由前k个小写拉丁字母组成,要求在该字符串后面补上n个字符(也从前k个小写拉丁字母里面选),使得最后得到的字符串含有本质不同的子序列的数量最大。

思路:要解决这个问题,首先要解决如何求字符串本质不同的子序列的个数的问题。定义dp[i][j]表示前i个字符内,以字符j结尾的本质不同的子序列的个数。那么可推得转移式:s[i]==j时,dp[i][j] = \(\sum_{t=1}^{k}dp[i-1][t]\) +1 (s[i]可自成一个子序列,故+1);s[i]!=j时,则dp[i][j]=dp[i-1][j].则最后答案为 \(\sum_{t=1}^{k}dp[len][t]\) 。接下来解决补n个字符的问题,观察dp转移式可以发现,每次会把 \(\sum_{t=1}^{k}dp[i-1][t]\) +1 赋给dp[i][s[i]],其余的则直接赋dp[i-1]对应的值,保持不变。因此要想使最后的答案最大,每次应该给最小的dp[i][j]赋值,因此后面的n个字符,每次都要填当前dp值最小的那个字符,而观察可知那个字符一定是最后出现位置最靠前的那个,因为dp值是递增的,越迟出现值一定越大。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<map>
#include<queue>
#include<string>
#include<vector>
#include<cmath>
#include<climits>
#include<functional>
#include<set>
#define dd(x) cout<<#x<<" = "<<x<<" "
#define de(x) cout<<#x<<" = "<<x<<endl
#define fi first
#define se second
#define mp make_pair
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef vector<int> V;
typedef map<int,int> M;
typedef queue<int> Q;
typedef priority_queue<int> BQ;
typedef priority_queue<int,vector<int>,greater<int> > SQ;
const int maxn=1e6+10,INF=0x3f3f3f3f,mod=1e9+7;
int dp[30],last[30];
char s[maxn<<1];
inline int add(int a,int b)
{
a+=b;
if (a>=mod)
a-=mod;
return a;
}
int sum(int k)
{
int res=0;
for (int i=0;i<k;++i)
res=add(res,dp[i]);
return res;
}
int main()
{
int n,k;
scanf("%d%d%s",&n,&k,s+1);
int m=strlen(s+1);
for (int i=1;i<=m;++i)
{
int c=s[i]-'a';
dp[c]=sum(k)+1;
last[c]=i;
}
for (int i=m+1;i<=m+n;++i)
{
int c=min_element(last,last+k)-last;
dp[c]=sum(k)+1;
last[c]=i;
}
printf("%d",add(sum(k),1));
return 0;
}

最新文章

  1. MySQL设置字段的默认值为当前系统时间
  2. redis事务详解
  3. CSS3绘制六边形
  4. Windows8远程桌面CentOS 6.5
  5. !!!!!122. Best Time to Buy and Sell Stock II
  6. BZOJ 1009 GT考试(ac自动机+矩阵DP)
  7. Emgu学习笔记(一)安装及运行Sample
  8. poj2459 Treasure Exploration (闭包+二分)
  9. Codeforces 432 D. Prefixes and Suffixes
  10. PADS 导Gerber文件
  11. Android如果有一个任意写入的漏洞,如何将写权限转成执行权限
  12. Karen and Coffee CodeForces - 816B (差分数组+预处理前缀和)
  13. memory.h
  14. Linux驱动之定时器在按键去抖中的应用
  15. Python3 tkinter基础 Entry get 点击按钮 将输入框中文字输出到控制台
  16. EJB开发第一期---EJB开发配置
  17. bzoj 1269 bzoj 1507 Splay处理文本信息
  18. SpringBoot与消息(RabbitMQ)
  19. C++的全部目标就是最优化资源的利用,以人付出更多为代价。Python刚好是另一个极端(Bjarne就说,一个人至少应该掌握两种计算机语言)
  20. C#之鼠标模拟技术

热门文章

  1. Ocelot + Consul的demo
  2. eventFlow 系列 &lt;一&gt; 入门
  3. VBA学习资料分享-2
  4. hexo发布后样式丢失
  5. linux文件目录详细介绍
  6. MSP432 BSL流程(UART)
  7. lua table vs closure
  8. 原创js脚本实现百度网盘任意文件强制下载
  9. Golang之初探
  10. TCP-HTTP ___UDP 应用场景