problem

F. Number of Subsequences
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a string s consisting of lowercase Latin letters “a”, “b” and “c” and question marks “?”.

Let the number of question marks in the string s be k. Let’s replace each question mark with one of the letters “a”, “b” and “c”. Here we can obtain all 3k possible strings consisting only of letters “a”, “b” and “c”. For example, if s=“ac?b?c” then we can obtain the following strings: [“acabac”, “acabbc”, “acabcc”, “acbbac”, “acbbbc”, “acbbcc”, “accbac”, “accbbc”, “accbcc”].

Your task is to count the total number of subsequences “abc” in all resulting strings. Since the answer can be very large, print it modulo 109+7.

A subsequence of the string t is such a sequence that can be derived from the string t after removing some (possibly, zero) number of letters without changing the order of remaining letters. For example, the string “baacbc” contains two subsequences “abc” — a subsequence consisting of letters at positions (2,5,6) and a subsequence consisting of letters at positions (3,5,6).

Input
The first line of the input contains one integer n (3≤n≤200000) — the length of s.

The second line of the input contains the string s of length n consisting of lowercase Latin letters “a”, “b” and “c” and question marks"?".

Output
Print the total number of subsequences “abc” in all strings you can obtain if you replace all question marks with letters “a”, “b” and “c”, modulo 109+7.

Examples
inputCopy
6
ac?b?c
outputCopy
24
inputCopy
7
???
outputCopy
2835
inputCopy
9
cccbbbaaa
outputCopy
0
inputCopy
5
a???c
outputCopy
46
Note
In the first example, we can obtain 9 strings:

“acabac” — there are 2 subsequences “abc”,
“acabbc” — there are 4 subsequences “abc”,
“acabcc” — there are 4 subsequences “abc”,
“acbbac” — there are 2 subsequences “abc”,
“acbbbc” — there are 3 subsequences “abc”,
“acbbcc” — there are 4 subsequences “abc”,
“accbac” — there is 1 subsequence “abc”,
“accbbc” — there are 2 subsequences “abc”,
“accbcc” — there are 2 subsequences “abc”.
So, there are 2+4+4+2+3+4+1+2+2=24 subsequences “abc” in total.

solution

/*
题意:
+ 给出一个长为n,只由a,b,c,?组成的字符串。将k个问号替换为a,b,c得到3^k种字符串。求这3^k种字符串中,有多少个子串abc。
思路:
+ 定义dp[i][0/1/2/3]表示前i个字符,序列,"a","ab","abc"的个数。然后用滚动数组优化为一维的。
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 600000+10;
const int mod = 1e9+7;LL f[10];int main(){ios::sync_with_stdio(false);int n;  cin>>n;string s;  cin>>s;  s="0"+s;f[0] = 1;for(int i = 1; i <= n; i++){if(s[i]=='a')f[1] = (f[1]+f[0])%mod;else if(s[i]=='b')f[2] = (f[2]+f[1])%mod;else if(s[i]=='c')f[3] = (f[3]+f[2])%mod;else if(s[i]=='?'){f[3] = (3*f[3]%mod+f[2])%mod;f[2] = (3*f[2]%mod+f[1])%mod;f[1] = (3*f[1]%mod+f[0])%mod;f[0] = 3*f[0]%mod;}}cout<<f[3]<<"\n";return 0;
}

【Codeforces 1426 F】Number of Subsequences,字符串计数DP相关推荐

  1. Codeforces Round #674 (Div. 3) F. Number of Subsequences 简单计数dp

    传送门 文章目录 题意: 思路: 题意: 给你一个长度为nnn的串,包含a,b,c,?a,b,c,?a,b,c,?四种字符,其中???可以变成为a,b,ca,b,ca,b,c的任意一种,让你求abca ...

  2. Codeforces 835 F Roads in the Kingdom(树形dp)

    F. Roads in the Kingdom(树形dp) 题意: 给一张n个点n条边的无向带权图 定义不便利度为所有点对最短距离中的最大值 求出删一条边之后,保证图还连通时不便利度的最小值 $n & ...

  3. codeforces F.Fibonacci String Subsequences

    题意 定义F(x)为F(x-1)与F(x-2)的连接(其中F(0) = '0',F(1) = '1'). 给出一个长度不超过100的字符串s,询问s在F(x)的所有子序列中出现了多少次. 题解 数量很 ...

  4. scala中字符串计数_如何在Scala中创建一系列字符?

    scala中字符串计数 The range is a set of data from a lower value to a larger value. In Scala, we have an ea ...

  5. 空字符串计数、让字典可排序...Python冷知识(五)

    本文转载自Python编程时光(ID: Python-Time) 冷知识系列,直至今日,已经更新至第五篇.前四篇给你准备好了,还没阅读的可以学习一下. 谈谈 Python 那些不为人知的冷知识(一) ...

  6. CodeForces - 1401 F Reverse and Swap(线段树, 区间翻转, 区间交换,清晰易懂)

    CodeForces - 1401 F Reverse and Swap(线段树, 区间翻转, 区间交换)   首先一共有四个操作,第一个和第四个都是线段树的基本操作,直接用线段树实现.      第 ...

  7. codeforces1303 F. Number of Components(并查集+添_正序、删_逆序)

    F. Number of Components 并查集,每次修改考虑的是这个修改带来的贡献,就是和相邻颜色的对比,如果不考虑先不考虑颜色覆盖,那么添加颜色首先会产生一个新的连通块,然后考虑合并,每合并 ...

  8. python字符串前面加f什么意思_Python 字符串前面加u,r,b,f的含义

    1.字符串前加 u 例:u"我是含有中文字符组成的字符串." 作用: 后面字符串以 Unicode 格式 进行编码,一般用在中文字符串前面,防止因为源码储存格式问题,导致再次使用时 ...

  9. Python 字符串前面加u,r,b,f的含义(字符串前缀)

    1.字符串前加 u 例:u"我是含有中文字符组成的字符串." 作用: 后面字符串以 Unicode 格式 进行编码,一般用在中文字符串前面,防止因为源码储存格式问题,导致再次使用时 ...

最新文章

  1. java aar 文件,将本地.aar文件添加到我的gradle构建中
  2. android xml引用系统资源文件,android开发教程之系统资源的使用方法 android资源文件...
  3. Python模拟ICMP包
  4. HTML/CSS 面试总结
  5. MediaMuxer的使用
  6. 防抖与节流方案_关于图片懒加载的几种方案
  7. Android源码大放送(实战开发必备)
  8. html5 app 原理,浅析开发html5 app的三大优势
  9. 微信小程序PNG图片去白底
  10. arm gdb 编译与安装
  11. 大数据技术基础与应用
  12. 怎么用计算机测出来体脂,如何测自己的体脂率?
  13. SQL Server 2014下载及安装教程
  14. MOS管的行业应用领域-KIA MOS管
  15. 阿里云ECS服务器安装Nginx
  16. 身份证/异地身份证在北京办理的解决办法
  17. 对偶量子计算机,广义量子干涉原理及对偶量子计算机
  18. Java毕设项目藏宝阁游戏交易系统(java+VUE+Mybatis+Maven+Mysql)
  19. pytorch 冻结参数
  20. 漫画 | 如何判断一家公司快不行了?

热门文章

  1. 写作之法 —— 如何切题与点题
  2. vmware tools 的安装(Read-only file system 的解决)
  3. 常见信号的模拟仿真(matlab)(spike signal)
  4. 骗术 —— 魔高一尺道高一丈
  5. utilities(matlab)—— 多元函数的数值梯度
  6. 从二分逼近领略计算科学的魅力
  7. python画三维散点图-Python 绘制酷炫的三维图步骤详解
  8. micropython入门教程-【ESP8266】MicroPython的快速入门教程
  9. python能做什么-揭秘python都能做什么?
  10. python100例详解-几个小例子给你讲解Python中类的描述符