Skip to content

Instantly share code, notes, and snippets.

@maehrm
Created March 15, 2026 01:44
Show Gist options
  • Select an option

  • Save maehrm/6f65ab9b013a75b37b1d75419d3d74d5 to your computer and use it in GitHub Desktop.

Select an option

Save maehrm/6f65ab9b013a75b37b1d75419d3d74d5 to your computer and use it in GitHub Desktop.
def get_lcp(str1, str2):
ret = 0
for s1, s2 in zip(str1, str2):
if s1 != s2:
break
ret += 1
return ret
N = int(input())
S = [[input()] + [i] for i in range(N)]
S.sort() # ソートして似た文字列で比較する
ans = [0] * N
for i in range(N - 1):
s1, idx1 = S[i]
s2, idx2 = S[i + 1]
lcp = get_lcp(s1, s2)
ans[idx1] = max(ans[idx1], lcp)
ans[idx2] = max(ans[idx2], lcp)
print("\n".join(map(str, ans)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment