CROC 2016 - Elimination Round (Rated Unofficial Edition) E - Intellectual Inquiry dp
思路:我自己YY了一个算本质不同子序列的方法, 发现和网上都不一样。
我们从每个点出发向其后面第一个a, b, c, d ...连一条边,那么总的不同子序列就是从0号点出发能走出多少条
不同点的路径。 dp[ i ]表示是到 i 这个点的不同路径数, 构成的图显然是个DAG,把这个dp出来就好啦。最后补
n个就是贪贪心。
网上的另外两种方法。
dp[ i ] 表示[1, i] 的字符串有多少不同子序列。
dp[ i ] = dp[i - 1] * 2 - dp[ pre[s[ i ] ] - 1]
dp[ i ][ j ]表示[1, i] 的字符串末尾为 j 的不同子序列有多少种。
如果s[ i ] != j dp[ i ][ j ] = dp[ i - 1] [ j ]
否则 dp[ i ] [ j ] = (dp[ i - 1][ 0 ] + dp[ i - 1][ 1 ].... + dp[ i - 1][ k - 1]) + 1
#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PII pair<int, int>
#define PLI pair<LL, int>
#define ull unsigned long long
using namespace std; const int N = 2e6 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-; int n, m, k, pr[];
char s[N]; inline void add(int &a, int b) {
a += b; if(a >= mod) a -= mod;
} struct Bit {
int a[N];
void modify(int x, int v) {
for(int i = x; i < N; i+=i&-i)
add(a[i], v);
}
int sum(int x) {
int ans = ;
for(int i = x; i; i-=i&-i)
add(ans, a[i]);
return ans;
}
int query(int l, int r) {
if(!l) return (sum(r)+mod+)%mod;
return (sum(r)-sum(l-)+mod)%mod;
}
} bit; void extend(int x, int c) {
int pos = s[x-]-'a' == c ? x- : pr[c];
int val = bit.query(pos, x-);
bit.modify(x, val);
if(x > ) pr[s[x-]-'a'] = x-;
} int main() {
scanf("%d%d", &n, &k);
scanf("%s", s + );
m = strlen(s + );
for(int i = ; i <= m; i++)
extend(i, s[i]-'a');
for(int i = m+; i <= m+n; i++) {
int z = -;
for(int j = ; j < k; j++) {
if(j != s[i-] - 'a') {
if(z == - || pr[z] > pr[j]) z = j;
}
}
if(z == -) z = ;
s[i] = 'a' + z;
extend(i, z);
}
printf("%d\n", bit.query(, n + m));
return ;
} /*
*/
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 题目连接: http://www.codeforces.com/contest/655/problem/E Description After gett ...
- 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 ...
随机推荐
- 在VMware安装Ubuntu后一直停留在VMware Easy Install
在VMware安装Ubuntu完成后,一直停留在VMware Easy Install,可以登录但是没有窗口界面,如图: 在此登录后依次运行以下命令: sudo mv /etc/issue.backu ...
- 数据结构编程实验——chapter8-采用树结构的非线性表编程
关于树结构的非线性表编程在数据结构中可以说占据了半壁江山,其中涉及的知识点繁杂,但也是数据结构体现运算优化的核心所在,下面我们将较为初步且系统得讨论数据结构中一系列有关树的表示. 首先我们再次明确树的 ...
- 关于mysql 5.7版本“报[Err] 1093 - You can't specify target table 'XXX' for update in FROM clause”错误的bug
不同于oracle和sqlserver,mysql并不支持在更新某个表的数据时又查询了它,而查询的数据又做了更新的条件,因此我们需要使用如下的语句绕过: , notice_code ) a) ; 本地 ...
- nodejs出现events.js:72中抛出错误 Error: listen EADDRINUSE
<pre>events.js:72 throw er; // Unhandled 'error' event ^ Error: listen EADDRINUSE at errnoExce ...
- .NET 下第一次接触Redis数据库
关于Redis 1.简介 Redis是著名的NOSQL(Not Only SQL)数据库,是键值对结构.(我只用过键值对结构的) 他为存储键值对做了优化,在大型网站中应用广泛.Redis提供了数据的自 ...
- Codeforces Round #540 (Div. 3)题解
题目链接: https://codeforces.com/contest/1118 A题: 题意: q次查询,给你一个n,要你用1和2来凑出n,1的花费为a,2的花费为b,求花费的最小值. 思路: 我 ...
- python初步学习-python 模块之 sys(持续补充)
sys sys 模块包括了一组非常实用的服务,内含很多函数方法和变量 sys 模块重要函数变量 sys.stdin 标准输出流 sys.stdout 标准输出流 sys.stderr 标准错误流 sy ...
- PHP内存溢出 Allowed memory size of 解决办法
PHP出现如下错误:Allowed memory size of xxx bytes exhausted at xxx:xxx (tried to allocate xxx bytes) 关于 ...
- flask基础之蓝图的使用(七)
前言 关于蓝图是什么?或为什么使用蓝图的详细介绍,官方文档讲的很详细,不再赘述.简单来说,在大型的应用中,我们不想视图函数显得杂乱无章,难以维护,将众多的视图函数按照Api的设计规则进行切割是一个好方 ...
- gpio口、内核定时器使用
/*申请gpio*/ int gpio_request(unsigned gpio, const char *label); /*设置gpio为输入状态,即设置如(GPH0CON)*/ int gpi ...