Created
September 14, 2017 13:01
-
-
Save asuka-mirai/11304bedf49b11a43ec66b46ccf377ee to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def lcs(s1, s2): | |
dp = [] | |
for s2_k in s2: | |
bgn_idx = 0 | |
for i, cur_idx in enumerate(dp): | |
chr_idx = s1.find(s2_k, bgn_idx) + 1 | |
if not chr_idx: | |
break | |
dp[i] = min(cur_idx, chr_idx) | |
bgn_idx = cur_idx | |
else: | |
chr_idx = s1.find(s2_k, bgn_idx) + 1 | |
if chr_idx: | |
dp.append(chr_idx) | |
return len(dp) | |
def solve(): | |
N = int(input()) | |
for _ in range(N): | |
x = input() | |
y = input() | |
print(lcs(x, y)) | |
if __name__ == '__main__': | |
solve() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment