E - Intellectual Inquiry

思路:我自己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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. CROC 2016 - Elimination Round (Rated Unofficial Edition) D. Robot Rapping Results Report 拓扑排序+二分

    题目链接: http://www.codeforces.com/contest/655/problem/D 题意: 题目是要求前k个场次就能确定唯一的拓扑序,求满足条件的最小k. 题解: 二分k的取值 ...

  4. CROC 2016 - Elimination Round (Rated Unofficial Edition) F - Cowslip Collections 数论 + 容斥

    F - Cowslip Collections http://codeforces.com/blog/entry/43868 这个题解讲的很好... #include<bits/stdc++.h ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. CF #CROC 2016 - Elimination Round D. Robot Rapping Results Report 二分+拓扑排序

    题目链接:http://codeforces.com/contest/655/problem/D 大意是给若干对偏序,问最少需要前多少对关系,可以确定所有的大小关系. 解法是二分答案,利用拓扑排序看是 ...

  9. 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 ...

随机推荐

  1. KVM基本实现原理

    KVM 虚拟化技术概述 http://blog.csdn.net/yearn520/article/details/6461047 KVM 虚拟化技术在 AMD 平台上的实现 1.http://www ...

  2. clock()、time()、clock_gettime()和gettimeofday()函数的用法和区别

    1. clock_gettime( ) 提供了纳秒的精确度 int clock_gettime(clockid_t clk_id, struct timespect *tp); clockid_t c ...

  3. 跨域问题 Uncaught DOMException: Blocked a frame with origin。。。

    第三方系统内嵌 到iframe中的 跨域问题. 解决方案: http://www.ruanyifeng.com/blog/2016/04/same-origin-policy.html

  4. 使用spring boot访问mongodb数据库

    一. spring boot中传参的方法 1.自动化配置 spring Boot 对于开发人员最大的好处在于可以对 Spring 应用进行自动配置.Spring Boot 会根据应用中声明的第三方依赖 ...

  5. [转载]PayPal为什么从Java迁移到Node.js,性能提高一倍,文件代码减少44%

    http://ourjs.com/detail/52a914f0127c763203000008 大家都知道PayPal是另一家迁移到Node.js平台的大型公司,Jeff Harrell的这篇博文 ...

  6. 基本控件文档-UITextField属性

    CHENYILONG Blog 基本控件文档-UITextField属性 Fullscreen   UITextField属性技术博客http://www.cnblogs.com/ChenYilong ...

  7. WeX5入门之HelloWorld

    学习目标:数据双向绑定 在ui2上右键 新建一个应用 然后会出现一个目录 右键hello 在创建页面 选择标准的空白模板 并起一个名 自动生成这两个文件 建立一个input组件 再建一个output组 ...

  8. css 实现圆形头像

    1.方法一 直接设置img为圆形,这种情况下如果图片不是正方形,图片会被拉伸 <img class="circleImg" src="../img/photo/im ...

  9. Sublime2编译Python程序EOFError:EOF when reading a line解决方法【转】

    在Sublime2中编译运行Python文件时,如果代码中包含用户输入的函数时(eg. raw_input()),Ctrl+b编译运行之后会提示以下错误: 解决方法:安装SublimeREPL打开Su ...

  10. leetcode刷刷刷

    1.链表节点的插入排序(写了个插入排序,但是报段错误,自己编译器里能运行) #include <iostream> #include <stdlib.h> #include & ...