Bacterial Melee CodeForces - 756D (dp去重)
大意: 给定字符串, 每次可以任选一个字符$x$, 将$x$左侧或右侧也改为$x$, 求最终能得到多少种字符串.
首先可以观察到最终字符串将连续相同字符合并后一定是原字符串的子序列
并且可以观察到相同长度(设为m)的子序列能产生的最终字符串种类数是相同的.
即$\binom{n-1}{m-1}$ (可以通过排列后m-1个字符首次出现位置得到).
然后问题就转化为求原字符串相邻字符不同且本质不同的子序列个数.
前缀优化一下复杂度为$O(n^2)$
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
//head const int N = 5e3+10;
int n, nn, a[N];
int f[N][27], g[N][27], sum[N], t[N], C[N][N]; int main() {
scanf("%d", &n),nn=n;
REP(i,1,n) {
char c;
scanf(" %c", &c);
if (c==a[i-1]+'a'-1) --i,--n;
else a[i] = c-'a'+1;
}
REP(i,1,n) {
memcpy(sum,t,sizeof t);
memcpy(g,f,sizeof f);
PER(j,1,i-1) {
int r = ((ll)sum[j]-g[j][a[i]]-g[j+1][a[i]])%P;
(f[j+1][a[i]]+=r)%=P, (t[j+1]+=r)%=P;
}
if (!f[1][a[i]]) f[1][a[i]]=1,++t[1];
}
REP(i,0,nn) {
C[i][0]=1;
REP(j,1,i) C[i][j]=(C[i-1][j]+C[i-1][j-1])%P;
}
ll ans = 0;
REP(i,1,n) REP(j,1,26) ans+=(ll)C[nn-1][i-1]*f[i][j]%P;
printf("%lld\n", (ans%P+P)%P);
}
Bacterial Melee CodeForces - 756D (dp去重)的更多相关文章
- Codeforces 356D Bacterial Melee dp
Bacterial Melee 我们发现所有合法串都是原序列的某个子序列(这个子序列相邻元素相等) 的扩展, 比如子序列为abc, 那么aabbbc, abbbcc 等都是合法串. 所以我们只需要dp ...
- Two Melodies CodeForces - 813D (DP,技巧)
https://codeforces.com/problemset/problem/813/D dp[i][j] = 一条链以i结尾, 另一条链以j结尾的最大值 关键要保证转移时两条链不能相交 #in ...
- Consecutive Subsequence CodeForces - 977F(dp)
Consecutive Subsequence CodeForces - 977F 题目大意:输出一序列中的最大的连续数列的长度和与其对应的下标(连续是指 7 8 9这样的数列) 解题思路: 状态:把 ...
- codeforces的dp专题
1.(467C)http://codeforces.com/problemset/problem/467/C 题意:有一个长为n的序列,选取k个长度为m的子序列(子序列中不能有位置重复),求所取的k个 ...
- Codeforces 721C [dp][拓扑排序]
/* 题意:给你一个有向无环图.给一个限定t. 问从1点到n点,在不超过t的情况下,最多可以拜访几个点. 保证至少有一条路时限不超过t. 思路: 1.由无后向性我们可以知道(取决于该图是一个DAG), ...
- CodeForces 607C (DP) Hard problem
题目:这里 题意:给定n个字符串,每个字符串可以进行一项操作,就是将这个字符串交换,就是该字符串的第一个和最后一个交换,第二个和倒数第二个交换,以此类推,当然可以选择对于 该字符串进行或不进行这项操作 ...
- Codeforces 611d [DP][字符串]
/* 题意:给一个长度不超过5000的字符串,每个字符都是0到9的数字. 要求将整个字符串划分成严格递增的几个数字,并且不允许前导零. 思路: 1.很开心得发现,当我在前i个区间以后再加一个区间的时候 ...
- Codeforces 404D [DP]
/* 我是一个习惯后悔,但是没办法忍受内疚感的二货== 这题是个无脑dp,但是比赛大概20min没出...其实最后5min我好好想想简单化边界条件,可以出的. 题意: 给你一个长度为1e6的由?*01 ...
- Codeforces 119C DP
题意: 有n天,m门课和常数k; 每天上一门课,每门课程有两个属性,最少作业量a,最多作业量b,和难度c. 1<=a<=b<=1e16 c<=100 1<=n<=m ...
随机推荐
- poj2774
思路 求出height之后 只要相邻两个子串是本串不同的来更新就好 因为这样一定是最优啊..取min显然越长越不好 (这里'%'当成'{'吧) abc%bca height i sa belong 0 ...
- ps2016新功能
1 内容识别缩放(alt ctrl shift c) 2 内容感知移动 3 画笔大小和画笔硬度调整 alt + 鼠标右键:上下为硬度 ...
- An Empirical Evaluation of Generic Convolutional and Recurrent Networks for Sequence Modeling
An Empirical Evaluation of Generic Convolutional and Recurrent Networks for Sequence Modeling 2018-0 ...
- 论文笔记:A Review on Deep Learning Techniques Applied to Semantic Segmentation
A Review on Deep Learning Techniques Applied to Semantic Segmentation 2018-02-22 10:38:12 1. Intr ...
- Windows系统零开始前端开发环境配置
1. 安装nodejs 国内下载页面(推荐) 官网下载页面 现在的nodejs自带NPM,只需点击下一步下一步安装即可. 为了加速国内NPM包下载,可配置淘宝NPM镜像 2. 安装git 国内下载页面 ...
- android studio 的基本使用和建立一个小项目
https://github.com/allenxieyusheng/Android-Studio
- React入门实例:组件化+react-redux实现网上商城(1)
项目运行 1.git clone https://github.com/soybeanxiaobi/React_demo_onlineShop 2.cd React_demo_onlineShop(文 ...
- _itemmod_hidden
该表中的物品放在背包或银行中中会计算属性 `entry`物品ID `comment` 备注
- Ubuntu 编译安装 Xdebug
安装xdebug 1.下载 https://xdebug.org/download.php 找到PHP5.6对应的版本 https://xdebug.org/files/xdebug-2.5.5.tg ...
- 设计模式(六)Prototype Pattern 原型模式
通过new产生一个对象非常繁琐,可以使用原型模式 原型模式实现: ——Cloneable接口和clone方法 ——Prototype模式实现起来最困难的地方是实现内存的复制和操作,Java中提供了cl ...