fork download
  1. import sys
  2. #짧은 기어를 긴 기어의 끝에 닿을 때까지 한 칸씩 밀면서 맞물리는지 봐야함
  3. #반대의 경우도 해봐야 하는데;;
  4.  
  5. #최장 길이는 두 기어의 길이 합보다 커질 수 없다.
  6. #맞물린다: 이(2)끼리 붙이치지 않으며, 하나라도 홈(1)과 이(2)가 만나야 한다.
  7. a = sys.stdin.readline().rstrip()
  8. b = sys.stdin.readline().rstrip()
  9.  
  10. def check(li, i, long, short):
  11. now_s = int(short[i])
  12. now_l = int(long[li + i])
  13.  
  14. if now_s == 2 and now_l == 1:
  15. if (i-1 >= 0) and (int(short[i-1]) == 1 and int(long[li+ i- 1]) == 2):
  16. return True
  17. else:
  18. return False
  19. elif now_s == 1 and now_l == 2:
  20. if (i+1 < len(short) and li+ i+ 1 < len(long)) and (int(short[i+1]) == 2 and int(long[li+ i+ 1]) == 1):
  21. return True
  22. else:
  23. return False
  24.  
  25.  
  26. def match(short, long):
  27. min_len = len(short) + len(long)
  28.  
  29. for li in range(len(long)):
  30. ee = False #이끼리 맞물리는가
  31. tooth = False #딱 맞물리는가
  32.  
  33. for i in range(len(short)):
  34. if li + i >= len(long):
  35. break
  36.  
  37. s = int(short[i])
  38. l = int(long[li + i])
  39.  
  40. if s + l == 4:
  41. ee = True
  42. break
  43.  
  44. if s + l == 3 and check(li, i, long, short):
  45. tooth = True
  46. break
  47.  
  48. if tooth and not ee:
  49. end_point = max(len(long), li + len(short))
  50. min_len = min(min_len, end_point)
  51.  
  52. return min_len
  53.  
  54. ans1 = match(a, b)
  55. ans2 = match(b, a)
  56.  
  57. print(min(ans1, ans2))
  58.  
Success #stdin #stdout 0.07s 14136KB
stdin
2112112112
2212112
stdout
10