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: Time Limit Exceeded

Solution code:
q = int(input().rstrip())
data_set = [[[i for i in input().rstrip()],
             [i for i in input().rstrip()]] for i in range(q)]

common_list = [[0] * 2000 for i in range(2000)]


def lcs(X: list, Y: list):
    global common_list
    m, n = len(X), len(Y)
    for i in range(1, m + 1):
        for k in range(1, n + 1):
            if X[i - 1] == Y[k - 1]:
                common_list[i][k] = common_list[i - 1][k - 1] + 1
            elif common_list[i - 1][k] >= common_list[i][k - 1]:
                common_list[i][k] = common_list[i - 1][k]
            else:
                common_list[i][k] = common_list[i][k - 1]
    return common_list[m][n]


for l, r in data_set:
    print(lcs(l, r))

Submission status: Time Limit Exceeded

Solution code:
from itertools import product

q = int(input().rstrip())
data_set = [[[i for i in input().rstrip()],
             [i for i in input().rstrip()]] for i in range(q)]
common_list = [[0] * 2000 for i in range(2000)]


def lcs(X: list, Y: list):
    global common_list
    m, n = len(X), len(Y)
    for i, k in product(range(1, m + 1), range(1, n + 1)):
            common_list[i][k] = common_list[i - 1][k - 1] + 1 if X[i - 1] == Y[k - 1] else max(common_list[i - 1][k], common_list[i][k - 1])
    return common_list[m][n]

[print(lcs(l, r)) for l, r in data_set]

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
for _ in range(n):
    a, b = input(), input()
    i, j = len(a), len(b)
    prev = [0] * (j + 1)
    for ca in a:
        cur = [0] * (j + 1)
        for l, cb in enumerate(b):
            if ca == cb:
                cur[l + 1] = prev[l] + 1
            else:
                cur[l + 1] = max(cur[l], prev[l + 1])
        prev = cur
    print(prev[-1])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
for _ in range(n):
    a, b = input(), input()
    i, j = len(a), len(b)
    prev = [0] * (j + 1)
    for ca in a:
        cur = [0] * (j + 1)
        cl = -1
        for l, cb in enumerate(b):
            if ca == cb:
                cur[l + 1] = cl = prev[l] + 1
            else:
                cur[l + 1] = cl = max(cl, prev[l + 1])
        prev = cur
    print(prev[-1])

Submission status: Time Limit Exceeded

Solution code:
q = int(input())

for i in range(q):
    x = list(input())
    y = list(input())
    lcs = [[0 for i in range(len(x)+1)] for j in range(len(y)+1)]
    for i in range(1,len(y) + 1):
        for j in range(1,len(x) + 1):
            if x[j-1] != y[i-1]:
                lcs[i][j] = max(lcs[i-1][j-1],lcs[i][j-1],lcs[i-1][j])
            else:
                lcs[i][j] = max(lcs[i-1][j-1] + 1,lcs[i][j-1],lcs[i-1][j])
    print(lcs[i][j])

Submission status: Time Limit Exceeded

Solution code:
q = int(input())

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

Submission status: Time Limit Exceeded

Solution code:
q = int(input())

for k in range(q):
    x = list(input())
    y = list(input())
    lcs = [0 for i in range(len(x)+1)] 
    for i in range(1,len(y) + 1):
        lcs_2 = [i for i in lcs]
        
        for j in range(1,len(x) + 1):
            if x[j-1] != y[i-1]:
                lcs[j] = max(lcs[j-1],lcs_2[j])
            else:
                lcs[j] = lcs_2[j-1] + 1
    print(lcs[len(x)])

Submission status: Time Limit Exceeded

Solution code:
q = int(input())

for k in range(q):
    x = input()
    y = input()
    lcs = [0 for i in range(len(x)+1)] 
    for i in range(1,len(y) + 1):
        lcs_2 = lcs[:]
        for j in range(1,len(x) + 1):
            if x[j-1] == y[i-1]:
                lcs[j] = lcs_2[j-1] + 1  
            elif lcs[j] < lcs[j-1]:
                lcs[j] = lcs[j-1]
    print(lcs[len(x)])

Submission status: Time Limit Exceeded

Solution code:
q = int(input())

for k in range(q):
    x = input()
    y = input()
    ex = len(x)+1
    lcs = [0] *ex 
    for i in range(1,len(y) + 1):
        lcs_2 = lcs[:]
        for j in range(1,ex):
            if x[j-1] == y[i-1]:
                lcs[j] = lcs_2[j-1] + 1  
            elif lcs[j] < lcs[j-1]:
                lcs[j] = lcs[j-1]
    print(lcs[-1])

Submission status: Time Limit Exceeded

Solution code:
q = int(input())

for k in range(q):
    x = input()
    y = input()
    lcs = [0]*(len(x)+1)
    for i in range(len(y)):
        w1 = y[i]
        lcs_2 = lcs[:]
        for j in range(len(x)):
            if x[j] == w1:
                lcs[j+1] = lcs_2[j] + 1  
            elif lcs[j+1] < lcs[j]:
                lcs[j+1] = lcs[j]
    print(lcs[-1])

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

import random
import sys
import os
import pprint


def LCS(s0, s1):
    m = len(s0)
    n = len(s1)

    # memo[i][j]
    memo = [[0 for j in range(n)] for i in range(m)]

    # fill at index 0
    for i in range(m):
        if s1[0] in s0[0:i+1]:
            memo[i][0] = 1
    for j in range(n):
        if s0[0] in s1[0:j+1]:
            memo[0][j] = 1

    # fill memo
    for i in range(1, m):
        for j in range(1, n):
            # same last
            if s0[i] == s1[j]:
                memo[i][j] = memo[i-1][j-1] + 1
            else:
                memo[i][j] = max(memo[i-1][j], memo[i][j-1])
    return memo[m-1][n-1]

#fd = os.open('ALDS1_10_C.txt', os.O_RDONLY)
#os.dup2(fd, sys.stdin.fileno())

n = int(input())
for _ in range(n):
    s0 = input()
    s1 = input()
    l = LCS(s0, s1)
    print(l)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

import random
import sys
import os
import pprint


def LCS(s0, s1):
    # print(s0)
    # print(s1)
    m = len(s0)
    n = len(s1)

    # memo[i][j]
    memo = [[0] * n for i in range(m)]
    # pprint.pprint(memo)

    # fill at index 0
    for i in range(m):
        if s1[0] in s0[0:i+1]:
            memo[i][0] = 1
    for j in range(n):
        if s0[0] in s1[0:j+1]:
            memo[0][j] = 1

    # fill memo
    for i in range(1, m):
        for j in range(1, n):
            # same last
            if s0[i] == s1[j]:
                memo[i][j] = memo[i-1][j-1] + 1
            else:
                memo[i][j] = max(memo[i-1][j], memo[i][j-1])
    return memo[m-1][n-1]

#fd = os.open('ALDS1_10_C.txt', os.O_RDONLY)
#os.dup2(fd, sys.stdin.fileno())

n = int(input())
for _ in range(n):
    s0 = input()
    s1 = input()
    l = LCS(s0, s1)
    print(l)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

import random
import sys
import os
import pprint


def LCS(s0, s1):
    # print(s0)
    # print(s1)
    m = len(s0)
    n = len(s1)

    # memo[i][j]
    memo = [[0] * n for i in range(m)]
    # pprint.pprint(memo)

    # fill at index 0
    index = s0.find(s1[0])
    if index != -1:
        for i in range(index, m):
            memo[i][0] = 1

    index = s1.find(s0[0])
    if index != -1:
        for j in range(index, n):
            memo[0][j] = 1

    """
    for i in range(m):
        if s1[0] in s0[0:i+1]:
            memo[i][0] = 1
    for j in range(n):
        if s0[0] in s1[0:j+1]:
            memo[0][j] = 1
    """

    # fill memo
    for i in range(1, m):
        for j in range(1, n):
            # same last
            if s0[i] == s1[j]:
                memo[i][j] = memo[i-1][j-1] + 1
            else:
                memo[i][j] = max(memo[i-1][j], memo[i][j-1])
    return memo[m-1][n-1]


"""
def LCS_better(s0, s1):
    dp = [0] * len(s1) # cost use till j
    for c in s0:
        tmp = dp[:]
        for j in range(len(s1)):
            if c == s1[j]:
                dp[j + 1] = tmp[j] + 1
            elif dp[j + 1] < dp[j]:
                dp[j + 1] = dp[j]
    return dp[-1]
"""

#fd = os.open('ALDS1_10_C.txt', os.O_RDONLY)
#os.dup2(fd, sys.stdin.fileno())

n = int(input())
for _ in range(n):
    s0 = input()
    s1 = input()
    l = LCS(s0, s1)
    #l2 = LCS_better(s0, s1)
    print(l)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

def array2d(d1, d2, init = None):
    return [[init for _ in range(d2)] for _ in range(d1)]


def solve():
    x = input().strip()
    y = input().strip()

    # dp[i][j]: x[:i], y[:j] ???LCS
    # dp[i][j] = max(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]+1(if x[i]==y[j]))
    dp = array2d(len(x), len(y), 0)
    for i in range(len(x)):
        for j in range(len(y)):
            if i == 0 and j == 0:
                dp[0][0] = int(x[0] == y[0])
                continue
            if i > 0:
                dp[i][j] = max(dp[i][j], dp[i-1][j])
            if j > 0:
                dp[i][j] = max(dp[i][j], dp[i][j-1])
            if i > 0 and j > 0:
                cand = dp[i-1][j-1]
            else:
                cand = 0
            dp[i][j] = max(dp[i][j], cand + (x[i] == y[j]))
    return dp[-1][-1]


def main():
    cases = int(input())
    for i in range(cases):
        print(solve())

main()

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

def array2d(d1, d2, init = None):
    return [[init for _ in range(d2)] for _ in range(d1)]


def solve():
    x = input().strip()
    y = input().strip()

    # dp[i][j]: x[:i], y[:j] ???LCS
    # dp[i][j] = max(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]+1(if x[i]==y[j]))
    dp = array2d(len(x), len(y), 0)
    dp[0][0] = int(x[0] == y[0])
    for j in range(1, len(y)):
        dp[0][j] = max(dp[0][j-1], int(x[0] == y[j]))
    for i in range(1, len(x)):
        dp[i][0] = max(dp[i-1][0], int(x[i] == y[0]))
        for j in range(1, len(y)):
            dp[i][j] = max(dp[i-1][j], dp[i][j-1])
            if x[i] == y[j]:
                dp[i][j] = max(dp[i][j], dp[i-1][j-1] + 1)
    return dp[-1][-1]


def main():
    cases = int(input())
    for i in range(cases):
        print(solve())

main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
input:
3
abcbdab
bdcaba
abc
abc
abc
bc

output:
4
3
2

"""
import sys


def lcs(w1, w2):
    m, n = len(w1), len(w2)
    dp = [[0] * (n + 1) for _ in range(m + 1)]
    max_l = 0
    for i in range(1, m + 1):
        for j in range(1, n + 1):
            if w1[i - 1] == w2[j - 1]:
                dp[i][j] = dp[i - 1][j - 1] + 1
            else:
                dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
            max_l = max(max_l, dp[i][j])

    return max_l


def solve():
    input_ = sys.stdin.readlines()
    pair_num = int(input_[0])

    word_list = input_[1:]
    # assert len(word_list) == 2 * pair_num

    for pair in zip(word_list[::2], word_list[1::2]):
        cw1, cw2 = map(lambda x: x.strip(), pair)
        print(lcs(cw1, cw2))
        # l1, l2 = len(cw1), len(cw2)
        # print(lcs_recursive(cw1, cw2, l1, l2))

    return


if __name__ == '__main__':
    solve()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
input:
3
abcbdab
bdcaba
abc
abc
abc
bc

output:
4
3
2

"""
import sys

sys.setrecursionlimit(int(1e5))


def lcs(w1, w2):
    m, n = len(w1), len(w2)
    dp = tuple([[0] * (n + 1) for _ in range(m + 1)])
    max_l = 0
    for i in range(1, m + 1):
        for j in range(1, n + 1):
            if w1[i - 1] == w2[j - 1]:
                dp[i][j] = dp[i - 1][j - 1] + 1
            else:
                dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
            max_l = max(max_l, dp[i][j])

    return max_l


def lcs_recursive(w1, w2, m, n):
    if m == 0 or n == 0:
        return 0
    elif w1[m - 1] == w2[n - 1]:
        return 1 + lcs_recursive(w1, w2, m - 1, n - 1)
    else:
        return max(
            lcs_recursive(w1, w2, m, n - 1), lcs_recursive(w1, w2, m - 1, n))


def solve():
    _input = sys.stdin.readlines()
    pair_num = int(_input[0])

    word_list = _input[1:]
    # assert len(word_list) == 2 * pair_num

    for pair in zip(word_list[::2], word_list[1::2]):
        cw1, cw2 = map(lambda x: x.strip(), pair)
        # print(lcs(cw1, cw2))
        print(lcs_recursive(cw1, cw2, len(cw1), len(cw2)))

    return None


if __name__ == '__main__':
    solve()

Submission status: Time Limit Exceeded

Solution code:
q=int(input())

def lastindex(a,x):
    try:
        return len(a)-a[::-1].index(x)-2
    except:
        return(-1)
for repeat in range(q):
    X=input()
    Y=input()
    lenX=len(X)
    lenY=len(Y)
    Z=[[0 for i in range(lenY)] for i in range(lenX)]

    if X[0]==Y[0]:
        Z[0][0]=1
    
    for i in range(1,lenY):
        if Z[0][i-1]==1:
            Z[0][i]=1
        else:
            if Y[i]==X[0]:
                Z[0][i]=1

    for j in range(lenX):
        if Z[j-1][0]==1:
            Z[j][0]=1
        else:
            if X[j]==Y[0]:
                Z[j][0]=1


    for i in range(1,lenX):
        for j in range(1,lenY):
            x=lastindex(X[:i+1:],Y[j])
            y=lastindex(Y[:j+1:],X[i])

            if x!=-1 and y!=-1:
                Z[i][j]=max(Z[x][j-1],Z[i-1][y])+1
            elif x!=-1:
                Z[i][j]=Z[x][j-1]+1
            elif y!=-1:
                Z[i][j]=Z[i-1][y]+1

            Z[i][j]=max(Z[i][j],Z[i-1][j],Z[i][j-1])

    print(Z[-1][-1])

Submission status: Time Limit Exceeded

Solution code:
def get_longest_count(X, Y):
  m = [[0] * (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]:
        m[i][j] = m[i - 1][j - 1] + 1
      else:
        m[i][j] = max(m[i][j - 1], m[i - 1][j])
  return m[-1][-1]

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

Submission status: Time Limit Exceeded

Solution code:
#Longest Common Subsequence
#?????????

def lastindex(a,x):
    #????????????a??????????????????????????????x??????????????????????????????????????????1???????????????????????????x????????¨???????????¨??????-1????????????
    try:
        return len(a)-a[::-1].index(x)-2
    except:
        return(-1)

'''
X={x1,x2,...xm}
Y={y1,y2,...yn}????????????

Xi={x1,x2,...xi}(1<=i<=m)
Yj={y1,y2,...yj}(1<=j<=n)
Z(i,j)=Xi??¨Yj???????????±?????¨???????????????
??¨?????????????????¨???

Zi,j=max{
	Z(lastindex(Xi,yj),j-1)+1
	Z(i-1,lastindex(Yj,xi))+1
	Z(i-1,j)
	Z(i,j-1)
}
??¨?????????
??¢??°lastindex??§???(Zi,j????¨????????????¨????????§?????????)??????1??????????????????????????????????????????lastindex(a,x)???0?????¨??????lastindex(a,i)-1=-1??¨?????£???????????????Z[lastindex(a,x)-1][...]?????¨?????\?????????????????£?????????????????????
a?????????x????????¨???????????????????????????-1??¨???????????¨??§?????????????????°???????????§?????¨??????-1?????????????????????
'''

#????????????????????°????????§?????£???????????????AOJ????????????????????¨??????????????????X??¨Y????\???????????????¨??????TLE??¨?????£???????????£??????

q=int(input())


for repeat in range(q):
    X=input()
    Y=input()
    lenX=len(X)
    lenY=len(Y)
    Z=[[0 for i in range(lenY)] for i in range(lenX)]

    if X[0]==Y[0]:
        Z[0][0]=1
    
    for i in range(1,lenY):
        if Z[0][i-1]==1:
            Z[0][i]=1
        else:
            if Y[i]==X[0]:
                Z[0][i]=1

    for j in range(lenX):
        if Z[j-1][0]==1:
            Z[j][0]=1
        else:
            if X[j]==Y[0]:
                Z[j][0]=1


    for i in range(1,lenX):
        for j in range(1,lenY):
            x=lastindex(X[:i+1:],Y[j])
            y=lastindex(Y[:j+1:],X[i])

            if x!=-1 and y!=-1:
                Z[i][j]=max(Z[x][j-1],Z[i-1][y])+1
            elif x!=-1:
                Z[i][j]=Z[x][j-1]+1
            elif y!=-1:
                Z[i][j]=Z[i-1][y]+1

            Z[i][j]=max(Z[i][j],Z[i-1][j],Z[i][j-1])

    print(Z[-1][-1])

Submission status: Time Limit Exceeded

Solution code:
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))

Submission status: Time Limit Exceeded

Solution code:
def lcs(X, Y):
    m = len(X)
    n = len(Y)
    c = [[0 for i in range(n+1)] for i 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
            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 str(c[m][n])

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

Submission status: Time Limit Exceeded

Solution code:
import sys
def lcs(X, Y):
    m = len(X)
    n = len(Y)
    c = [[0 for i in range(n+1)] for i 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
            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 str(c[m][n])

q = int(input())
for i in range(q):
    X = sys.stdin.readline().rstrip('\r\n')
    Y = sys.stdin.readline().rstrip('\r\n')
    print(lcs(X, Y))

Submission status: Time Limit Exceeded

Solution code:
import sys
def lcs(X, Y):
    m = len(X)
    n = len(Y)
    # c = [[0 for i in range(n+1)] for i in range(m+1)]
    c = [None]*(m+1)
    for i in range(m+1):
        c[i] = [0]*(n+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
            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 str(c[m][n])

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

Submission status: Time Limit Exceeded

Solution code:
import sys
def lcs(X, Y):
    m = len(X)
    n = len(Y)
    # c = [[0 for i in range(n+1)] for i in range(m+1)]
    c = [None]*(m+1)
    for i in range(m+1):
        c[i] = [0]*(n+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
            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 str(c[m][n])

q = int(input())
for i in range(q):
    X = sys.stdin.readline().rstrip('\r\n')
    Y = sys.stdin.readline().rstrip('\r\n')
    print(lcs(X, Y))

Submission status: Time Limit Exceeded

Solution code:
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 = input()
        Y = input()
        print(answer(X, Y))

Submission status: Time Limit Exceeded

Solution code:
def answer(X, Y):
    m = len(X)
    n = len(Y)
    C = [[0 for _ in range(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 = input()
        Y = input()
        print(answer(X, Y))

Submission status: Time Limit Exceeded

Solution code:
import sys

def solve():
    q = int(sys.stdin.readline())

    for qi in range(q):
        x = sys.stdin.readline().rstrip()
        y = sys.stdin.readline().rstrip()

        ans = compute_lcs(x, y)

        print(ans)

def compute_lcs(x, y):
    n = len(x)
    m = len(y)

    x = '#' + x
    y = '#' + y

    dp = [[0]*(m + 1) for i in range(n + 1)]

    for i in range(1, n + 1):
        for j in range(1, m + 1):
            if x[i] == y[j]:
                dp[i][j] = dp[i - 1][j - 1] + 1
            else:
                dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])

    return dp[n][m]

if __name__ == '__main__':
    solve()

Submission status: Time Limit Exceeded

Solution code:
MAX = 1005
n = int(input())
dp = [[0 for j in range(MAX)] for i in range(MAX)]
for _ in range(n):
    X = input()
    Y = input()
    len_X = len(X)
    len_Y = len(Y)

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

Submission status: Time Limit Exceeded

Solution code:
MAX = 1005
n = int(input())
dp = [[0 for j in range(MAX)] for i in range(MAX)]
X = Y = ""
len_X = len_X = 0
for _ in range(n):
    X = input()
    Y = input()
    len_X = len(X)
    len_Y = len(Y)

    for i in range(len_X):
        for j in range(len_Y):
            # dp[i + 1][j + 1] = dp[i][j] + 1 if X[i] == Y[j] else max(dp[i][j + 1], dp[i + 1][j])
            if X[i] == Y[j]:
                dp[i + 1][j + 1] = dp[i][j] + 1
            else:
                dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j])
    print(dp[len_X][len_Y])

Submission status: Time Limit Exceeded

Solution code:
q = int(input())


def lcs(X, Y):
    x_len = len(X)
    y_len = len(Y)
    dp = [[0 for _ in range(y_len+1)] for _ in range(x_len+1)]
    
    for i in range(1, x_len+1):
        for j in range(1, y_len+1):
            if X[i-1] == Y[j-1]:
                dp[i][j] = dp[i-1][j-1] + 1
            else:
                dp[i][j] = max(dp[i-1][j], dp[i][j-1])
                               
    return max(max(dp))
    
for _ in range(q):
    X = input()
    Y = input()
    print(lcs(X, Y))

Submission status: Time Limit Exceeded

Solution code:
q = int(input())

def lcs(X, Y):
    x_len = len(X)
    y_len = len(Y)
    dp = [[0 for _ in range(y_len+1)] for _ in range(x_len+1)]
    
    for i in range(1, x_len+1):
        for j in range(1, y_len+1):
            if X[i-1] == Y[j-1]:
                dp[i][j] = dp[i-1][j-1] + 1
            else:
                dp[i][j] = max(dp[i-1][j], dp[i][j-1])
                               
    return max(max(dp))
    
for _ in range(q):
    X = input()
    Y = input()
    print(lcs(X, Y))

Submission status: Time Limit Exceeded

Solution code:
q = int(input())

def lcs(X, Y):
    x_len = len(X)
    y_len = len(Y)
    dp = [[0 for _ in range(y_len+1)] for _ in range(x_len+1)]
    
    for i, x in enumerate(X):
        for j, y in enumerate(Y):
            if x == y:
                dp[i][j] = dp[i-1][j-1] + 1
            else:
                dp[i][j] = max(dp[i-1][j], dp[i][j-1])
                               
    return max(max(dp))
    
for _ in range(q):
    X = input()
    Y = input()
    print(lcs(X, Y))

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
import re
import math
import collections
import itertools
import functools

DEBUG = True
DEBUG = False

def dbg(*args):
    if DEBUG:
        print("DBG: ", file=sys.stderr, end="")
        print(*args, file=sys.stderr)

dp = [[0] * 1001 for _ in range(1001)]

def solve(x, y):
    m = len(x)
    n = len(y)

    for i in range(m+1):
        dp[i][0] = 0
    for j in range(n+1):
        dp[0][j] = 0

    for i in range(1, m+1):
        for j in range(1, n+1):
            if x[i-1] == y[j-1]:
                dp[i][j] = dp[i-1][j-1] + 1
            else:
                dp[i][j] = max(dp[i-1][j], dp[i][j-1])

    return dp[m][n]

def main():
    n = int(input())

    for _ in range(n):
        x = input()
        y = input()
        dbg(x, y)

        lcs_len = solve(x, y)
        print(lcs_len)

if __name__ == "__main__": main()

Submission status: Time Limit Exceeded

Solution code:
import sys


def lcs(X, Y):
    m = len(X)
    n = len(Y)
    c = [[0] * (n+1) if i == 0 else [0] + [None] * n for i 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
            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]


if __name__ == "__main__":
    n = int(sys.stdin.readline())
    inp = sys.stdin.readlines()
    for i in range(n):
        print(lcs(inp[2*i][:-1], inp[2*i+1][:-1]))

Submission status: Time Limit Exceeded

Solution code:
# coding=utf-8

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

q = int(input())

for _ in range(q):
    X = list(input())
    Y = list(input())
    c = [[0 for _ in range(len(Y))] for _ in range(len(X))]
    max_c = lcs(X, Y)
    print(max_c)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = input()
    for i in xrange(n):
        X = raw_input()
        Y = raw_input()
        print lcs(X,Y)

def lcs(X,Y):
    m = len(X)
    n = len(Y)
    c = [[0]*(n+1) for i in xrange(m+1)]

    for i in xrange(m):
        for j in xrange(n):
            if X[i] == Y[j]:
                c[i+1][j+1] = c[i][j] + 1
            else:
                c[i+1][j+1] = max(c[i][j+1],c[i+1][j])
    return c[m][n]
    
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = input()
    for i in xrange(n):
        X = raw_input()
        Y = raw_input()
        print lcs(X,Y)

def lcs(X,Y):
    m = len(X)
    n = len(Y)
    c = [[0]*(n+1) for i in xrange(m+1)]

    for i in xrange(m):
        for j in xrange(n):
            if X[i] == Y[j]:
                c[i+1][j+1] = c[i][j] + 1
            elif c[i][j+1] >= c[i+1][j]:
                c[i+1][j+1] = c[i][j+1]
            else:
                c[i+1][j+1] = c[i+1][j]
    return c[m][n]
    
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = input()
    for i in xrange(n):
        X = raw_input()
        Y = raw_input()
        print lcs(X,Y)

def lcs(X,Y):
    m = len(X)
    n = len(Y)
    c = [[0]*(n+1) for i in xrange(m+1)]

    for i in xrange(m):
        for j in xrange(n):
            if X[i] == Y[j]:
                c[i+1][j+1] = c[i][j] + 1
            else:
                if c[i][j+1] >= c[i+1][j]:
                    c[i+1][j+1] = c[i][j+1]
                else:
                    c[i+1][j+1] = c[i+1][j]
    return c[m][n]
    
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = input()
    for i in xrange(n):
        X = raw_input()
        Y = raw_input()
        print lcs(X,Y)

def lcs(X,Y):
    m = len(X)
    n = len(Y)
    c = [[0] for i in xrange(m+1)]
    c[0] += [0]*n
    
    for i in xrange(m):
        for j in xrange(n):
            if X[i] == Y[j]:
                c[i+1].append(c[i][j] + 1)
            else:
                if c[i][j+1] >= c[i+1][j]:
                    c[i+1].append(c[i][j+1])
                else:
                    c[i+1].append(c[i+1][j])
    return c[m][n]
    
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
dp = [[0] * 1001 for i in xrange(1001)]

def main():
	n = input()
	for i in xrange(n):
		X = raw_input()
		Y = raw_input()
		print lcs(X,Y)

def lcs(X,Y):
	m = len(X)
	n = len(Y)
	for i in xrange(m):
		for j in xrange(n):
			if X[i] == Y[j]:
				dp[i+1][j+1] = dp[i][j] +1
			else:
				if dp[i][j+1] >= dp[i+1][j]:
					dp[i+1][j+1] = dp[i][j+1]
				else:
					dp[i+1][j+1] = dp[i+1][j]
	return dp[m][n]

if __name__ == "__main__":
	main()

Submission status: Time Limit Exceeded

Solution code:
num = int(raw_input())
input = []
for i in range(num*2):
    input.append(raw_input())
for i in range(num):
    a = input[2*i]
    b = input[2*i+1]
    m = len(a)
    n = len(b)
    c = [[0 for i in range(n+1)] for j in range(m+1)]
    for i in range(m):
        for j in range(n):
            if a[i] == b[j]:
                c[i+1][j+1] = c[i][j] + 1
            elif c[i][j+1] >= c[i+1][j]:
                c[i+1][j+1] = c[i][j+1]
            else:
                c[i+1][j+1] = c[i+1][j]
    print c[-1][-1]

Submission status: Time Limit Exceeded

Solution code:
num = int(raw_input())
input = []
for i in range(num*2):
    input.append(raw_input())
for i in range(num):
    a = input[2*i]
    b = input[2*i+1]
    m = len(a)
    n = len(b)
    c = [0 for i in range(n+1)]
    for i in range(m):
        tmp = c[:]
        for j in range(n):
            if a[i] == b[j]:
                c[j+1] = tmp[j] + 1
            elif c[j+1] < c[j]:
                c[j+1] = c[j]
    print c[-1]

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict


def longest_common_subsequence(s, t):
    dp = [[0] * (len(t) + 1) for _ in range(len(s) + 1)]
    for i in range(len(s)):
        for j in range(len(t)):
            if s[i] == t[j]:
                dp[i + 1][j + 1] = dp[i][j] + 1
            else:
                dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1])
    return dp[-1][-1]


def main():
    n = int(input())
    for _ in range(n):
        X, Y = input(), input()
        print(longest_common_subsequence(X, Y))


if __name__ == '__main__':
    main()