Go: // AOJ ALDS1_10_C: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_10_C

package main

import (
	"fmt"
)

func main() {
	var dataNum int
	var s1, s2 string

	fmt.Scanf("%d", &dataNum)

	for i := 0; i < dataNum; i++ {
		fmt.Scanf("%s", &s1)
		fmt.Scanf("%s", &s2)

		lcs := make([][]int, len(s1)+1)
		for j := 0; j < len(s1)+1; j++ {
			lcs[j] = make([]int, len(s2)+1)
		}

		for m, vs1 := range s1 {
			for n, vs2 := range s2 {
				if vs1 == vs2 {
					lcs[m+1][n+1] = lcs[m][n] + 1
				} else if lcs[m+1][n] > lcs[m][n+1] {
					lcs[m+1][n+1] = lcs[m+1][n]
				} else {
					lcs[m+1][n+1] = lcs[m][n+1]
				}
			}
		}

		// Answer
		fmt.Println(lcs[len(s1)][len(s2)])
	}
}

Python: q = int(input())
X = ""
Y = ""
memo = [[]]

def LCS(x, y):
    if (x < 0) or (y < 0):
        return 0
    elif memo[x][y] != -1:
        return memo[x][y]
    else:
        memo[x][y] = max(LCS(x-1,y), LCS(x,y-1), LCS(x-1, y-1)+int(X[x] == Y[y]))
        return memo[x][y]

for c in range(q):
    X = input()
    Y = input()
    memo = [[-1 for j in range(len(Y))] for i in range(len(X))]
    print(LCS(len(X)-1, len(Y)-1))

Python: q = int(input())
for _ in range(q):
    X = input()
    Y = input()

    dp = [[0 for _ in range(len(Y) + 1)] for _ in range(len(X) + 1)]
    for i in range(len(X)):
        for j in range(len(Y)):
            if X[i] == Y[j]:
                dp[i + 1][j + 1] = max(dp[i][j] + 1, dp[i + 1][j], dp[i][j + 1])
            else:
                dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1])
    print(dp[len(X)][len(Y)])

Python: def lcs(a_str, b_str):
    dp = [[0 for n in range(len(a))] for m in range(len(b_str))]
    for i in range(len(b_str)):
        for j in range(len(a_str)):
            if i == 0 or j == 0:
                dp[i][j] = int(a_str[j] == b_str[i])
            else:
                dp[i][j] = max(dp[i - 1][j - 1] + int(a_str[j] == b_str[i]), dp[i - 1][j], dp[i][j - 1])
    return dp[len(b_str) - 1][len(a_str) - 1]

trial = int(input())

for _ in range(trial):
    a = input()
    b = input()
    a = {k:v for k, v in enumerate(a)}
    b = {k:v for k, v in enumerate(b)}
    print(lcs(a, b))

Python: def lcs_len(x, y):
    ly = len(y)
    tcsl = [0 for i in range(ly + 1)]
    for x_i in x:
        pre_tcsl = list(tcsl)
        for j in range(ly):
            if x_i == y[j]:
                tcsl[j] = pre_tcsl[j - 1] + 1
            else:
                tcsl[j] = max(pre_tcsl[j], tcsl[j - 1])
    print(tcsl[j])


q = int(input())

for i in range(q):
    ax = input()
    ay = input()
    lcs_len(ax, ay)

Python: q = int(input())
for k in range(q):
	x,y = list(input()),list(input())
	dp = [[0 for j in range(len(y)+1)] for i in range(len(x)+1)]
	for i in range(1,len(x)+1):
		for j in range(1,len(y)+1):
			if x[i-1] == y[j-1]:
				dp[i][j] = dp[i-1][j-1] + 1
			dp[i][j] = max(dp[i][j],dp[i-1][j],dp[i][j-1])
	print(dp[len(x)][len(y)])

Python: import pprint as p
def main():
    n = int(input())
    result = []
    for i in range(n):
        a = input()
        b = input()
        array = [[0 for i in range(len(b))] for j in range(len(a))]
        for i in range(len(a)):
            if a[i]==b[0]:
                array[i][0]=1
        for j in range(len(b)):
            if b[j]==a[0]:
                array[0][j]=1 

        for i in range(1, len(a)):
            for j in range(1, len(b)):
                c = 0
                if a[i] == b[j]:
                    c = 1
                array[i][j] = max(
                                    array[i-1][j],
                                    array[i][j-1],
                                    array[i-1][j-1]+c)
        result.append(array[len(a)-1][len(b)-1])
        #p.pprint(array)
    for i in result:
        print(i)
if __name__ == "__main__":
    main()

Python: import sys

def len_lcs(s1, s2):
    """Find length of the longest common sequence(LCS) of strings s1 and s2.
    The complexity is ~MN.

    >>> len_lcs("abc", "abc")
    3
    >>> len_lcs("abcbdaba", "bdcaba")
    5
    """
    len1 = len(s1)
    len2 = len(s2)
    if len1 == 0 or len2 == 0:
        return 0

    primary = [0] * len1
    secondary = [0] * len1

    for i in range(len2):
        for j in range(len1):
            if s1[j] == s2[i]:
                match = 1
            else:
                match = 0
            if j == 0:
                secondary[j] = max(primary[j], match)
            else:
                secondary[j] = max(secondary[j-1], primary[j],
                                   primary[j-1] + match)

        primary, secondary = secondary, primary

    return primary[len1-1]


def run():
    i = 2 * int(input())
    s = ['', '']
    for line in sys.stdin:
        s[i % 2] = line.strip()
        if i % 2 == 1:
            print(len_lcs(*s))
        i -= 1


if __name__ == '__main__':
    run()

Python: def answer(X, Y):
    m, n = len(X), len(Y)
    C = [[0] * (n + 1) for _ in range(m + 1)]
    for i in range(1, m + 1):
        for j in range(1, n + 1):
            if X[i - 1] == Y[j - 1]:
                C[i][j] = C[i-1][j-1] + 1
            else:
                C[i][j] = max(C[i][j -1], C[i -1][j])
    return C[m][n]


if __name__ == '__main__':
    q = int(input())
    for _ in range(q):
        X = list(input())
        Y = list(input())
        print(answer(X, Y))

Python: c = [[0 for i in range(1001)] for j in range(1001)]
def lcs(X, Y):
    global c
    m, n = len(X), len(Y)
    for i in range(m+1):
        c[i][0] = 0
    for j in range(n+1):
        c[0][j] = 0
    for i in range(1, m+1):
        for j in range(1, n+1):
            if X[i-1] == Y[j-1]:
                c[i][j] = c[i-1][j-1] + 1
            else:
                c[i][j] = max(c[i-1][j], c[i][j-1])
    return c[m][n]

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

Python: for _ in range(int(input())):
    a = input()
    b = input()
    indices = []
    for cb in b:
        bgn_idx = 0
        for i, cur_idx in enumerate(indices):
            chr_idx = a.find(cb, bgn_idx) + 1
            if not chr_idx:
                break
            if chr_idx < cur_idx:
                indices[i] = chr_idx
            bgn_idx = cur_idx
        else:
            chr_idx = a.find(cb, bgn_idx) + 1
            if chr_idx:
                indices.append(chr_idx)
    print(len(indices))

Python: def lcs(x,y):
    L=[]
    for chk in y:
        bg_i=0
        for i,chr_i in enumerate(L):
            cur_i=x.find(chk,bg_i)+1
            if not cur_i:
                break
            L[i]=min(cur_i,chr_i)
            bg_i=chr_i
        else:
            cur_i=x.find(chk,bg_i)+1
            if cur_i:
                L.append(cur_i)
    return len(L)

n=int(input())
ans=[]
for i in range(n):
    x=input()
    y=input()
    ans.append(lcs(x,y))
for i in ans:
    print(i)

Python: #coding:utf-8
from copy import deepcopy
n = int(input())

def lcs(X,Y):
    m = len(X)
    n = len(Y)
    c = [[False for i in range(n+1)] for j in range(m+1)]
    for i in range(m+1):
        c[i][0] = 0
    for i in range(n+1):
        c[0][i] = 0
    for i in range(1,m+1):
        for j in range(1,n+1):
            if X[i-1] == Y[j-1]:
                c[i][j] = c[i-1][j-1] + 1
            elif c[i-1][j] >= c[i][j-1]:
                c[i][j] = c[i-1][j]
            else:
                c[i][j] = c[i][j-1]
    return c[m][n]
                            
        
for  i in range(n):
    A = input()
    B = input()
    a = lcs(A,B)
    print(a)

Python: q = int(input())
for i in range(q):
    X = input()
    Y = input()
    memo = [[0 for j in range(len(Y)+1)] for i in range(len(X)+1)]
    for i in range(1, len(X)+1):
        for j in range(1, len(Y)+1):
            x = X[:i]
            y = Y[:j]
            if x[-1] == y[-1]:
                memo[i][j] = memo[i-1][j-1] + 1
            else:
                memo[i][j] = max(memo[i-1][j], memo[i][j-1])
    print(memo[len(X)][len(Y)])