CF1499E.Chaotic Merge

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given two strings xx and yy, both consist only of lowercase Latin letters. Let ∣s∣|s| be the length of string ss.

Let's call a sequence aa a merging sequence if it consists of exactly ∣x∣|x| zeros and exactly ∣y∣|y| ones in some order.

A merge zz is produced from a sequence aa by the following rules:

  • if ai=0a_i=0, then remove a letter from the beginning of xx and append it to the end of zz;
  • if ai=1a_i=1, then remove a letter from the beginning of yy and append it to the end of zz.

Two merging sequences aa and bb are different if there is some position ii such that ai≠bia_i \neq b_i.

Let's call a string zz chaotic if for all ii from 22 to ∣z∣|z| zi−1≠ziz_{i-1} \neq z_i.

Let s[l,r]s[l,r] for some 1≤l≤r≤∣s∣1 \le l \le r \le |s| be a substring of consecutive letters of ss, starting from position ll and ending at position rr inclusive.

Let f(l1,r1,l2,r2)f(l_1, r_1, l_2, r_2) be the number of different merging sequences of x[l1,r1]x[l_1,r_1] and y[l2,r2]y[l_2,r_2] that produce chaotic merges. Note that only non-empty substrings of xx and yy are considered.

Calculate ∑1≤l1≤r1≤∣x∣1≤l2≤r2≤∣y∣f(l1,r1,l2,r2)\sum \limits_{1 \le l_1 \le r_1 \le |x| \\ 1 \le l_2 \le r_2 \le |y|} f(l_1, r_1, l_2, r_2). Output the answer modulo 998 244 353998\,244\,353.

给你两个字符串 xx 和 yy,它们均由小写拉丁字母组成。记 ∣s∣|s| 为字符串 ss 的长度。

我们称一个序列 aa 为合并序列,若它恰好包含 ∣x∣|x| 个 00 和 ∣y∣|y| 个 11(以某种顺序排列)。

由序列 aa 生成的合并串 zz 按如下规则构造:

  • 若 ai=0a_i = 0,则从 xx 的开头移除一个字符,并将其追加到 zz 的末尾;
  • 若 ai=1a_i = 1,则从 yy 的开头移除一个字符,并将其追加到 zz 的末尾。

两个合并序列 aa 和 bb 被称为不同,当且仅当存在某个位置 ii,使得 ai≠bia_i \neq b_i。

我们称一个字符串 zz 是混乱的(chaotic),若对所有 ii 满足 2≤i≤∣z∣2 \le i \le |z|,均有 zi−1≠ziz_{i-1} \neq z_i。

对任意满足 1≤l≤r≤∣s∣1 \le l \le r \le |s| 的整数 l,rl, r,记 s[l,r]s[l,r] 为字符串 ss 中从位置 ll 开始、到位置 rr 结束(含端点)的连续子串。

定义 f(l1,r1,l2,r2)f(l_1, r_1, l_2, r_2) 为:使 x[l1,r1]x[l_1,r_1] 与 y[l2,r2]y[l_2,r_2] 的合并结果为混乱串的不同合并序列的个数。注意:此处仅考虑 xx 和 yy 的非空子串。

请计算

∑1≤l1≤r1≤∣x∣1≤l2≤r2≤∣y∣f(l1,r1,l2,r2)\sum \limits_{1 \le l_1 \le r_1 \le |x| \\ 1 \le l_2 \le r_2 \le |y|} f(l_1, r_1, l_2, r_2)

并将结果对 998 244 353998\,244\,353 取模后输出。

输入格式

The first line contains a string xx (1≤∣x∣≤10001 \le |x| \le 1000).

The second line contains a string yy (1≤∣y∣≤10001 \le |y| \le 1000).

Both strings consist only of lowercase Latin letters.

第一行包含一个字符串 xx(1≤∣x∣≤10001 \le |x| \le 1000)。

第二行包含一个字符串 yy(1≤∣y∣≤10001 \le |y| \le 1000)。

两个字符串均由小写拉丁字母组成。

输出格式

Print a single integer — the sum of f(l1,r1,l2,r2)f(l_1, r_1, l_2, r_2) over 1≤l1≤r1≤∣x∣1 \le l_1 \le r_1 \le |x| and 1≤l2≤r2≤∣y∣1 \le l_2 \le r_2 \le |y| modulo 998 244 353998\,244\,353.

输出一个整数——对所有满足 1≤l1≤r1≤∣x∣1 \le l_1 \le r_1 \le |x| 和 1≤l2≤r2≤∣y∣1 \le l_2 \le r_2 \le |y| 的四元组 (l1,r1,l2,r2)(l_1, r_1, l_2, r_2),求 f(l1,r1,l2,r2)f(l_1, r_1, l_2, r_2) 的和,并对 998 244 353998\,244\,353 取模。

输入输出样例

  • 输入#1

    aaa
    bb

    输出#1

    24
  • 输入#2

    code
    forces

    输出#2

    1574
  • 输入#3

    aaaaa
    aaa

    输出#3

    0
  • 输入#4

    justamassivetesttocheck
    howwellyouhandlemodulooperations

    输出#4

    667387032

说明/提示

In the first example there are:

  • 66 pairs of substrings "a" and "b", each with valid merging sequences "01" and "10";
  • 33 pairs of substrings "a" and "bb", each with a valid merging sequence "101";
  • 44 pairs of substrings "aa" and "b", each with a valid merging sequence "010";
  • 22 pairs of substrings "aa" and "bb", each with valid merging sequences "0101" and "1010";
  • 22 pairs of substrings "aaa" and "b", each with no valid merging sequences;
  • 11 pair of substrings "aaa" and "bb" with a valid merging sequence "01010";

Thus, the answer is 6⋅2+3⋅1+4⋅1+2⋅2+2⋅0+1⋅1=246 \cdot 2 + 3 \cdot 1 + 4 \cdot 1 + 2 \cdot 2 + 2 \cdot 0 + 1 \cdot 1 = 24.

在第一个例子中,存在:

  • 66 对子串 "a" 和 "b",每对均有有效的合并序列 "01" 和 "10";
  • 33 对子串 "a" 和 "bb",每对均有一个有效的合并序列 "101";
  • 44 对子串 "aa" 和 "b",每对均有一个有效的合并序列 "010";
  • 22 对子串 "aa" 和 "bb",每对均有有效的合并序列 "0101" 和 "1010";
  • 22 对子串 "aaa" 和 "b",每对均无有效的合并序列;
  • 11 对子串 "aaa" 和 "bb",具有一个有效的合并序列 "01010"。

因此,答案为 6⋅2+3⋅1+4⋅1+2⋅2+2⋅0+1⋅1=246 \cdot 2 + 3 \cdot 1 + 4 \cdot 1 + 2 \cdot 2 + 2 \cdot 0 + 1 \cdot 1 = 24。

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

首页