Problem:


ABC Gene

文字列 ABC で表される遺伝子配列がある。あなたは次の操作を何回か行い、この遺伝子配列を書き換えていくことができる。

文字 A，B，C のうち 1 つを選ぶ。これを x とおく。遺伝子配列に含まれるすべての x をそれぞれ ABC へ同時に置き換える。

A，B，C だけからなる文字列 S が与えられる。遺伝子配列を S に一致させられるか判定せよ。

Constraints

1 ≤ |S|  ≤ 5,000

S は A，B，C だけからなる。

Input Format

入力は以下の形式で標準入力から与えられる。

S

Output Format

遺伝子配列を S に一致させられるならば Yes を、一致させられないならば No を一行に出力せよ。

Sample Input 1

ABC

Sample Output 1

Yes

遺伝子配列ははじめから ABC である。

Sample Input 2

AABCC

Sample Output 2

Yes

B を選んで操作を行うと ABC → AABCC となる。

Sample Input 3

AABCABC

Sample Output 3

No

例えば、C を選んで操作を行っても AABCC → AABCABC とはならない。すべての C をそれぞれ ABC へ同時に置き換えるので、実際は AABCC → AABABCABC となる。


Sample input:
ABC


Sample output:
Yes

Submission status: Wrong Answer

Solution code:
special=False
target=input()
if target[0]=="A" and target[-1]=="C" and "B" in target and "ABC" in target:
    pass
else:
    special=True

print("special",special)
    
old=["ABC"]
new=[]
word=["A","B","C"]
while old != [] and special ==False:
    if target in old:
        print("Yes")
        break
    for i in old:
        for j in word:
            new_word=i.replace(j,"ABC")
            if len(new_word)==len(target):
                if new_word==target:
                    new.append(new_word)
                    break
                else:
                    pass
            elif len(new_word)< len(target):
                new.append(new_word)
            else:
                pass
    old=new[:]
    new=[]
else:
    print("No",special)

Submission status: Wrong Answer

Solution code:
target=input()
list_abc=["A","B","C"]
old=[]
new=[]
flag=False
if "ABC" in target:
    old.append(target)
while old !=[] and flag==False:
    for i in old:
        if i=="ABC":
            flag=True
            break
        for j in list_abc:
            element=i.replace("ABC",j)
            if "ABC" in element:
                new.append(element)
    else:
        old=new[:]
        new=[]
print("Yes" if flag==True else "No")

Submission status: Wrong Answer

Solution code:
import sys
s = raw_input()
used = set()
sys.setrecursionlimit(10000)
def dfs(s):
    if s in used:
        return 0
    if s == "ABC":
        return 1
    if "ABC" in s:
        for c in "ABC":
            if dfs(s.replace("ABC", c)):
                return 1
    used.add(s)
    return 0
print dfs(s)*"Yes"or"No"

Submission status: Wrong Answer

Solution code:
s = raw_input()
used = set()
def dfs(m):
    if m in used or len(m) > len(s):
        return 0
    if m == s:
        return 1
    for c in "ABC":
        if dfs(s.replace(c, "ABC")):
            return 1
    used.add(s)
    return 0
print dfs("ABC")*"Yes" or "No"

Submission status: Wrong Answer

Solution code:
# Str -> Bool
def solve(s):

    if len(s) < 3:
        return False

    t = s
    while t.find("ABC") != -1:
        if t[0:3] == "ABC":
            t = t.replace("ABC", "A")
        elif t[-3:] == "ABC":
            t = t.replace("ABC", "C")
        else:
            t = t.replace("ABC", "B")

    return len(t) == 1

def main():
    s = input()
    if solve(s):
        print("Yes")
    else:
        print("No")

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
line = input()
ABC1 = "ABC"
ABC2 = "ABC"
ABC3 = "ABC"
while 1:
    if line == ABC1:
        print("Yes")
        break
    elif line == ABC2:
        print("Yes")
        break
    elif line == ABC3:
        print("Yes")
        break
    elif len(line) <= len(ABC1):
        print("No")
        break
    ABC1 = ABC1.replace("A","ABC")
    ABC2 = ABC2.replace("B","ABC")
    ABC3 = ABC3.replace("C","ABC")

Submission status: Wrong Answer

Solution code:
import re
S = input()
n = len(S)
while n > 3:
    n = len(S)
    if re.match(r'^ABC',S):
        S=S.replace('ABC','A')
        tmp = len(S)
    elif re.match(r'ABC$',S):
        S=S.replace('ABC','C')
        tmp = len(S)
    else:
        S=S.replace('ABC','B')
        tmp = len(S)
    if n == tmp:
        break
if 'ABC' == S :
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
S = raw_input()

while S != 'ABC':
	S2 = ""
	i = 0
	while True:
		if i >= len(S):
			break
		if S[i:min(len(S), i + 3)] == "ABC":
			S2 += "X"
			i += 2
		else:
			S2 += S[i]
		i += 1
	a_flag = 'A' in S2
	b_flag = 'B' in S2
	c_flag = 'C' in S2
	if not 'X' in S2:
		print "NO"
		exit()
	if a_flag + b_flag + c_flag != 2:
		print "NO"
		exit()
	SS = ''
	if not a_flag:
		SS = 'A'
	if not b_flag:
		SS = 'B'
	if not c_flag:
		SS = 'C'
	S3 = ''
	for c in S2:
		if c == 'X':
			S3 += SS
		else:
			S3 += c
	S = S3

print "YES"

Submission status: Wrong Answer

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

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(0,-1),(1,0),(0,1),(-1,0)]
ddn = [(0,-1),(1,-1),(1,0),(1,1),(0,1),(-1,-1),(-1,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 pf(s): return print(s, flush=True)


def main():
    s = S()

    m = {}
    m['ABC'] = True
    def f(s):
        if s in m:
            return m[s]
        t = s.replace('ABC', 'x')
        if s == t:
            m[s] = False
            return False
        for c in 'ABC':
            if f(t.replace('x',c)):
                return True
        m[s] = False
        return False

    if f(s):
        return 'Yes'

    return 'No'


print(main())

Submission status: Time Limit Exceeded

Solution code:
s = raw_input()
p = "ABC"
used = set()

while s != p:
    idx = 0
    c = [0]*3
    while idx < len(s):
        if idx+3 <= len(s) and s[idx:idx+3] == p:
            idx += 3
        else:
            c[p.index(s[idx])] += 1
            idx += 1
    if sum(e == 0 for e in c) != 1 or sum(c) == s:
        print "No"
        exit(0)
    s = s.replace("ABC", p[[i for i in [0,1,2] if c[i]==0][0]])
print "Yes"

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/python

if __name__ == "__main__":
	S = raw_input()
	c = ['A','B','C']
	
	while True:
		if S == "ABC":
			print "Yes"
			break
		
		T = S.strip().split("ABC")
		P = ''.join(T)
		
		cnt = 0
		for x in c:
			if x in P:
				cnt += 1
			else:
				S = x.join(T)
		
		if cnt != 2:
			print "No"
			break

Submission status: Time Limit Exceeded

Solution code:
s = input()
end = "ABC"
while True:
  if s == end:
    print("Yes")
    break
  s = s.replace("ABC", "X")
  if ("A" in s) + ("B" in s) + ("C" in s) != 2:
    print("No")
    break

  for c in "ABC":
    if c not in s:
      s = s.replace("X", c)
      break

Submission status: Memory Limit Exceeded

Solution code:
target=input()
old=["ABC"]
new=[]
word=["A","B","C"]
while old != []:
    if target in old:
        print("Yes")
        break
    for i in old:
        for j in word:
            new_word=i.replace(j,"ABC")
            if len(new_word)<=len(target):
                new.append(new_word)
    old=new[:]
    new=[]
else:
    print("No")

Submission status: Memory Limit Exceeded

Solution code:
target=input()
old=["ABC"]
new=[]
word=["A","B","C"]
while old != []:
    if target in old:
        print("Yes")
        break
    for i in old:
        for j in word:
            new_word=i.replace(j,"ABC")
            if len(new_word)==len(target):
                if new_word==target:
                    new.append(new_word)
                    break
                else:
                    pass
            elif len(new_word)< len(target):
                new.append(new_word)
            else:
                pass
    old=new[:]
    new=[]
else:
    print("No")

Submission status: Memory Limit Exceeded

Solution code:
special=False
target=input()
if target[0]=="A" and target[-1]=="C" and "B" in target and "ABC" in target:
    pass
else:
    special=True
    
old=["ABC"]
new=[]
word=["A","B","C"]
while old != [] and special ==False:
    if target in old:
        print("Yes")
        break
    for i in old:
        for j in word:
            new_word=i.replace(j,"ABC")
            if len(new_word)==len(target):
                if new_word==target:
                    new.append(new_word)
                    break
                else:
                    pass
            elif len(new_word)< len(target):
                new.append(new_word)
            else:
                pass
    old=new[:]
    new=[]
else:
    print("No")

Submission status: Accepted

Solution code:
target=input()
list_abc=["A","B","C"]
old=[]
new=[]
check=[]
flag=False
if "ABC" in target:
    old.append([target,[]])
while old !=[] and flag==False:
    for i in old:
        if i[0]=="ABC":
            check.append(i[1])
            flag=True
            break
        for j in list_abc:
            element=i[0].replace("ABC",j)
            if "ABC" in element:
                new.append([element,i[1]+[j]])
    else:
        old=new[:]
        new=[]
flag=False
for i in check:
    abc="ABC"
    li=i
    li.reverse()
    for j in li:
        abc=abc.replace(j,"ABC")
    if abc==target:
        flag=True
print("Yes" if flag==True else "No")

Submission status: Accepted

Solution code:
s = raw_input()
p = "ABC"
used = set()

while s != p:
    idx = 0
    c = [0]*3
    while idx < len(s):
        if idx+3 <= len(s) and s[idx:idx+3] == p:
            idx += 3
        else:
            c[p.index(s[idx])] += 1
            idx += 1
    if sum(e == 0 for e in c) != 1 or sum(c) == len(s):
        print "No"
        exit(0)
    s = s.replace("ABC", p[[i for i in [0,1,2] if c[i]==0][0]])
print "Yes"

Submission status: Accepted

Solution code:
# Str -> Bool
def solve(s):

    if len(s) < 3:
        return False

    history = []
    t = s
    while t.find("ABC") != -1:
        if t[0:3] == "ABC":
            t = t.replace("ABC", "A")
            history.append("A")
        elif t[-3:] == "ABC":
            t = t.replace("ABC", "C")
            history.append("C")
        else:
            t = t.replace("ABC", "B")
            history.append("B")

    # ??????1???????????§????????????
    if len(t) > 1:
        return False

    # ?§?????????´???????¢????
    for cmd in reversed(history):
        t = t.replace(cmd, "ABC")
    
    return t == s

def main():
    s = input()
    if solve(s):
        print("Yes")
    else:
        print("No")

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/python

if __name__ == "__main__":
	S = raw_input()
	c = ['A','B','C']
	
	while True:
		if len(S) <= 3:
			print "Yes" if S == "ABC" else "No"
			break
		
		T = S.strip().split("ABC")
		if len(T) == 1:
			print "No"
			break
			
		P = ''.join(T)
		cnt = 0
		for x in c:
			if x in P:
				cnt += 1
			else:
				S = x.join(T)
		
		if cnt != 2:
			print "No"
			break

Submission status: Accepted

Solution code:
#!/usr/bin/python

if __name__ == "__main__":
	S = raw_input()
	c = ['A','B','C']
	
	while True:
		if len(S) <= 3:
			print "Yes" if S == "ABC" else "No"
			break
		
		T = S.strip().split("ABC")			
		P = ''.join(T)
		
		cnt = 0
		for x in c:
			if x in P:
				cnt += 1
			else:
				S = x.join(T)
		
		if len(T) == 1 or cnt != 2:
			print "No"
			break

Submission status: Accepted

Solution code:
#!/usr/bin/env python
S = raw_input()

while S != 'ABC':
	S2 = ""
	i = 0
	while True:
		if i >= len(S):
			break
		if S[i:min(len(S), i + 3)] == "ABC":
			S2 += "X"
			i += 2
		else:
			S2 += S[i]
		i += 1
	a_flag = 'A' in S2
	b_flag = 'B' in S2
	c_flag = 'C' in S2
	if not 'X' in S2:
		print "No"
		exit()
	if a_flag + b_flag + c_flag != 2:
		print "No"
		exit()
	SS = ''
	if not a_flag:
		SS = 'A'
	if not b_flag:
		SS = 'B'
	if not c_flag:
		SS = 'C'
	S3 = ''
	for c in S2:
		if c == 'X':
			S3 += SS
		else:
			S3 += c
	S = S3

print "Yes"

Submission status: Accepted

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

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(0,-1),(1,0),(0,1),(-1,0)]
ddn = [(0,-1),(1,-1),(1,0),(1,1),(0,1),(-1,-1),(-1,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 pf(s): return print(s, flush=True)


def main():
    s = S()

    m = {}
    m['ABC'] = True
    def f(s):
        if s in m:
            return m[s]
        t = s.replace('ABC', 'x')
        if s == t:
            m[s] = False
            return False
        for c in 'ABC':
            if c in t:
                continue
            if f(t.replace('x',c)):
                return True
        m[s] = False
        return False

    if f(s):
        return 'Yes'

    return 'No'


print(main())

Submission status: Accepted

Solution code:
s = input()
end = "ABC"
while True:
  if s == end:
    print("Yes")
    break
  s = s.replace("ABC", "X")
  if ("X" not in s) or ("A" in s) + ("B" in s) + ("C" in s) != 2:
    print("No")
    break

  for c in "ABC":
    if c not in s:
      s = s.replace("X", c)
      break

Submission status: Accepted

Solution code:
s = input()
end = "ABC"
while True:
  if s == end:
    print("Yes")
    break
  s = s.replace(end, "X")
  if ("X" not in s) or sum((c in s for c in end)) != 2:
    print("No")
    break
  for c in end:
    if c not in s:
      s = s.replace("X", c)
      break

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(5010)

def solve(s): 
    if len(s) ==3:
        if s[0]=='A' and s[1]=='B' and s[2]=='C':
            return True
        else:
            return False
    i=0
    a = 0
    b = 0
    c = 0
    lis =[]
    while i < len(s):
        if i+2<len(s) and s[i] =='A' and s[i+1] == 'B' and s[i+2] == 'C':
            lis.append('X')
            i += 3
        else:
            lis.append(s[i])
            if s[i] == 'A':
                a = 1
            if s[i] == 'B':
                b = 1
            if s[i] == 'C':
                c = 1
            i += 1

    if not(a+b+c == 2):
        return False
    if a==0:
        for i in range(len(lis)):
            if lis[i] == 'X':
                lis[i]='A'
    if b==0:
        for i in range(len(lis)):
            if lis[i] == 'X':
                lis[i]='B'
    if c==0:
        for i in range(len(lis)):
            if lis[i] == 'X':
                lis[i]='C'
    if len(s) == len(lis):
        return False

    return solve(lis)


if solve(input()):
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
from collections import deque
S = input()
Q = deque([S])

while Q:
    s = Q.pop()
    if s == "ABC":
        print("Yes")
        break
    s = s.replace("ABC", "x")
    if not "x" in s:
        continue
    for y in "ABC":
        if not y in s:
            Q.append(s.replace("x", y))
else:
    print("No")

Submission status: Accepted

Solution code:
# coding: utf-8
moto=input()
s=moto
gene='ABC'
f=True
log=[]
while len(s)>3 and f:
    f=False
    if s[:3]=='ABC':
        s=s.replace('ABC','A')
        log.append('A')
        f=True
    elif s[len(s)-3:]=='ABC':
        s=s.replace('ABC','C')
        log.append('C')
        f=True
    elif 'ABC' in s:
        s=s.replace('ABC','B')
        log.append('B')
        f=True
if s!=gene:
    print('No')
    exit()
l=len(log)
for i in range(l):
    s=s.replace(log[l-1-i],'ABC')
if s==moto:
    print('Yes')
else:
    print('No')

Submission status: Runtime Error

Solution code:
special=False
target=input()
if target[0]=="A" and target[-1]=="C" and "B" in target and "ABC" in target:
    pass
else:
    special=True
    
special="True"
old=["ABC"]
new=[]
word=["A","B","C"]
while old != [] or special=True:
    if target in old:
        print("Yes")
        break
    for i in old:
        for j in word:
            new_word=i.replace(j,"ABC")
            if len(new_word)==len(target):
                if new_word==target:
                    new.append(new_word)
                    break
                else:
                    pass
            elif len(new_word)< len(target):
                new.append(new_word)
            else:
                pass
    old=new[:]
    new=[]
else:
    print("No")

Submission status: Runtime Error

Solution code:
s = raw_input()
used = set()
def dfs(s):
    if s in used:
        return 0
    if s == "ABC":
        return 1
    if "ABC" in s:
        for c in "ABC":
            if dfs(s.replace("ABC", c)):
                return 1
    used.add(s)
    return 0
print dfs(s)*"Yes"or"No"

Submission status: Runtime Error

Solution code:
import sys
s = raw_input()
used = set()
sys.setrecursionlimit(len(s))
def dfs(s):
    if s in used:
        return 0
    if s == "ABC":
        return 1
    if "ABC" in s:
        for c in "ABC":
            if dfs(s.replace("ABC", c)):
                return 1
    used.add(s)
    return 0
print dfs(s)*"Yes"or"No"

Submission status: Runtime Error

Solution code:
#!/usr/bin/python

if __name__ == "__main__":
	S = raw_input()
	c = ['A','B','C']
	
	while True:
		if len(S) <= 3:
			print "Yes" if S == "ABC" else "No"
			break
		
		T = S.strip().split("ABC")			
		P = ''.join(T)
		
		cnt = 0
		for x in c:
			if x in P:
				cnt += 1
			else:
				S = x.join(T)
		
		if len(t) == 1 or cnt != 2:
			print "No"
			break

Submission status: Runtime Error

Solution code:
def solve(s):
    if len(s) ==3:
        if s[0]=='A' and s[1]=='B' and s[2]=='C':
            return True
        else:
            return False
    i=0
    a = 0
    b = 0
    c = 0
    lis =[]
    while i < len(s):
        if i+2<len(s) and s[i] =='A' and s[i+1] == 'B' and s[i+2] == 'C':
            lis.append('X')
            i += 3
        else:
            lis.append(s[i])
            if s[i] == 'A':
                a = 1
            if s[i] == 'B':
                b = 1
            if s[i] == 'C':
                c = 1
            i += 1

    if not(a+b+c == 2):
        return False
    if a==0:
        for i in range(len(lis)):
            if lis[i] == 'X':
                lis[i]='A'
    if b==0:
        for i in range(len(lis)):
            if lis[i] == 'X':
                lis[i]='B'
    if c==0:
        for i in range(len(lis)):
            if lis[i] == 'X':
                lis[i]='C'
    if len(s) == len(lis):
        return False

    return solve(lis)


if solve(input()):
    print("Yes")
else:
    print("No")