CF1426F.Number of Subsequences

普及+/提高

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a string ss consisting of lowercase Latin letters "a", "b" and "c" and question marks "?".

Let the number of question marks in the string ss be kk. Let's replace each question mark with one of the letters "a", "b" and "c". Here we can obtain all 3k3^{k} 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+710^{9} + 7.

A subsequence of the string tt is such a sequence that can be derived from the string tt 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)(2, 5, 6) and a subsequence consisting of letters at positions (3,5,6)(3, 5, 6).

给你一个由小写拉丁字母 "a"、"b" 和 "c" 以及问号 "?" 组成的字符串 ss。

设字符串 ss 中问号的个数为 kk。我们将每个问号替换成 "a"、"b" 或 "c" 中的一个字母,从而可以得到全部 3k3^{k} 个仅由字母 "a"、"b" 和 "c" 构成的字符串。例如,若 $s = "ac?b?c""ac?b?c",则我们可以得到如下字符串:[["acabac", "acabbc", "acabcc", "acbbac", "acbbbc", "acbbcc", "accbac", "accbbc", "accbcc"]]。

你的任务是计算所有这些结果字符串中子序列 "abc" 的总数。由于答案可能非常大,请将结果对 109+710^{9} + 7 取模后输出。

字符串 tt 的一个子序列是指从 tt 中删除若干(可能为零)个字符后,剩余字符保持原有顺序所构成的序列。例如,字符串 "baacbc" 包含两个子序列 "abc":一个由位置 (2,5,6)(2, 5, 6) 上的字符构成,另一个由位置 (3,5,6)(3, 5, 6) 上的字符构成。

输入格式

The first line of the input contains one integer nn (3≤n≤200 000)(3 \le n \le 200\,000) — the length of ss.

The second line of the input contains the string ss of length nn consisting of lowercase Latin letters "a", "b" and "c" and question marks"?".

输入的第一行包含一个整数 nn(3≤n≤200 0003 \le n \le 200\,000)—— 字符串 ss 的长度。

输入的第二行包含一个长度为 nn 的字符串 ss,由小写拉丁字母 "a"、"b"、"c" 和问号 "?" 组成。

输出格式

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+710^{9} + 7.

打印在将所有问号替换为字母 “a”、“b” 和 “c” 后,所能得到的所有字符串中子序列 “abc” 的总个数,结果对 109+710^{9} + 7 取模。

输入输出样例

  • 输入#1

    6
    ac?b?c

    输出#1

    24
  • 输入#2

    7
    ???????

    输出#2

    2835
  • 输入#3

    9
    cccbbbaaa

    输出#3

    0
  • 输入#4

    5
    a???c

    输出#4

    46

说明/提示

In the first example, we can obtain 99 strings:

  • "acabac" — there are 22 subsequences "abc",
  • "acabbc" — there are 44 subsequences "abc",
  • "acabcc" — there are 44 subsequences "abc",
  • "acbbac" — there are 22 subsequences "abc",
  • "acbbbc" — there are 33 subsequences "abc",
  • "acbbcc" — there are 44 subsequences "abc",
  • "accbac" — there is 11 subsequence "abc",
  • "accbbc" — there are 22 subsequences "abc",
  • "accbcc" — there are 22 subsequences "abc".

So, there are 2+4+4+2+3+4+1+2+2=242 + 4 + 4 + 2 + 3 + 4 + 1 + 2 + 2 = 24 subsequences "abc" in total.

在第一个例子中,我们可以得到 99 个字符串:

  • "acabac" — 包含 22 个子序列 "abc",
  • "acabbc" — 包含 44 个子序列 "abc",
  • "acabcc" — 包含 44 个子序列 "abc",
  • "acbbac" — 包含 22 个子序列 "abc",
  • "acbbbc" — 包含 33 个子序列 "abc",
  • "acbbcc" — 包含 44 个子序列 "abc",
  • "accbac" — 包含 11 个子序列 "abc",
  • "accbbc" — 包含 22 个子序列 "abc",
  • "accbcc" — 包含 22 个子序列 "abc"。

因此,子序列 "abc" 的总数为 2+4+4+2+3+4+1+2+2=242 + 4 + 4 + 2 + 3 + 4 + 1 + 2 + 2 = 24。

输入解题思路,AI测评打分。不知道怎么写?

首页