[CodeForces-759D]Bacterial Melee
题目大意:
有一串n个字母,每个位置的字母可以同化边上的一个字母,
比如:ab可以变成aa或者bb。
相对的两个同化不能同时发生,比如ab不能变成ba。
现在给你一个字符串,问你经过任意次数的同化过程,最多能生成多少个字符串。
思路:
考虑同化过后的字符串与同化前的字符串的关系。
如果我们把一个字符串中相邻且相同的字母缩在一起,那么我们可以发现每一次同化就相当于从原串中去掉了一个字符。
这也就意味着同化过后的串一定是原串的一个子序列。
同样,如果一个串是原串的一个子序列,它一定能由原串同化而来。
我们可以先统计一下原串不同长度子序列的个数。
对于一个长度为l的子序列,它里面有n-l个字符被缩过,那么缩之前的串总共有C(n,l)种可能。
不同的子序列数量可以用DP求出来。
f[i][j]表示以字符j结尾的长度为i的子序列数量,则f[i][j]=sum{f[i-1][k]|k≠j}+1,枚举i,j,k,时间复杂度O(n^2*26)。
如果直接枚举k会TLE,只能过11个点,我们可以考虑用sum[i]记录长度为i的子串的数量和。
由于结尾位置的字符已确定,所以组合数用C(n-1,l-1)算,时间复杂度O(n^2)。
#include<cstdio>
#include<cctype>
typedef long long int64;
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'';
while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
return x;
}
const int N=,SIGMA=,mod=1e9+;
char s[N];
int f[N][SIGMA],sum[N],fact[N],factinv[N];
inline int idx(const char &ch) {
return ch-'a'+;
}
void exgcd(const int &a,const int &b,int &x,int &y) {
if(!b) {
x=;
y=;
return;
}
exgcd(b,a%b,y,x);
y-=a/b*x;
}
inline int inv(const int &x) {
int ret,tmp;
exgcd(x,mod,ret,tmp);
return (ret%mod+mod)%mod;
}
inline int C(const int &n,const int &m) {
return (int64)fact[n]*factinv[n-m]%mod*factinv[m]%mod;
}
int main() {
const int n=getint();
scanf("%s",s);
fact[]=factinv[]=;
for(register int i=;i<n;i++) {
fact[i]=(int64)fact[i-]*i%mod;
factinv[i]=inv(fact[i]);
}
sum[]=;
for(register int i=;i<n;i++) {
const int ch=idx(s[i]);
for(register int i=;i<=n;i++) {
sum[i]=(sum[i]-f[i][ch]+mod)%mod;
f[i][ch]=(sum[i-]-f[i-][ch]+mod)%mod;
sum[i]=(sum[i]+f[i][ch])%mod;
}
}
int ans=;
for(register int i=;i<=n;i++) {
ans=(ans+(int64)C(n-,i-)*sum[i]%mod)%mod;
}
printf("%d\n",ans);
return ;
}
[CodeForces-759D]Bacterial Melee的更多相关文章
- Codeforces 356D Bacterial Melee dp
Bacterial Melee 我们发现所有合法串都是原序列的某个子序列(这个子序列相邻元素相等) 的扩展, 比如子序列为abc, 那么aabbbc, abbbcc 等都是合法串. 所以我们只需要dp ...
- Bacterial Melee CodeForces - 756D (dp去重)
大意: 给定字符串, 每次可以任选一个字符$x$, 将$x$左侧或右侧也改为$x$, 求最终能得到多少种字符串. 首先可以观察到最终字符串将连续相同字符合并后一定是原字符串的子序列 并且可以观察到相同 ...
- Codeforces Round #330 (Div. 1) A. Warrior and Archer 贪心 数学
A. Warrior and Archer Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/594 ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
随机推荐
- Python3.3.3 安装(Linux系统)
1.wget http://www.python.org/ftp/python/3.3.3/Python-3.3.3.tgz //检查http://www.python.org/ftp/python网 ...
- Android中TextView设置字体
最近项目中出现把字体设置成宋体,微软雅黑,黑体,楷体等的需求; 度娘发现Android系统默认支持三种字体,分别为:“sans”, “serif”, “monospace",除此之外还可以使 ...
- Revison
- LINUX-内核-中断分析-中断向量表(3)-arm【转】
转自:http://blog.csdn.net/haolianglh/article/details/51986987 arm中断概念 在<ARM体系结构与编程>第9章中说到,ARM 中有 ...
- java===java基础学习(12)---方法的重写和重载
覆盖 / 重写(Override) 重写是子类对父类的允许访问的方法的实现过程进行重新编写, 返回值和形参都不能改变.即外壳不变,核心重写! 重写的好处在于子类可以根据需要,定义特定于自己的行为. 也 ...
- 自动化测试===requests+unittest+postman的接口测试
postman是一个跨平台的接口测试工具,下载链接在这里:https://www.getpostman.com/ unittest是一个单元测试框架,python中安装:pip install uni ...
- iptables 操作
iptables --list 查看列表 iptables删除规则 iptables -nL --line-number Chain INPUT (policy ACCEPT)num target p ...
- 文字顺时针旋转90度(纵向)&古诗词排版
1.文字旋转90度 width: 100px; height: 200px; line-height: 100px; text-align: center; writing-mode: vertica ...
- 20180104 wdcp中的mysql重启不成功
1.重启不成功是由于/www/wdlinux/mysql-5.5.54/data 中的ib_logfile0.ib_logfile1 和ibdata1的文件存在,可用netstat -lnpt查看当前 ...
- JS怎么刷新当前页面
JS怎么刷新当前页面 reload 方法,该方法强迫浏览器刷新当前页面.语法:location.reload([bForceGet]) 参数: bForceGet, 可选参数, 默认为 false,从 ...