题目描述:
有最大长度十万的多个字符串。任意给两个字符串的编号,返回这两个字符串的最长公共前缀长度。
输入
第1行输入一个整数n,代表字符串数量,n最大为10000;
第2~n+1行,每行一个字符串,字符串长度最大为100000;
第n+2行开始,每行输入两个整数a和b,代表需要计算公共前缀的字符串编号。
输出
返回a、b对应的字符串的最长公共前缀长度。如果a或b不是有效的字符串编号,则对该行不输出结果。
样例输入
4
abcdefg
acdef
acdfghijk
cdfg
1 2
2 3
3 4
样例输出
1
3
0
def search(s1, s2):
count = 0
for idx, i in enumerate(s1):
if i == s2[idx]:
count += 1
else:
break
return count
n, s = int(input()), []
for _ in range(n):
s.append(input())
while 1:
a, b = list(map(int, input().split()))
s1, s2 = s[a-1], s[b-1]
count = search(s1, s2) if len(s1) > len(s2) else search(s2, s1)
print(count)