Codeforces 356D Bacterial Melee dp
我们发现所有合法串都是原序列的某个子序列(这个子序列相邻元素相等) 的扩展, 比如子序列为abc, 那么aabbbc, abbbcc 等都是合法串。
所以我们只需要dp出原串有多少相邻元素不同的子序列就好啦。
#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long
using namespace std; const int N = + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-; int n, f[N][], g[N][];
int comb[N][N];
char s[N];
bool vis[]; inline void add(int &a, int b) {
a += b; if(a >= mod) a -= mod;
} int main() {
for(int i = ; i < N; i++)
for(int j = comb[i][] = ; j <= i; j++)
comb[i][j] = (comb[i - ][j - ] + comb[i - ][j]) % mod;
scanf("%d%s", &n, s + );
f[][s[] - 'a'] = ; vis[s[] - 'a'] = true;
for(int i = ; i <= n; i++) {
memcpy(g, f, sizeof(g));
for(int j = i - ; j >= ; j--) {
for(int k = ; k < ; k++) {
if(k != s[i] - 'a') add(f[j + ][s[i] - 'a'], g[j][k]);
}
add(f[j + ][s[i] - 'a'], mod - g[j + ][s[i] - 'a']);
}
if(!vis[s[i] - 'a']) {
vis[s[i] - 'a'] = true;
f[][s[i] - 'a'] = ;
}
}
int ans = ;
for(int i = ; i <= n; i++)
for(int j = ; j < ; j++)
add(ans, 1ll * comb[n - ][i - ] * f[i][j] % mod);
printf("%d\n", ans);
return ;
} /*
*/
Codeforces 356D Bacterial Melee dp的更多相关文章
- [Codeforces 1201D]Treasure Hunting(DP)
[Codeforces 1201D]Treasure Hunting(DP) 题面 有一个n*m的方格,方格上有k个宝藏,一个人从(1,1)出发,可以向左或者向右走,但不能向下走.给出q个列,在这些列 ...
- Bacterial Melee CodeForces - 756D (dp去重)
大意: 给定字符串, 每次可以任选一个字符$x$, 将$x$左侧或右侧也改为$x$, 求最终能得到多少种字符串. 首先可以观察到最终字符串将连续相同字符合并后一定是原字符串的子序列 并且可以观察到相同 ...
- codeforces Hill Number 数位dp
http://www.codeforces.com/gym/100827/attachments Hill Number Time Limits: 5000 MS Memory Limits: ...
- codeforces Educational Codeforces Round 16-E(DP)
题目链接:http://codeforces.com/contest/710/problem/E 题意:开始文本为空,可以选择话费时间x输入或删除一个字符,也可以选择复制并粘贴一串字符(即长度变为两倍 ...
- codeforces #round363 div2.C-Vacations (DP)
题目链接:http://codeforces.com/contest/699/problem/C dp[i][j]表示第i天做事情j所得到最小的假期,j=0,1,2. #include<bits ...
- codeforces round367 div2.C (DP)
题目链接:http://codeforces.com/contest/706/problem/C #include<bits/stdc++.h> using namespace std; ...
- CodeForces 176B Word Cut dp
Word Cut 题目连接: http://codeforces.com/problemset/problem/176/C Description Let's consider one interes ...
- codeforces 148D之概率DP
http://codeforces.com/problemset/problem/148/D D. Bag of mice time limit per test 2 seconds memory l ...
- codeforces 369 div2 C dp
http://codeforces.com/contest/711 C. Coloring Trees time limit per test 2 seconds memory limit per t ...
随机推荐
- hue报错StructuredException: timed out (code THRIFTSOCKET): None的处理
通过hue的web界面进行hive的sql查询,无法显示结果并报错timeout 报错如下:[28/Jul/2017 11:23:29 +0800] decorators ERROR error ru ...
- 51nod--1242 斐波那契数列第N项 (矩阵乘法优化)
题目: 1242 斐波那契数列的第N项 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 斐波那契数列的定义如下: F(0) = 0 F(1) = 1 F(n) ...
- windows IDEA注册码激活方法(2018.4.8)靠谱可用!
简介 intellij idea 2018注册码是针对intellij idea 2018软件研发推出,它的使用需要和激活补丁一起使用,安装包里面也拥有破解补丁, 所有无需在进行单独的下载:不过如果选 ...
- mysql定时任务,每天的零点执行一个存储过程
1 前言 利用navicat工具来写存储过程及定时执行,此文章是按照自身经验总结的,仅作为记录使用. 2 步骤 2.1 新建过程 2.2 在函数体写你需要执行的代码 CREATE DEFINER=`r ...
- Confluence 6 内存使用和需求和一些问题
系统备份和恢复 Confluence 的备份和恢复是与数据库中数据量的大小有关.这个操作可能会对 Confluence 的性能产生很多关键性的影响并且大量消耗内存.如果你在 Confluence 的 ...
- Confluence 6 手动备份站点
Confluence 被配置自动备份数据,使用压缩的 XML 格式.同时你也可以通过 Confluence 的 管理员控制台(Administration Console)手动进行备份. 你需要具有 ...
- ES6之Set与Map加深理解
Set 类似于数组,但是成员的值都是唯一的,没有重复的值,有序. Set函数可以接受一个数组(或者具有 iterable 接口的其他数据结构)作为参数,用来初始化. 用途 数组去重: [...new ...
- angularjs 监听 文档click 事件
项目 上遇到 innHTML 放入 一大段的html 内容 中带有 click 事件 如onclick="callInMethod("http://www.crm.bmcc.c ...
- Python判断字符串是否xx开始或结尾
判断是否xx开始 使用startswith 示例代码: String = "12345 上山打老虎" if str(String).startswith('1'): #判断Stri ...
- 编程语言,执行python程序,变量(命名规范)
编程语言 分类: 计算语言/汇编语言/高级语言 计算语言: 站在计算机的角度,说计算机能听懂的语言,就是直接用二进制编程,直接操作硬件 优点是最底层,执行速度最快 缺点是最复杂,开发效率最低 ...