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. pom.xml错误:org.codehaus.plexus.archiver.jar.Manifest.write(java.io.PrintWriter)的解决方法

    pom.xml文件在添加了新的依赖后,一直报:org.codehaus.plexus.archiver.jar.Manifest.write(java.io.PrintWriter)的错误,Maven ...

  2. (转)Tomcat配置调优与安全总结

    tomcat配置调优与安全总结 作为运维,避免不了与tomcat打交道,然而作者发现网络上关于tomcat配置和调优安全的文章非常散,通过参考各位大神的相关技术文档,根据作者对tomcat的运维经验, ...

  3. logstash定义表达式

    例如: echo '[2018/02/02 08:34:43.032]' >> tomcat_catalina.out 过滤方式 "message" => &qu ...

  4. 修改home下的中文目录为英文目录

    原文地址:http://blog.csdn.net/esonbest1234/article/details/50729582 习惯问题,喜欢使用fedora为您在home目录下自创建的“桌面”.“文 ...

  5. 微服务深入浅出(9)-- Nginx

    Nginx ("engine x") 是一个高性能的HTTP和反向代理服务器,处理请求是异步非阻塞的,多个连接(万级别)可以对应一个进程.而Apache是同步多进程模型,一个连接对 ...

  6. 【leetcode 简单】 第五十六题 快乐数

    编写一个算法来判断一个数是不是“快乐数”. 一个“快乐数”定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,也可能是无限循环但始终变不到 1.如 ...

  7. python概念-Socket到底有多骚

    Socket究竟是什么呢? 简单来说Socket就是用来完成客户端与服务器之间的通信 例如浏览器访问网页,例如网络游戏等一切基于客户端服务器来实现的C/S架构程序 Socket是基于互联网OSI七层协 ...

  8. C# Func的同步、异步调用

    using System; namespace ActionDemo { class Program { static void Main(string[] args) { Console.Write ...

  9. 系统学习(javascript)_基础(数据类型之间的转换)

    在js中数据类型转换一般分为两种,即强制类型转换和隐式类型转换(利用javascript弱变量类型转换). 强制转换: 利用js提供的函数parseInt(),parseFloat(),Number( ...

  10. windbg分析net程序内存泄漏问题

    1       问题简介 有客户反馈,打了最新补丁后,服务器的内存暴涨,一直降不下来,程序非常卡.在客户的服务器上抓了一个dump文件,开始分析. 分析问题的思路: 1.找到是那些资源占用了大量内存? ...