๋ฌธ์
ํ์ด
// 23:14~23:57
#include <iostream>
using namespace std;
int dp[4001][4001];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
string s1,s2; cin>>s1>>s2;
int ans = 0;
for(int i=0; i<s1.length(); i++){
for(int k=0; k<s2.length(); k++){
if(i==0||k==0){
if(s1[i]==s2[k]) dp[i][k]=1;
}
else if(s1[i]==s2[k]) dp[i][k] = dp[i-1][k-1]+1;
ans = max(ans, dp[i][k]);
}
}
cout<<ans;
}
ํ์ด
DP๋ฌธ์ ์์ต๋๋ค.
์ ์ด๋ฏธ์ง์ ๊ฐ์ด ๋ฌธ์๊ฐ ๊ฐ๋ค๋ฉด ์ข์ธก ์๋จ ๋๊ฐ์ ๊ฐ์์ +1 ํด์ฃผ๋ ์์ผ๋ก ์ ๋ถ ํ์ํ ํ, ๊ฐ์ฅ ํฐ ๊ฐ์ ์ถ๋ ฅํ๋ฉด ์ฐ์ํ๋ ๊ฐ์ฅ ํฐ ๊ธธ์ด๊ฐ ๋ฉ๋๋ค.
'Algorithm ๐ง๐ปโ๐ป > ๋ฐฑ์ค(BOJ)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค,c++] 1449๋ฒ - ์๋ฆฌ๊ณต ํญ์น (0) | 2022.03.27 |
---|---|
[๋ฐฑ์ค,c++] 1254๋ฒ - ํฐ๋ฆฐ๋๋กฌ ๋ง๋ค๊ธฐ (0) | 2022.03.26 |
[๋ฐฑ์ค,c++] 4396๋ฒ - ์ง๋ขฐ ์ฐพ๊ธฐ (0) | 2022.03.26 |
[๋ฐฑ์ค,c++] 24499๋ฒ - blobyum (0) | 2022.03.23 |
[๋ฐฑ์ค,c++] 24498๋ฒ - blobnom (0) | 2022.03.23 |
[๋ฐฑ์ค,c++] 1120๋ฒ - ๋ฌธ์์ด (0) | 2022.03.22 |
๋๊ธ