[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 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
随机推荐
- js刷新页面方法 -- (转)
1,reload 方法,该方法强迫浏览器刷新当前页面. 语法:location.reload([bForceGet]) 参数: bForceGet, 可选参数, 默认为 false,从客户端缓存里 ...
- 2.0 docker安装
问题列表: Error: Cannot retrieve metalink for repository: epel. Please verify its path and try again 解:处 ...
- linux驱动基础系列--Linux下Spi接口Wifi驱动分析
前言 本文纯粹的纸上谈兵,我并未在实际开发过程中遇到需要编写或调试这类驱动的时候,本文仅仅是根据源码分析后的记录!基于内核版本:2.6.35.6 .主要是想对spi接口的wifi驱动框架有一个整体的把 ...
- 測試 battery capacity curve 的負載
昨天有同事問說, 他要測試 battery capacity curve, 並且負載要使用 33mA, 於是我想到有一個 apk 名稱為 快速放電 (最下方),可以控制 cpu 的 load, 他試了 ...
- java===java基础学习(5)---文件读取,写入操作
文件的写入读取有很多方法,今天学到的是Scanner和PrintWriter 文件读取 Scanner in = new Scanner(Paths.get("file.txt") ...
- binlog_server备份binlogs
在主库上建一个复制用的账号: root@localhost [(none)]>grant replication slave on *.* to 'wyz'@'%' identified by ...
- AC日记——聪明的质监员 洛谷 P1314
聪明的质监员 思路: 二分: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 200005 #define l ...
- 从(0)新开始vue2.0【安装】
安装node:测试安装结果:node -v 安装cnpm(如果你可以***,可以不安装) 安装webpack:npm install webpack -g npm install vue-cli -g ...
- Rsync+Inotify 搭建实时同步数据
1.安装软件包 # yum install inotify-tools # yum -y install rsync 2.同步机器相互添加信任 [root@host-10-0-100-106 ~]# ...
- POJ 2777 Count Color(线段树 + 染色问题)
传送门:Count Color Description Chosen Problem Solving and Program design as an optional course, you are ...