Problem:


ヘビ

ある世界には，文字だけでできた不思議なヘビが住んでいます。このヘビには現在A種とB種の2種類が確認されていますが、それ以外の種類がいる可能性もあります。

A種は，">'"の後に"="が1個以上並んだ後、"#"が来て、さらに前と同じ個数の"="が来た後、"~"（半角チルダ）で終わります。

B種は，">^"の後に "Q="が1個以上並んだ後、"~~"で終わります。

A種の例： >'====#====~        >'==#==~

B種の例： >^Q=Q=Q=Q=~~        >^Q=Q=~~

ヘビを文字列データとして受け取り、それがどんな種類であるかを判別して、A種の場合は「A」、B種の場合は「B」、それ以外の種類の場合は「NA」を出力するプログラムを作成してください。

Input

入力は以下の形式で与えられます。

n
S1
S2
:
Sn

1 行目に判別されるヘビの数 n（1 ≤ n ≤ 10000）、続く n 行に i 匹目のヘビを表す文字列 Si (200文字以下の、空白を含まない文字列) がそれぞれ１行に与えられます。

Output

i 行目に i 匹目のヘビの種類 A、B または NA を出力してください。

Sample Input

3
>'======#======~
>^Q=Q=Q=Q=Q=Q=Q=Q=Q=~~
>'===#====~

Output for the Sample Input

A
B
NA



Sample input:
3
>'======#======~
>^Q=Q=Q=Q=Q=Q=Q=Q=Q=~~
>'===#====~


Sample output:
A
B
NA

Submission status: Wrong Answer

Solution code:
for i in [0]*input():
  s=raw_input()
  n=(len(s)-4)/2
  if s==">'"+"="*n+"#"+"="*n+"~": print "A"
  elif s==">^"+"Q="*n+"~~": print "B"
  else: print "NA"

Submission status: Wrong Answer

Solution code:
n=int(input())
for times in range(n):
	s=input()
	ans="NA"

	#A???????????§
	if s[:2]==">'" and s[-1]=="~":
		t=s[2:-1].split("#")

		if len(t)==2:
			valid=True
			for v in t[0]:
				if v!="=":
					valid=False
					break
			if valid and t[0]==t[1]:
				ans="A"

	#B???????????§
	elif s[:2]==">^" and s[-2:]=="~~":
		t=s[2:-2]

		if len(t)%2==0:
			valid=True
			for i in range(0,len(t),2):
				if t[i:i+2]!="Q=":
					valid=False
					break
			if valid:
				ans="B"

	print(ans)

Submission status: Wrong Answer

Solution code:
for _ in [0]*int(input()):
    s=input()
    if s[1]=='\'':
        l=s.find('#')
        r=s.rfind('~')
        print('A' if l-1==r-l else 'NA')
    else:
        r=s.rfind('~~')
        print('B' if s[1:r].count('Q')==s[1:r].count('=') else 'NA')

Submission status: Wrong Answer

Solution code:
for _ in [0]*int(input()):
    s=input()
    if s[:2]==">'" and s[-1]=='~' and '#' in s:
        s=s[2:-1].split('#')
        print(['NA','A'][set(s[0])==set(s[1])=={'='} and len(set(s))==1])
    elif s[:2]=='>^' and s[-2:]=='~~':
        s=s[2:-2]
        print(['NA','B'][len(s)==2*s.count('Q=') and len(s)>0])

Submission status: Wrong Answer

Solution code:
for _ in [0]*int(input()):
    s=input()
    a='NA'
    if s[1]=="'" and s[-1]=='~' and '#' in s:
        s=s[2:-1].split('#')
        a=[a,'A'][set(s[0])==set(s[1])=={'='} and len(set(s))==1]
    elif s[1]=='^' and s[-2:]=='~~':
        s=s[2:-2]
        a=['NA','B'][len(s)==2*s.count('Q=') and len(s)>0]
    print(a)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import sys
import os
import math

N = int(input())

def is_A(s):
    if s[0:2] != ">'":
        return False

    if s[-1] != '~':
        return False


    body = s[2:-1]
    lst = body.split('#')
    if len(lst) != 2:
        return False

    if lst[0] == lst[1] and len(lst[0]) == lst[0].count('='):
        return True
    else:
        return False

def is_B(s):
    if s[0:2] != ">^":
        return False

    if s[-2:] != '~~':
        return False

    body = s[2:-2]

    if body.count('Q=') == len(body) // 2:
        return True
    else:
        return False

for s in sys.stdin:
    s = s.strip()
    
    if is_A(s):
        print('A')
    elif is_B(s):
        print('B')
    else:
        print('NA')

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import sys
import os
import math

N = int(input())

def is_A(s):
    if s[0:2] != ">'":
        return False

    if s[-1] != '~':
        return False


    body = s[2:-1]

    if len(body) % 2 == 0:
        return False

    lst = body.split('#')
    if len(lst) != 2:
        return False

    if lst[0] == lst[1] and len(lst[0]) == lst[0].count('='):
        return True
    else:
        return False

def is_B(s):
    if s[0:2] != ">^":
        return False

    if s[-2:] != '~~':
        return False

    body = s[2:-2]

    if len(body) % 2 != 0:
        return False
    elif body.count('Q=') == len(body) / 2:
        return True
    else:
        return False

for s in sys.stdin:
    s = s.strip()

    if is_A(s):
        print('A')
    elif is_B(s):
        print('B')
    else:
        print('NA')

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import sys
import os

N = int(input())

def is_A(s):
    if s[0:2] != ">'":
        return False

    if s[-1] != '~':
        return False

    body = s[2:-1]

    if len(body) % 2 == 0:
        return False

    # body = '==#=='
    center_index = len(body) // 2
    if body[center_index] != '#':
        return False

    if body.count('#') != 1:
        return False

    lst = body.split('#')
    if len(lst) != 2:
        return False

    if lst[0] == lst[1] and len(lst[0]) == lst[0].count('='):
        return True
    else:
        return False

def is_B(s):
    if s[0:2] != ">^":
        return False

    if s[-2:] != '~~':
        return False

    body = s[2:-2]

    if len(body) % 2 != 0:
        return False
    elif body.count('Q=') == len(body) / 2:
        return True
    else:
        return False

for s in sys.stdin:
    s = s.strip()

    if is_A(s):
        print('A')
    elif is_B(s):
        print('B')
    else:
        print('NA')

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import sys
import os

N = int(input())

def is_A(s):
    if s[0:2] != ">'":
        return False

    if s[-1] != '~':
        return False

    body = s[2:-1]

    if len(body) % 2 == 0:
        return False

    # body = '==#=='
    center_index = len(body) // 2
    if body[center_index] != '#':
        return False

    if body.count('#') != 1:
        return False

    lst = body.split('#')
    if len(lst) != 2:
        return False

    if lst[0] == lst[1] and len(lst[0]) == lst[0].count('='):
        return True
    else:
        return False

def is_B(s):
    if s[0:2] != ">^":
        return False

    if s[-2:] != '~~':
        return False

    body = s[2:-2]

    if len(body) % 2 != 0:
        return False
    elif body.count('Q=') > 0 and body.count('Q=') == len(body) // 2:
        return True
    else:
        return False

for s in sys.stdin:
    s = s.strip()

    if is_A(s):
        print('A')
    elif is_B(s):
        print('B')
    else:
        print('NA')

Submission status: Wrong Answer

Solution code:
class Ex(Exception):
    pass
n=int(input())
for i in range(n):
    try:
        snk=input()
        l=len(snk)
        head=snk[:2]
        body=snk[2:l-1]
        tail=snk[l-1]
        if tail!='~' or snk[0]!='>':
            print("NA")
            continue
        spl_a=body.split('#')
        if len(spl_a)==2 and spl_a[0]==spl_a[1]:
            for char in spl_a[0]:
                if char!='=':
                    print("NA")
                    raise Ex()
            if head[1]=='\'':
                print('A')
                raise Ex()
            else:
                print("NA")
                raise Ex()
        bars=body.split('=')
        for j in range(len(bars)-1):
            if bars[j]!='Q':
                print("NA")
                raise Ex()
        if head[1]=='^':print('B')
        else:print("NA")
    except Ex:pass

Submission status: Wrong Answer

Solution code:
class Ex(Exception):
    pass
n=int(input())
for i in range(n):
    try:
        snk=input()
        l=len(snk)
        head=snk[:2]
        body=snk[2:l-1]
        tail=snk[l-1]
        if tail!='~' or snk[0]!='>':
            print("NA")
            continue
        spl_a=body.split('#')
        if len(spl_a)==2 and spl_a[0]==spl_a[1]:
            for char in spl_a[0]:
                if char!='=':
                    print("NA")
                    raise Ex()
            if head[1]=='\'':
                print('A')
                raise Ex()
            else:
                print("NA")
                raise Ex()
        bars=body.split('=')
        for j in range(len(bars)-1):
            if bars[j]!='Q':
                print("NA")
                raise Ex()
        if head[1]=='^'and bars[len(bars)-1]=='~':print('B')
        else:print("NA")
    except Ex:pass

Submission status: Wrong Answer

Solution code:
class Ex(Exception):
    pass
n=int(input())
for i in range(n):
    try:
        snk=input()
        l=len(snk)
        head=snk[:2]
        body=snk[2:l-1]
        tail=snk[l-1]
        if tail!='~' or snk[0]!='>':
            print("NA")
            continue
        spl_a=body.split('#')
        if len(spl_a)==2 and spl_a[0]==spl_a[1] and len(spl_a[0])!=0:
            for char in spl_a[0]:
                if char!='=':
                    print("NA")
                    raise Ex()
            if head[1]=='\'':
                print('A')
                raise Ex()
            else:
                print("NA")
                raise Ex()
        bars=body.split('=')
        for j in range(len(bars)-1):
            if bars[j]!='Q':
                print("NA")
                raise Ex()
        if head[1]=='^'and bars[len(bars)-1]=='~':print('B')
        else:print("NA")
    except Ex:pass

Submission status: Wrong Answer

Solution code:
# Aizu Problem 00139: Snakes
#
import sys, math, os, copy

# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input.txt", "rt")


def is_A_snake(snake):
    if snake[1] != "'":
        return False
    snake = snake[2:-1]
    segments = snake.split('#')
    L = len(segments[0])
    target = '=' * L
    for segment in segments:
        if segment != target:
            return False
    return True


def is_B_snake(snake):
    if snake[1] != "^" or snake[-2] != '~' or snake[2] != 'Q' or snake[-3] != '=':
        return False
    snake = snake[2:-2]
    L = len(snake)
    return snake == "Q=" * (L // 2)


n = int(input())
for _ in range(n):
    snake = input().strip()
    if snake[0] != '>' or snake[-1] != '~':
        print("NA")
    else:
        if '#' in snake:
            print("A" if is_A_snake(snake) else "NA")
        else:
            print("B" if is_B_snake(snake) else "NA")

Submission status: Wrong Answer

Solution code:
# Aizu Problem 00139: Snakes
#
import sys, math, os, copy

# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input.txt", "rt")


def is_A_snake(snake):
    if snake[1] != "'":
        return False
    snake = snake[2:-1]
    segments = snake.split('#')
    if len(segments) != 2:
        return False
    L = len(segments[0])
    target = '=' * L
    for segment in segments:
        if segment != target:
            return False
    return True


def is_B_snake(snake):
    if snake[1] != "^" or snake[-2] != '~' or snake[2] != 'Q' or snake[-3] != '=':
        return False
    snake = snake[2:-2]
    L = len(snake)
    return snake == "Q=" * (L // 2)


n = int(input())
for _ in range(n):
    snake = input().strip()
    if snake[0] != '>' or snake[-1] != '~':
        print("NA")
    else:
        if '#' in snake:
            print("A" if is_A_snake(snake) else "NA")
        else:
            print("B" if is_B_snake(snake) else "NA")

Submission status: Wrong Answer

Solution code:
n = int(input())
for i in range(n):
    s = input()
    if s[:2] == ">'" and s[-1] == '~' and len(s) % 2 == 0:
        temp = ">'" + '=' * (s.count('=')//2) + '#' + '=' * (s.count('=')//2) + '~'
        if s == temp:
            print('A')
            continue
    elif s[:2] == ">^" and s[-2:] == '~~' and len(s) % 2 == 0:
        temp = ">^" + 'Q=' * ((len(s)-4)//2) + '~~'
        if s == temp:
            print('B')
            continue
    print('NA')

Submission status: Wrong Answer

Solution code:
def checkA(s):
  if s[:2] == ">'" and s[-1] == "~":
    body = s[2:-1]
    if len(body) >= 3 and body.count("#") == 1 and body.index("#") == len(body) // 2:
      return True
    else:
      return False
  else:
    return False

def checkB(s):
  if s[:2] == ">^" and s[-2:] == "~~":
    body = s[2:-2]
    if len(body) > 0 and len(body) % 2 == 0:
      for i in range(0, len(body), 2):
        if body[i:i + 2] != "Q=":
          return False
      else:
        return True
    else:
      return False
  else:
    return False

n = int(input())
for _ in range(n):
  s = input()
  if len(s) <= 5:
    print("NA")
  else:
    if checkA(s):
      print("A")
    elif checkB(s):
      print("B")
    else:
      print("NA")

Submission status: Wrong Answer

Solution code:
# AOJ 0139 Snakes
# Python3 2018.6.19 bal4u

import re

# A >'======#======~
# B >^Q=Q=Q=Q=Q=Q=Q=Q=Q=~~
pa = ">'(=*)#(=*)~$"
pb = ">\\^(Q=)+~~$"

for i in range(int(input())):
	s = input()
	r = re.match(pa, s)
	if r and r.group(1) == r.group(2): print('A')
	elif re.match(pb, s): print('B')
	else: print('NA')

Submission status: Wrong Answer

Solution code:
def snakeA(s):
    if s[:2]==">'" and s[-1]=="~":
        t=s[2:-1].split("#")
        if t[0]==t[-1]:
            return True
    return False

def snakeB(s):
    if s[:2]==">^" and s[-2:]=="~~":
        t=s[2:-2]
        if set(t.split("Q="))==set(['']):
            return True
    return False

for i in range(input()):
    s=raw_input()
    if snakeA(s):
        print "A"
    elif snakeB(s):
        print "B"
    else:
        print "NA"

Submission status: Wrong Answer

Solution code:
for r in range(input()):
	snake=raw_input()
	L=len(snake)
	if snake == ">'" + "="*((L-4)/2) + "#" + "="*((L-4)/2) + "~":
		print "A"
	elif snake == ">^" + "Q="*((L-4)/2) + "~~":
		print "B"
	else:
		print "NA"

Submission status: Wrong Answer

Solution code:
import re

def A(s):
  if s.count('#') != 1 or s[-1] != '~':
    return False
  a, b = s[:-1].split('#')
  return a.count('=') == b.count('=') == len(a)
def B(s):
  return re.match('^(Q=)+~~$', s)

N = input()
for i in range(N):
  s = raw_input()
  a = 'NA'
  if s.startswith(">'"):
    if A(s[2:]):
      a = 'A'
  if s.startswith('>^'):
    if B(s[2:]):
      a = 'B'
  print(a)

Submission status: Accepted

Solution code:
import re

A = ">'(=+)#\\1~$"
B = ">\^(Q=)+~~$" 

for i in range(int(input())):
    s = input()
    r = re.match(A, s)
    if re.match(A, s): print('A')
    elif re.match(B, s): print('B')
    else: print('NA')

Submission status: Accepted

Solution code:
for i in [0]*input():
  s=raw_input()
  n=(len(s)-4)/2
  if s==">'"+"="*n+"#"+"="*n+"~": t="A"
  elif s==">^"+"Q="*n+"~~": t="B"
  else: t="NA"
  if n<1: t="NA"
  print t

Submission status: Accepted

Solution code:
for i in [0]*input():
  s=raw_input()
  n=(len(s)-4)/2
  if n>0 and s==">'"+"="*n+"#"+"="*n+"~": t="A"
  elif n>0 and s==">^"+"Q="*n+"~~": t="B"
  else: t="NA"
  print t

Submission status: Accepted

Solution code:
def A():
    p = s[2:]
    count = 0
    index = 0
    for i, c in enumerate(p):
        if c == '=':
            count += 1
        elif c == '#':
            index = i
            break
        else:
            print 'NA'
            return
    else:
        if not index:
            print 'NA'
            return
    p = p[index+1:-1]
    for c in p:
        if c == '=':
            count -= 1
        else:
            print 'NA'
            return
    if count == 0 and s[-1] == '~':
        print 'A'
    else:
        print 'NA'
         
def B():
    p = s[2:-2]
    for i in range(0, len(p), 2):
        if not (p[i]+p[i+1] == 'Q='):
            print 'NA'
            return
    if s[-2:] == '~~':
        print 'B'
    else:
        print 'NA'

for i in range(input()):
    s = raw_input()
    if s[:3] == ">'=":
        A()
    elif s[:4] == ">^Q=":
        B()
    else:
        print 'NA'

Submission status: Accepted

Solution code:
n=int(input())
for times in range(n):
	s=input()
	ans="NA"

	#A???????????§
	if s[:2]==">'" and s[-1]=="~":
		t=s[2:-1].split("#")

		if len(t)==2 and len(t[0])>0:
			valid=True
			for v in t[0]:
				if v!="=":
					valid=False
					break
			if valid and t[0]==t[1]:
				ans="A"

	#B???????????§
	elif s[:2]==">^" and s[-2:]=="~~":
		t=s[2:-2]

		if len(t)>0 and len(t)%2==0:
			valid=True
			for i in range(0,len(t),2):
				if t[i:i+2]!="Q=":
					valid=False
					break
			if valid:
				ans="B"

	print(ans)

Submission status: Accepted

Solution code:
for _ in [0]*int(input()):
    s=input()
    if s[:2]==">'" and s[-1]=='~' and '#' in s:
        s=s[2:-1].split('#')
        print(['NA','A'][set(s[0])==set(s[1])=={'='} and len(set(s))==1])
    elif s[:2]=='>^' and s[-2:]=='~~':
        s=s[2:-2]
        print(['NA','B'][len(s)==2*s.count('Q=') and len(s)>0])
    else:print('NA')

Submission status: Accepted

Solution code:
for _ in [0]*int(input()):
    s=input()
    a='NA'
    if s[:2]==">'" and s[-1]=='~' and '#' in s:
        s=s[2:-1].split('#')
        a=[a,'A'][set(s[0])==set(s[1])=={'='} and len(set(s))==1]
    elif s[:2]=='>^' and s[-2:]=='~~':
        s=s[2:-2]
        a=['NA','B'][len(s)==2*s.count('Q=') and len(s)>0]
    print(a)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys
import os

N = int(input())

def is_A(s):
    if s[0:2] != ">'":
        return False

    if s[-1] != '~':
        return False

    body = s[2:-1]

    if len(body) % 2 == 0:
        return False

    # body = '==#=='
    center_index = len(body) // 2
    if body[center_index] != '#':
        return False

    if body.count('#') != 1:
        return False

    lst = body.split('#')
    if len(lst) != 2:
        return False

    if len(lst[0]) > 0 and lst[0] == lst[1] and len(lst[0]) == lst[0].count('='):
        return True
    else:
        return False

def is_B(s):
    if s[0:2] != ">^":
        return False

    if s[-2:] != '~~':
        return False

    body = s[2:-2]

    if len(body) % 2 != 0:
        return False
    elif body.count('Q=') > 0 and body.count('Q=') == len(body) // 2:
        return True
    else:
        return False

for s in sys.stdin:
    s = s.strip()

    if is_A(s):
        print('A')
    elif is_B(s):
        print('B')
    else:
        print('NA')

Submission status: Accepted

Solution code:
class Ex(Exception):
    pass
n=int(input())
for i in range(n):
    try:
        snk=input()
        l=len(snk)
        head=snk[:2]
        body=snk[2:l-1]
        tail=snk[l-1]
        if tail!='~' or snk[0]!='>':
            print("NA")
            continue
        spl_a=body.split('#')
        if len(spl_a)==2 and spl_a[0]==spl_a[1] and len(spl_a[0])!=0:
            for char in spl_a[0]:
                if char!='=':
                    print("NA")
                    raise Ex()
            if head[1]=='\'':
                print('A')
                raise Ex()
            else:
                print("NA")
                raise Ex()
        bars=body.split('=')
        if len(bars)==1:
            print("NA")
            continue
        for j in range(len(bars)-1):
            if bars[j]!='Q':
                print("NA")
                raise Ex()
        if head[1]=='^'and bars[len(bars)-1]=='~':print('B')
        else:print("NA")
    except Ex:pass

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0139

"""
import sys
from sys import stdin
import re
input = stdin.readline


def identify_snake(snake):
    # ????????????>`???????¶???????1?????\?????????=]??????#??????????????§1?????\?????????=??????????????????~??????????????¨?????????=?????°?????????
    patternA = re.compile("^>'(=+)#(=+)~")
    resultA = patternA.search(snake)
    if resultA:
        pre, post = resultA.groups()
        if pre == post:
            return 'A'

    # ????????????>^???????¶???????1?????\?????????Q=]???????????????????????????~~???
    patternB = re.compile("^>\^(Q=)+~~")
    resultB = patternB.search(snake)
    if resultB:
        return 'B'

    return 'NA'


def main(args):
    n = int(input())
    for _ in range(n):
        snake = input()
        result = identify_snake(snake)
        print(result)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
import re

SNAKE_A = re.compile(r">'(=+)#\1~")
SNAKE_B = re.compile(r">\^(Q=)+~~")


def answer(regex, string, output):
    result = regex.fullmatch(string)

    if result is not None:
        print(output)
    else:
        print("NA")


for _ in range(int(input())):

    snake = input()

    if snake[1] == "'":
        answer(SNAKE_A, snake, "A")
    else:
        answer(SNAKE_B, snake, "B")

Submission status: Accepted

Solution code:
n = int(input())
for i in range(n):
    s = input()
    if s[:2] == ">'" and s[-1] == '~' and len(s) % 2 == 0 and '=#=' in s:
        temp = ">'" + '=' * (s.count('=')//2) + '#' + '=' * (s.count('=')//2) + '~'
        if s == temp:
            print('A')
            continue
    elif s[:2] == ">^" and s[-2:] == '~~' and len(s) % 2 == 0 and 'Q=' in s:
        temp = ">^" + 'Q=' * ((len(s)-4)//2) + '~~'
        if s == temp:
            print('B')
            continue
    print('NA')

Submission status: Accepted

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

ans = ['A', 'B', 'NA']
for _ in range(int(input())):
    s = input()
    l = max([1, (len(s) - 4) // 2])
    p = [r">'"+("="*l)+"#"+("="*l)+"~", r'>\^(Q=)+~~', r'\S']
    for i in range(3):
        if re.match(p[i], s):
            print(ans[i])
            break

Submission status: Accepted

Solution code:
def checkA(s):

    l = len(s)
    if l % 2 == 0:
        return False

    ll = (l-1)//2

    for i in range(ll):
        if s[i] != "=" or s[l-1-i] != "=":
            return False

    if s[ll] != "#":
        return False

    return True

def checkB(s):
    l = len(s)

    if l % 2 == 1:
        return False

    ll = l // 2
    for i in range(ll):
        if s[2*i] != "Q" or s[2*i+1] != "=":
            return False

    return True

def check(s):
    if len(s) < 6:
        return "NA"

    if s[0:2] == ">'" and s[-1:] == "~":
        if checkA(s[2:-1]):
            return "A"
        else:
            return "NA"

    if s[0:2] == ">^" and s[-2:] == "~~":
        if checkB(s[2:-2]):
            return "B"
        else:
            return "NA"

    return "NA"

N = int(input())

for i in range(N):
    s = input()
    print(check(s))

Submission status: Accepted

Solution code:
def checkA(s):
  if s[:2] == ">'" and s[-1] == "~":
    body = s[2:-1]
    if len(body) >= 3 and body.count("#") == 1 and body.index("#") == len(body) // 2 and body.count("=") == len(body) - 1:
      return True
    else:
      return False
  else:
    return False

def checkB(s):
  if s[:2] == ">^" and s[-2:] == "~~":
    body = s[2:-2]
    if len(body) > 0 and len(body) % 2 == 0:
      for i in range(0, len(body), 2):
        if body[i:i + 2] != "Q=":
          return False
      else:
        return True
    else:
      return False
  else:
    return False

n = int(input())
for _ in range(n):
  s = input()
  if len(s) <= 5:
    print("NA")
  else:
    if checkA(s):
      print("A")
    elif checkB(s):
      print("B")
    else:
      print("NA")

Submission status: Accepted

Solution code:
# AOJ 0139 Snakes
# Python3 2018.6.19 bal4u

import re

pa = ">'(=+)#(=+)~$"	# A >'======#======~
pb = ">\^(Q=)+~~$"		# B >^Q=Q=Q=Q=Q=Q=Q=Q=Q=~~

for i in range(int(input())):
	s = input()
	r = re.match(pa, s)

	if r and r.group(1) == r.group(2): print('A')
	elif re.match(pb, s): print('B')
	else: print('NA')

Submission status: Accepted

Solution code:
import re
ptA = re.compile(r"^>'(=+)#\1~$")
ptB = re.compile(r"^>\^(?:Q=)+~~$")
for i in xrange(input()):
	s = raw_input()
	if ptA.match(s):
		print "A"
	elif ptB.match(s):
		print "B"
	else:
		print "NA"

Submission status: Accepted

Solution code:
import re

ptn_A = r'>\'(=+)#(=+)~'
ptn_B = r'>\^(Q=)+~~'

for i in range(int(raw_input())):
    snake = raw_input()
    matchA = re.search(ptn_A, snake)
    matchB = re.search(ptn_B, snake)
    if matchA and matchA.group(1) == matchA.group(2):
        print 'A'
    elif matchB:
        print 'B'
    else:
        print 'NA'

Submission status: Accepted

Solution code:
def snakeA(s):
    if s.startswith(">'") and s.endswith("~"):
        if s.count("#")==1:
            t=s[2:-1].split("#")
            if len(t)==2 and t[0]==t[1]:
                if set(list(t[0]))==set(list(t[1]))==set(["="]):
                    return True
    return False

def snakeB(s):
    if s.startswith(">^") and s.endswith("~~"):
        t=s[2:-2].split("Q=")
        if len(t)>1 and set(t)==set([""]):
            return True
    return False

for i in range(input()):
    s=raw_input()
    if snakeA(s):
        print "A"
    elif snakeB(s):
        print "B"
    else:
        print "NA"

Submission status: Accepted

Solution code:
from re import match
A = r"^>'(=+)#\1~$"
B = r"^>\^(Q=)+~~$"
for _ in xrange(input()):
    s = raw_input()
    if match(A,s) != None:
        print "A"
    elif match(B,s) != None:
        print "B"
    else:
        print "NA"

Submission status: Accepted

Solution code:
for r in range(input()):
	snake=raw_input()
	L=len(snake)
	if L>5 and snake == ">'" + "="*((L-4)/2) + "#" + "="*((L-4)/2) + "~":
		print "A"
	elif L>5 and snake == ">^" + "Q="*((L-4)/2) + "~~":
		print "B"
	else:
		print "NA"

Submission status: Accepted

Solution code:
for r in range(input()):
	snake=raw_input()
	m=(len(snake)-4)/2
	if m>0 and snake == ">'" + "="*m + "#" + "="*m + "~":
		print "A"
	elif m>0 and snake == ">^" + "Q="*m + "~~":
		print "B"
	else:
		print "NA"

Submission status: Accepted

Solution code:
for i in range(input()):
  s = raw_input()
  l = (len(s) - 4) / 2
  if l > 0 and s == ">'%s#%s~" % (('=' * l,) * 2):
    print('A')
  elif l > 0 and s == '>^%s~~' % ('Q=' * l):
    print('B')
  else:
    print('NA')

Submission status: Accepted

Solution code:
import re
snakeA = re.compile('>\'(=+)#\\1~$')
snakeB = re.compile('>\^(Q=)+~~$')
for i in xrange(input()):
    s = raw_input()
    if snakeA.match(s) is not None:
        print "A"
    elif snakeB.match(s) is not None:
        print "B"
    else:
        print "NA"

Submission status: Accepted

Solution code:
import re

n = int(input())
for i in range(n):
    s = input()
    if re.match(r">'(=+)#\1~", s):
        print('A')
    elif re.match(r">\^(Q=)+~~", s):
        print('B')
    else:
        print('NA')

Submission status: Accepted

Solution code:
import re

n=int(input())

def A(snake):
    r=re.match("^>\'(\=+)\#(\=+)\~$",snake)
    if r:
        if len(r.group(1))==len(r.group(2)):
            return True
    return False
def B(snake):
    r=re.match("^\>\^(Q=)+\~\~$",snake)
    if r:
        return True
    return False

for i in range(n):
    S=input()
    if A(S):
        print("A")
    elif B(S):
        print("B")
    else:
        print("NA")

Submission status: Accepted

Solution code:
import re
for i in range(int(input())):
    s=input()
    if re.match(">'(=+)#(=+)~",s):print('A')
    elif re.match(">\^(Q=)+~~",s):print('B')
    else:print('NA')

Submission status: Accepted

Solution code:
import re

a = ">'(=+)#(=+)~$"
b = ">\^(Q=)+~~$"
n = int(input())

for _ in range(n):
    s = input()
    r = re.match(a, s)
    if r and r.group(1) == r.group(2):
        print('A')
    elif re.match(b, s):
        print('B')
    else:
        print('NA')

Submission status: Accepted

Solution code:
import re
A = ">'=+#=+~$"
B = ">\^(Q=)+~~$"
n = int(input())
for i in range(n):
    s = input()
    if re.match(A,s):
        a, b = re.match(A,s).group().split("#")
        print("A" if a.count("=") == b.count("=") else "NA")
    elif re.match(B,s):
        print("B")    
    else:
        print("NA")

Submission status: Accepted

Solution code:
n = int(input())
ans_list = []
for i in range(n):
    s = input()
    if s[0] == "~" and s[-1] == ">":
        s = s[::-1]
    if s[0] == ">" and s[1] == "'" and s[-2] == "=" and s[-1] == "~":
        check_s = s[2:-1].split("#")
        if len(check_s) == 2:
            front = check_s[0]
            back = check_s[1]
            if front == back and set(front) == {"="}:
                ans = "A"
            else:
                ans = "NA"
        else:
            ans = "NA"
    elif s[0] == ">" and s[1] == "^" and s[-2] == "~" and s[-1] == "~":
        check_s = s[2:-2]
        check_list = set(check_s.split("Q="))
        if check_list == {""} and "Q=" in check_s:
            ans = "B"
        else:
            ans = "NA"
    else:
        ans = "NA"
    ans_list.append(ans)
for a in ans_list:
    print(a)

Submission status: Runtime Error

Solution code:
def snakeJudge(snake):
	if snake[:2] == ">'" : 
		snake = snake[2 : len(snake) - 1]
		x1 = snake[0 : snake.index("#")]
		x2 = snake[snake.index("#") + 1: len(snake)]
		if x1 == x2 : return "A"
	if snake[:2] == ">^" :
		judge = True
		snake = snake[2 : len(snake) - 2]
		x1 = snake.count("Q=")
		x2 = len(snake)
		if x2 / 2 == x1 : return "B"
	return "NA"
		
n = input()

for i in range(0, n):
	snake = raw_input()
	print snakeJudge(snake)

Submission status: Runtime Error

Solution code:
for _ in [0]*int(input()):
    s=input()
    if s[1]=='\'':
        a=s[2:-1].split('#')
        print(['NA','A'][set(a[0])==set(a[1])=={'='} and len(set(a))==1])
    else:
        s=s[2:-2]
        print(['NA','B'][len(s)==2*s.count('Q=')])

Submission status: Runtime Error

Solution code:
for _ in [0]*int(input()):
    s=input()
    if s[:2]=='>\'' and s[-1]=='~':
        a=s[2:-1].split('#')
        print(['NA','A'][set(a[0])==set(a[1])=={'='} and len(set(a))==1])
    elif s[:2]=='>^' and s[-2:]=='~~':
        s=s[2:-2]
        print(['NA','B'][len(s)==2*s.count('Q=')])