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 ...
随机推荐
- MariaDB主从备份记录
一.预期效果: 环境: centos 6.5 mariadb 10.0.14 (mysql -V) 主服务器:192.168.5.206 从服务器:192.168.5.207 主服务器数据 ...
- Linux多台主机间配置SSH免密登陆
1.安装ssh. sudo apt-get install ssh. 安装完成后会在~目录(当前用户主目录,即这里的/home/xuhui)下产生一个隐藏文件夹.ssh(ls -a 可以查看隐藏文件 ...
- Django makemigrations 不行时
- xampp 搭建好本地服务器以后手机无法访问
转载地址:https://blog.csdn.net/weixin_35773751/article/details/80076492 解决办法: 1.将网络连接修改为工作网络,然后关闭工作局域网的防 ...
- JVM·垃圾收集器与内存分配策略之对象是否可被回收!
1.判断对象已经死去/不再被引用. 1.1.引用计数算法:给对象添加引用计数器,有个地方引用就+1,引用失效就-1.任何时刻,引用为0,即判断对象死亡. 1.1.1.优点:实现 ...
- 软件测试作业 - fault error failure
给出的题目如下: 我的解答如下: For program 1:1. where i > 0 is the fault , it should be changed to i>= 0 to ...
- Confluence 6 安全相关问题提交链接
找到和报告安全漏洞 Atlassian 有关安全漏洞的报告细节,请参考如何报告一个安全问题(How to Report a Security Issue)链接. 发布 Confluence 安全公共 ...
- Confluence 6 SQL Server 测试你的数据库连接
在你的数据库设置界面,有一个 测试连接(Test connection)按钮可以检查: Confluence 可以连接你的数据库服务器 数据库字符集和隔离级别是正确的 你的数据库用户有正确的数据库权限 ...
- 暑假里的第八篇Java
日期:2018.9.1 博客期:008 星期六 这几天刚到学校,Java方面写的少了!目前在做老师头放假前发布的那一套题目,就是哪个Java程序测试卷.至于自己能不能都做出来我自己心里十分清楚!今天就 ...
- 初识ActiveMQ
消息中间件的初步认识 什么是消息中间件? 消息中间件是利用高效可靠的消息传递机制进行平台无关的数据交流,并基于数据通信来进行分布式系统的集成.通过提供消息传递和消息排队模型,可以在分布式架构下扩展进程 ...