Bacterial Melee

我们发现所有合法串都是原序列的某个子序列(这个子序列相邻元素相等) 的扩展, 比如子序列为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的更多相关文章

  1. [Codeforces 1201D]Treasure Hunting(DP)

    [Codeforces 1201D]Treasure Hunting(DP) 题面 有一个n*m的方格,方格上有k个宝藏,一个人从(1,1)出发,可以向左或者向右走,但不能向下走.给出q个列,在这些列 ...

  2. Bacterial Melee CodeForces - 756D (dp去重)

    大意: 给定字符串, 每次可以任选一个字符$x$, 将$x$左侧或右侧也改为$x$, 求最终能得到多少种字符串. 首先可以观察到最终字符串将连续相同字符合并后一定是原字符串的子序列 并且可以观察到相同 ...

  3. codeforces Hill Number 数位dp

    http://www.codeforces.com/gym/100827/attachments Hill Number Time Limits:  5000 MS   Memory Limits: ...

  4. codeforces Educational Codeforces Round 16-E(DP)

    题目链接:http://codeforces.com/contest/710/problem/E 题意:开始文本为空,可以选择话费时间x输入或删除一个字符,也可以选择复制并粘贴一串字符(即长度变为两倍 ...

  5. codeforces #round363 div2.C-Vacations (DP)

    题目链接:http://codeforces.com/contest/699/problem/C dp[i][j]表示第i天做事情j所得到最小的假期,j=0,1,2. #include<bits ...

  6. codeforces round367 div2.C (DP)

    题目链接:http://codeforces.com/contest/706/problem/C #include<bits/stdc++.h> using namespace std; ...

  7. CodeForces 176B Word Cut dp

    Word Cut 题目连接: http://codeforces.com/problemset/problem/176/C Description Let's consider one interes ...

  8. codeforces 148D之概率DP

    http://codeforces.com/problemset/problem/148/D D. Bag of mice time limit per test 2 seconds memory l ...

  9. codeforces 369 div2 C dp

    http://codeforces.com/contest/711 C. Coloring Trees time limit per test 2 seconds memory limit per t ...

随机推荐

  1. web@HTML重要标签详介绍.

    1.table标签<table border="1px " rules="groups" cellpadding="5px" cell ...

  2. Ex 7_17 考虑如下的网络(其中数字为对应边的容量)...第十三次作业

    (a) 利用ford-fulkerson算法即可求出最大流和最小分割. (b) 剩余网络为 由S可达的顶点为A.B.可达T的顶点为C. (c) 瓶颈边有e(A,C),e(B,C). (d) 下图中不包 ...

  3. Go 常见严格格式汇总(struct,func...)不定期更新!

    1 引言 Golang对格式有着较为严格的规定,例如方法大括号一定要放在方法名后,否则编译不过:变量(常量除外)未使用,也编译不过等等 2 例子 2.1 struct type person stru ...

  4. sql 迈安

    SELECT b.* FROM dbo.[耕地肥料_01土壤肥料机构建设基本情况]as b ,bs_org where--县(b.任务id in((select bs_task.id--,bs_org ...

  5. iOS UIDatePicker设置为中文的方法

    UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 20, 200, 30)]; datePick ...

  6. Confluence 6 数据收集隐私策略

    为什么 Confluence 收集使用数据? 针对 Confluence 我们很自豪 Confluence 是这个星球上最高效和强大的协作工具,我们也计划继续保持这个特性,尽我们最大的努力提供更新的 ...

  7. Confluence 6 升级你的许可证

    如果你修改了你的许可证(例如为你的许可证增加了更多的用户),或者从 Cloud 中整合到你本地,你需要更新你的许可证. 希望更新你的额许可证: 进入  > 基本配置(General Config ...

  8. linux下安装nginx及初步认识

    linux下安装配置nginx nginx:是一个高性能的反向代理服务器正向代理代理的是客户端,反向代理代理的是服务端. 这里以nginx-1.12.2版本为例子 1.首先去官网下载nginx-1.1 ...

  9. LeetCode(122):卖股票的最佳时机 II

    Easy! 题目描述: 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你可以尽可能地完成更多的交易(多次买卖一支股票). 注意:你不能同时参 ...

  10. 对<tr><td>标签里的input 循环取值

    需求描述:单击table整行,跳转到具体的信息页面  关键就是获取整行的id,传给后台做查询,返回list 解决思路:用带参数函数传过去id,然后在js的函数中用$("#id"). ...