You are given a string, and you have to validate whether it's a valid Roman numeral. If it is valid, print True. Otherwise, print False. Try to create a regular expression for a valid Roman numeral.

Input Format

A single line of input containing a string of Roman characters.

Output Format

Output a single line containing True or False according to the instructions above.

Constraints

The number will be between $\mbox{I}$ and $\textbf{3999}$ (both included).

Sample Input

CDXXI

Sample Output

True

References  

Regular expressions are a key concept in any programming language. A quick explanation with Python examples is available here. You could also go through the link below to read more about regular expressions in Python.  

https://developers.google.com/edu/python/regular-expressions

Difficulty: EASY

Solutions:
import re
p = re.compile('^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$')
if p.match(input()):
	print('True')
else:
	print('False')


import re
num = input()
if re.match('^(M{0,3})(CM|DC{0,3}|C{0,3}|CD)(XC|LX{0,3}|X{0,3}|XL)(IX|VI{0,3}|I{0,3}|IV)$', num):
	print('True')
else:
	print('False')


import re
x = input()
if re.match('(M{0,3})(CM|DC{0,3}|CD|C{0,3})?(XC|LX{0,3}|XL|X{0,3})?(IX|VI{0,3}|IV|I{0,3})?$', x):
	print('True')
else:
	print('False')


import re
thou = '(?:M{0,3})'
hund = '(?:CM|CD|D?C{0,3})'
tens = '(?:XC|XL|L?X{0,3})'
ones = '(?:IX|IV|V?I{0,3})'
numeralre = re.compile('^' + thou + hund + tens + ones + '$')
print('True' if numeralre.match(input()) else 'False')


import re
rome_pat = '^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$'
if re.match(rome_pat, input()):
	print('True')
else:
	print('False')


import re
if re.search('^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$', input()):
	print('True')
else:
	print('False')


import re
match = re.match('^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$', input())
if match:
	print(True)
else:
	print(False)


import re
pattern = '\\AM{0,3}((C{0,3})|(C?D)|(DC{0,3})|(CM))?((X{0,3})|(X?L)|(LX{0,3})|(XC))?((I{0,3})|(I?V)|(VI{0,3})|(IX))?\\Z'
print('True' if re.match(pattern, input()) else 'False')


import re
teststring = input()
match = re.search('^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$', teststring)
if match:
	print('True')
else:
	print('False')


import re
text = input()
pattern = '^M?M?M?(CM|CD|D?C?C?C?)(XC|XL|L?X?X?X?)(IX|IV|V?I?I?I?)$'
match_object = re.search(pattern, text)
if match_object:
	print('True')
else:
	print('False')


import re
match = re.search('^M{0,3}(C{0,3}|CD|DC{0,3}|CM)(X{0,3}|XL|LX{0,3}|XC)(I{0,3}|IV|VI{0,3}|IX)$', input())
if match:
	print('True')
else:
	print('False')


import fileinput
import re
for line in fileinput.input():
	if re.search('^(M(MM?)?)?((CD)|(D?(C(CC?)?)?)?|(CM))?((XL)|(L?(X(XX?)?)?)?|(XC))?((IV)|(V?(I(II?)?)?)?|(IX))?$', line):
		print(True)
	else:
		print(False)


import re
pattern = re.compile('^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$')
s = input()
if pattern.match(s):
	print(True)
else:
	print(False)


import re
ROMAN = re.compile('M{0,3}(C{0,3}|CM|CD|DC{0,3})(X{0,3}|XC|XL|LX{0,3})(I{0,3}|IV|VI{1,3}|IX)$')
print(ROMAN.match(input()) is not None)


import re
rymRe = '^(M{0,3})(D?C{0,3}|C[DM])(L?X{0,3}|X[LC])(V?I{0,3}|I[VX])$'
rym = input()
if re.fullmatch(rymRe, rym):
	print(True)
else:
	print(False)


import re
if re.match('^M{0,3}(C[DM]|D?C{0,3})(X[LC]|L?X{0,3})(I[VX]|V?I{0,3})$', str(input())):
	print('True')
else:
	print('False')


from sys import stdin
from re import compile
from re import match
data = stdin.read().strip().split()
pat = '^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$'
exp = compile(pat)
print(*[exp.match(num) != None for num in data], sep='\n')


import re
mregex = re.compile('^M{0,3}(CM|DC{0,3}|CD|C{0,3})(XC|LX{0,3}|XL|X{0,3})(IX|VI{0,3}|IV|I{0,3})$')
l = input()
x = mregex.match(l)
if x:
	print('True')
else:
	print('False')


import re
RN = str(input())
p = re.compile('X{0,3}(?:IV|V{0,1}I{0,3}|IX)|LX{0,3}(?:IV|V{0,1}I{0,3}|IX)|LIX|XL(?:IV|V{0,1}I{0,3}|IX)|C(?:XL|L{0,1}X{0,3})(?:IV|V{0,1}I{0,3}|IX)|XC(?:IV|V{0,1}I{0,3}|IX)|DC{0,3}(?:XL|L{0,1}X{0,3})(?:IV|V{0,1}I{0,3}|IX)|DXC(?:IV|V{0,1}I{0,3}|IX)|CD(?:XL|L{0,1}X{0,3})(?:IV|V{0,1}I{0,3}|IX)|CDXC(?:IV|V{0,1}I{0,3}|IX)|M{1,3}(?:CD(?:XL|L{0,1}X{0,3})(?:IV|V{0,1}I{0,3}|IX)|D{0,1}XC(?:IV|V{0,1}I{0,3}|IX)|D{0,1}C{0,3}(?:XL|L{0,1}X{0,3})(?:IV|V{0,1}I{0,3}|IX))|CM(?:XL|L{0,1}X{0,3})(?:IV|V{0,1}I{0,3}|IX)|M{1,3}CM(?:IV|V{0,1}I{0,3}|IX|LX{0,3}(?:IV|V{0,1}I{0,3}|IX)|LIX|XL(?:IV|V{0,1}I{0,3}|IX)|XC(?:IV|V{0,1}I{0,3}|IX))')
m = p.fullmatch(RN)
if m:
	print('True')
else:
	print('False')


import re
print(re.match('^[M]{0,3}(CM|CD|[D]?[C]{0,3})(XC|XL|[L]?[X]{0,3})(IX|IV|[V]?[I]{0,3})$', input()) is not None)


import re
a = input()
ro = '^M{,3}(CM|DC{,3}|CD|C{,3})(XC|LX{,3}|XL|X{,3})(IX|VI{,3}|IV|I{,3})$'
if re.search(ro, a):
	print('True')
else:
	print('False')


import fileinput
import re
print(True if re.match('^M{0,3}(C[DM]|D?C{0,3})(X[LC]|L?X{0,3})(I[VX]|V?I{0,3})$', input()) else False)


import re
print(None != re.match('^M{0,3}(CD|CM|D?C{0,3})(XL|XC|L?X{0,3})(IV|IX|V?I{0,3})$', input()))


import re
romanNumeralMap = (('M', 1000), ('CM', 900), ('D', 500), ('CD', 400), ('C', 100), ('XC', 90), ('L', 50), ('XL', 40), ('X', 10), ('IX', 9), ('V', 5), ('IV', 4), ('I', 1))
romanNumeralPattern = '^M?M?M?(CM|CD|D?C?C?C?)(XC|XL|L?X?X?X?)(IX|IV|V?I?I?I?)$'

def fromRoman(s):
	if not re.search(romanNumeralPattern, s):
		return s
	result = 0
	index = 0
	for (numeral, integer) in romanNumeralMap:
		while s[index:index + len(numeral)] == numeral:
			result += integer
			index += len(numeral)
	return result
s = str(input())
s = str(fromRoman(s))
if s.isdigit():
	print('True')
else:
	print('False')


def check(s):
	yes = True
	for i in range(len(s)):
		yes = yes and s[i] in 'IVXLCDM'
	return yes

def repeat(s):
	yes = True
	count = 0
	for i in range(0, len(s) - 1):
		if s[i] == s[i + 1]:
			if s[i] == 'V' or s[i] == 'L':
				yes = False
				break
			count += 1
			if count == 3:
				yes = False
				break
		else:
			count = 0
	return yes
number = input()
print(check(number) and repeat(number))


import re
print(bool(re.match('^M{0,3}(C[MD]|D?C{0,3})?(X[CL]|L?X{0,3})?(I[XV]|V?I{0,3})?$', input())))


import re
rx = '^('
rx += '|'.join(('(?<!' + a + ')' + a + '{1,3}' for a in 'IXCDM'))
rx += '|' + '|'.join(('(?<!' + a + ')' + a for a in 'VL'))
rx += ')+$'
print(re.match(rx, input()) != None)


import re

def CheckRomNum(s):
	if re.match('^[IXCMVLD]+$', s) == None:
		return False
	if re.search('([IXCM])\\1{3,}', s) != None:
		return False
	if re.search('([VLD])\\1{1,}', s) != None:
		return False
	if re.search('[I]{2,}V', s) != None:
		return False
	if re.search('[I]{2,}X', s) != None:
		return False
	if re.search('[V]{1,}X', s) != None:
		return False
	if re.search('[IV]{1,}L', s) != None:
		return False
	if re.search('[X]{2,}L', s) != None:
		return False
	if re.search('[IV]{1,}C', s) != None:
		return False
	if re.search('[X]{2,}C', s) != None:
		return False
	if re.search('[IXVL]{1,}D', s) != None:
		return False
	if re.search('[C]{2,}D', s) != None:
		return False
	if re.search('[IVXLD]{1,}M', s) != None:
		return False
	if re.search('[C]{2,}M', s) != None:
		return False
	return True
print(CheckRomNum(input()))


import re
print(re.match('M{,3}(CM|CD|(D?C{,3}))(XC|XL|(L?X{,3}))(IX|IV|V?I{,3})$', input()) != None)


import re
print(bool(re.match('^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$', input())))


import sys
if sys.version_info[0] >= 3:
	raw_input = input
checkio = lambda data: ['', 'M', 'MM', 'MMM'][data // 1000] + ['', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM'][data // 100 % 10] + ['', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC'][data // 10 % 10] + ['', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX'][data % 10]
a = set(map(checkio, range(1, 4000)))
print('True' if raw_input().rstrip() in a else 'False')


import re
p = re.compile('M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})')
s = input()
if p.fullmatch(s):
	print('True')
else:
	print('False')


import re
s = input()
p = re.compile('^(M{0,3})?(C{0,3}|CD|DC{0,3}|CM)?(X{0,3}|XL|LX{0,3}|XC)?(I{0,3}|IV|VI{0,3}|IX)?$')
print(p.match(s) is not None)


import re

def my_func(s):
	s = s.upper()
	res = re.search('^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$', s)
	if s == 'MMMM':
		print('False')
	elif res:
		print('True')
	else:
		print('False')
my_func(input())


import re

def valid(roman_number):
	reg = re.compile('^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$')
	return reg.match(roman_number)
roman_number = input()
if valid(roman_number):
	print('True')
else:
	print('False')


import re
print(True if re.search('^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$', input()) else False)


import re
pat = '^(?=[MDCLXVI])M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$'
str1 = input()
if re.search(pat, str1):
	print('True')
else:
	print('False')


s = input()
s = list(s)
lenght = len(s)
i = 0
ss = []
while i < lenght:
	if s[i] == 'I':
		ss.append(1)
	elif s[i] == 'V':
		ss.append(2)
	elif s[i] == 'X':
		ss.append(3)
	elif s[i] == 'L':
		ss.append(4)
	elif s[i] == 'C':
		ss.append(5)
	elif s[i] == 'D':
		ss.append(6)
	else:
		ss.append(7)
	i = i + 1
i = 0
same = 1
flag = 1
while i < lenght - 1:
	if ss[i] == ss[i + 1]:
		same = same + 1
	else:
		same = 1
		if ss[i] < ss[i + 1]:
			if ss[i + 1] - ss[i] > 3:
				flag = 0
	if ss[i] == ss[i + 1] and (s[i] == 'V' or s[i] == 'L' or s[i] == 'D'):
		flag = 0
	if same > 3 and (s[i] == 'I' or s[i] == 'X' or s[i] == 'C' or (s[i] == 'M')):
		flag = 0
	i = i + 1
if flag == 1:
	print('True')
else:
	print('False')


import re
number = input().strip()
m = re.search('^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$', number)
if m:
	print('True')
else:
	print('False')


import re
N = input()
print(True if re.match('^M{0,3}(C[DM]|D?C{0,3})(X[LC]|L?X{0,3})(I[VX]|V?I{0,3})$', N) else False)


import re
roman = input()
match = re.match('^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$', roman)
if match:
	print('True')
else:
	print('False')


import re
s = input()
m = re.match('^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$', s)
if m:
	print(True)
else:
	print(False)


import re
roman = input()
if re.search('^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$', roman):
	print('True')
else:
	print('False')


number = input()
import re
pattern = '(M{0,3})(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})'
pattern = re.compile('^' + pattern + '$')
print(True if pattern.match(number) else False)


import fileinput
import re
print(True if re.match('^M{0,3}(C[MD]|D?C{0,3})(X[LC]|L?X{0,3})(I[VX]|V?I{0,3})$', input()) else False)


import re
print(re.match('^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$', input()) is not None)


import re
a = input()
if re.search('^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$', a):
	print('True')
else:
	print('False')


import sys, re
roman = input()
print(re.search('^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$', roman) != None)


import re
b = input()
pattern = '^M?M?M?(CM|CD|D?C?C?C?)(XC|XL|L?X?X?X?)(IX|IV|V?I?I?I?)$'
match = re.search(pattern, b)
if match:
	print('True')
else:
	print('False')


import re
s = input()
pattern = '^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$'
if re.search(pattern, s):
	print(True)
else:
	print(False)


import re
line = input()
if line and re.match('M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$', line):
	print('True')
else:
	print('False')


import re

def validate(numeral):
	temp = re.match('^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$', numeral)
	if temp:
		return True
	else:
		return False

def main():
	print(validate(input()))
main()


import re
pattern = "\n    ^                   # beginning of string\n    M{0,3}              # thousands - 0 to 4 M's\n    (CM|CD|D?C{0,3})    # hundreds - 900 (CM), 400 (CD), 0-300 (0 to 3 C's),\n                        #            or 500-800 (D, followed by 0 to 3 C's)\n    (XC|XL|L?X{0,3})    # tens - 90 (XC), 40 (XL), 0-30 (0 to 3 X's),\n                        #        or 50-80 (L, followed by 0 to 3 X's)\n    (IX|IV|V?I{0,3})    # ones - 9 (IX), 4 (IV), 0-3 (0 to 3 I's),\n                        #        or 5-8 (V, followed by 0 to 3 I's)\n    $                   # end of string\n    "
r = input()
if re.search(pattern, r, re.VERBOSE):
	print('True')
else:
	print('False')


numb = input().strip()
cur = ''
count = 1
ans = True
for c in numb:
	if c == cur:
		count += 1
	else:
		cur = c
		count = 1
	if count == 4:
		ans = False
		break
	if count == 2:
		if cur == 'V' or cur == 'L':
			ans = False
			break
print(ans)


import re
r = input()
tens = ['I', 'X', 'C', 'M']
fives = ['V', 'L', 'D']
valid = True
for t in tens:
	if re.search(t + '{4,}', r):
		valid = False
for f in fives:
	if re.search(f + '{2,}', r):
		valid = False
if valid:
	print('True')
else:
	print('False')


import re
num = input()
pattern = '^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$'
exp = re.search(pattern, num)
if exp:
	print('True')
else:
	print('False')


import re
number = input()
validity = re.search('^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$', number)
if validity != None and number != '':
	print('True')
else:
	print('False')


import re
print(bool(re.match('^(M{0,3})(C{0,3}|CD|DC{0,3}|CM)(X{0,3}|XL|LX{0,3}|XC)(I{0,3}|IV|VI{0,3}|IX)$', input())))


import re
roman_nr = input()
match = re.search('^M{0,3}(CM)?(C?D)?C{0,3}(XC)?(X?L)?X{0,3}(IX)?(I?V)?I{0,3}$', roman_nr)
if match:
	print('True')
else:
	print('False')


import re
z = input()
b = re.match('[CDMLXVI]+', z) != None
if b:
	tens = 'IXCM'
	fives = 'VLD'
	for j in range(len(tens)):
		if b:
			b = re.search(tens[j] * 4, z) == None
	for j in range(len(fives)):
		if b:
			b = re.search(fives[j] * 2, z) == None
print(b)


hundreds = ['', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM']
decades = ['', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC']
units = ['', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX']

def arabToRoman(n):
	t = n // 1000
	n %= 1000
	h = n // 100
	n %= 100
	d = n // 10
	n %= 10
	u = n
	r = ''
	for i in range(t):
		r += 'M'
	r += hundreds[h]
	r += decades[d]
	r += units[u]
	return r
cache = []
for i in range(1, 4000):
	cache.append(arabToRoman(i))
while True:
	try:
		num = input()
	except EOFError:
		break
	if num in cache:
		print('True')
	else:
		print('False')


import re
TENS = ['I', 'X', 'C', 'M']
FIVES = ['V', 'L', 'D']
ALL = TENS + FIVES

def is_valid(s):
	if re.search('[^IXCMVLD]', s):
		return 'False'
	elif re.search('I{4}', s):
		return 'False'
	elif re.search('X{4}', s):
		return 'False'
	elif re.search('C{4}', s):
		return 'False'
	elif re.search('M{4}', s):
		return 'False'
	elif re.search('V{2}', s):
		return 'False'
	elif re.search('L{2}', s):
		return 'False'
	elif re.search('D{2}', s):
		return 'False'
	else:
		return 'True'
s = input()
print(is_valid(s))


import re
v_pattern = '^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$'
validation = re.compile(v_pattern)
if validation.match(input()):
	print(True)
else:
	print(False)


import re
num = str(input())
if re.search('XXXX+', num) or re.search('CCCC+', num) or re.search('MMMM+', num) or re.search('LL+', num) or re.search('DD+', num) or re.search('IIII+', num) or re.search('VV+', num):
	print('False')
else:
	print('True')


import re

def isRomanNumeral(s):
	return True if re.match('^M{0,3}(CM|CD|D?C{0,3})(C|X{0,3}|XL|LX{0,3}|XC)(I{0,3}|IV|VI{0,3}|IX)$', s) else False
print(isRomanNumeral(input()))


import re

def main():
	possible_roman_numeral = str(input()).strip()
	if re.match('^[IXC]?M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$', possible_roman_numeral):
		print('True')
	else:
		print('False')
main()


import re
x = input()
if re.match('^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$', x) != None:
	print(True)
else:
	print(False)


import re
my_list = input()
pattern = "\n^ # beginning of string\nM{0,3} # thousands - 0 to 3 M's\n(CM|CD|D?C{0,3}) # hundreds - 900 (CM), 400 (CD), 0-300 (0 to 3 C's),\n# or 500-800 (D, followed by 0 to 3 C's)\n(XC|XL|L?X{0,3}) # tens - 90 (XC), 40 (XL), 0-30 (0 to 3 X's),\n# or 50-80 (L, followed by 0 to 3 X's)\n(IX|IV|V?I{0,3}) # ones - 9 (IX), 4 (IV), 0-3 (0 to 3 I's),\n# or 5-8 (V, followed by 0 to 3 I's)\n$ # end of string\n"
if re.match(pattern, my_list, re.VERBOSE):
	print('True')
else:
	print('False')


import re
number = input()
match = re.search('^M{0,3}(CM|CD|D?C{0,3})(XL|XC|L?X{0,3})(IV|V|IX|V?I{0,3})$', number)
if match:
	print('True')
else:
	print('False')


import re
numeral = input()
match = re.search('^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$', numeral)
print('True' if match else 'False')


import re
n = input()

def roman(str):
	if re.search('^[M]{0,3}(CM|CD|D?C{0,3})(XL|XC|L?X{0,3})(IV|IX|V?I{0,3})$', str):
		return 'True'
	return 'False'
a = roman(n)
print(a)


import re
roman = re.compile('^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3}$)')
n = input()
if roman.match(n):
	print('True')
else:
	print('False')


import re
print('True' if re.search('^M?M?M?(C|CC|CCC|CD|D|DC|DCC|DCCC|CM)?(X|XX|XXX|XL|L|LX|LXX|LXXX|XC)?(I|II|III|IV|V|VI|VII|VIII|IX)?$', input()) else 'False')


import re
part = lambda d: '({T}{{0,3}}{O}[{T}{F}]|{T}{{0,3}}{F}?)?'.format(O=d[0], F=d[1], T=d[2])
roman = re.compile(part('CDM') + part('XLC') + part('IVX') + 'I{0,3}$')
ambiguity = ['CMC', 'CDC', 'XCX', 'XLX', 'IXI', 'IVI']
correct = lambda str: roman.match(str) is not None and (not any((a in str for a in ambiguity)))
print(correct(input()))


import re
regex = re.compile('^M{0,3}(CM)?((?<!CM)C?D)?((?<!CD|CM)C{0,3})?(XC)?((?<!XC)X?L)?((?<!XL|XC)X{0,3})?(IX)?((?<!IX)I?V)?((?<!IV|IX)I{1,3})?$')
number = input()
print('True' if regex.match(number) else 'False')


import re
s = input()
if s == '':
	print('False')
else:
	print('True' if re.match('^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$', s) != None else 'False')


import re
pattern = '^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$'
number = input()
if re.search(pattern, number):
	print('True')
else:
	print('False')


import re
roman = input()
if re.search('^(I|V|X|L|C|D|M)+$', roman) == None:
	print('False')
elif re.search('(IIII|XXXX|CCCC|MMMM|VV|LL|DD)', roman) != None:
	print('False')
else:
	print('True')


string = input().strip()
import re
print(bool(re.match('^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$', string)))


import re
string = input()
rn = 'M{,3}(C[DM]|D?C{,3})(X[LC]|L?X{,3})(I[VX]|V?I{,3})'
if re.match('^' + rn + '$', string):
	print('True')
else:
	print('False')


import re
ROMAN = re.compile('^M{0,3}((D?C{0,3})|CM|CD)((L?X{0,3})|XC|XL)((V?I{0,3})|IX|IV)$')
s = input()
print(bool(s and re.match(ROMAN, s)))


import re
romanNum = input()
valid = re.search('^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$', romanNum)
print('True' if valid else 'False')