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: Wrong Answer

Solution code:
import sys

N=int(raw_input())
S=[]
for n in xrange(2):
    S.append(raw_input().strip())

M=1000000007
if N==1:
    cnt = 3
else:
    if S[0][0]==S[1][0]:
        cnt = 3
        i=1
        ant=1
    else:
        cnt = 6
        i = 2
        ant = 0
    while i<N:
        if S[0][i]==S[1][i]:
            if ant==1:
                cnt = (cnt +cnt)%M
            else:
                ant=1
            i += 1
        else:
            if ant==0:
                cnt = (cnt *3)%M
            else:
                cnt = (cnt + cnt)%M
                ant = 1
            i += 2
print cnt

Submission status: Accepted

Solution code:
n = int(input())
s1 = input()
s2 = input()
mod = 10 ** 9 + 7
s1 += "1"
state = 0
res = 1
i = 0
while i < n:
    if s1[i] == s1[i + 1]:
        if state == 0:
            res = res * 6 % mod
        elif state == 1:
            res = res * 2 % mod
        else:
            res = res * 3 % mod
        state = 2
        i += 2
    else:
        if state == 0:
            res = res * 3 % mod
        elif state == 1:
            res = res * 2 % mod
        else:
            res = res * 1 % mod
        state = 1
        i += 1

print(res % mod)

Submission status: Accepted

Solution code:
def main():
	N = int(input())
	S1 = input()
	S2 = input()
	p = 1000000007
	dominoes = []
	pattern = 0
	i = 0
	while i < N:
		if S1[i] == S2[i]:
			if len(dominoes) == 0:
				pattern = 3
			elif dominoes[-1] == "|":
				pattern = (pattern * 2) % p
			elif dominoes[-1] == "-":
				pattern = (pattern * 1) % p
			dominoes.append("|")
			i += 1
		else:
			if len(dominoes) == 0:
				pattern = 6
			elif dominoes[-1] == "|":
				pattern = (pattern * 2) % p
			elif dominoes[-1] == "-":
				pattern = (pattern * 3) % p
			dominoes.append("-")
			i += 2
	print(pattern)

main()

Submission status: Accepted

Solution code:
MOD=1000000007
N=int(input())
S=[input() for _ in range(2)]
i=0
ans=1
flag=True
if S[0][0]==S[1][0]:
    ans=3
    i+=1
else:
    ans=6
    i+=2
    flag=False
while i<N:
    if S[0][i]==S[1][i]:
        if flag==True:
            ans*=2
            ans%=MOD
        i+=1
        flag=True
    else:
        if flag:
            ans*=2
        else:
            ans*=3
        ans%=MOD
        i+=2
        flag=False
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(input())
B = list(input())

p = 10**9 + 7

domino = []
d_=''
for d in range(len(A)):
    d_0 = A[d]
    d_1 = B[d]
    if d_ == d_0:
        continue
        
    if d_0 == d_1:
        domino.append(0) # 0:縦
    if d_0 != d_1:
        domino.append(1) # 1:横
    d_ = d_0

ans = 3
if domino[0] == 1:
    ans = 6
    
for d_0,d_1 in zip(domino[:-1],domino[1:]):
    if d_0 == 0:
        ans = ans*2
    elif d_0 == 1:
        if d_1 == 0:
            pass
        else:
            ans = ans*3
    ans = ans % p
ans = ans % p 
print(ans)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time, copy,bisect
from operator import itemgetter

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

stdin = sys.stdin

ni = lambda: int(ns())
nf = lambda: float(ns())
na = lambda: list(map(int, stdin.readline().split()))
nb = lambda: list(map(float, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

N = ni()
S = [ns() for _ in range(2)]
ct = [0] * N
if S[0][0] == S[1][0]:
    ct[0] = 3
else:
    ct[0] = 6

for i in range(N-1):
    if S[0][i] == S[1][i]:
        if S[0][i+1] == S[1][i+1] and S[0][i] == S[0][i+1]:
            ct[i+1] = ct[i]
        else:
            ct[i+1] = (ct[i] * 2) % mod
    else:
        if S[0][i] != S[0][i+1] and S[1][i] != S[1][i+1] and S[0][i+1] != S[1][i+1]:
            ct[i+1] = (ct[i] * 3) % mod
        else:
            ct[i+1] = ct[i]
print(ct[N-1])

Submission status: Accepted

Solution code:
mod = 1000000007
with open(0) as f:
    N, s, t = f.read().split()

#ドミノの並べ方を0,1で整理
domino = []
s += '_'
c = s[0]
lying = False
for x in s[1:]:
    if x == c:
        lying = True
        continue

    if lying:
        domino.append(1)
    else:
        domino.append(0)
    c = x
    lying = False

#初期値
if domino[0] == 0:
    ans = 3
else:
    ans = 6
#右へ走査
for idx,d in enumerate(domino[1:]):
    if domino[idx] == 0:
        ans *= 2
    else:
        if d == 0:
            ans *= 1
        else:
            ans *= 3
    ans %= mod
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
hw=[]
i=0
while i<N:
  if i+1<N and S[i]==S[i+1]:
    hw.append(2)
    i+=2
  else:
    hw.append(1)
    i+=1
mod=10**9+7
ans=0
for i in range(len(hw)):
  if i==0:
    if hw[i]==1:
      ans=3
    else:
      ans=6
  if i!=0:
    if hw[i]==1 and hw[i-1]==1:
      ans*=2
      ans%=mod
    if hw[i]==1 and hw[i-1]==2:
      ans*=1
    if hw[i]==2 and hw[i-1]==1:
      ans*=2
      ans%=mod
    if hw[i]==2 and hw[i-1]==2:
      ans*=3
      ans%=mod

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S1 = list(input())
S2 = list(input())
i = 0
ans = 1
lis = []
while i < N:
    if S1[i] == S2[i]:
        lis.append(0)
        if(i == 0):
            ans *= 3
        else:
            if lis[-2] == 0:
                ans *= 2
        i += 1
    else:
        lis.append(1)
        if(i == 0):
            ans *= 6
        else:
            if lis[-2] == 0:
                ans *= 2
            else:
                ans *= 3
        i += 2
    ans %= 10 ** 9 + 7
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
s1 = str(input())
s2 = str(input())
mod = 10**9+7
if s1[0] == s2[0]:
  c = 3
  state = True
  i = 1
else:
  c = 6
  state = False
  i = 2
while i < n:
  if s1[i] == s2[i]:
    i += 1
    if state:
      c *= 2
    else:
      state = True
  else:
    i += 2
    if state:
      c *= 2
      state = False
    else:
      c *= 3
print(c%(mod))

Submission status: Accepted

Solution code:
mod = 1000000007

n = int(input())
s1 = input()
s2 = input()

if s1[0] == s2[0]:
    ans = 3
    now = 1
    flag = True
else:
    ans = 6
    now = 2
    flag = False

while now < n:
    if flag:
        if s1[now] == s2[now]:
            ans *= 2
            ans %= mod
            now += 1
            flag = True
        else:
            ans *= 2
            ans %= mod
            now += 2
            flag = False
    else:
        if s1[now] == s2[now]:
            now += 1
            flag = True
        else:
            ans *= 3
            ans %= mod
            now += 2
            flag = False

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
s1=list(input())
s2=list(input())
i=0
beflag=0        #0…前は何もない(先頭)、1…前は横ミノ×2、2…前は縦ミノ×1
ans=1
p=10**9+7
while i<N:
    if s1[i]==s2[i]:        #もし縦ミノなら
        if beflag==0:
            ans=(ans*3)%p
        elif beflag==1:
            ans=ans
        elif beflag==2:
            ans=(ans*2)%p
        beflag=2
        i=i+1
    else:                   #それ以外は横ミノ*2を保証するはず
        if beflag==0:
            ans=(ans*6)%p
        elif beflag==1:
            ans=(ans*3)%p
        elif beflag==2:
            ans=(ans*2)%p
        beflag=1
        i=i+2
    #print(i,ans,N)
print(ans)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
     
def main():
    N = I()
    s1 = S()
    s2 = S()
    seq = ''
    i = 0
    while i < N:
        if s1[i] == s2[i]:
            seq += 'v'
            i += 1
        else:
            seq += 'h'
            i += 2
    if seq[0] == 'v':
        ans = 3
    else:
        ans = 6

    prev = seq[0]
    for i in range(1, len(seq)):
        cur = seq[i]
        if prev == 'v' and cur == 'h':
            ans *= 2
        if prev == 'v' and cur == 'v':
            ans *= 2
        if prev == 'h' and cur == 'h':
            ans *= 3
        if prev == 'h' and cur == 'v':
            ans *= 1
        prev = cur
        ans %= mod
    print(ans)






main()

Submission status: Accepted

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

flag = [False]*N
if S[0] == T[0]:
    ct = 3
    flag[0] = True
    i = 1
else:
    ct = 6
    i = 2
while i < N:
    if S[i] == T[i]:
        flag[i] = True
        if flag[i-1]:
            ct *= 2
        i += 1
    else:
        if flag[i-1]:
            ct *= 2
        else:
            ct *= 3
        i += 2
    ct = ct % C
    # print(i,ct)
print(ct)

Submission status: Accepted

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

for n in range(1,N):
  if S1[n-1]==S2[n-1] or n==1:
    ans*=2
  elif S1[n-1]!=S1[n] and S1[n]!=S2[n]:
    ans*=3

print(ans%(10**9+7))

Submission status: Accepted

Solution code:
""" ABC071D
"""

N = int(input())
S = [input() for _ in range(2)]
C = [[0]*N for _ in range(2)]
MOD = 1000000007
pos = 0
ans = 0
if S[0][pos] == S[1][pos]:
    ans = 3
else:
    ans = 6
    pos += 1

while 1:
    if pos >= N:
        break
    if S[0][pos] == S[1][pos]:
        pos += 1
        if pos >= N:
            break
        if S[0][pos] == S[1][pos]:
            ans *= 2
            ans %= MOD
            
        else:
            ans *= 2
            ans %= MOD
            pos += 1
    else:
        pos += 1
        if pos >= N:
            break
        if S[0][pos] == S[1][pos]:
            continue
        else:
            ans *= 3
            ans %= MOD
            pos += 1

print(ans % MOD)

Submission status: Accepted

Solution code:
N = int(input())
s = input()
c = input()

#データの加工
d = 0 #d=1:横向き, d=0:縦向き
domino = []
pre = s[0]
s += '_'
for x in s[1:]:
    if x == pre:
        d += 1
    else:
        domino.append(d)
        d = 0
    pre = x

#初期値（最初のドミノが横向きか縦向きか）
if domino[0] == 1: ans = 6
else: ans = 3

prex = domino[0]
for x in domino[1:]:
    if prex == 0: ans *= 2
    elif x == 1: ans *= 3
    #elif x == 0: ans *= 1
    prex = x
ans %= 1000000007
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = [list(input()), list(input())]

MOD = 1000000007

dp = [0]*n
if s[0][0] == s[1][0]: # tate
    dp[0] = 3
else: # yoko
    dp[0] = 6

for i in range(1,n):
    if s[0][i] == s[0][i-1]:
        dp[i] = dp[i-1]
    elif s[0][i-1] == s[1][i-1] and s[0][i] == s[1][i]: # tate & tate
        dp[i] = dp[i-1] * 2 % MOD
    elif s[0][i-1] == s[1][i-1] and s[0][i] != s[1][i]: # tate & yoko
        dp[i] = dp[i-1] * 2 % MOD
    elif s[0][i-1] != s[1][i-1] and s[0][i] == s[1][i]: # yoko & tate
        dp[i] = dp[i-1] 
    else: # yoko & yoko
        dp[i] = dp[i-1] * 3 % MOD
print(dp[n-1])

Submission status: Accepted

Solution code:

n=int(input())
s1=input()
s2=input()

mod=10**9+7

if s1[0]==s2[0]:
    nn=0
    istat="tate"
    icnt=3
elif s1[0]!=s2[0]:
    nn=1    
    istat="yoko"
    icnt=6

while nn<n-1:
    nn+=1
    if s1[nn]==s2[nn]:
        if istat=="tate":
            icnt=(icnt*2)%mod
        elif istat=="yoko":
            icnt=icnt
        istat="tate"
    elif s1[nn]!=s2[nn]:
        nn+=1
        if istat=="tate":
            icnt=(icnt*2)%mod
        elif istat=="yoko":
            icnt=(icnt*3)%mod
        istat="yoko"

print(icnt)

Submission status: Accepted

Solution code:
n = int(input())
s1 = list(input())
s2 = list(input())
if s1[0] == s2[0]:
    ans = 3
    i = 1
    flg = "x"
else:
    ans = 6
    i = 2
    flg = "y"
while i < n:
    if s1[i] == s2[i]:
        if flg == "x":
            ans *= 2
        else:
            flg = "x"
        i += 1
    else:
        if flg == "x":
            ans *= 2
            flg = "y"
        else:
            ans *= 3
        i += 2
    ans %= 10 ** 9 + 7
print(ans)

Submission status: Accepted

Solution code:
MOD=1000000007

N = int(input())
a = list(input())
b = list(input())

ans = 1
pre = 0
i=0
if a[i]!=b[i]:
    ans = 6
    i+=2
    pre = 2
else:
    ans = 3
    i+=1
    pre = 1

while i < N:
    if a[i]!=b[i]:
        if pre==1:
            ans = ans*2
            ans = ans%MOD
            i+=2
            pre = 2
        else:
            ans = ans*3
            ans = ans%MOD
            i+=2
            pre = 2
    else:
        ans = ans*(3-pre)
        ans = ans%MOD
        i+=1
        pre = 1

print(ans)

Submission status: Accepted

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

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

yoko_flag = False
domino = []
for i in range(n-1):
    if s1[i+1] == s1[i]:  # yoko
        yoko_flag = True
        domino.append("yoko")
    elif s1[i+1] != s1[i] and yoko_flag:
        yoko_flag = False
    elif s1[i+1] != s1[i] and not yoko_flag:  # tate
        domino.append("tate")
    if i == n-2:
        if s1[i+1] != s1[i]:
            domino.append("tate")
#print(domino)
len_domino = len(domino)
pre_domino = domino[0]
if domino[0] == "yoko":
    ans = 6
else:
    ans = 3
for i in range(1, len_domino):
    if domino[i] == "tate" and pre_domino == "tate":
        ans = ans*2 % mod
        pre_domino = domino[i]
    elif domino[i] == "tate" and pre_domino == "yoko":
        ans = ans*1 % mod
        pre_domino = domino[i]
    elif domino[i] == "yoko" and pre_domino == "tate":
        ans = ans*2 % mod
        pre_domino = domino[i]
    elif domino[i] == "yoko" and pre_domino == "yoko":
        ans = ans*3 % mod
        pre_domino = domino[i]
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict,deque
import math
def main():
    n = int(input())
    s1 = input()
    s2 = input()
    s = ""
    i=0
    while(i<n):
        if(s1[i]==s2[i]):
            s+="|"
            i+=1
        else:
            s+="="
            i+=2
        if(i>=n):
            break
    res = 0
    if(s[0]=="|"):
        res = 3
    else:
        res = 6
    MOD = 10**9 + 7
    for i in range(1,len(s)):
        if(s[i-1]=="|" and s[i]=="|"):
            res *= 2
        elif(s[i-1]=="|" and s[i]=="="):
            res *= 2
        elif(s[i-1]=="=" and s[i]=="|"):
            res *= 1
        else:
            res *= 3

        res%=MOD
    print(res)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
s = [list(input()) for _ in range(2)]
mod = 10**9+7
ans = 1
i = 0
pre = -1
while i < N:
  if s[0][i] == s[1][i]: # 縦
    if pre == -1:
      ans *= 3
    elif pre == 0:
      ans *= 1
    elif pre == 1:
      ans *= 2

    ans %= mod
    pre = 1
    i += 1
  elif s[0][i] == s[0][i+1]: # 横
    if pre == -1:
      ans *= 6
    elif pre == 0:
      ans *= 3
    elif pre == 1:
      ans *= 2
    
    ans %= mod
    pre = 0
    i += 2
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
t = input()

ans = 1
mod = 10**9+7
bef = True
skip = -1
for i in range(n):
  if skip == i:
      continue
  if i == 0:
    if s[i] == t[i]:
      ans *= 3
    else:
      ans *= 6
  elif bef:
    ans *= 2
  else:
    if s[i] != t[i]:
      ans *= 3
  if s[i] == t[i]:
    bef = True
  else:
    bef = False
    skip = i+1
  ans %= mod
print(ans)

Submission status: Accepted

Solution code:
MOD = 10**9 + 7
N = int(input())
S1 = input()
S2 = input()
check = []
index = 0
while index < N:
    if S1[index] == S2[index]:
        check.append(1)
        index += 1
    else:
        check.append(2)
        index += 2
ans = 3 if check[0] == 1 else 6
for i in range(1,len(check)):
    if check[i-1] == 1:
        ans *= 2
    elif check[i] == 2:
        ans *= 3
    ans %= MOD
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s1=input()
s2=input()
mod=1000000007
t=0
flg=True
ans=1
while t<n:
    if t==0:
        if s1[t]==s2[t]:
            t+=1
            ans*=3
        else:
            t+=2
            ans*=6
            flg=False
    elif flg and s1[t]==s2[t]:
        ans*=2
        ans%=mod
        t+=1
    elif flg and s1[t]!=s2[t]:
        t+=2
        ans*=2
        ans%=mod
        flg=False
    elif flg==False and s1[t]==s2[t]:
        flg=True
        t+=1
    elif flg==False and s1[t]!=s2[t]:
        t+=2
        ans*=3
        ans%=mod
print(ans%mod)

Submission status: Accepted

Solution code:
n=int(input())
L=input()+'?'
S=input()
T=[]
i=0


while n-1>=i:
	if L[i]==L[i+1]:
		T.append(0)
		i+=2
	else:
		T.append(1)
		i+=1

        
def f(x,y):
	if x==0 and y==1:
		return 1
	if x==0 and y==0:
		return 3
	if x==1 and y==1:
		return 2
	if x==1 and y==0:
		return 2
if T[0]==0:
	ans=6
else:
	ans=3
mod = 10**9 + 7
for i in range(len(T)-1):
	ans=ans*f(T[i],T[i+1])
	ans=ans%mod
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s1 = input()
s2 = input()
i = 0
ans = 1 
while i < len(s1):
	if s1[i] == s2[i] and i == 0:
		ans = 3
		i += 1
		continue
	if s1[i] != s2[i] and i == 0:
		ans = 6
		i += 2
		continue
	if s1[i] != s2[i] and i >= 1 and s1[i-1] == s2[i-1]:
		ans = (ans*2)%(pow(10,9) + 7)
		i += 2
		continue
	if s1[i] != s2[i] and i >= 1 and s1[i-1] != s2[i-1]:
		ans = (ans*3)%(pow(10,9) + 7)
		i += 2
		continue
	if s1[i] == s2[i] and i >= 1 and s1[i-1] != s2[i-1]:
		i += 1
		continue
	if s1[i] == s2[i] and i >= 1 and s1[i-1] == s2[i-1]:
		ans = (ans*2)%(pow(10,9) + 7)
		i += 1
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

D = defaultdict(int)

mod = 10**9 + 7

for x in S1:
  D[x] += 1

def cnt(x, y):
  if x == 1 and y == 1:
    return 2
  elif x == 1 and y == 2:
    return 2
  elif x == 2 and y == 1:
    return 1
  else:
    return 3

L = list(D.values())
n = len(L)

ans = 1
if L[0] == 1:
  ans *= 3
else:
  ans *= 6

for i in range(1, n):
  ans *= cnt(L[i-1], L[i])
  ans %= mod

print(ans)

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:
import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())

N = int(input())
S = [0] * 2
S[0] = input()
S[1] = input()

ans = 1
prev = 1 # 1 = vertical, 2 = horizontal
i = 0
if S[0][0] == S[1][0]:
    ans *= 3
    i += 1
    prev = 1
else:
    ans *= 6
    i += 2
    prev = 2

while i < N:
    if S[0][i] == S[1][i]:
        if prev == 1:
            ans *= 2 
        else:
            ans *= 1
        i += 1
        prev = 1
    else:
        if prev == 2:
            ans *= 3
        else:
            ans *= 2 
        i += 2
        prev = 2
    ans %= MOD
print(ans)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
mod = 10**9+7

def main():
    N = int(input())
    S1 = input()
    S2 = input()
    cut = []
    pre = None
    for i, c in enumerate(S1):
        if c != pre: cut.append(i)
        pre = c
    cut.append(N)
    ans = 1
    l = len(cut)
    pre = True  # vertical domino
    for i in range(l - 1):
        if i == 0 and cut[1] - cut[0] == 1:
            ans = 3
            pre = True
            continue
        if i == 0 and cut[1] - cut[0] == 2:
            ans = 6
            pre = False
            continue
        if pre and cut[i + 1] - cut[i] == 1:
            ans *= 2
            pre = True
        elif pre:
            ans *= 2
            pre = False
        elif not pre and cut[i + 1] - cut[i] == 1:
            pre = True
        else:
            ans *= 3
            pre = False
        ans %= mod
    print(ans)
    

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# code like professional " write main function "

import sys
def main():
	n = int(input())
	S1 =input()
	S2 =input()
	ans  = 1
	mod = 10**9+7
	i = 0
	if S1[0]==S2[0]:
		ans = 3
		i=1
	else:
		ans = 6
		i = 2
	while i<n:
		if S1[i]==S2[i]:
			if S1[i-1]==S2[i-1]:
				ans = (ans*2)%mod
			i = i+1
		else:
			if S1[i-1]==S2[i-1]:
				ans = (ans*2)%mod
			else:
				ans = (ans*3)%mod
			i = i+2
	print(ans)

if __name__ == "__main__":
	main()

Submission status: Accepted

Solution code:
import sys
def main():
	n = int(input())
	S1 =input()
	S2 =input()
	ans  = 1
	mod = 10**9+7
	i = 0
	if S1[0]==S2[0]:
		ans = 3
		i=1
	else:
		ans = 6
		i = 2
	while i<n:
		if S1[i]==S2[i]:
			if S1[i-1]==S2[i-1]:
				ans = (ans*2)%mod
			i = i+1
		else:
			if S1[i-1]==S2[i-1]:
				ans = (ans*2)%mod
			else:
				ans = (ans*3)%mod
			i = i+2
	print(ans)

if __name__ == "__main__":
	main()

Submission status: Accepted

Solution code:
import sys
readline=sys.stdin.readline

def main():
    p=10**9+7
    n=int(input())
    s1=readline().strip()
    s2=readline().strip()
    pl=[]
    tempiter=iter(range(n))
    for i in tempiter:
        if s1[i]==s2[i]:
            pl.append(0)
        else:
            pl.append(1)
            _=next(tempiter)
    ans=1
    #print(pl)
    for i,_ in enumerate(pl[:-1]):
        s=pl[i]-pl[i+1]+2*pl[i]*pl[i+1]
        if s==0:
            ans=ans*2 % p
        elif s==1:
            ans=ans*2 % p
        elif s==2:
            ans=ans*3 % p
    if pl[-1]==0:
        ans=ans*3 % p
    else:
        ans=ans*6 % p
    print(ans)

if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N = int(readline())
    S1 = readline().strip()
    S2 = readline().strip()

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

    while i < N:
        if S1[i] == S2[i]:
            if S1[i - 1] == S2[i - 1]:
                ans = ans * 2 % MOD
            i += 1
        else:
            if S1[i - 1] == S2[i - 1]:
                ans = ans * 2 % MOD
            else:
                ans = ans * 3 % MOD
            i += 2

    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
MOD = 10**9+7
n = int(input())
s1 = input()
s2 = input()
res = 1
skip = False
for i in range(n):
    if skip:
        skip = False
        continue
    if i == 0:
        if s1[i] != s2[i]:
            res *= 3 * 2
            skip = True
        else:
            res *= 3
        res %= MOD
    else:
        if s1[i] != s2[i]:
            if s1[i-1] != s2[i-1]:
                res *= 3
            else:
                res *= 2
            skip = True
        else:
            if s1[i-1] != s2[i-1]:
                pass
            else:
                res *= 2
        res %= MOD
print(res)

Submission status: Accepted

Solution code:
n = int(input())
L = list(input())
R = list(input())
mod = 1000000007
if n==1:print(3);exit()
ans = 3 if R[0]==L[0] else 6
for i in range(1,n):
    if L[i-1]==R[i-1]:
        if L[i]==R[i]:
            ans *=2
        else:
            ans *=2
    else:
        if L[i]!=L[i-1]:
            if L[i]==R[i]:
                continue
            else:
                ans *= 3
    ans %=mod
print(ans)

Submission status: Accepted

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

if len(S1) == 1:
    print(3)
elif len(S1) == 2:
    print(6)
else:
    if S1[0] != S2[0]:
        t = 2
        i = 2
        a = 6
    else:
        t = 1
        i = 1
        a = 3
    while True:
        if S1[i] != S2[i]:
            if t == 2:
                a *= 3
            else:
                a *= 2
            i += 2
            t = 2
        else:
            if t == 2:
                a *= 1
            else:
                a *= 2
            i += 1
            t = 1
        a %= 1000000007
        if i == len(S1):
            print(a)
            break

Submission status: Accepted

Solution code:
def abc071_d():
    N = int(input())
    S1 = str(input())
    S2 = str(input())
    MOD = 10**9 + 7
    if len(S1) == 1: return 3

    ans, sidx, prev = -1, -1, -1
    if S1[0] == S1[1]:  # 横
        ans = 6
        sidx = 2
        prev = 2
    else:  # 縦
        ans = 3
        sidx = 1
        prev = 1

    if N <= sidx: return ans
    S = S1 + '_'

    for i in range(sidx, N):
        if S[i-1] == S[i]:  # 横置きの2つ目
            continue
        if S[i] == S[i+1]:  # 横
            if prev == 1: x = 2
            else: x = 3
            prev = 2
        else:  # 縦
            if prev == 1: x = 2
            else: x = 1
            prev = 1
        ans = ans * x % MOD
    return ans

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

Submission status: Accepted

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

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

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

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

Submission status: Accepted

Solution code:
n=int(input())
s=[input() for i in range(2)]
mod=10**9+7

if s[0][0]==s[1][0]:
    ans=3
    i=1
    tate=True
else:
    ans=6
    i=2
    tate=False

while i<n:
    if s[0][i]==s[1][i]:
        if tate==True:
            ans*=2
        else:
            pass
        tate=True
        i+=1
    else:
        if tate==True:
            ans*=2
        else:
            ans*=3
        i+=2
        tate=False
    
    ans=ans%mod
print(ans)

Submission status: Accepted

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

MOD = 10**9+7
i = 0
ans = 0
prev_double = True

if s1[0] == s2[0]:
    prev_double = False
    i = 1
    ans = 3
else:
    prev_double = True
    i = 2
    ans = 6

while i < n:
    if s1[i] == s2[i]:
        i += 1
        if prev_double:
            ans *= 1
        else:
            ans *= 2
        prev_double = False
    else:
        i+=2
        if prev_double:
            ans *= 3
        else:
            ans *= 2
        prev_double = True
    ans %= MOD
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s1=input()
s2=input()
ans=0
i=0
prev=None#左のドミノの状態
if s1[0]==s2[0]:#縦
    ans=3
    i+=1
    prev='tate'
else:#横
    ans=6
    i+=2
    prev='yoko'
    
mod=10**9+7
while i<n:
    if s1[i]==s2[i]:#縦
        ans=(ans*2)%mod if prev=='tate' else ans 
        i+=1
        prev='tate'
    else:#横
        ans=(ans*2)%mod if prev=='tate' else (ans*3)%mod 
        i+=2
        prev='yoko'
print(ans)

Submission status: Accepted

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

m = 1000000007

if S1[0] == S2[0]: # 全部横向きの場合
    result = 3
else:
    result = 6

x = 0
W = len(S1)

while x < W:
    if S1[x] == S2[x]:
        if x + 1 == W:
            break
        result *= 2
        result %= m
        x += 1
    else:
        if x + 2 == W:
            break
        if S1[x + 2] != S2[x + 2]:
            result *= 3
            result %= m
        x += 2
print(result)

Submission status: Accepted

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

queue = [0, 1, 2]
ans = 1
p = 0
for i in range(N):
  if p <= 1:
    if S1[i] == S2[i]:
      if i == 0:
        ans *= 3
      else:
        if p == 0:
          ans *= 2
        else:
          ans = ans
      p = 0
    else:
      if i == 0:
        ans *= 6
      else:
        if p == 0:
          ans *= 2
        else:
          ans *= 3 
      p = 2
  else:
    p -= 1
  ans = ans % mod  
      
print(ans)

Submission status: Accepted

Solution code:
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LS2(): return list(sys.stdin.readline().rstrip())  #空白なし

N = I()
S1 = LS2()
S2 = LS2()
mod = 10**9+7

A = []
a = 0
while a < N:
    if S1[a] == S2[a]:
        a += 1
        A.append(1)
    else:
        a += 2
        A.append(2)

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

print(ans)