CodeForces 204D Little Elephant and Retro Strings
来源:互联网 发布:linux chgrp chown 编辑:程序博客网 时间:2024/05/17 07:31
CodeForces 204D Little Elephant and Retro Strings
题目描述:
输入一个由 W 、 B 和 X 组成的字符串,你可以把 X 字符替换成 W 或者 B ,问有多少种方案使得,可以找到两个不相交的长度问
题解:
为了避免重复,仅考虑最左边的长度为
这个方案数很好求,以左边的为例,假设在前缀
右边的用同样的方式求,最后把两个合并起来就行了。
题目链接: vjudge 原网站
代码:
#include <cstdio>#include <cstdlib>#include <algorithm>using namespace std;#define MAXN 1000007const long long MOD = 1000000007LL;static int N, K;static long long sum, not_b[MAXN], not_w[MAXN], dp[MAXN], num[MAXN];static char S[MAXN];inline long long pow(long long x, long long y){ long long ans = 1; for (; y; y >>= 1, (x *= x) %= MOD) if (y & 1) (ans *= x) %= MOD; return ans;}int main(){ scanf("%d%d%s", &N, &K, S + 1); num[N + 1] = 1; for (int i = N; i; i--) num[i] = S[i] == 'X' ? 2LL * num[i+1] % MOD : num[i+1]; not_b[0] = 1; for (int i = 1, last = 0; i <= N; i++) { last = S[i] == 'W' ? 0 : last + 1; not_b[i] = S[i] == 'X' ? 2LL * not_b[i-1] % MOD : not_b[i-1]; dp[i] = dp[i-1]; if (K <= last && S[i-K] != 'B') { long long sub = i == K ? 1 : not_b[i-K-1]; (not_b[i] += MOD - sub) %= MOD; (dp[i] += sub * num[i+1]) %= MOD; } } not_w[N + 1] = not_w[N + 2] = 1; for (int i = N, last = 0; i; i--) { last = S[i] == 'B' ? 0 : last + 1; not_w[i] = S[i] == 'X' ? 2LL * not_w[i+1] % MOD : not_w[i+1]; if (K <= last && S[i+K] != 'W') { long long sub = not_w[i+K+1]; (not_w[i] += MOD - sub) %= MOD; (sum += sub * dp[i-1] % MOD * pow(num[i], MOD - 2) % MOD) %= MOD; } } printf("%lld\n", sum); return 0;}
提交记录(AC / Total = 1 / 1):
0 0
- CodeForces 204D Little Elephant and Retro Strings
- codeforces 204E Little Elephant and Strings
- codeforces 221D Little Elephant and Array
- codeforces 221D Little Elephant and Array
- Codeforces Round #129 (Div. 1)E. Little Elephant and Strings
- codeforces 204A Little Elephant and Interval
- CodeForces 204B - Little Elephant and Cards
- codeforces 204B- Little Elephant and Cards
- CodeForces 204A Little Elephant and Interval
- CodeForces 204A Little Elephant and Interval
- Codeforces 204B Little Elephant and Cards
- Codeforces Round #136 (Div. 2) D. Little Elephant and Array
- Codeforces Round #136 (Div. 2) D. Little Elephant and Array
- codeforces 258D Little Elephant and Broken Sorting
- Codeforces 258D Little Elephant and Broken Sorting
- <codeforces>Little Elephant and Sorting
- CF204E Little Elephant and Strings(同bzoj3277)
- [广义后缀自动机] BZOJ 3473 字符串 & BZOJ 3277 串 & Codeforces 204E #129 (Div. 1) E. Little Elephant and Strings
- USACO 3.1 Score Inflation (完全背包) P2722 总分 Score Inflation
- java中Iterator有什么用,举例说明?
- spring使用加密的属性文件
- oracle 除数为 0
- 杭电oj(Java版)——2005 第几天?
- CodeForces 204D Little Elephant and Retro Strings
- Android应用开发之数据加密浅析
- PL/SQL 异常处理
- Volley源码解析
- HTML5、CSS雪碧图
- 杭电oj(Java版)——2006 求奇数的乘积
- Linux的一些基本命令--入门级Linux基础知识
- 走心的a元素伪类顺序分析
- From 李慧琳 竞赛总分