fork(1) 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 match(short, long):
  11. min_len = len(short) + len(long)
  12.  
  13. for li in range(len(long)):
  14. ee = False #이끼리 맞물리는가
  15. tooth = False #딱 맞물리는가
  16.  
  17. for i in range(len(short)):
  18. if li + i >= len(long):
  19. break
  20.  
  21. s = int(short[i])
  22. l = int(long[li + i])
  23.  
  24. if s + l == 4:
  25. ee = True
  26. break
  27.  
  28. if s + l == 3:
  29. tooth = True
  30.  
  31. if tooth and not ee:
  32. end_point = max(len(long), li + len(short))
  33. min_len = min(min_len, end_point)
  34.  
  35. return min_len
  36.  
  37. ans1 = match(a, b)
  38. ans2 = match(b, a)
  39.  
  40. print(min(ans1, ans2))
  41.  
Success #stdin #stdout 0.1s 14136KB
stdin
1221
1221
stdout
6