一个字符串,由前k个字母组成,长度为m + n,其中前m个字符已经确定,后面n个由你自由选择,

使得这个串的不同的子序列的个数最多,空串也算一个子序列。

1 <= m <= 10^6,0 <= n <= 10^6,1 <= k <= 26

首先,我们考虑n = 0的情况,

问题就为给定一个字符串,求它有多少个不同的子序列。

pre[i]表示字母i最后出现的位置,初始化为0

f[i]表示以第i个字符结尾的与前面已经出现的子序列都不同的子序列个数

g[i] = ∑0<=j<=if[j]

则我们知道f[i] = ∑pre[i]<=j<=i-1f[j]

则 if pre[i] == 0  then f[i] = g[i-1]

if pre[i] > 0    then f[i] = g[i-1] - g[pre[str[i]]-1]

答案就是g[m]

那如果n > 0 呢?

从f的递推式我们知道,要使得f[i]最大,i处填的字符应该是pre值最小的i

那么我们接着遍历,每次拿pre最小的字符,更新f和pre值

最终答案就是g[n + m]

代码:

  //File Name: cf645E.cpp
//Created Time: 2017年01月05日 星期四 13时53分24秒 #include <bits/stdc++.h>
#define LL long long
#define fir first
#define sec second
#define pii pair<int,int>
using namespace std;
const int MAXN = + ;
const int P = (int)1e9 + ;
LL f[MAXN],g[MAXN];
char str[MAXN];
int pre[];
set<pii> rem;
LL solve(int n,int k){
int m = strlen(str + );
memset(pre,,sizeof(pre));
f[] = g[] = ;
for(int i=;i<=m;++i){
int v = str[i] - 'a';
if(pre[v] == )
f[i] = g[i - ];
else
f[i] = (g[i - ] - g[pre[v] - ] + P) % P;
g[i] = (g[i - ] + f[i]) % P;
pre[v] = i;
// printf("i = %d f = %lld g = %lld\n",i,f[i],g[i]);
}
rem.clear();
for(int i=;i<k;++i)
rem.insert(pii(pre[i],i));
for(int i=m+;i<=m+n;++i){
// puts("ffff");
pii now = *rem.begin();
int pos = now.fir,v = now.sec;
if(pos == )
f[i] = g[i - ];
else
f[i] = (g[i - ] - g[pos - ] + P) % P;
g[i] = (g[i - ] + f[i]) % P;
pre[v] = i;
rem.erase(rem.begin());
rem.insert(pii(i,v));
}
// for(int i=0;i<=m+n;++i)
// printf("i = %d f = %lld\n",i,f[i]);
return g[m + n];
}
int main(){
int n,k;
scanf("%d %d",&n,&k);
scanf("%s",str + );
printf("%lld\n",solve(n,k));
return ;
}

codeforces 645 E. Intellectual Inquiry的更多相关文章

  1. Codeforces 645E. Intellectual Inquiry(DP,贪心)

    Codeforces 645E. Intellectual Inquiry 题意:给定一串字符,由前k个小写拉丁字母组成,要求在该字符串后面补上n个字符(也从前k个小写拉丁字母里面选),使得最后得到的 ...

  2. CROC 2016 - Elimination Round (Rated Unofficial Edition) E. Intellectual Inquiry 贪心 构造 dp

    E. Intellectual Inquiry 题目连接: http://www.codeforces.com/contest/655/problem/E Description After gett ...

  3. CROC 2016 - Elimination Round (Rated Unofficial Edition) E - Intellectual Inquiry dp

    E - Intellectual Inquiry 思路:我自己YY了一个算本质不同子序列的方法, 发现和网上都不一样. 我们从每个点出发向其后面第一个a, b, c, d ...连一条边,那么总的不同 ...

  4. codeforces 645E . Intellectual Inquiry

    题目链接 如果不考虑重复的元素, 那么我们可以很容易的发现, 长度为n的字符串它的子串数量是 $ 2^n $ . 我们设每个到位置i, 答案的数量为f[i]. 然后我们考虑重复的, 我们发现, 每加入 ...

  5. CF CROC 2016 Intellectual Inquiry

    题目链接:http://codeforces.com/contest/655/problem/E 大意是Bessie只会英文字母表中的前k种字母,现在有一个长度为m+n的字母序列,Bessie已经知道 ...

  6. codeforces 645 D. Robot Rapping Results Report 二分+拓扑排序

    题目链接 我们可以发现, 这是一个很明显的二分+拓扑排序.... 如何判断根据当前的点, 是否能构造出来一个唯一的拓扑序列呢. 如果有的点没有出现, 那么一定不满足. 如果在加进队列的时候, 同时加了 ...

  7. CodeForces - 645 C.Enduring Exodus

    快乐二分 用前缀和随便搞一下 #include <cstdio> using namespace std; ; int p[N]; ; inline int msum(int a, int ...

  8. 「CF645E」 Intellectual Inquiry

    题目链接 CF645E 题意 有一个长为\(n\)的由小写字母组成的字符串,需要用小写字母再填\(m\)位,使最后的字符串中本质不同的子串数量尽量多,答案对\(10^9+7\)取模. 本题数据:\(n ...

  9. 05.24 ICPC 2019-2020 North-Western Russia Regional Contest复现赛+Codeforces Round #645 (Div. 2)

    A.Accurate Movement(复现赛) 题意:两个木块最左边都在0的位置,最右边分别为a,b(b>a),并且短的木条只能在长木条内移动,问两个木条需要移动多少次才能使两个木条的右端都在 ...

随机推荐

  1. POJ 2318

    题目来源:http://poj.org/problem?id=2318 题目内容:给定一个矩形盒子(左上和右下端点的坐标),再给定n条线段,将盒子分为n+1份,之后给定m个点的坐标,对于盒子的每一段, ...

  2. 分布式系统(Distributed System)资料

    这个资料关于分布式系统资料,作者写的太好了.拿过来以备用 网址:https://github.com/ty4z2008/Qix/blob/master/ds.md 希望转载的朋友,你可以不用联系我.但 ...

  3. C++模板元编程(C++ template metaprogramming)

    实验平台:Win7,VS2013 Community,GCC 4.8.3(在线版) 所谓元编程就是编写直接生成或操纵程序的程序,C++ 模板给 C++ 语言提供了元编程的能力,模板使 C++ 编程变得 ...

  4. django中间件

    django的中间件就是一个对整体请求过程的装饰器,可以对请求到达view之前,view处理和响应后,通过定义process_request,process_view,process_response ...

  5. Can only set Cookies for the current domain

    # -*- coding: utf-8 -*- """ Created on Mon Dec 12 14:35:49 2016 @author: yaru "& ...

  6. This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes.

    -- :::] This application is modifying the autolayout engine from a background thread, which can lead ...

  7. 数据库SQL优化总结

    1.对查询进行优化,要尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...

  8. 实现AJAX的异步交互的步骤

    <input type="button" value="异步请求"id="btn"> <script> 实现ajax ...

  9. android 底层开发入门(一)

    第一个Linux驱动程序:统计单词个数 一.首先了解一下: 打印机驱动写入数据:对于打印机驱动来说,需要接收这些被写入的数据,并将它们通过PC的并口.USB等端口发送给打印机.要实现这一过程就需要Li ...

  10. Express4.x常用API(二):Request

    这是第二篇了,打算每天都拿些时间去详细的看看文档来自学下express吧,接下来就开始了,这次依然是有选择性的找API根据自己的理解翻译过来,方便日后开发时候的自己查阅,由于水平有限,大概也会在使用过 ...