213. 공통 부분 문자열

아현·2021년 7월 16일
0

Algorithm

목록 보기
221/400

백준



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;
}

profile
For the sake of someone who studies computer science

0개의 댓글