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. Redis学习三:Redis数据类型

    一.Redis的五大数据类型 1.String(字符串) string是redis最基本的类型,你可以理解成与Memcached一模一样的类型,一个key对应一个value.string类型是二进制安 ...

  2. Web Api问题汇总

    在公网上布署Web Api的时候,不能调用,返回404 在web.config中 Adding the following to the web.config file worked for me: ...

  3. Swift控制手电筒操作(iOS)

    手电筒是iphone的一个常用功能,最常用的操作就是turn on和turn off,下面我们来实现一个简单的手电筒操作程序:一个按钮来控制iphone手电筒的On和Off,并且按钮的text也做相应 ...

  4. CSS3 实现的一个简单的"动态主菜单" 示例[转]

    其实这个示例蛮无聊的 很简单 也没什么实际的用处. 主要是展示了 CSS3 如何实现动画效果. 写这个主要是想看一看 完成这样的效果 我到底要写多少代码. 同时和我熟悉的java做个比较. 比较结果不 ...

  5. JS回调函数的应用,原来这么简单!

    JS的回调函数很简单,看代码: 在a.js中 var myback = null; function load(obj){ myback = obj; } function save(){ // 后台 ...

  6. bzoj1485 有趣的数列

    传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=1485 [题解] Catalan数,注意不能直接用逆元,需要分解质因数. # include ...

  7. 20155303 2016-2017-2 《Java程序设计》第七周学习总结

    20155303 2016-2017-2 <Java程序设计>第七周学习总结 教材学习中的问题和解决过程 『问题一』:SimpleDateFormat中每个字符的含义都是什么? 『问题一解 ...

  8. weblogic 反序列化补丁绕过漏洞的一个批量检测shell脚本(CVE-2017-3248 )

    ~ 以下内容,仅供学习参考 ~ weblogic 反序列化补丁绕过漏洞已经出了两个月了,balabala ~~~ 废话不说,拿到该漏洞的利用工具weblogic.jar,但只能一个个检测ip和端口,效 ...

  9. vue项目的配置

    1.第一首先我们先安装:visual studio code开发环境,可以换成中文版的. 2.安装note.js   下载链接:https://nodejs.org/en/ 3.安装npm使用淘宝镜像 ...

  10. php Only variables should be passed by reference 报错问题

    这个错误是变量引用引起的非致命错误,可修改php.ini文件的error_reporting = E_ALL & E_NOTICE 使其屏蔽此错误