题目大意:
  有一串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的更多相关文章

  1. Codeforces 356D Bacterial Melee dp

    Bacterial Melee 我们发现所有合法串都是原序列的某个子序列(这个子序列相邻元素相等) 的扩展, 比如子序列为abc, 那么aabbbc, abbbcc 等都是合法串. 所以我们只需要dp ...

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

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

  3. 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 ...

  4. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  5. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  6. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  7. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  8. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  9. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

随机推荐

  1. python中filter函数

    python中filter()函数   filter()函数是 Python 内置的另一个有用的高阶函数,filter()函数接收一个函数 f 和一个list,这个函数 f 的作用是对每个元素进行判断 ...

  2. DTW 算法(转)

    DTW为(Dynamic Time Warping,动态时间归准)的简称.应用很广,主要是在模板匹配中,比如说用在孤立词语音识别,计算机视觉中的行为识别,信息检索等中.可能大家学过这些类似的课程都看到 ...

  3. python基础===tkinter学习链接

    http://effbot.org/tkinterbook/tkinter-classes.htm

  4. pam_examples

    blank.c /* * $Id$ */ /* Andrew Morgan (morgan@parc.power.net) -- a self contained `blank' * applicat ...

  5. JavaScript实现Fly Bird小游戏

    1.分析页面结构,理清需求和功能 游戏有三个界面,分别是开始界面,游戏界面和游戏结束界面. 1.1 开始界面 start.gif 游戏的大背景 上下移动的游戏标题和翅膀摆动的小鸟 start 按钮,点 ...

  6. awk处理之案例六:awk根据条件插入文本

    编译环境 本系列文章所提供的算法均在以下环境下编译通过. [脚本编译环境]Federa 8,linux 2.6.35.6-45.fc14.i686 [处理器] Intel(R) Core(TM)2 Q ...

  7. 《The art of software testing》的一个例子

    这几天一直在看一本书,<The art of software testing>,里面有一个例子挺有感触地,写出来和大家分享一下: [问题] 从输入对话框中读取三个整数值,这三个整数值代表 ...

  8. 简单的WSGI server

    参考:https://ruslanspivak.com/lsbaws-part1/ 简单的WSGI server server程序 webserver.py # Tested with Python ...

  9. 带有定位当前位置的百度地图web api 前端代码

    <!DOCTYPE html> <html lang="zh-cn"> <head> <meta name="viewport& ...

  10. 转:使用IDA动态调试WanaCrypt0r中的tasksche.exe

    逆向分析——使用IDA动态调试WanaCrypt0r中的tasksche.exe 转:http://www.4hou.com/technology/4832.html 2017年5月19日发布 导语: ...