Created
February 28, 2013 04:21
-
-
Save jittat/5054156 to your computer and use it in GitHub Desktop.
This code calculates the length of the LCS between two strings. It also deals with Thai digits and roman digits.
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 to_roman(a): | |
if a >= '๐' and a <= '๙': | |
return chr(ord(a) - ord('๐') + ord('0')) | |
return a | |
def is_same_digit(a,b): | |
if a < '0' and a > '9': | |
return False | |
aa = to_roman(a) | |
bb = to_roman(b) | |
return aa == bb | |
def lcs_len(s,t): | |
n = len(s) | |
m = len(t) | |
tab = [] | |
tab.append([]) | |
for j in range(m+1): | |
tab[0].append(0) | |
for i in range(1,n+1): | |
tab.append([]) | |
tab[i].append(0) | |
for j in range(1,m+1): | |
diag = tab[i-1][j-1] | |
if s[i-1] == t[j-1] or (is_same_digit(s[i-1],t[j-1])): | |
diag += 1 | |
tab[i].append(max(tab[i-1][j], tab[i][j-1], diag)) | |
return tab[n][m] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment