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 ...
随机推荐
- 解决mysql 主从数据库同步不一致的方法
接着上文 配置完Mysql 主从之后,在使用中可能会出现主从同步失败的情况. mysql> show slave status\G Slave_IO_Running: Yes Slave_SQL ...
- python学习第40天
# 多表查询 # 索引的基础理论 + 数据准备
- 【原创】大叔经验分享(31)CM金丝雀Canary报错
CM金丝雀Canary报错 1 HDFS 金丝雀Canary 测试无法为 /tmp/.cloudera_health_monitoring_canary_files 创建父目录. 2 Hive Met ...
- [转]Fiddler模拟post四种请求数据
1 前言 仅作为记录使用. 2 内容 post请求主体详解: 对于get请求来说没有请求主体entity-body.对于post请求而言,不会对发送请求的数据格式进行限制,理论上你可以发任意数据,但是 ...
- [JavaScript]iframe的contentWindow
HTMLIFrameElement.contentWindow返回的是HTMLIFrameElement类型元素的window对象 通过此对象可以修改iframe实体内的window行为 <if ...
- swift 实践- 13 -- UIStepper
import UIKit class ViewController: UIViewController { var stepper: UIStepper! var label: UILabel! ov ...
- LeetCode(69):x 的平方根
Easy! 题目描述: 实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去. 示例 1: 输入: ...
- input标签 disabled 和 readonly的区别
需求描述:今天提交代码,老大审了一下,给我指出了一个改正的地方,XXX的详细信息页面(不是修改页面) input的内容是不能改的,给我指出的时候,我立马就知道了这个该怎么改了,加个readonly不就 ...
- PyCharm+SVN
首先电脑安装svn,并且确svn/bin下面有svn.exe文件 没有bin/svn.exe解决方法: 重新打开TortoiseSVN安装文件-Modify-Next后在command line cl ...
- Centos6.10部署TeamViewer
1.在官网下载支持Linux系统的包,建议下载TeamViewer12的包,官网URL:https://www.teamviewer.com/cn/download/linux/ 2.将下载的软件包导 ...