Problem:


  MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Longest Common Subsequence

For given two sequences $X$ and $Y$, a sequence $Z$ is a common subsequence of $X$ and $Y$ if $Z$ is a subsequence of both $X$ and $Y$. For example, if $X = \{a,b,c,b,d,a,b\}$ and $Y = \{b,d,c,a,b,a\}$, the sequence $\{b,c,a\}$ is a common subsequence of both $X$ and $Y$. On the other hand, the sequence $\{b,c,a\}$ is not a longest common subsequence (LCS) of $X$ and $Y$, since it has length 3 and the sequence $\{b,c,b,a\}$, which is also common to both $X$ and $Y$, has length 4. The sequence $\{b,c,b,a\}$ is an LCS of $X$ and $Y$, since there is no common subsequence of length 5 or greater.

  Write a program which finds the length of LCS of given two sequences $X$ and $Y$. The sequence consists of alphabetical characters.

Input

  The input consists of multiple datasets. In the first line, an integer $q$ which is the number of datasets is given. In the following $2 \times q$ lines, each dataset which consists of the two sequences $X$ and $Y$ are given.

Output

  For each dataset, print the length of LCS of $X$ and $Y$ in a line.

Constraints

$1 \leq q \leq 150$

$1 \leq$ length of $X$ and $Y$ $\leq 1,000$

$q \leq 20$ if the dataset includes a sequence whose length is more than 100

Sample Input 1

3
abcbdab
bdcaba
abc
abc
abc
bc

Sample Output 1

4
3
2

Reference

Introduction to Algorithms, Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. The MIT Press.



Sample input:
3
abcbdab
bdcaba
abc
abc
abc
bc


Sample output:
4
3
2

Submission status: Accepted

Solution code:
ascii_letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

def llcs(x, y):
    pm = dict((zip(ascii_letters, [0] * 52)))
    for c in pm:
        for i, xc in enumerate(x):
            if c == xc:
                pm[c] |= (1 << i)
    
    V = (1 << len(x)) - 1
    
    for yc in y:
        V = ((V + (V & pm[yc])) | (V & ~pm[yc]))
    
    ans = bin(V)[-len(x):].count('0')
    
    return ans

from sys import stdin

def solve():
    file_input = stdin
    q = int(file_input.readline())
    for i in range(q):
        s1 = file_input.readline().rstrip()
        s2 = file_input.readline().rstrip()
        print(llcs(s1, s2))

solve()

Submission status: Accepted

Solution code:
def L(str1,str2):
    A = [0 for _ in range(len(str2)+1)]
    for char1 in str1:
        A_copy = A[:]
        for j,char2 in enumerate(str2):
            if(char1==char2):
                A[j+1] = A_copy[j]+1
            elif(A[j] > A[j+1]):
                A[j+1] = A[j]
    return A[-1]


m = int(input())
for i in range(m):
    str1 = input()
    str2 = input()
    print(L(str1,str2))

Submission status: Accepted

Solution code:
def LCS(str1,str2):
    A = [0 for _ in range(len(str2)+1)]
    for i in range(len(str1)):
        A_copy = A[:]
        for j in range(len(str2)):
            if(str1[i]==str2[j]):
                A[j+1] = A_copy[j]+1
            elif(A[j] > A[j+1]):
                A[j+1] = A[j]
    return A[-1]


m = int(input())
for i in range(m):
    str1 = input()
    str2 = input()
    print(LCS(str1,str2))

Submission status: Accepted

Solution code:
def LCS(str1,str2):
    A = [0 for _ in range(len(str2)+1)]
    for i in range(len(str1)):
        A_copy = A[:]
        for j in range(len(str2)):
            if(str1[i]==str2[j]):
                A[j+1] = A_copy[j]+1
            elif(A[j] > A[j+1]):
                A[j+1] = A[j]
    print(A[-1])


m = int(input())
for i in range(m):
    str1 = input()
    str2 = input()
    LCS(str1,str2)

Submission status: Accepted

Solution code:

def f(x,y):
	list=[0]
	for z in y:
		for i in range(len(list)-1,-1,-1):
			tmp=x.find(z,list[i])+1
			if tmp:
				if i+1 < len(list):
					list[i+1]=min(list[i+1],tmp)
				else:
					list.append(tmp)
	return len(list)-1
 
n=int(input())
for i in range(n):
	x=input()
	y=input()
	print(f(x,y))

Submission status: Accepted

Solution code:
def L_common(str1,str2):
    d = [0 for i in range(len(str2)+1)]
    
    for n1 in str1:
        tmp = d[:]
        for (j,n2) in enumerate(str2):
            if n1 == n2:
                d[j+1] = tmp[j]+1
            elif d[j+1]<d[j]:
                d[j+1] = d[j]
    return d[len(str2)]

n = int(input())
stock = []
for i in range(n):
    x = input()
    y = input()
    stock.append(L_common(x,y))

for i in stock:
    print(i)

Submission status: Accepted

Solution code:
ans=[]
n=int(input())
for i in range(n):
    X=input()
    Y=input()
    com=[0]
    for i in Y:
        for j in range(len(com)-1,-1,-1):
            tmp=X.find(i,com[j])
            if tmp+1:
                if j+1<len(com):
                    com[j+1]=min(com[j+1],tmp+1)
                else:
                    com.append(tmp+1) 
    ans.append(len(com)-1)
for i in range(n):
    print(ans[i])

Submission status: Accepted

Solution code:
def ask(X,Y):
 cost=[0]
 for j in Y:
  for i in range(len(cost)-1,-1,-1):
   a=X.find(j,cost[i])+1
   if a:
    if i+1<len(cost):
     cost[i+1]=min(cost[i+1],a)
    else:
     cost.append(a)
 return len(cost)-1
 


q=int(input())
Z=[]
for p in range(q):
 X=input()
 Y=input()
 z=ask(X,Y)
 Z.append(z)

for p in range(q):
 print(Z[p])

Submission status: Accepted

Solution code:
#dj 16D8101012J ito jun
import sys
def search(a,b):
    na=len(a)
    nb=len(b)
    array = [[0]*(nb+1) for _ in range(na+1)]
    for i,x in enumerate(a,1):
        prerow=array[i-1]
        row=array[i]
        for j,y in enumerate(b,1):
            if x == y:
                row[j]=prerow[j-1]+1
            elif prerow[j] < row[j-1]:
                row[j] = row[j-1]
            else:
                row[j] = prerow[j]
    return array[-1][-1]

n = int(input())
strs = [list(i) for i in sys.stdin.read().splitlines()]
result = [search(strs[i*2], strs[i*2+1]) for i in range(n)]
print(*result, sep="\n")

Submission status: Accepted

Solution code:
#16D8104007J kanehiro Maho

def LCSSolver():
    X   = raw_input()
    Y   = raw_input()
    XN  = len(X)
    YN  = len(Y)
    LCS1 = [0 for i in range(YN+1)]
 
    for i in range(1, XN + 1):
        LCS2 = LCS1 + []
        for j in range(1, YN + 1):
            if X[i - 1] == Y[j - 1]:
                LCS1[j] =LCS2[j - 1] + 1
            elif LCS2[j] < LCS1[j - 1]:
                LCS1[j] = LCS1[j - 1]
    print LCS1[j]
 
Rep = input()
for c in range(Rep):
    LCSSolver()

Submission status: Accepted

Solution code:
 
def lcs(X, Y):
    dp = [0]*(len(Y)+1)
     
    for x in X:
        tmp = dp[:]
        for j, y in enumerate(Y):
            if x == y:
                dp[j+1] = tmp[j] + 1
            elif dp[j+1] < dp[j]:
                dp[j+1] = dp[j]
                                
    return dp[-1]
 
n = int(input()) 

for i in range(n):
    X = input()
    Y = input()
    print(lcs(X, Y))

Submission status: Accepted

Solution code:
def lcs(x, y):
    a = len(x)
    b = len(y)
    c1 = [0] * (b + 1)
    for i in range(a):
        e1 = x[i]
        c2 = c1[:]
        for j in range(b):
            if e1 == y[j]:
                c1[j+1] = c2[j] + 1
            elif c1[j+1] < c1[j]:
                c1[j+1] = c1[j]
    return c1[-1]
     
n = int(input().rstrip())
ret = []
for i in range(n):
    s1 = input().rstrip()
    s2 = input().rstrip()
    ret.append(lcs(s1, s2))
print(*ret, sep="\n")

Submission status: Accepted

Solution code:
def calc(x,y):
    n = len(x)
    m = len(y)
    dp1 = [0]*(m+1)
    for i in range(n):
        s = x[i]
        dp2 = dp1[:]
        for j in range(m):
            if s == y[j]:
                dp1[j+1] = dp2[j]+1
            elif dp1[j+1] < dp1[j]:
                dp1[j+1] = dp1[j]
    return dp1[-1]

q = int(input().rstrip())
ans = []
for i in range(q):
    x = input()
    y = input()
    ans.append(calc(x,y))
print(*ans, sep="\n")

Submission status: Accepted

Solution code:
def calc(x,y):
    n = len(x)
    m = len(y)
    dp1 = [0]*(m+1)
    for i in range(n):
        s = x[i]
        dp2 = dp1[:]
        for j in range(m):
            if s == y[j]:
                dp1[j+1] = dp2[j]+1
            elif dp1[j+1] < dp1[j]:
                dp1[j+1] = dp1[j]
    return dp1[-1]

q = int(input())
ans = []
for i in range(q):
    x = input()
    y = input()
    print(calc(x,y))

Submission status: Accepted

Solution code:
def calc(x,y):
    n = len(x)
    m = len(y)
    dp1 = [0]*(m+1)
    for i in range(n):
        dp2 = dp1[:]
        for j in range(m):
            if x[i] == y[j]:
                dp1[j+1] = dp2[j]+1
            elif dp1[j+1] < dp1[j]:
                dp1[j+1] = dp1[j]
    return dp1[-1]

q = int(input())
for i in range(q):
    x = input()
    y = input()
    print(calc(x,y))

Submission status: Accepted

Solution code:
def calc(x,y):
    n = len(x)
    m = len(y)
    dp1 = [0]*(m+1)
    for i in range(n):
        s = x[i]
        dp2 = dp1[:]
        for j in range(m):
            if s == y[j]:
                dp1[j+1] = dp2[j]+1
            elif dp1[j+1] < dp1[j]:
                dp1[j+1] = dp1[j]
    return dp1[-1]

q = int(input())
for i in range(q):
    x = input()
    y = input()
    print(calc(x,y))

Submission status: Accepted

Solution code:
from bisect import bisect_left
q = int(input())
for _ in [0]*q:
    s1, s2 = input(), input()
    l1, l2 = len(s1), len(s2)

    p = []
    for i, c in enumerate(s1):
        s = 0
        while True:
            j = s2[s:].find(c)
            if j == -1:
                break
            p.append((i, s+j))
            s += j+1

    lis = []
    for _, y in sorted(p, key=lambda x: (x[0], -x[1])):
        i = bisect_left(lis, y)
        if len(lis) <= i:
            lis.append(y)
        else:
            lis[i] = y
    print(len(lis))

Submission status: Accepted

Solution code:
def lcs_hs(s1, s2):
    from bisect import bisect_left

    p = []
    append_p = p.append
    for i, c in enumerate(s1):
        j = s2.find(c)+1
        while j:
            append_p((i, -j))
            j = s2.find(c, j)+1

    lis, result = [], 0
    append_lis = lis.append
    for _, y in sorted(p):
        i = bisect_left(lis, -y)
        if i >= result:
            append_lis(-y)
            result += 1
        else:
            lis[i] = -y

    return result


print(*(lcs_hs(input(), input()) for _ in [0]*int(input())), sep="\n")

Submission status: Accepted

Solution code:
def lcs_hs(s1, s2):
    from bisect import bisect_left

    s2 = s2[::-1]
    lis, result = [], 0
    append = lis.append

    for c in s1:
        i = s2.find(c)+1
        while i:
            j = bisect_left(lis, -i)
            if j >= result:
                append(-i)
                result += 1
            else:
                lis[j] = -i
            i = s2.find(c, i)+1

    return result


print(*(lcs_hs(input(), input()) for _ in [0]*int(input())), sep="\n")

Submission status: Accepted

Solution code:
def lcs_hs(s1, s2):
    from bisect import bisect_left

    s2, lis, result = s2[::-1], [], 0
    find, append = s2.find, lis.append

    for c in s1:
        i = find(c)+1
        while i:
            j = bisect_left(lis, -i)
            if j >= result:
                append(-i)
                result += 1
            else:
                lis[j] = -i
            i = find(c, i)+1

    return result


print(*(lcs_hs(input(), input()) for _ in [0]*int(input())), sep="\n")

Submission status: Accepted

Solution code:




def lsc(str1,str2):
    table1=[0]*(len(str2)+1)
    for i in range(len(str1)):
       table2=table1[:]
       for j in range(len(str2)):
            if(str1[i] == str2[j]):
               table1[j+1] = table2[j] +1
            else:
                if(table1[j+1] < table1[j]):
                    table1[j+1]=table1[j]
    return table1[-1]




n=int(input())

for loop in range(n):
    str1=input()
    str2=input()
    print(lsc(str1,str2))

Submission status: Accepted

Solution code:
def main():
    X=input()
    Y=input()
    dp=[]
    for y in range(len(Y)):
        now=0
        for i  in range(len(dp)):
            d=dp[i]
            if Y[y] in X[now:d]:
                dp[i]=now+X[now:d].index(Y[y])
            now=d+1
        if Y[y] in X[now:]:
            dp.append(now+X[now:].index(Y[y]))
    return len(dp)
for i in range(int(input())):
    print(main())

Submission status: Accepted

Solution code:
def longest_common(x, y):
    cost = [0]
    for c in y:
        for i in range(len(cost) -1, -1, -1):
            index = x.find(c, cost[i]) + 1
            if index:
                if i + 1 < len(cost):
                    cost[i + 1] = min(cost[i + 1], index)
                else:
                    cost.append(index)
    return len(cost)  - 1
            

n = int(input())
for _ in range(n):
    x = input()
    y = input()
    print(longest_common(x, y))

Submission status: Accepted

Solution code:

def solve():
	seq1 = raw_input()
	seq2 = raw_input()

	a, b = len(seq1), len(seq2)
	x1 = [0 for i in range(b+1)]
	for i in range(a):
		e1 = seq1[i]
		x2 = x1+[]
		for j in range(b):
			if e1 == seq2[j]:
				x1[j+1] = x2[j] + 1
			elif x1[j+1] < x1[j]:
				x1[j+1] = x1[j]
	return x1[-1]

n = input()
for i2 in range(n):
	print solve()

Submission status: Accepted

Solution code:
def solve():
    seq1 = raw_input()
    seq2 = raw_input()
 
    a, b = len(seq1), len(seq2)
    x1 = [0 for i in range(b+1)]
    for i in range(a):
        e1 = seq1[i]
        x2 = x1+[]
        for j in range(b):
            if e1 == seq2[j]:
                x1[j+1] = x2[j] + 1
            elif x1[j+1] < x1[j]:
                x1[j+1] = x1[j]
    return x1[-1]
 
n = input()
for i2 in range(n):
    print solve()

Submission status: Accepted

Solution code:
"""
problem statment url: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_10_C
"""


def lcs():
	s1 = raw_input()
	s2 = raw_input()
	la, lb = len(s1), len(s2)
	dp1 = [0 for _ in range(lb + 1)]

	for i in range(la):
		dp2 = dp1 + []
		for j in range(lb):
			if s1[i] == s2[j]:
				dp1[j + 1] = dp2[j] + 1
			elif dp1[j + 1] < dp1[j]:
				dp1[j + 1] = dp1[j]

	return dp1[-1]

if __name__ == '__main__':
	tc = int(raw_input())
	for _ in range(tc):
		print(lcs())

Submission status: Accepted

Solution code:
#!/usr/bin/python3
import itertools
# main
q = int(input())
for a in range(q):
    X = input()
    Y = input()
    cost = [0]
    for c in Y:
        for i in range(len(cost) - 1, -1, -1):
            index = X.find(c, cost[i]) + 1
            if index:
                if i + 1 < len(cost):
                    cost[i + 1] = min(cost[i + 1], index)
                else:
                    cost.append(index)
    print(len(cost) - 1)

Submission status: Accepted

Solution code:
def solve():
    seq1 = raw_input()
    seq2 = raw_input()
 
    a, b = len(seq1), len(seq2)
    x1 = [0 for i in range(b+1)]
    for i in range(a):
        e1 = seq1[i]
        x2 = x1+[]
        for j in range(b):
            if e1 == seq2[j]:
                x1[j+1] = x2[j] + 1
            elif x1[j+1]<x1[j]:
                x1[j+1] = x1[j]
    return x1[-1]
 
n = input()
for i2 in range(n):
 
    print solve()

Submission status: Accepted

Solution code:
def solve():
 seq1 = raw_input()
 seq2 = raw_input()
 a, b = len(seq1), len(seq2)
 x1 = [0 for i in range(b+1)]
 for i in range(a):
  e1 = seq1[i]
  x2 = x1+[]
  for j in range(b):
   if e1 == seq2[j]:
    x1[j+1] = x2[j] + 1
   elif x1[j+1]<x1[j]:
    x1[j+1] = x1[j]
 return x1[-1]
  
n = input()
for i2 in range(n):
 print solve()

Submission status: Accepted

Solution code:
def solve():
 seq1 = raw_input()
 seq2 = raw_input()
 a, b = len(seq1), len(seq2)
 x1 = [0 for i in range(b+1)]
 for i in range(a):
  e1 = seq1[i]
  x2 = x1+[]
  for j in range(b):
   if e1 == seq2[j]:
    x1[j+1] = x2[j] + 1
   elif x1[j+1]<x1[j]:
    x1[j+1] = x1[j]
 return x1[-1]
n = input()
for i2 in range(n):
 print solve()

Submission status: Accepted

Solution code:
def lcs_size():
  X = raw_input()
  Y = raw_input()
  a = len(X)
  b = len(Y)
  z2 = [0 for i in range(b+1)]

  for i in range(a):
    ch = X[i]
    z1 = z2 + []
    for j in range(b):
      if ch == Y[j]:
        z2[j+1] = z1[j] + 1
      elif z2[j+1] < z2[j]:
        z2[j+1] = z2[j]

  return z2[-1]

q = input()
for k in range(q):
  print lcs_size()

Submission status: Accepted

Solution code:
def edit_distance():
 s = raw_input()
 t = raw_input()
 a = len(s)
 b = len(t)
 x = [0 for i in range(b+1)]
 for i in range(a):
   q = s[i]
   x2 = x+[]
   for j in range(b):
    if (q ==t[j]):
     x[j+1] =x2[j]+1
    elif (x[j+1]<x[j]):
     x[j+1] =x[j]
 return x[-1]

n = input()
for i2 in range(n):
 print edit_distance()

Submission status: Accepted

Solution code:
def solve():
  X = raw_input()
  Y = raw_input()
  a = len(X)
  b = len(Y)
  c1 = [0 for i in range(b+1)]
 
  for i in range(a):
    c2 = c1 + []
    for j in range(b):
      if X[i] == Y[j]:
        c1[j+1] = c2[j] + 1
      elif c1[j+1] < c1[j]:
        c1[j+1] = c1[j]
 
  return c1[-1]
 
n = input()
for k in range(n):
  print solve()

Submission status: Accepted

Solution code:
def solve():
    a1 = raw_input()
    a2 = raw_input()
    a = len(a1)
    b = len(a2)
    c1 = [0 for i in range(b+1)]
    for i in range(a):
        e1 = a1[i]
        c2 = c1+[]
        for j in range(b):
            if e1 == a2[j]:
                c1[j+1] = c2[j] + 1
            elif c1[j+1] < c1[j]:
                c1[j+1] = c1[j]
    return c1[-1]
 
n = input()
for i2 in range(n):
    print solve()

Submission status: Accepted

Solution code:
def solve():
    x = raw_input()
    y = raw_input()
 
    a, b = len(x), len(y)
    x1 = [0 for k in range(b+1)]
    for i in xrange(a):
        e1 = x[i]
        x2 = x1+[]
        for j in xrange(b):
            if e1 == y[j]:
                x1[j+1] = x2[j] + 1
            elif x1[j+1] < x1[j]:
                x1[j+1] = x1[j]
    return x1[-1]
 
n = input()
for i2 in range(n):
    print solve()

Submission status: Accepted

Solution code:
def lcs():
    x = raw_input()
    y = raw_input()
    a, b = len(x), len(y)
    x1 = [0 for k in range(b+1)]
    for i in xrange(a):
        e = x[i]
        x2 = x1+[]
        for j in xrange(b):
            if e == y[j]:
                x1[j+1] = x2[j] + 1
            elif x1[j+1] < x1[j]:
                x1[j+1] = x1[j]
    print max(x1)
 
n = input()
for i in xrange(n):
    lcs()

Submission status: Accepted

Solution code:
def lcs():
    x = raw_input()
    y = raw_input()
    x1 = [0 for k in range(len(y)+1)]
    for i in xrange(len(x)):
        e = x[i]
        x2 = x1 + []
        for j in xrange(len(y)):
            if e == y[j]:
                x1[j+1] = x2[j] + 1
            elif x1[j+1] < x1[j]:
                x1[j+1] = x1[j]
    print max(x1)
 
n = input()
for i in xrange(n):
    lcs()

Submission status: Accepted

Solution code:
def lcs():
    x, y = raw_input(), raw_input()
    x1 = [0 for k in range(len(y)+1)]
    for i in xrange(len(x)):
        e = x[i]
        x2 = x1 + []
        for j in xrange(len(y)):
            if e == y[j]:
                x1[j+1] = x2[j] + 1
            elif x1[j+1] < x1[j]:
                x1[j+1] = x1[j]
    print max(x1)
 
n = input()
for i in xrange(n):
    lcs()

Submission status: Accepted

Solution code:
def lcs():
    x, y = raw_input(), raw_input()
    b1 = [0 for k in range(len(y)+1)]
    for i in xrange(len(x)):
        a = x[i]
        b2 = b1 + []
        for j in xrange(len(y)):
            if a == y[j]:
                b1[j+1] = b2[j] + 1
            elif b1[j+1] < b1[j]:
                b1[j+1] = b1[j]
    print max(b1)
 
n = input()
for i in xrange(n):
    lcs()

Submission status: Accepted

Solution code:
def LCS():
 x = raw_input()
 y = raw_input()
 a, b = len(x), len(y)
 dsp1 = [0 for i in xrange(b+1)]
 for i in xrange(a):
  dsp2 = dsp1+[]
  for j in xrange(b):
   if x[i] == y[j]:
    dsp1[j+1] = dsp2[j] + 1
   elif dsp1[j+1] < dsp1[j]:
    dsp1[j+1] = dsp1[j]
 print max(dsp1)
  
n = input()
for a in xrange(n):
    LCS()

Submission status: Accepted

Solution code:
import sys

def lcs():
    seq1 = raw_input()
    seq2 = raw_input()
    
    a, b = len(seq1), len(seq2)
    x1 = [0 for i in range(b+1)]
    for i in range(a):
        e1 = seq1[i]
        x2 = x1 + []
        for j in range(b):
            if e1 == seq2[j]:
                x1[j+1] = x2[j] + 1
            if x1[j+1] < x1[j] :
                x1[j+1] = x1[j]
    return x1[-1]

n = input()
for i2 in range(n):
    print lcs()

Submission status: Accepted

Solution code:
def common():
   X = raw_input()
   Y = raw_input()
  
   s = [0 for i in range(len(Y)+1)]
 
   for j in range(0, len(X)):
      t = s + []
      for k in range(0, len(Y)):
         if X[j] == Y[k]:
            s[k+1] = t[k] + 1
         elif s[k+1] < s[k]:
            s[k+1] = s[k]
  
   return s[-1]
 
q = input()
for l in range(0, q):
  print common()

Submission status: Accepted

Solution code:
def common():
   X = raw_input()
   Y = raw_input()
  
   s = [0 for i in range(len(Y)+1)]
 
   for j in range(0, len(X)):
      t = s + []
      for k in range(0, len(Y)):
         if X[j] == Y[k]:
            s[k+1] = t[k] + 1
         elif s[k+1] < s[k]:
            s[k+1] = s[k]
  
   print s[-1]
 
q = input()
for l in range(0, q):
   common()

Submission status: Accepted

Solution code:
def LCS():
  X = raw_input()
  Y = raw_input()
  a = len(X)
  b = len(Y)
  c = [ 0 for i in range(b+1) ]
  
  for i in range(a):
    d = c + []
    for j in range(b):
      if X[i] == Y[j]:
        c[j+1] = d[j] + 1
      elif c[j+1] < c[j]:
        c[j+1] = c[j]
  
  return c[-1]
  
q = input()
for k in range(q):
  print LCS()

Submission status: Accepted

Solution code:
def solve():
    x = raw_input()
    y = raw_input()
    a, b = len(x), len(y)
    x1 = [0 for k in range(b+1)]
    for i in xrange(a):
        c = x[i]
        x2 = x1+[]
        for j in xrange(b):
            if c == y[j]:
                x1[j+1] = x2[j] + 1
            elif x1[j+1] < x1[j]:
                x1[j+1] = x1[j]
    print max(x1)
  
n = input()
for i in xrange(n):
    solve()

Submission status: Accepted

Solution code:
def LCS():
    str1 = raw_input()
    str2 = raw_input()
    
    after = [0] * (len(str2) + 1)
    
    for i in xrange(1, len(str1) + 1):
        before = after + []
        for j in xrange(1, len(str2) + 1):
             if str1[i - 1] == str2[j - 1]:
                 after[j] = before[j - 1] + 1
             elif after[j - 1] > before[j]:
                 after[j] = after[j - 1]
    return after[j]

n = input()
for k in xrange(n):
    print LCS()

Submission status: Accepted

Solution code:
def LCS():
    str1 = raw_input()
    str2 = raw_input()
    
    after = [0] * (len(str2) + 1)
    
    for i in xrange(1, len(str1) + 1):
        before = after + []
        for j in xrange(1, len(str2) + 1):
             if str1[i - 1] == str2[j - 1]:
                 after[j] = before[j - 1] + 1
             elif after[j - 1] > before[j]: #this is equal to 'max(after[j - 1], before[j])'
                 after[j] = after[j - 1]
    return after[j]

n = input()
for k in xrange(n):
    print LCS()

Submission status: Accepted

Solution code:
def lcs():
  x = raw_input()
  y = raw_input()
  x_len = len(x)
  y_len = len(y)
  z2 = [0 for i in range(y_len+1)]

  for i in range(x_len):
    z1 = z2 + []
    ele = x[i]
    for k in range(y_len):
      if ele == y[k]:
        z2[k+1] = z1[k] + 1
      elif z2[k+1] < z2[k]:
        z2[k+1] = z2[k]

  return z2[-1]

num = input()
for j in range(num):
  print lcs()

Submission status: Accepted

Solution code:
def solve():
    seq1 = raw_input()
    seq2 = raw_input()
  
    a, b = len(seq1), len(seq2)
    x1 = [0 for i in range(b+1)]
    for i in range(a):
        e1 = seq1[i]
        x2 = x1+[]
        for j in range(b):
            if e1 == seq2[j]:
                x1[j+1] = x2[j] + 1
            elif x1[j+1] < x1[j]:
                x1[j+1] = x1[j]
    return x1[-1]
  
n = input()
for i2 in range(n):
    print solve()

Submission status: Accepted

Solution code:
def solve_lcs():
    x = raw_input()
    y = raw_input()
 
    a = len(x)
    b = len(y)
    c1 = [0 for i in range(b+1)]
    for i in range(a):
        e1 = x[i]
        c2 = c1+[]
        for j in range(b):
            if e1 == y[j]:
                c1[j+1] = c2[j] + 1
            elif c1[j+1] < c1[j]:
                c1[j+1] = c1[j]
    return c1[-1]
 
n = input()
for i2 in range(n):
    print solve_lcs()

Submission status: Accepted

Solution code:
def longest():
    seq1 = raw_input()
    seq2 = raw_input()
  
    a, b = len(seq1), len(seq2)
    x1 = [0 for i in range(b+1)]
    for i in range(a):
        e1 = seq1[i]
        x2 = x1+[]
        for j in range(b):
            if e1 == seq2[j]:
                x1[j+1] = x2[j] + 1
            elif x1[j+1] < x1[j]:
                x1[j+1] = x1[j]
    return x1[-1]
  
n = input()
for i2 in range(n):
    print longest()

Submission status: Accepted

Solution code:
def longest():
    seq1 = raw_input()
    seq2 = raw_input()
   
    a, b = len(seq1), len(seq2)
    x1 = [0 for i in range(b+1)]
    for i in range(a):
        e1 = seq1[i]
        x2 = x1+[]
        for j in range(b):
            if e1 == seq2[j]:
                x1[j+1] = x2[j] + 1
            elif x1[j+1] < x1[j]:
                x1[j+1] = x1[j]

    return x1[-1]
   
n = input()
for i2 in range(n):
    print longest()

Submission status: Accepted

Solution code:
def longest():
    alps1 = raw_input()
    alps2 = raw_input()
    
    a, b = len(alps1), len(alps2)
    x1 = [0 for i in range(b+1)]
    for i in range(a):
        e1 = alps1[i]
        x2 = x1+[]
        for j in range(b):
            if e1 == alps2[j]:
                x1[j+1] = x2[j] + 1
            elif x1[j+1] < x1[j]:
                x1[j+1] = x1[j]
 
    return x1[-1]
    
n = input()
for i2 in range(n):
    print longest()

Submission status: Accepted

Solution code:
def longest():
    alps1 = raw_input()
    alps2 = raw_input()
    
    x, y = len(alps1), len(alps2)
    k = [0 for i in range(y+1)]
    for i in range(x):
        e1 = alps1[i]
        l = k+[]
        for j in range(y):
            if e1 == alps2[j]:
                k[j+1] = l[j] + 1
            elif k[j+1] < k[j]:
                k[j+1] = k[j]
 
    return k[-1]
    
n = input()
for i in range(n):
    print longest()

Submission status: Accepted

Solution code:
def solve():
  X = raw_input()
  Y = raw_input()
  a = len(X)
  b =len(Y)
  lcs = [0 for i in range(b+1)]
  
  for i in range(a):
    lcs2 = lcs + []
    for j in range(b):
      if X[i] == Y[j]:
        lcs[j+1] = lcs2[j] + 1
      elif lcs[j+1] < lcs[j]:
        lcs[j+1] = lcs[j]
  
  return lcs[-1]
  
n = input()
for k in range(n):
  print solve()

Submission status: Accepted

Solution code:
def LCS():
    X = raw_input()
    Y = raw_input()
 
    a, b = len(X), len(Y)
    c1 = [0 for i in range(b+1)]
    for i in range(a):
        e1 = X[i]
        c2 = c1+[]
        for j in range(b):
            if e1 == Y[j]:
                c1[j+1] = c2[j] + 1
            elif c1[j+1] < c1[j]:
                c1[j+1] = c1[j]
    return c1[-1]
 
n = input()
for i in range(n):
    print LCS()

Submission status: Accepted

Solution code:
def solve():
    seq1 = raw_input()
    seq2 = raw_input()
 
    x, y = len(seq1), len(seq2)
    a1 = [0 for i in range(y+1)]
    for i in range(x):
        e1 = seq1[i]
        a2 = a1+[]
        for j in range(y):
            if e1 == seq2[j]:
                a1[j+1] = a2[j] + 1
            elif a1[j+1] < a1[j]:
                a1[j+1] = a1[j]
    return a1[-1]
 
n = input()
for i2 in range(n):
    print solve()

Submission status: Accepted

Solution code:
def solve_L():
    X = input()
    Y = input()
  
    a = len(X)
    b = len(Y)
    c1 = [0 for i in range(b+1)]
    for i in range(a):
        e1 = X[i]
        c2 = c1+[]
        for j in range(b):
            if e1 == Y[j]:
                c1[j+1] = c2[j] + 1
            elif c1[j+1] < c1[j]:
                c1[j+1] = c1[j]
    return c1[-1]
  
n = int(input())
ans = [[] for i in range(n)]
for i in range(n):
    ans[i] = solve_L()
for i in range(n):
  print(ans[i])

Submission status: Accepted

Solution code:
def LCSSolver():
    X   = raw_input()
    Y   = raw_input()
    XN  = len(X)
    YN  = len(Y)
    LaterLCS = [0 for i in range(YN+1)]

    for i in range(1, XN + 1):
        EarlierLCS = LaterLCS + []
        for j in range(1, YN + 1):
            if X[i - 1] == Y[j - 1]:
                LaterLCS[j] = EarlierLCS[j - 1] + 1
            elif EarlierLCS[j] < LaterLCS[j - 1]:
                LaterLCS[j] = LaterLCS[j - 1]
    print LaterLCS[j]

Rep = input()
for c in range(Rep):
    LCSSolver()

Submission status: Accepted

Solution code:
n = input()

def solve(a, b):
	if len(a) < len(b):
		a, b = b, a
	x = [0 for i in range(len(b)+1)]
	empty = []
	for i in xrange(len(a)):
		e = a[i]
		f = x + empty
		for j in xrange(len(b)):
			if e == b[j]:
				x[j+1] = f[j] + 1
			elif x[j+1] < x[j]:
				x[j+1] = x[j]
	return x[-1]

for i in range(n):
	x = raw_input()
	y = raw_input()
	print solve(x, y)

Submission status: Accepted

Solution code:
def common():
  X = raw_input()
  Y = raw_input()
  a = len(X)
  b = len(Y)
  k1 = [0 for i in range(b+1)]
 
  for i in range(a):
     k2 = k1 + []
     for j in range(b):
       if X[i] == Y[j]:
          k1[j+1] = k2[j] + 1
       elif k1[j+1] < k1[j]:
          k1[j+1] = k1[j]
 
  return k1[-1]
 
n = input()
for l in range(n):
  print common()

Submission status: Accepted

Solution code:
def lcs(X, Y):
    #X = " " + X
    #Y = " " + Y
    #c = [[0 for j in xrange(len(Y))] for i in xrange(len(X))]
    c2 = [0 for i in xrange(len(Y) + 1)]
    #maxl = 0
     
    for i in xrange(len(X)):
        c1 = c2 + []
        for j in xrange(len(Y)):
            if X[i] == Y[j]:
                c2[j + 1] = c1[j] + 1
            elif c2[j + 1] < c2[j]:
                c2[j + 1] = c2[j]
    return c2[-1]
n = int(raw_input())
  
for i in xrange(n):
    s1 = raw_input()
    s2 = raw_input()
    print lcs(s1, s2)

Submission status: Accepted

Solution code:
import sys
 
def longSub():
    seq1 = raw_input()
    seq2 = raw_input()
 
    a, b = len(seq1), len(seq2)
    x1 = [0 for i in range(b+1)]
    for i in range(a):
        e1 = seq1[i]
        x2 = x1+[]
        #x1 = [0 for i2 in range(b+1)]
        for j in range(b):
            if e1 == seq2[j]:
                x1[j+1] = x2[j] + 1
            elif x1[j+1]<x1[j]:
                x1[j+1] = x1[j]
    return x1[-1]
 
n = input()
for i2 in range(n):
 
    print longSub()

Submission status: Accepted

Solution code:
def lcs(x,y):
    a, b = len(x), len(y)
    x1 = [0 for k in range(b+1)]
    for i in xrange(a):
        e = x[i]
        x2 = x1+[]
        for j in xrange(b):
            if e == y[j]:
                x1[j+1] = x2[j] + 1
            elif x1[j+1] < x1[j]:
                x1[j+1] = x1[j]
    print max(x1)
    return 0

def main():
    q = int(raw_input())
    for i in xrange(q):
        X = raw_input()
        Y = raw_input()
        lcs(X,Y)
    return 0

main()

Submission status: Accepted

Solution code:
def lcs(x, y):
    a = len(x)
    b = len(y)
    c1 = [0] * (b + 1)
    for i in range(a):
        e1 = x[i]
        c2 = c1[:]
        for j in range(b):
            if e1 == y[j]:
                c1[j+1] = c2[j] + 1
            elif c1[j+1] < c1[j]:
                c1[j+1] = c1[j]
    return c1[-1]
   
n = int(input().rstrip())
ret = []
for i in range(n):
    s1 = input().rstrip()
    s2 = input().rstrip()
    ret.append(lcs(s1, s2))
print(*ret, sep="\n")