CF794G.Replace All

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

Igor the analyst is at work. He learned about a feature in his text editor called "Replace All". Igor is too bored at work and thus he came up with the following problem:

Given two strings xx and yy which consist of the English letters 'A' and 'B' only, a pair of strings (s,t)(s,t) is called good if:

  • ss and tt consist of the characters '0' and '1' only.
  • 1<=s,t<=n1<=|s|,|t|<=n , where z|z| denotes the length of string zz , and nn is a fixed positive integer.
  • If we replace all occurrences of 'A' in xx and yy with the string ss , and replace all occurrences of 'B' in xx and yy with the string tt , then the two obtained from xx and yy strings are equal.

For example, if x=x= AAB, y=y= BB and n=4n=4 , then (01, 0101) is one of good pairs of strings, because both obtained after replacing strings are "01010101".

The flexibility of a pair of strings xx and yy is the number of pairs of good strings (s,t)(s,t) . The pairs are ordered, for example the pairs (( 0, 1 )) and (( 1, 0 )) are different.

You're given two strings cc and dd . They consist of characters 'A', 'B' and '?' only. Find the sum of flexibilities of all possible pairs of strings (c,d)(c',d') such that cc' and dd' can be obtained from cc and dd respectively by replacing the question marks with either 'A' or 'B', modulo 109+710^{9}+7 .

输入格式

The first line contains the string cc ( 1<=c<=31051<=|c|<=3·10^{5} ).

The second line contains the string dd ( 1<=d<=31051<=|d|<=3·10^{5} ).

The last line contains a single integer nn ( 1<=n<=31051<=n<=3·10^{5} ).

输出格式

Output a single integer: the answer to the problem, modulo 109+710^{9}+7 .

输入输出样例

  • 输入#1

    A?
    ?
    3
    

    输出#1

    2
    
  • 输入#2

    A
    B
    10
    

    输出#2

    2046
    

说明/提示

For the first sample, there are four possible pairs of (c,d)(c',d') .

If (c,d)=((c',d')=( AA ,, A )) , then the flexibility is 00 .

If (c,d)=((c',d')=( AB ,, A )) , then the flexibility is 00 .

If (c,d)=((c',d')=( AA ,, B )) , then the flexibility is 22 , as the pairs of binary strings (( 1 ,, 11 )) , (( 0 ,, 00 )) are the only good pairs.

If (c,d)=((c',d')=( AB ,, B )) , then the flexibility is 00 .

Thus, the total flexibility is 22 .

For the second sample, there are 21+22+...+210=20462^{1}+2^{2}+...+2^{10}=2046 possible binary strings of length not greater 1010 , and the set of pairs of good strings is precisely the set of pairs (s,s)(s,s) , where ss is a binary string of length not greater than 1010 .

首页