【Codeforces 1426 F】Number of Subsequences,字符串计数DP

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;
}
已标记关键词 清除标记
相关推荐
<p> <strong><span style="font-size:20px;color:#FF0000;">本课程主要针对计算机相关专业的正在做毕设的学生与需要项目实战练习的Java学习者</span></strong> </p> <p> <span style="color:#FF0000;"><strong><span style="font-size:18px;">1. 包含:<span style="color:#FFFF00;background-color:#FF0000;">项目源码、</span><span style="color:#FFFF00;background-color:#FF0000;">项目文档、数据库脚本、软件工具</span>等所有资料</span></strong></span> </p> <p> <span style="color:#FF0000;"><strong><span style="font-size:18px;">2. 手把手的带你从零开始部署运行本套系统</span></strong></span> </p> <p> <span style="color:#FF0000;"><strong><span style="font-size:18px;">3. 该项目附带的源码资料可作为毕设使用</span></strong></span> </p> <p> <span style="color:#FF0000;"><strong><span style="font-size:18px;">4. 提供技术答疑和远程协助指导</span></strong></span><strong><span style="font-size:18px;"></span></strong> </p> <p> <br /> </p> <p> <span style="font-size:18px;"><strong>项目运行截图:</strong></span> </p> <p> <strong><span style="font-size:18px;">1)系统登陆界面</span></strong> </p> <p> <strong><span style="font-size:18px;"><img src="https://img-bss.csdn.net/202002241015433522.png" alt="" /><br /> </span></strong> </p> <p> <strong><span style="font-size:18px;"><strong><span style="font-size:18px;">2)学生模块</span></strong></span></strong> </p> <p> <strong><span style="font-size:18px;"><img src="https://img-bss.csdn.net/202002241015575966.png" alt="" /></span></strong> </p> <p> <strong><span style="font-size:18px;"><strong><span style="font-size:18px;">3)教师模块</span></strong></span></strong> </p> <p> <strong><span style="font-size:18px;"><img src="https://img-bss.csdn.net/202002241016127898.png" alt="" /></span></strong> </p> <p> <strong><span style="font-size:18px;"><strong><span style="font-size:18px;">4)系统管理员</span></strong></span></strong> </p> <p> <strong><span style="font-size:18px;"><img src="https://img-bss.csdn.net/202002241016281177.png" alt="" /></span></strong> </p> <p> <strong><span style="font-size:18px;"><img src="https://img-bss.csdn.net/202002241016369884.png" alt="" /></span></strong> </p> <p> <strong><span style="font-size:18px;"><br /> </span></strong> </p> <p> <strong><span style="font-size:18px;"><strong><span style="font-size:18px;">更多Java毕设项目请关注我的毕设系列课程 <a href="https://edu.csdn.net/lecturer/2104">https://edu.csdn.net/lecturer/2104</a></span></strong></span></strong> </p> <p> <strong><span style="font-size:18px;"><br /> </span></strong> </p>
©️2020 CSDN 皮肤主题: 技术工厂 设计师:CSDN官方博客 返回首页