Problem:


Score : 400 points

Problem Statement

We have a board with a 2 \times N grid.
Snuke covered the board with N dominoes without overlaps.
Here, a domino can cover a 1 \times 2 or 2 \times 1 square.

Then, Snuke decided to paint these dominoes using three colors: red, cyan and green.
Two dominoes that are adjacent by side should be painted by different colors.
Here, it is not always necessary to use all three colors.

Find the number of such ways to paint the dominoes, modulo 1000000007.

The arrangement of the dominoes is given to you as two strings S_1 and S_2 in the following manner:

Each domino is represented by a different English letter (lowercase or uppercase).

The j-th character in S_i represents the domino that occupies the square at the i-th row from the top and j-th column from the left.

Constraints

1 \leq N \leq 52

|S_1| = |S_2| = N

S_1 and S_2 consist of lowercase and uppercase English letters.

S_1 and S_2 represent a valid arrangement of dominoes.

Input

Input is given from Standard Input in the following format:

N
S_1
S_2

Output

Print the number of such ways to paint the dominoes, modulo 1000000007.

Sample Input 1

3
aab
ccb

Sample Output 1

6

There are six ways as shown below:

Sample Input 2

1
Z
Z

Sample Output 2

3

Note that it is not always necessary to use all the colors.

Sample Input 3

52
RvvttdWIyyPPQFFZZssffEEkkaSSDKqcibbeYrhAljCCGGJppHHn
RLLwwdWIxxNNQUUXXVVMMooBBaggDKqcimmeYrhAljOOTTJuuzzn

Sample Output 3

958681902



Sample input:
3
aab
ccb


Sample output:
6

Submission status: Accepted

Solution code:
import sys
from heapq import heappush, heappop
def input(): return sys.stdin.readline().rstrip()
def ii(): return int(input())
def mi(): return map(int, input().split())
def li(): return list(mi())

mod = 10 **9 + 7
def main():
    n = ii()
    s = []
    s.append(input())
    s.append(input())
    if s[0][0] == s[1][0]:
        ans = 3
    else:
        ans = 6

    for i in range(1,n):
        if s[0][i-1] == s[1][i-1]:
            ans  *= 2
            ans %= mod
        else:
            if s[0][i] != s[0][i-1] and s[1][i] != s[1][i-1] and s[0][i] != s[1][i]:
                ans *= 3
                ans %= mod
    print(ans)
            

            





if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
S1 = input()
S2 = input()

if S1[0] == S2[0]:
    ans = 3
    t = 1
else:
    ans = 6
    t = 2 

for i in range(1, N):
    if S1[i] == S1[i-1]:
        continue
    if S1[i] == S2[i]:
        if t == 1:
            ans *= 2
        t = 1
    else:
        if t == 1:
            ans *= 2
        else:
            ans *= 3
        t = 2
    ans %= 10 ** 9 + 7

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S1=input()
S2=input()

m1=""
m=10**9+7
S1+="."
for i in range(len(S1)-1):
    if S1[i]==S1[i+1]:
        m1+='y'
    else:
        m1+='t'
m1 = m1.replace('yt','y')
score = [1]*len(m1)
for i in range(1,len(m1)):
    if m1[i-1]=='t' and m1[i]=='t':
        score[i]=2
    elif m1[i-1]=='y' and m1[i]=='t':
        score[i]=1
    elif m1[i-1]=='t' and m1[i]=='y':
        score[i]=2
    elif m1[i-1]=='y' and m1[i]=='y':
        score[i] = 3
if m1[0]=='y':
    score[0]=6
else:
    score[0]=3

ans = 1
for i in range(len(m1)):
    ans =(ans*score[i])%m
print(ans)

Submission status: Accepted

Solution code:
import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = float('inf')
MOD = 1000000007
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    N = I()
    S = [SS() for _ in range(2)]

    # ドミノの縦横を判別 0行目だけ見れば良い
    t = []
    i = 0
    if N == 1:
        t.append('v')
    else:
        while i + 1 < N:
            if S[0][i] == S[0][i+1]:
                t.append('h')
                i += 2
            else:
                t.append('v')
                i += 1
        if S[0][-2] != S[0][-1]:
            t.append('v')
    # print(t)

    ans = 1
    if t[0] == 'h':
        ans *= 6
    else:
        ans *= 3
    ans %= MOD
    for i in range(len(t) - 1):
        if t[i] == 'h':
            if t[i+1] == 'h':
                ans *= 3
            else:
                ans *= 1
        else:
            if t[i+1] == 'h':
                ans *= 2
            else:
                ans *= 2
        ans %= MOD
    
    print(ans)
                
if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
n = int(input())
s = input() + "@"
t = input() + "@"
mod = 10 ** 9 + 7

# 情報を1次元にして処理しやすくするぜ
# 縦で一致 True 横で一致 False
b = []
for i in range(n):
    if s[i] == s[i + 1]:
        continue
    b.append(s[i] == t[i])

# 処理するぜ
if b[0]:
    ans = 3
else:
    ans = 6

for i in range(1, len(b)):
    if b[i]:
        if b[i - 1]:
            ans *= 2
    else:
        if b[i - 1]:
            ans *= 2
        else:
            ans *= 3

print(ans % mod)

Submission status: Accepted

Solution code:
n = int(input())
s = input() + "@"
t = input() + "@"
mod = 10 ** 9 + 7

# 情報を1次元にして処理しやすくするぜ
# 縦で一致 True 横で一致 False
b = []
for i in range(n):
    if s[i] == s[i + 1]:
        continue
    b.append(s[i] == t[i])

# 処理するぜ
if b[0]:
    ans = 3
else:
    ans = 6

for i in range(1, len(b)):
    if b[i]:
        if b[i - 1]:
            ans *= 2
    else:
        if b[i - 1]:
            ans *= 2
        else:
            ans *= 3
    ans %= mod

print(ans)

Submission status: Accepted

Solution code:
from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf,comb
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
from functools import reduce
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n = INT()
s1 = input()
s2 = input()

ans = 3
i = 1
while i < n:
    if i == 1 or s1[i-1] != s1[i-2]:
        if i == n-1 or s1[i] != s1[i+1]:
            ans = ans * 2 % ( 10**9 + 7 )
            i += 1
        else:
            ans = ans * 2 % ( 10**9 + 7 )
            i += 2
    else:
        if i == n-1 or s1[i] != s1[i+1]:
            ans = ans * 1 % ( 10**9 + 7 )
            i += 1
        else:
            ans = ans * 3 % ( 10**9 + 7 )
            i += 2
print(ans)

Submission status: Accepted

Solution code:
mod = 1000000007
n = int(input())
s = input()
s2 = input()
a = [] if n > 1 else [1]
for i in range(n-1):
  if i != 0 and s[i-1] == s[i]:
    continue
  if s[i] == s[i+1]:
    a.append(2)
    if i == n-3:
      a.append(1)
  else:
    a.append(1)
    if i == n-2:
      a.append(1)
ans = 3 if a[0] == 1 else 6
for i in range(len(a)-1):
  b,c = a[i:i+2]
  if b == 1:
    ans = (ans * 2) % mod
  else:
    if c == 2:
      ans = (ans * 3) % mod
print(ans)

Submission status: Accepted

Solution code:
MOD = 10**9 + 7

N = int(input())
S1 = input()
S2 = input()

l = []
res = 1
while res < N:
    if S1[res-1] == S1[res]:
        l.append(2)
        res += 2
    else:
        l.append(1)
        res += 1
if res == N:
    l.append(1)

n = len(l)
if l[0] == 1:
    ans = 3
else:
    ans = 6

for i in range(1, n):
    if l[i] == 1 and l[i-1] == 1:
        ans = ans * 2 % MOD
    elif l[i] == 2 and l[i-1] == 1:
        ans = ans * 2 % MOD
    elif l[i] == 2 and l[i-1] == 2:
        ans = ans * 3 % MOD

print(ans)

Submission status: Accepted

Solution code:
import math


def read_int():
    return int(input().strip())


def read_ints():
    return list(map(int, input().strip().split(' ')))


def solve():
    N = read_int()
    s1 = input()
    s2 = input()
    P = []
    modulo = 10**9+7
    H, V = 0, 1
    for i in range(N):
        if i > 0 and s2[i] == s2[i-1]:
            P[-1] = H
        else:
            P.append(V)
    dp = []
    if P[0] == H:
        dp.append(6)
    else:
        dp.append(3)
    """
    A B     |C
    B A|C    A

    dp[i] = 3*dp[i-1] if P[i]=H&P[i-1]=H
            2*dp[i-1]    P[i]=H&P[i-1]=V
            2*dp[i-1]    P[i]=V&P[i-1]=V
            1*dp[i-1]    P[i]=V&P[i-1]=V
    """
    for i in range(1, len(P)):
        if P[i] == H and P[i-1] == H:
            dp.append(3*dp[i-1])
        elif P[i] == H and P[i-1] == V:
            dp.append(2*dp[i-1])
        elif P[i] == V and P[i-1] == V:
            dp.append(2*dp[i-1])
        else:
            dp.append(dp[i-1])
        dp[-1] %= modulo
    return dp[-1]


if __name__ == '__main__':
    print(solve())

Submission status: Accepted

Solution code:
n=int(input())
a=input()
b=input()
if a[0]==b[0]:
  c=3
  i=1
  d=0
else:
  c=6
  i=2
  d=1

while i<n:
  if a[i]==b[i]:
    if d==0:
      c=(c*2)%(10**9+7)
    else:
      d=0
  else:
    i+=1
    if d==0:
      c=(c*2)%(10**9+7)
      d=1
    else:
      c=(c*3)%(10**9+7)
  i+=1
print(c)

Submission status: Accepted

Solution code:
from itertools import groupby

N = int(input())
s = input()
S = input()
mod = 10 ** 9 + 7

s = [len(list(j)) for i, j in groupby(s)]

ans = 3 * s[0]

for i in range(1,len(s)):
    if s[i-1] == 1:
        ans *= 2
    elif s[i] == 2:
        ans *= 3
    ans %= mod

print(ans)

Submission status: Accepted

Solution code:
#coding: utf-8

N = int(input())
S = [list(input()) for _ in range(2)]

num  = 0

itr = zip(S[0], S[1])
c1, c2 = itr.__next__()

if c1 == c2:
    prev = 1 
    num = 3
else:
    prev = 2 
    num = 6
    itr.__next__()

for c1, c2 in itr:
    if c1 == c2:
        # vertical
        if prev == 1:
            num *= 2
            num %= 1000000007
        prev = 1 
    else:
        # horizonal
        if prev == 1:
            num *= 2
        else:
            num *= 3
        num %= 1000000007
        itr.__next__()
        prev = 2

print(num)

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/arc081/tasks/arc081_b
from collections import defaultdict
n = int(input())
s1 = input()
s2 = input()
ans = 0
mod = 10 ** 9 + 7

if s1[0] == s2[0]:
    i = 1
    ans += 3
    flag = False
else:
    i = 2
    ans += 6
    flag = True
while i < n:
    if flag:
        if s1[i] == s2[i]:
            flag = False
        else:
            ans *= 3 % mod
            i += 1
    else:
        ans *= 2 % mod
        if s1[i] != s2[i]:
            flag = True
            i += 1
    i += 1
print(ans % mod)

Submission status: Accepted

Solution code:
N=int(input())
s1=input()
s2=input()
ans=0
mod=10**9+7

if s1[0]==s2[0]:
    i=1
    ans+=3
    dual=False
else:
    i=2
    ans+=6
    dual=True
while i<N:
    if dual:
        if s1[i]==s2[i]:
            dual=False
        else:
            ans*=3
            ans%=mod
            i+=1
    else:
        ans*=2
        ans%=mod
        if s1[i]!=s2[i]:
            dual=True
            i+=1
    i+=1
print(ans%mod)

Submission status: Accepted

Solution code:
N = int(input())
S1 = input()
S2 = input()
mod = 10**9+7

ans = 1
if(S1[0] == S2[0]):
    ans *= 3
    ans %= mod
    
    idx = 1
else:
    ans *= 6
    ans %= mod
    idx = 2

while(idx <= N-1):
    if(S1[idx] == S2[idx]):
        if(S1[idx-1] == S2[idx-1]):
            ans *= 2
            ans %= mod
            idx += 1
        else:
            ans *= 1
            ans %= mod
            idx += 1
    else:
        if(S1[idx-1] == S2[idx-1]):
            ans *= 2
            ans %= mod
            idx += 2
        else:
            ans *= 3
            ans %= mod
            idx += 2
print(ans)

Submission status: Accepted

Solution code:
MOD = 10**9 + 7
n = int(input())
s1 = input()
s2 = input()
if s1[0] == s2[0]:
    end = 1
    count = 1
    ans = 3
else:
    end = 0
    count = 2
    ans = 6
while count < n:
    if end == 1:
        if s1[count] == s2[count]:
            ans *= 2
            ans %= MOD
            count += 1
        else:
            ans *= 2
            ans %= MOD
            count += 2
            end = 0
    else:
        if s1[count] == s2[count]:
            count += 1
            end = 1
        else:
            ans *= 3
            ans %= MOD
            count += 2
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
S = [list(input()) for _ in range(2)]
mod = 1000000007
if S[0][0]==S[1][0]:
    ans = 3
    i = 1
    typ = '2x1'
else:
    ans = 6
    i = 2
    typ = '2x2'
while i<n:
    if S[0][i]==S[1][i]:
        if typ=='2x1':
            ans *= 2
        typ = '2x1'
        i += 1
    else:
        if typ=='2x1':
            ans *= 2
        else:
            ans *= 3
        typ = '2x2'
        i += 2
    ans %= mod
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s1=input()
s2=input()
k=1
i=0
if s1[0]==s2[0]:
    i+=1
    k=3
else:
    i+=2
    k=6
mod=1000000007
while i<n:
    if s1[i]!=s2[i] and s1[i-1]!=s2[i-1]:
        k*=3
        i+=2
    elif s1[i]!=s2[i] and s1[i-1]==s2[i-1]:
        k*=2
        i+=2
    else:
        if s1[i-1]==s2[i-1]:
            k*=2
        i+=1
    k%=mod
print(k)

Submission status: Accepted

Solution code:
N = int(input())
S_1 = input()
S_2 = input()
MOD = 10**9 + 7

if N == 1:
    print(3)
    exit()

ans = 3 if S_1[0] == S_2[0] else 6

for i in range(1, N):
    if S_1[i-1] == S_1[i]:
        continue
    if S_1[i-1] == S_2[i-1]:
        ans = ans * 2 % MOD
    elif S_1[i] != S_2[i]:
        ans = ans * 3 % MOD
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
import sys
read = sys.stdin.read
readline = sys.stdin.readline

n, = map(int,readline().split())
s = input()
t = input()

MOD = 10**9+7
v = 2 # 初期状態
ans = 1
i = 0
while i < n:
    if s[i]==t[i]:
        i += 1
        if v==0:
            ans *= 2
        if v==2:
            ans *= 3
        v = 0
        ans %= MOD
    else:
        i += 2
        if v==0:
            ans *= 2
        if v==1:
            ans *= 3
        if v==2:
            ans *= 6
        v = 1
        ans %= MOD

print(ans%MOD)

Submission status: Accepted

Solution code:
N = int(input())
s1 = list(input())
s2 = list(input())

s = list(zip(s1, s2))
ss = []

skip = 0
for i in s:
    if skip:
        skip=0
        continue

    if i[0]!=i[1]:
        skip = 1
    ss.append(i)
# print(ss)


mod = 10**9+7
ans = 1
if ss[0][0]==ss[0][1]:
    f = 1
    ans *= 3
else:
    f = 0
    ans *= 6

#f = 1 : X
for i in range(1, len(ss)):
    if ss[i][0]==ss[i][1]:
        nf = 1
    else:
        nf = 0

    if f:
        ans *= 2
    else:
        if not nf:
            ans *= 3
    ans %= mod
    f = nf
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
t = input()
prev = -1
i = 0
ans = 1
mod = pow(10,9)+7
while i != n:
    if s[i] == t[i]:
        if prev == -1:
            ans *= 3
        elif prev == 0:
            ans *= 2
        prev = 0
        i += 1
    else:
        if prev == -1:
            ans *= 6
        elif prev == 0:
            ans *= 2
        else:
            ans *= 3
        prev = 1
        i += 2
    ans %= mod
print(ans)

Submission status: Accepted

Solution code:
import sys

N = int(sys.stdin.readline())
S = ["", ""]
S[0] = sys.stdin.readline().strip()
S[1] = sys.stdin.readline().strip()

mod = 10**9 + 7

pre = [S[0][0], S[1][0]]
ans = 3
if S[0][0] != S[1][0]:
    ans *= 2
# print(0, 1, ans)

for i in range(1, N):
    # 一つ前と同じ場合
    if pre[0] == S[0][i] and pre[1] == S[1][i]:
        continue

    if pre[0] == pre[1]:
        ans *= 2
        ans %= mod
        # 縦詰が連続
        # if S[0][i] == S[1][i]:
        #     ans *= 2
        #     ans %= mod
        # else:
        #     ans *= 2
        #     ans %= mod
    else:
        # 1パターンしかない
        if S[0][i] == S[1][i]:
            ans *= 1
        else:
            ans *= 3
            ans %= mod

    # print(ans)

    pre[0] = S[0][i]
    pre[1] = S[1][i]

print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    S = []
    S.append(input())
    S.append(input())
    MOD = 10**9 + 7
    ans = 1
    pre_mino_flag = -1 # vertical:0, horizontal:1
    pass_flag = False
    if S[0][0] == S[1][0]:
        ans *= 3
        pre_mino_flag = 0
    else:
        ans *= 6
        pre_mino_flag = 1
        pass_flag = True
    
    for i in range(1, N):
        if pass_flag:
            pass_flag = False
            continue
        
        if S[0][i] == S[1][i]:
            if pre_mino_flag == 0:
                ans *= 2
                ans %= MOD
            else:
                pass
            pre_mino_flag = 0
        else:
            pass_flag = True
            if pre_mino_flag == 0:
                ans *= 2
                ans %= MOD
            else:
                ans *= 3
                ans %= MOD
            pre_mino_flag = 1
            
    print(ans)



if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n = int(input())
s = [input() for _ in range(2)]
mod = 10 ** 9 + 7
""" 
たて*たて = *2
たて*よこ = *2
よこ*よこ = *3 (1,2)=(2,3)(2,1),(3,1)
よこ*たて = *1
"""
tmp = 1
pos = 0  # 移動させる
pre_state = -1  # 0:tate 1:yoko
while pos < n:
    if s[0][pos] == s[1][pos]:
        if pos == 0:
            tmp *= 3
        else:
            if pre_state == 0:
                tmp *= 2
            else:
                tmp *= 1
        pre_state = 0
        pos += 1
    else:
        if pos == 0:
            tmp *= 6
        else:
            if pre_state == 0:
                tmp *= 2
            else:
                tmp *= 3
        pre_state = 1
        pos += 2
    tmp %= mod
print(tmp)

Submission status: Accepted

Solution code:
n=int(input())
s1=list(input())
s2=list(input())
ans=0
if s1[0]==s2[0]:
    ans=3
else:
    ans=6
for i in range(n-1):
    if s1[i]==s2[i]:
        ans*=2
    else:
        if s1[i]==s1[i+1]:
            continue
        else:
            if s1[i+1]!=s2[i+1]:
                ans*=3
            else:
                ans*=1
print(ans%1000000007)

Submission status: Accepted

Solution code:
n=int(input())
s1=input()
s2=input()
mod=10**9+7

def rle(string):
    rle_str = string[0]
    rle_cnt = 1
    ans_l = []
    for i in range(1, len(string)):
        if rle_str == string[i]:
            rle_cnt += 1
        else:
            ans_l.append([rle_str, rle_cnt])
            rle_str = string[i]
            rle_cnt = 1
    ans_l.append([rle_str, rle_cnt])
    return ans_l

rle_s=rle(s1)

ans=rle_s[0][1]*3
left=rle_s[0][1]

for s, i in rle_s[1:]:
    if left==1 and i==1:
        ans*=2
    elif left==1 and i==2:
        ans*=2
    elif left==2 and i==1:
        ans*=1
    elif left==2 and i==2:
        ans*=3
    left=i

print(ans%mod)

Submission status: Accepted

Solution code:
n=int(input())
s1=input()
s2=input()
mod=10**9+7

def rle(string):
    rle_str = string[0]
    rle_cnt = 1
    ans_l = []
    for i in range(1, len(string)):
        if rle_str == string[i]:
            rle_cnt += 1
        else:
            ans_l.append([rle_str, rle_cnt])
            rle_str = string[i]
            rle_cnt = 1
    ans_l.append([rle_str, rle_cnt])
    return ans_l

rle_s=rle(s1)

if rle_s[0][1]==1:
    ans=3
    left=1
elif rle_s[0][1]==2:
    ans=6
    left=2

for s, i in rle_s[1:]:
    if left==1 and i==1:
        ans*=2
    elif left==1 and i==2:
        ans*=2
    elif left==2 and i==1:
        ans*=1
    elif left==2 and i==2:
        ans*=3
    left=i

print(ans%mod)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

from sys import stdin, setrecursionlimit


def main():
    mod = 10 ** 9 + 7
    input = stdin.buffer.readline
    n = int(input())
    s1 = list(input()[:-1].decode())
    s2 = list(input()[:-1].decode())
    ans = 0
    if s1[0] == s2[0]:
        ans = 3
    else:
        ans = 6
    for i in range(1, n):
        if s1[i - 1] == s2[i - 1]:
            if s1[i] == s2[i]:
                ans *= 2
            else:
                ans *= 2
            ans %= mod
        else:
            if s1[i - 1] == s1[i] and s2[i - 1] == s2[i]:
                ans *= 1
            elif s1[i] != s2[i]:
                ans *= 3
            else:
                ans *= 1
            ans %= mod
    print(ans % mod)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Submission status: Accepted

Solution code:
MOD = 10 ** 9 + 7
n = int(input())
s1 = input()
s2 = input()

if s1[0] == s2[0]:
    ans = 3
    pos = 1
else:
    ans = 6
    pos = 2

while pos < n:
    if s1[pos] == s2[pos]:
        if s1[pos - 1] == s2[pos - 1]:
            ans *= 2
            ans %= MOD
        pos += 1
    else:
        if s1[pos - 1] == s2[pos - 1]:
            ans *= 2
        else:
            ans *= 3
        pos += 2
        ans %= MOD
print(ans)

Submission status: Accepted

Solution code:
import sys
import itertools

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_nl = lambda: list(map(int, readline().split()))
in_na = lambda: map(int, read().split())
in_s = lambda: readline().rstrip().decode('utf-8')


def main():
    N = in_n()
    S = in_s()
    _ = in_s()

    mod = 1000000007

    gr = itertools.groupby(S)
    domino = []
    for k, v in gr:
        domino.append(len(list(v)))

    if domino[0] == 1:
        ans = 3
    else:
        ans = 6

    for i in range(len(domino) - 1):
        if domino[i] == 1 and domino[i + 1] == 1:
            ans *= 2
        elif domino[i] == 1 and domino[i + 1] == 2:
            ans *= 2
        elif domino[i] == 2 and domino[i + 1] == 1:
            pass
        elif domino[i] == 2 and domino[i + 1] == 2:
            ans *= 3
        ans %= mod

    print(ans % mod)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
s1=input()
s2=input()

mod=10**9+7

if s1[0]==s2[0]:
  bef=0
  inx=1
  ans=3
else:
  bef=1
  inx=2
  ans=6

while inx<n:
  if s1[inx]==s2[inx]:
    if bef==0:
      ans*=2
      ans%=mod
    bef=0
    inx+=1
  else:
    if bef==1:
      ans*=3
    else:
      ans*=2
    ans%=mod
    bef=1
    inx+=2

print(ans)

Submission status: Accepted

Solution code:
N, S1, S2 = open(0)
N = int(N)
m = 10**9+7

i = 0
dominos = []
while i < N:
    if S1[i] == S2[i]:# 縦のドミノ
        dominos.append(0)
        i += 1
    else:
        dominos.append(1)
        i += 2

ans = 3
for i in range(len(dominos)):
    if dominos[i] == 0:
        if i > 0 and dominos[i-1] == dominos[i]:
            ans = ans * 2 % m
    else:
        if i > 0 and dominos[i-1] == dominos[i]:
            ans = ans * 3 % m
        else:
            ans = ans * 2 % m
print(ans)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N = int(input())
mod = 1000000007
s1 = list(input())
s2 = list(input())

idx = 0
before = -1
ans = 1
while idx<N:
    if s1[idx]==s2[idx]:
        if before==-1:
            ans *= 3
            ans %= mod
        elif before==1:
            ans *= 2
            ans %= mod
        else:
            pass
        before = 1
        idx += 1
    else:
        if before==-1:
            ans *= 6
            ans %= mod
        elif before==1:
            ans *= 2
            ans %= mod
        else:
            ans *= 3
            ans %= mod
        before = 2
        idx += 2
print(ans)

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
#mod = 998244353
INF = 10**18
eps = 10**-7

N = int(readline())
S1 = readline().rstrip()
S2 = readline().rstrip()

if S1[0] == S2[0]:
    passflag = 0
    bef = 1
    ans = 3
else:
    passflag = 1
    bef = 0
    ans = 6

for i in range(1,N):
    if passflag:
        passflag = 0
        continue
    if bef:
        if S1[i] == S2[i]:
            ans = ans*2%mod
            passflag = 0
            bef = 1
        else:
            ans = ans*2%mod
            passflag = 1
            bef = 0
    else:
        if S1[i] == S2[i]:
            passflag = 0
            bef = 1
        else:
            ans = ans*3%mod
            passflag = 1
            bef = 0
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s1=input()
s2=input()
MOD=10**9+7
flag=0
i=0
ans=1
while i<n:
  if s1[i]==s2[i]:
    if flag==0:
      ans=3
    if flag==1:
      ans=ans*2%MOD
    flag=1
    i+=1
  else:
    if flag==0:
      ans=6
    if flag==1:
      ans=ans*2%MOD
    if flag==-1:
      ans=ans*3%MOD
    flag=-1
    i+=2
print(ans)

Submission status: Accepted

Solution code:
p = 10**9+7
N = int(input())
S1 = input().strip()
S2 = input().strip()
cur = 0
while cur<N:
    if cur==0:
        if S1[cur]==S2[cur]:
            cnt = 3
            cur += 1
        else:
            cnt = 6
            cur += 2
    else:
        if S1[cur]==S2[cur]:
            if S1[cur-1]==S2[cur-1]:
                cnt = (cnt*2)%p
            cur += 1
        else:
            if S1[cur-1]==S2[cur-1]:
                cnt = (cnt*2)%p
            else:
                cnt = (cnt*3)%p
            cur += 2
print(cnt)

Submission status: Accepted

Solution code:
MOD = pow(10,9)+7
N = int(input())
S = str(input())
T = str(input())
if N == 1:
  print(3)
  exit()
now = 0
if S[now] != T[now]: #横
  now = 2
  ans = 6
  Flag = True
else: #縦
  now = 1
  ans = 3
  Flag = False

while now <= N-1:
  if S[now] != T[now]: #横
    if Flag: #前が横
      ans = 3*ans%MOD
    else: #前が縦
      ans = 2*ans%MOD
    Flag = True
    now += 2
  else: #縦
    if Flag: #前が横
      ans *= 1
    else: #前が縦
      ans = 2*ans%MOD
    Flag = False
    now += 1
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
S=[input() for i in range(2)]
P=1
mod=10**9+7
B=True
if S[0][0]==S[1][0]:
  P=3
else:
  P=6
  B=False
for i in range(1,N):
  if B:
    if S[0][i]==S[1][i]:
      if S[0][i-1]==S[1][i-1]:
        P=P*2%mod
    else:
      B=False
      if S[0][i-1]==S[1][i-1]:
        P=P*2%mod
      else:
        P=P*3%mod
  else:
    B=True
print(P)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
T = input()
MOD = 10**9+7

ans = 3
if S[0] != T[0]:
    ans *= 2
for s1,t1,s2,t2 in zip(S,T,S[1:],T[1:]):
    if s1==s2 and t1==t2: continue
    if s1==t1:
        if s2==t2:
            ans *= 2
        else:
            ans *= 2
    else:
        if s2==t2:
            ans *= 1
        else:
            ans *= 3
print(ans%MOD)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
t = input()
mod = 10**9+7
if s[0]==t[0]:
  cou = 3
  start = 1
else:
  cou = 6
  start = 2
second = False
for i in range(start,n):
  if second:
    second = False
    continue
  else:
    if s[i]==t[i]:
      if s[i-1]==t[i-1]:
        cou *= 2
        cou %= mod
        
    else:
      if s[i-1]==t[i-1]:
        cou *= 2
        cou %= mod
        second = True
      else:
        cou *= 3
        cou %= mod
        second = True
        
 # print(i,cou)
print(cou)

Submission status: Accepted

Solution code:
MOD = 10**9 + 7
n = int(input())
S1 = input()
S2 = input()
A = []  # 縦かどうかboolean
tmp = ""
i = 0
while i < n:
    if S1[i] == S2[i]:
        A.append(True)
        i += 1
    else:
        A.append(False)
        i += 2

l = len(A)
dp = [[0] * 2 for _ in range(l)]
if A[0]:
    dp[0][True] = 3
else:
    dp[0][False] = 6

for i in range(1, l):
    if A[i - 1]:
        if A[i]:
            dp[i][True] = dp[i - 1][True] * 2 % MOD
        else:
            dp[i][False] = dp[i - 1][True] * 2 % MOD
    else:
        if A[i]:
            dp[i][True] = dp[i - 1][False]
        else:
            dp[i][False] = dp[i - 1][False] * 3 % MOD

print(sum(dp[l - 1]) % MOD)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
T = input()
mod = 10**9+7
lis = []
ans = 1
if N==1:
  print(3)
  exit()
if S[0]!=S[1]:
  lis.append(1)
for i in range(1,N-1):
  if S[i]!=S[i-1] and S[i]!=S[i+1]:
    lis.append(1)
  elif S[i]==S[i-1]:
    lis.append(2)
if S[-1]==S[-2]:
  lis.append(2)
else:
  lis.append(1)
ans = 3
if lis[0]==2:
  ans *= 2
for i in range(1,len(lis)):
  if lis[i-1]==1:
    ans *= 2
    ans %= mod
  elif lis[i]==2:
    ans *= 3
    ans %= mod
print(ans)

Submission status: Accepted

Solution code:
p = 10**9+7
N = int(input())
S1 = input().strip()
S2 = input().strip()
cnt = 1
cur = 0
if S1[0]==S2[0]:
    cnt = 3
    cur += 1
else:
    cnt = 6
    cur += 2
while cur<N:
    if S1[cur]==S2[cur]:
        if S1[cur-1]==S2[cur-1]:
            cnt = (cnt*2)%p
        cur += 1
    else:
        if S1[cur-1]==S2[cur-1]:
            cnt = (cnt*2)%p
        else:
            cnt = (cnt*3)%p
        cur += 2
print(cnt)

Submission status: Accepted

Solution code:
mod = 10**9 + 7
n = int(input())
s1 = input()
s2 = input()

if s1[0] == s2[0]:
    idx = 1
    ans = 3
    tate = 1
else:
    idx = 2
    ans = 6
    tate = 0

while idx < n:
    if s1[idx] == s2[idx]:
        if tate:
            ans *= 2
            ans %= mod
        tate = 1
        idx += 1
    else:
        if tate:
            ans *= 2
            ans %= mod
        else:
            ans *= 3
            ans %= mod
        tate = 0
        idx += 2

print(ans)

Submission status: Accepted

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n = int(input())
s1 = input()
s2 = input()
ans = 1
M = 10**9+7
if n==1:
    ans = 3
else:
    p = []
    prev = s1[0]
    count = 1
    for c in s1[1:]:
        if prev==c:
            count += 1
        else:
            p.append(count)
            count = 1
        prev = c
    p.append(count)
    if p[0]==1:
        ans = 3
    else:
        ans = 3*2
    prev = p[0]
    for num in p[1:]:
        if num==1:
            if prev==1:
                ans *= 2
            else:
                ans *= 1
        else:
            if prev==2:
                ans *= 3
            else:
                ans *= 2
        prev = num
        ans %= M
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S1 = input()
S2 = input()
MOD = 1_000_000_007
i = 0
directions = []
while i < N:
    if S1[i] == S2[i]:
        directions.append("V")
        i += 1
    else:
        directions.append("H")
        i += 2
prev = directions[0]
ans = 3 if prev == "V" else 6
for d in directions[1:]:
    if prev == "V" and d == "V" or prev == "V" and d == "H":
        ans *= 2
    elif prev == "H" and d == "H":
        ans *= 3
    ans %= MOD
    prev = d
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
mod=10**9+7
if n==1:
    print(3)
    exit()

s1=list(input())
s2=list(input())
x=[]
i=0
while i<n-1:
    if s1[i]==s1[i+1]:
        x.append(2)
        i+=2
    else:
        x.append(1)
        i+=1
if s1[-1]!=s1[-2]:
    x.append(1)

for i in range(len(x)):
    if i==0 and x[0]==1:
        ans=3
    elif i==0 and x[0]==2:
        ans=6
    else:
        if x[i-1]==1 and x[i]==2:
            ans*=2
        elif x[i-1]==2 and x[i]==2:
            ans*=3
        elif x[i-1]==1 and x[i]==1:
            ans*=2
        ans%=mod
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=input()
b=input()
m=10**9+7
if n==1:exit(print(3))
c=3*(2-(a[0]==b[0]))
for i in range(1,n):
 if a[i-1]==a[i]:continue
 if a[i-1]==b[i-1]:c*=2;c%=m
 elif a[i]!=b[i]:c*=3;c%=m
print(c)