백준
1. Python
a = '0' + input()
b = '0' + input()
a_len = len(a)
b_len = len(b)
dp = [[0]* b_len for _ in range(a_len)]
result = 0
for i in range(1, a_len):
for j in range(1, b_len):
if a[i] == b[j]:
dp[i][j] = dp[i-1][j-1]+ 1
result = max(result, dp[i][j])
print(result)
2. C++
#include<bits/stdc++.h>
using namespace std;
string S, T;
int dp[4001][4001], ret;
void fastIO(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
return;
}
int main(){
fastIO();
cin >> S >> T;
for(int i = 0; i < S.size(); i++){
for(int j = 0; j < T.size(); j++){
if(S[i] == T[j]){
if(i - 1 >= 0 && j - 1 >= 0){
dp[i][j] = dp[i - 1][j - 1] + 1;
ret = max(ret, dp[i][j]);
}else dp[i][j] = 1;
}
}
}
cout << ret << "\n";
return 0;
}