





from __future__ import annotations

from collections import deque

class 		UpperCamelCase__ :



     '''simple docstring'''

     def __init__(							self						:    Optional[Any]						,lowerCamelCase__						:    str       )     ->		Dict:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						[]
           self.adlist.append(
               {"""value""": """""", """next_states""": [], """fail_state""": 0, """output""": []}       )

           for keyword in keywords:
                 self.add_keyword(lowercase_       )
           self.set_fail_transitions()






     def 				SCREAMING_SNAKE_CASE__  (							self						:    Tuple						,lowerCamelCase__						:    str						,lowerCamelCase__						:    Union[str, Any]       )     ->		List[Any]:




           '''simple docstring'''


           for state in self.adlist[current_state]["next_states"]:
                 if char == self.adlist[state]["value"]:
                       return state
           return None






     def 				SCREAMING_SNAKE_CASE__  (							self						:    Tuple						,lowerCamelCase__						:    str       )     ->		List[Any]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						0
           for character in keyword:
                 SCREAMING_SNAKE_CASE		=						self.find_next_state(lowercase_						,lowercase_       )
                 if next_state is None:
                       self.adlist.append(
                           {
                               """value""": character,
                               """next_states""": [],
                               """fail_state""": 0,
                               """output""": [],
                           }       )
                       self.adlist[current_state]["next_states"].append(len(self.adlist       ) - 1       )
                       SCREAMING_SNAKE_CASE		=						len(self.adlist       ) - 1
                 else:
                       SCREAMING_SNAKE_CASE		=						next_state
           self.adlist[current_state]["output"].append(lowercase_       )






     def 				SCREAMING_SNAKE_CASE__  (							self						:    List[Any]       )     ->		Optional[Any]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						deque()
           for node in self.adlist[0]["next_states"]:
                 q.append(lowercase_       )
                 SCREAMING_SNAKE_CASE		=						0
           while q:
                 SCREAMING_SNAKE_CASE		=						q.popleft()
                 for child in self.adlist[r]["next_states"]:
                       q.append(lowercase_       )
                       SCREAMING_SNAKE_CASE		=						self.adlist[r]["fail_state"]
                       while (
                           self.find_next_state(lowercase_						,self.adlist[child]["""value"""]       ) is None
                           and state != 0
                       ):
                             SCREAMING_SNAKE_CASE		=						self.adlist[state]["fail_state"]
                       SCREAMING_SNAKE_CASE		=						self.find_next_state(
                           lowercase_						,self.adlist[child]["""value"""]       )
                       if self.adlist[child]["fail_state"] is None:
                             SCREAMING_SNAKE_CASE		=						0
                       SCREAMING_SNAKE_CASE		=						(
                           self.adlist[child]["output"]
                           + self.adlist[self.adlist[child]["fail_state"]]["output"]
                       )






     def 				SCREAMING_SNAKE_CASE__  (							self						:    List[Any]						,lowerCamelCase__						:    Any       )     ->		List[str]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						{}  # returns a dict with keywords and list of its occurrences
           SCREAMING_SNAKE_CASE		=						0
           for i in range(len(lowercase_       )       ):
                 while (
                     self.find_next_state(lowercase_						,string[i]       ) is None
                     and current_state != 0
                 ):
                       SCREAMING_SNAKE_CASE		=						self.adlist[current_state]["fail_state"]
                 SCREAMING_SNAKE_CASE		=						self.find_next_state(lowercase_						,string[i]       )
                 if next_state is None:
                       SCREAMING_SNAKE_CASE		=						0
                 else:
                       SCREAMING_SNAKE_CASE		=						next_state
                       for key in self.adlist[current_state]["output"]:
                             if key not in result:
                                   SCREAMING_SNAKE_CASE		=						[]
                             result[key].append(i - len(lowercase_       ) + 1       )
           return result


if __name__ == "__main__":
      import doctest

      doctest.testmod()




"""simple docstring"""





import os

_a		       =							{'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1_000}







def 	__a     (		__lowerCamelCase  ):
   UpperCAmelCase_					:					Union[str, Any]			     =    0

   UpperCAmelCase_					:					List[str]			     =    0
   while index < len(__lowerCamelCase  ) - 1:
      UpperCAmelCase_					:					Tuple			     =    SYMBOLS[numerals[index]]
      UpperCAmelCase_					:					List[str]			     =    SYMBOLS[numerals[index + 1]]
      if current_value < next_value:
         total_value -= current_value
      else:
         total_value += current_value
      index += 1
   total_value += SYMBOLS[numerals[index]]

   return total_value







def 	__a     (		__lowerCamelCase  ):
   UpperCAmelCase_					:					List[str]			     =    ""

   UpperCAmelCase_					:					Any			     =    num // 1000
   numerals += m_count * "M"
   num %= 1000

   UpperCAmelCase_					:					Any			     =    num // 100
   if c_count == 9:
      numerals += "CM"
      c_count -= 9
   elif c_count == 4:
      numerals += "CD"
      c_count -= 4
   if c_count >= 5:
      numerals += "D"
      c_count -= 5
   numerals += c_count * "C"
   num %= 100

   UpperCAmelCase_					:					str			     =    num // 10
   if x_count == 9:
      numerals += "XC"
      x_count -= 9
   elif x_count == 4:
      numerals += "XL"
      x_count -= 4
   if x_count >= 5:
      numerals += "L"
      x_count -= 5
   numerals += x_count * "X"
   num %= 10

   if num == 9:
      numerals += "IX"
      num -= 9
   elif num == 4:
      numerals += "IV"
      num -= 4
   if num >= 5:
      numerals += "V"
      num -= 5
   numerals += num * "I"

   return numerals







def 	__a     (		__lowerCamelCase = "/p089_roman.txt"  ):
   UpperCAmelCase_					:					int			     =    0

   with open(os.path.dirname(__lowerCamelCase  ) + roman_numerals_filename  ) as filea:
      UpperCAmelCase_					:					Optional[Any]			     =    filea.readlines()

   for line in lines:
      UpperCAmelCase_					:					Tuple			     =    line.strip()
      UpperCAmelCase_					:					Optional[Any]			     =    parse_roman_numerals(__lowerCamelCase  )
      UpperCAmelCase_					:					Tuple			     =    generate_roman_numerals(__lowerCamelCase  )
      savings += len(__lowerCamelCase  ) - len(__lowerCamelCase  )

   return savings


if __name__ == "__main__":
    print(f"""{solution() = }""")


