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:
#!/usr/bin/python3
# -*- coding: utf-8 -*-

n = int(input())
s = []
s.append(input())
s.append(input())
MOD = 1000000007
ans = 1
last = 0
i = 0
while i < n:
    if s[0][i] == s[1][i]:
        if last == 1:
            ans = ans * 2 % MOD
        elif last == 0:
            ans = 3
        last = 1
        i += 1
    else:
        if last == 1:
            ans = ans * 2 % MOD
        elif last == 2:
            ans = ans * 3 % MOD
        else:
            ans = 6
        last = 2
        i += 2
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
ss = list(input())
ans = []
i = 0
while i < n:
	if i == 0:
		if i+1 == n:
			ans.append(3)
			i = 10000
		elif s[i] == s[i+1]:
			ans.append(3)
			ans.append(2)
			i += 2
		else:
			ans.append(3)
			i += 1
	else:
		if s[i] == ss[i]:
			if s[i-1] == ss[i-1]:
				ans.append(2)
				i += 1
			else:
				ans.append(1)
				i += 1
		else:
			if s[i-1] == ss[i-1]:
				ans.append(2)
				i += 2
			else:
				ans.append(3)
				i += 2

t = 1
for k in range(len(ans)):
	t = (t*ans[k])%1000000007
print(t)

Submission status: Accepted

Solution code:
def check(i,j):
    if i > 0:
        k = S[j][i-1] 
        if k in checked and not k in C:
            C.append(k)
    if j > 0:
        l = S[j-1][i]
        if l in checked and not l in C:
            C.append(l)
N = int(input())
S = [list(input()) for _ in range(2)]
ans = 1
checked = {}
for i in range(N):
    for j in range(2):
        p = S[j][i]
        if not p in checked:
            C = []
            check(i,j)
            if j < 1 and S[j+1][i] == p:
                check(i,j+1)
            checked[p] = C
            if len(C) > 1:
                if not C[0] in checked[C[-1]]:
                    ans = ((ans*3)//2)
            else:
                ans = (ans*(3-len(C)))
print(ans%(10**9+7))

Submission status: Accepted

Solution code:
N = int(input())
blocks = [input().rstrip() for x in range(2)]

MOD = 1000000007
result = 0
count = 0
prev = False
while count <= N-1:
  if count == 0:
    if blocks[0][count] == blocks[1][count]:
      result = 3
      prev = True
    else:
      result = 6
      count += 1
  else:
    if prev:
      result = (result*2) % MOD
      # a bb
      # a cc 
      if blocks[0][count] != blocks[1][count]:
        prev = False
        count += 1
    else:
      if blocks[0][count] == blocks[1][count]:
        prev = True
      else:
        result = (result*3) % MOD
        count += 1
  count += 1
print(result)

Submission status: Accepted

Solution code:
mod = 1000000007

N = int(input())

S = input()
S_sub = input()

P = []
now = 0
for i in range(N):
    if i>0 and S[i] != S[i-1]:
        P.append(now)
        now = 0
    now += 1
    
P.append(now)

ans = 3 if P[0]==1 else 6
now = P[0]
for p in P[1:]:
    if now == 1:
        ans = (ans*2)%mod
    elif now == 2:
        if p == 1: ans = (ans*1)%mod
        else: ans = (ans*3)%mod
    now = p
    
print(ans)

Submission status: Accepted

Solution code:
MOD = 10 ** 9 + 7

n = int(input())
s = list(input())
s2 = list(input())

array = []

i = 0
while i < n:
	if i == n - 1:
		array.append("t")
		i += 1
	elif s[i] != s[i + 1]:
		array.append("t")
		i += 1
	else:
		array.append("y")
		i += 2

if array[0] == "t":
	ans = 3
else:
	ans = 6

if len(array) == 1:
	print(ans)
else:
	for i in range(1, len(array)):
		if array[i-1] == "t":
			ans = (ans * 2) % MOD
		elif array[i] == "y":
			ans = (ans * 3) % MOD

	print(int(ans))

Submission status: Accepted

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

mod = 10**9+7

# Y:横 T:縦
S = ""
flg = False
for i in range(len(S1)):
    if flg:
        flg = False
        continue

    if S1[i] == S2[i]:
        S += "T"
    else:
        S += "Y"
        flg = True

if S[0] == "T":
    ans = 3
    now = "T"
else:
    ans = 6
    now = "Y"

for s in S[1:]:
    if now=="T":
        if s=="T":
            ans *= 2
            now = "T"
        else:
            ans *= 2
            now = "Y"
        ans %= mod

    else:
        if s=="T":
            now = "T"
        else:
            ans *= 3
            ans %= mod
            now = "Y"

print(ans % mod)

Submission status: Accepted

Solution code:
n=int(input())
a1=list(input().strip())
a2=list(input().strip())

p1='0'
p2='1'
for i1,i2 in zip(a1,a2):
  if p1=='0':
    if i1 != i2:
      x=6
    else:
      x=3
  elif i1 != p1 and i2 !=p2 and i1 !=i2 and p1 != p2:
    x*=3
  elif i1 != p1 and i2 !=p2 and i1 !=i2 and p1 == p2:
    x*=2
  elif i1 != p1 and i2 != p2 and i1 ==i2 and p1 == p2:
    x*=2
  p1=i1
  p2=i2
  
print(x%(10**9+7))

Submission status: Accepted

Solution code:
N = int(input())
s_lst=[]
for i in range(2):
    s_lst.append(input())


if s_lst[0][0] == s_lst[1][0]:
    wasx = True
    ans, pos = 3, 1
else:
    wasx = False
    ans, pos = 6, 2

while pos < N:
    if s_lst[0][pos] == s_lst[1][pos]:
        if wasx:
            ans = (ans * 2) %1000000007
        wasx = True
        pos += 1
    else:
        if wasx:
            ans = (ans * 2) %1000000007
        else:
            ans = (ans * 3) %1000000007
        wasx = False
        pos += 2
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from collections import Counter
import math
N = int(input())
S1 = input()
S2 = input()

i = 0
ans = 1
before = -1 #0:直前が縦、1:直前が横
while i < N :
    if S1[i] == S2[i]:#縦に置かれている
        if before == 1:#直前が横
            ans *= 1
        elif before == 0:#直前が縦
            ans *=2
        else:#最初
            ans *= 3
        ans = ans % 1000000007
        i += 1
        before = 0
    else:#横に置かれている
        if before == 1:#直前が横
            ans *= 3
        elif before == 0:#直前が縦
            ans *= 2
        else:#最初
            ans *= 6
        ans = ans % 1000000007
        i += 2
        before = 1

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s1 = input()
s2 = input()
ans = 1
start = 0
yoko = 0
mae = 0
for i in range(len(s1)):
    if yoko == 1:
        yoko = 0
        continue
    if mae == 0:
        if s1[i] == s2[i]:
            ans *= 3
            mae = 1
        else:
            yoko = 1
            ans *= 6
            mae = 2
        start = 1
    else:
        if mae == 1:
            #縦→縦
            if s1[i] == s2[i]:
                ans *= 2
            #縦→横
            else:
                ans *= 2
                yoko = 1
                mae = 2
        else:
            #横→縦
            if s1[i] == s2[i]:
                mae = 1
            #横→横
            else:
                ans *= 3
                yoko = 1
print(ans % 1000000007)

Submission status: Accepted

Solution code:
n = int(input())

s1 = input()
s2 = input()



i = 0
while i < n:
    #最初ならば表示
    if i==0:
        if s1[i] == s2[i]:
            r = 3
        else:
            r = 6
    else:
        if (s1[i - 1]==s2[i - 1]):
            r *= 2
        elif (s1[i - 1]!=s2[i - 1]) and (s1[i]!=s2[i]):
            r *= 3
    if s1[i]!=s2[i]:
        i += 1
    i += 1
print(r % 1000000007)

Submission status: Accepted

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

if(s1[0] == s2[0]):
	ans = 3
else:
	ans = 6

for i in range(1, N):
	if s1[i] == s1[i-1] and s2[i] == s2[i-1]: continue
	if s1[i-1] == s2[i-1] : 
		ans *= 2
	else :
		if s1[i] != s2[i] : ans *= 3

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

Submission status: Accepted

Solution code:
# Coloring Dominoes
N = int(input())
s1 = input()
s2 = input()

if (s1[0]==s2[0]):
    c = 3
else:
    c = 6


for i in range(N-1):
    if (s1[i]==s2[i]):
        c*=2
    else:
        if (s1[i]!=s1[i+1]):
            if (s1[i+1]!=s2[i+1]):
                c*=3
print(c%1000000007)

Submission status: Accepted

Solution code:
MOD = 1000000007

START = 0
SINGLE = 1
DOUBLE = 2

n = int(raw_input())

d = [raw_input() for i in xrange(2)]

ind = 0
prev = START
total = 1
while ind < n:
    x = d[0][ind]
    if x == d[1][ind]:
        if prev == START:
            total *= 3
        elif prev == SINGLE:
            total *= 2
        elif prev == DOUBLE:
            total *= 1

        prev = SINGLE
        
    elif ind + 1 < n and (x == d[0][ind+1] and d[1][ind] == d[1][ind+1]):
        if prev == START:
            total *= 6
        elif prev == SINGLE:
            total *= 2
        elif prev == DOUBLE:
            total *= 3

        ind += 1
        prev = DOUBLE

    ind += 1
    total %= MOD

print total

Submission status: Accepted

Solution code:
def is_wide(t1, t2):
	if t1 == t2:
		return True
	return False

n = int(input())
t1 = input()
#t2 = list(input().split())

ans = 1
pos = 0
while pos < len(t1):
	if pos == 0:
		w = 6
		l = 3
	else:
		if prev_wide:
			w = 3
			l = 1
		else:
			w = 2
			l = 2
	prev_wide = False
	if len(t1) > pos+1:
		if is_wide(t1[pos], t1[pos+1]):
			ans *= w
			pos += 1
			prev_wide = True
		else:
			ans *= l
	else:
		ans *= l
	ans %= 1000000007
	pos += 1
print(ans)

Submission status: Accepted

Solution code:
def solve():
    n = int(input())
    pattern = [[a, b] for a, b in zip(list(input()), list(input()))]
    pointer, ans = 0, 0
    if pattern[0][0] == pattern[0][1]:
        ans = 3
    else:
        ans = 6
    while pointer < n - 1:
        current = pattern[pointer][0]
        if current == pattern[pointer][1]:
            if pattern[pointer+1][0] == pattern[pointer+1][1]:
                ans *= 2
                # print("X->X")
            else:
                ans *= 2
                # print("X->Y")
            pointer += 1
        else:
            if pointer + 2 >= n - 1:
                break
            if pattern[pointer+2][0] == pattern[pointer+2][1]:
                ans *= 1
                # print("Y->X")
            else:
                ans *= 3
                # print("Y->Y")
            pointer += 2
    print(ans % 1000000007)

if __name__=="__main__":
    solve()

Submission status: Accepted

Solution code:
n=int(input())
S1=input()
S2=input()
skip=False
p1,p2=False, False
dp=1
for i in range(n):
    if skip:
        skip=False
        continue
    elif S1[i]==S2[i]:
        if p1:
            dp*=2
        elif p2:
            p1=True
            p2=False
        else:
            p1=True
            dp*=3
    elif S1[i]==S1[i+1]:
        skip=True
        if p1:
            dp*=2
            p1=False
            p2=True
        elif p2:
            dp*=3
        else:
            p2=True
            dp*=6
else:
    print(dp%1000000007)

Submission status: Accepted

Solution code:
N = int(input())
s1 = input()
s2 = input()
sum = 1
mul = 1
flag = 0
i = 0

while True:
    if i >= N:
        print(sum%1000000007)
        break

    elif s1[i]==s2[i] and flag==0:
        sum = 3
        i += 1
        flag = 1
    elif s1[i]!=s2[i] and flag==0:
        sum = 6
        i += 2
        flag = 2
    elif s1[i]==s2[i] and flag==1:
        mul = 2
        i += 1
        flag = 1
    elif s1[i]!=s2[i] and flag==1:
        mul = 2
        i += 2
        flag = 2
    elif s1[i]==s2[i] and flag==2:
        mul = 1
        i += 1
        flag = 1
    elif s1[i]!=s2[i] and flag==2:
        mul = 3
        i += 2
        flag = 2
    sum = sum*mul

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    N = int(input())
    S1 = input()
    S2 = input()
    sum = 1
    mul = 1
    mod = 1000000007
    fg = 0
    i = 0

    while True:

        if i >= N:
            print(sum)
            exit()

        if S1[i] == S2[i] and fg == 0:
            sum = 3
            i += 1
            fg = 1
        elif S1[i] != S2[i] and fg == 0:
            sum = 6
            i += 2
            fg = 2
        elif S1[i] == S2[i] and fg == 1:
            mul = 2
            i += 1
            fg = 1
        elif S1[i] != S2[i] and fg == 1:
            mul = 2
            i += 2
            fg = 2
        elif S1[i] == S2[i] and fg == 2:
            mul = 1
            i += 1
            fg = 1
        elif S1[i] != S2[i] and fg == 2:
            mul = 3
            i += 2
            fg = 2
        sum = ((sum % mod) * (mul % mod)) % mod

Submission status: Accepted

Solution code:
import sys

N=int(input())

S=input()

if len(S)==1:
    print(3)
    sys.exit()

if S[0]==S[1]:
    before=2
    S=S[2::]
    pattern=6
else:
    before=1
    S=S[1::]
    pattern=3
while len(S)>1:
    if before==2:
        if S[0]==S[1]:
            pattern=pattern*3
            S=S[2::]
        else:
            before=1
            S=S[1::]
    else:
        if S[0]==S[1]:
            S=S[2::]
            before=2
        else:
            S=S[1::]
        pattern=pattern*2



if len(S)==1 and before==1:
    pattern=pattern*2
print (pattern%1000000007)

Submission status: Accepted

Solution code:
MOD = 10**9+7

N = int(input())

X = input()
input()

from itertools import chain

d = {(2,2):3, (1,2):1, (2,1):2, (1,1):2, (2,0):6, (1,0):3, (0,0):1}

r = 1
p = 0
n = 0
l = None
for x in chain(X,(None,)):
  if x == l:
    n += 1
  else:
    l = x
    r *= d[(n,p)]
    r %= MOD
    p = n
    n = 1

print(r)

Submission status: Accepted

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


if S1[i] != S2[i]:
    res = 6
    i+=2
    rear = 1
else :
    res = 3
    i+=1
    rear = 0

while i != N:
    if S1[i] != S2[i]:
        if rear == 1:
            res *= 3
        else:
            res *= 2
        rear = 1
        i += 2

    else:
        if rear == 0:
            res *= 2
        i += 1
        rear = 0


print(res%1000000007)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
S1 = tuple(input())
S2 = tuple(input())

domino = []

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

if domino[0]:
    res = 3
else:
    res = 6

for n in range(1, len(domino)):
    if domino[n]: # 1
        if domino[n-1]: # 1, 0
            res *= 2
    else:
        if domino[n-1]:
            res *= 2
        else:
            res *= 3
     
print(res%1000000007)

Submission status: Accepted

Solution code:
import numpy as np


def getInput():
    separator = ' '
    ret_array = []
    while True:
        try:
            row = input().split(sep=separator)
            ret_array.append(row)
        except EOFError:
            break
    return ret_array


g = getInput()

N = np.int32(g[0][0])

# ls:0 initial 1 horz 2 vert
ls = 0
cs = None
l = []
i = 0
hoge=[]
try:
    while True:
        # cc = currently checking u = upper l = lower
        hoge.append(g[2][0][i])
        if g[1][0][i] == g[2][0][i]:
            cs = 2
            i += 1
        else:
            cs = 1
            i += 2

        if ls == 0:
            if cs == 1:
                l.append(6)
            else:
                l.append(3)
        elif ls == 1:
            if cs == 1:
                l.append(3)
            else:
                l.append(1)
        else:
            l.append(2)

        ls = cs

except IndexError:
    print(np.mod(np.prod(np.array(l, dtype=np.int32)),1000000007))

Submission status: Accepted

Solution code:
#D_Coloring Dominoes

def ColoringDominoes(N, S1, S2):

    pattern = list()
    i = 0
    while True:
        if S1[i] == S2[i]:
            pattern.append(True)
            i += 1
        else:
            pattern.append(False)
            i += 2
        if i >= N:
            break

    if pattern[0]:
        n_pattern = 3
    else:
        n_pattern = 6
    for i in range(1, len(pattern)):
        if pattern[i-1] and pattern[i]:
            n_pattern *= 2
        elif pattern[i-1] and not(pattern[i]):
            n_pattern *= 2
        elif not(pattern[i-1]) and not(pattern[i]):
            n_pattern *= 3

    return n_pattern % 1000000007




if __name__ == "__main__":
    N = int(input())
    S1 = list(str(input()))
    S2 = list(str(input()))
    print(ColoringDominoes(N, S1, S2))

Submission status: Accepted

Solution code:
a = []
x = input()
str1 = raw_input()
str2 = raw_input()
count = 0
while True:
    if count>x-1:
        break
    if str1[count] == str2[count]:
        a.append("v")
        count+=1
    else:
        a.append("h")
        count+=2
total = 1
if a[0] == "h":
    total*=6
if a[0] == "v":
    total*=3
for i in range(1, len(a)):
    if a[i] == "v" and a[i-1] == "h":
        total *=1
        total = total%1000000007
    elif a[i] == "h" and a[i-1] == "v":
        total*=2
        total = total%1000000007
    elif a[i] == "v" and a[i-1] == "v":
        total*=2
        total = total%1000000007
    elif a[i] == "h" and a[i-1] == "h":
        total*=3
        total = total%1000000007
print total

Submission status: Accepted

Solution code:
n = input()
st1 = raw_input()
st2 = raw_input()
m = 1
mod = 1000000007
pat = 0 # or 1,2
bl = False
for i in range(n):
  #print m
  if pat == 2 and bl:
    bl = False
    continue
  if st1[i] == st2[i] and pat == 0:
    m = m*3%mod
    pat = 1
  elif st1[i] == st2[i] and pat == 1:
    m = m*2%mod
    pat = 1
  elif st1[i] == st2[i] and pat == 2:
    pat = 1
  elif st1[i] != st2[i] and pat == 0:
    m = m*6%mod
    pat = 2
    bl = True
  elif st1[i] != st2[i] and pat == 1:
    m = m*2%mod
    pat = 2
    bl = True
  elif st1[i] != st2[i] and pat == 2:
    m = m*3%mod
    pat = 2
    bl = True
print m

Submission status: Accepted

Solution code:
def pop_when_x():
    S1.pop(0)
    S2.pop(0)
    global left_state
    left_state = "X"


def pop_when_y():
    S1.pop(0)
    S1.pop(0)
    S2.pop(0)
    S2.pop(0)
    global left_state
    left_state = "Y"


if __name__ == '__main__':
    N = int(input())
    S1 = list(input())
    S2 = list(input())

    if S1[0] == S2[0]:
        cases = 3
        left_state = "X"
        pop_when_x()
    else:
        cases = 6
        left_state = "Y"
        pop_when_y()

    while len(S1) != 0:
        # print("".join(S1))
        # print("".join(S2))
        # print(cases)
        if S1[0] == S2[0]:
            state = "X"
        else:
            state = "Y"
        # print("left_state:" + left_state)
        # print("state:" + state)
        if left_state == "X" and state == "X":
            cases *= 2
            pop_when_x()
        elif left_state == "X" and state == "Y":
            cases *= 2
            pop_when_y()
        elif left_state == "Y" and state == "X":
            cases *= 1
            pop_when_x()
        elif left_state == "Y" and state == "Y":
            cases *= 3
            pop_when_y()

    print(cases % 1000000007)

Submission status: Accepted

Solution code:
input()

a =raw_input()

edge = []

while len(a) > 1:
      if a[0] == a[1]:
            edge.append(2)
            a = a[2:]
      else:
            edge.append(1)
            a = a[1:]
         
if len(a) == 1:
      edge.append(1)

ans = 1

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

for i in range(1, len(edge)):
      if (edge[i-1], edge[i]) == (1, 1):ans *= 2
      if (edge[i-1], edge[i]) == (1, 2):ans *= 2
      if (edge[i-1], edge[i]) == (2, 1):ans *= 1
      if (edge[i-1], edge[i]) == (2, 2):ans *= 3
      ans %= 10**9 + 7

print ans

Submission status: Accepted

Solution code:
n = int(input())
s1 = input() + " "
s2 = input() + " "
 
mod = 1000000007
 
num = 1
i = 0
b = 0
tmp_t = -1
while(b + i < n):
    if s1[i + b] == s2[i + b]: # t 0
        if tmp_t == 0:
            num = (num * 2) % mod
        elif tmp_t == 1:
            num = (num * 1) % mod
        else:
            num = 3
        tmp_t = 0
    else: # t 1
        if tmp_t == 0:
            num = (num * 2) % mod
        elif tmp_t == 1:
            num = (num * 3) % mod
        else:
            num = 6
        tmp_t = 1
        b += 1
    i += 1
 
print(num)

Submission status: Accepted

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

mod = 1000000007

num = 1
i = 0
b = 0
tmp_t = -1
while(b + i < n):
    if s1[i + b] == s2[i + b]: # t 0
        if tmp_t == 0:
            num = (num * 2) % mod
        elif tmp_t == 1:
            num = (num * 1) % mod
        else:
            num = 3
        tmp_t = 0
    else: # t 1
        if tmp_t == 0:
            num = (num * 2) % mod
        elif tmp_t == 1:
            num = (num * 3) % mod
        else:
            num = 6
        tmp_t = 1
        b += 1
    i += 1

print(num)

Submission status: Accepted

Solution code:
N=int(input())

field=[list(input())]
field.append(list(input()))

M=1000000007

b=[field[0][0],field[1][0]]
if b[0]==b[1]:
    bdeffer=False
    count=3
else:
    bdeffer=True
    count=6

for j in range(1,len(field[0])):
    count=count%M
    a=[field[0][j],field[1][j]]

    if a[0]==b[0]:
        b=a
        if a[0]==a[1]:
            bdeffer=False
        else:
            bdeffer=True
        continue

    if a[0]==a[1]:
        if not bdeffer:
            count*=2
    else:
        if bdeffer:
            count*=3
        else:
            count*=2

    b=a[:]

    if a[0]==a[1]:
        bdeffer=False
    else:
        bdeffer=True

print(count%M)

Submission status: Accepted

Solution code:
N = int(input())
a1 = list(input())
a2 = list(input())
t, i = 0, 0
x = 1

while (i < len(a1) - 1):
	if(t == 0):
		if(a1[i] == a1[i + 1]):
			x = x*6
			t = 1
			i += 2
		
		else:
			x = x*3
			t = 2
			i += 1

			if(i == len(a1) - 1):
				x = x*2

		continue

	if(t == 1):
		if(a1[i] == a1[i + 1]):
			x = x*3
			t = 1
			i += 2

		else:
			x = x*1
			t = 2
			i += 1

			if(i == len(a1) - 1):
				x = x*2


		continue

	if(t == 2):
		if(a1[i] == a1[i + 1]):
			x = x*2
			t = 1
			i += 2

		else:
			x = x*2
			t = 2
			i += 1

			if(i == len(a1) - 1):
				x = x*2

		continue


if(len(a1) == 1):
	print(3)

else:
	print(x % 1000000007)

Submission status: Accepted

Solution code:
n = int(input())
s1 = input()
s2 = input()
l = []
m = 1
mod = 10**9 + 7

after = False
for i in range(n):
	if i == n-1:
		if after:
			pass
		else:
			l.append(0)
	else:
		if after:
			after = False
			pass
		else:
			if s1[i] == s1[i+1]:
				l.append(1)
				after = True
			else:
				l.append(0)

for i, x in enumerate(l):
	if i == 0:
		if x == 0:
			m = 3
		else:
			m = 6 
	else:
		if x == 0 and l[i-1] == 0:
			m = m * 2
			m = m % mod
		elif x == 1 and l[i-1] == 0:
			m = m * 2
			m = m % mod
		elif x == 0 and l[i-1] == 1:
			pass
		else:
			m = m * 3
			m = m % mod

print(m)

Submission status: Accepted

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

MOD = 1000000007
N = int(input())
U = input() + ' '
D = input()

s = ''
c = 0
while c < len(U)-1:
    if U[c] == U[c+1]:
        s += 'L'
        c += 2
    else:
        s += 'S'
        c += 1


l = s[0]
a = 3 if l == 'S' else 6
for i in s[1:]:
    if l == i == 'S':
        a = (a * 2) % MOD
    elif l == i == 'L':
        a = (a * 3) % MOD
    elif l == 'S' and i == 'L':
        a = (a * 2) % MOD
    l = i
print(a)

Submission status: Accepted

Solution code:
n=int(input())
s=[list(input()),list(input())]
a=1
if n>1:
 for i in range(n-1):
  if s[0][i]==s[0][i+1]:
   if i>0:
    if s[0][i-1]==s[1][i-1]:
      a*=2
    else:
      a*=3
   else:
      a*=6
 for i in range(n):
  if s[0][i]==s[1][i]:
   if i>0:
     if s[0][i-1]==s[1][i-1]:
       a*=2
   else:
     a*=3
  a%=1000000007
else:
  a=3
print(a)

Submission status: Accepted

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

ans = 1

s = 0
for i in range(N):
    p = 1
    if i > 0 and A[i] == A[i-1]:
        continue
    elif i+1 < N and A[i] == A[i+1]:
        p = [6, 3, 2][s]
        s = 1
    else:
        p = [3, 1, 2][s]
        s = 2
    ans = (ans * p) % (int(1e9) + 7)

print(ans)

Submission status: Accepted

Solution code:
def solve(s1):
    div = 1000000007
    s1 = iter(s1)
    try:
        a = next(s1)
        b = next(s1)
    except:
        return 3
    if a != b:
        a = b
        try:
            b = next(s1)
        except:
            return 6
    ans = 3
    state = 0
    while True:
        if a == b:
            ans *= 3 if state else 2
            ans %= div
            try:
                a = next(s1)
                b = next(s1)
            except:
                return ans
            state = 1
        else:
            ans *= 1 if state else 2
            ans %= div
            try:
                a = b
                b = next(s1)
            except:
                return ans * 2 % div
            state = 0


input()
print(solve(input().strip()))

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    S1 = list(input())
    S2 = list(input())
    d = 10**9 + 7
    # print(N, S1, S2)

    skip = False
    Map = {("p","v"):3, ("p","h"):6, ("v","h"):2, ("h","v"):1, ("h","h"):3, ("v","v"):2}
    pre = "p"

    possibles = 1
    for n in range(N):
        if skip:
            skip = False
            continue
        pass
        if S1[n] == S2[n]:  # vertical
            # print("v")
            now = "v"
        else:  # horizonal
            # print("h")
            now = "h"
            skip = True
        possibles *= Map[(pre,now)]
        possibles %= d
        # print("possibles", pre, now, possibles)
        pre = now

    print(possibles)

        
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
N=int(input())
S=str(input())
M=str(input())
Mo=10**9+7
ans=1
i=0
if S[i]==M[i]:
  Ag=1
  ans=ans*3
  i+=1
else:
  Ag=2
  ans=ans*6
  i+=2
while i<N:
  if S[i]==M[i]:
    if Ag==1:#連続で楯だった
      ans=ans*2%Mo
      Ag=1
      i+=1
    elif Ag==2:#横の次のたてだった
      Ag=1
      i+=1
  else:#よこがでた
    if Ag==1:#たてのつぎのよこだった
      ans=ans*2%Mo
      Ag=2
      i+=2
    elif Ag==2:#連続でよこだった
      ans=ans*3%Mo
      Ag==2
      i+=2
print(ans%Mo)

Submission status: Accepted

Solution code:
n = int(input())
s1 = input()
s2 = input()
s3 = ""
i = 0
while i < len(s1):
    if s1[i] == s2[i]:
        s3 += "T"
    else:
        s3 += "Y"
        i += 1
    i += 1
ret = 1
for i in range(len(s3)):
    if i == 0:
        if s3[i] == "T":
            ret *= 3
        else:
            ret *= 6
    else:
        if s3[i] == "T" and s3[i-1] == "T":
            ret *= 2
        if s3[i] == "Y":
            if s3[i-1] == "T":
                ret *= 2
            else:
                ret *= 3
        ret %= 1000000007
print(ret)

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

Solution code:
def ri(): return int(input())
def rli(): return list(map(int, input().split()))
def rls(): return list(input())
def pli(a): return "".join(list(map(str, a)))
def plis(a): return " ".join(list(map(str, a)))

N = ri()
S1 = rls()
S2 = rls()
mode = None
mode_p = None
ans = 0
mod = 10**9 + 7

i = 0
while(i < N):
    if(S1[i] == S2[i]):
        if(mode is None):
            mode = "c"
        else:
            mode_p = mode
            mode = "c"
    else:
        i += 1
        if(mode is None):
            mode = "r"
        else:
            mode_p = mode
            mode = "r"
    
    if(mode_p is None):
        ans = 3 if mode == "c" else 6
    else:
        if(mode_p == "c"):
            ans = ans*2%mod
        elif(mode == "r"):
            ans = ans*3%mod
    i += 1

print(ans)

Submission status: Accepted

Solution code:
# encoding:utf-8
import sys

def main():
	n = int(input())
	str1 = input()
	str2 = input()

	ptrn = []
	i = 0
	while True:
		if str1[i] == str2[i]:
			ptrn.append("t")
			i += 1
		else:
			ptrn.append("y")
			i += 2
		if i >= n:
			break
	
	#print(ptrn)

	if ptrn[0] == "t":
		n_ptrn = 3
	else:
		n_ptrn = 6

	for i in range(1, len(ptrn)):
		#print(n_ptrn)
		if ptrn[i - 1] == "t" and ptrn[i] == "t":
			n_ptrn = n_ptrn * 2
		elif ptrn[i - 1] == "t" and ptrn[i] == "y":
			n_ptrn = n_ptrn * 2
		elif ptrn[i - 1] == "y" and ptrn[i] == "y":
			n_ptrn = n_ptrn * 3

	print(n_ptrn % 1000000007)



if __name__ == '__main__':
	main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
modulus = 1000000007

n = input()
s = list(zip(input(), input()))

def sliding_window(it):
    it = iter(it)
    fst = next(it)
    for snd in it:
        yield fst, snd
        fst = snd

if n == 1:
    print(3)
elif n == 2:
    print(6)
else:
    ans = 0
    if s[0][0]==s[0][1]:
        ans += 3
    else:
        ans += 6
    for x in sliding_window(s):
        if x[0][0] == x[0][1]:
            ans = ans * 2 % modulus
        elif x[0][0] != x[1][0]:
            if x[1][0] != x[1][1]:
                ans = ans * 3 % modulus
    print(ans)