https://www.acmicpc.net/problem/1958
๋ฌธ์
๋ฌธ์์ด๊ณผ ๋๊ธฐ๋ฅผ ์ธ์์์ ์ ์ผ ์ข์ํ๋ ์์์ด๋ ์ค๋๋ ๋ฌธ์์ด 2๊ฐ์ LCS(Longest Common Subsequence)๋ฅผ ๊ตฌํ๊ณ ์์๋ค. ์ด๋ ๋ ์์์ด๋ ์กฐ๊ต๋ค์ด ๋ฌธ์์ด 3๊ฐ์ LCS๋ฅผ ๊ตฌํ๋ ๊ฒ์ ๋ณด์๋ค. ์์์ด๋ ๋์ ํด ๋ณด์์ง๋ง ์คํจํ๊ณ ๋ง์๋ค.
์ด์ ์ฐ๋ฆฌ๊ฐ ํ ์ผ์ ๋ค์๊ณผ ๊ฐ๋ค. ์์์ด๋ฅผ ๋์์ ๋ฌธ์์ด 3๊ฐ์ LCS๋ฅผ ๊ตฌํ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ๋ผ.
์ ๋ ฅ
์ฒซ ์ค์๋ ์ฒซ ๋ฒ์งธ ๋ฌธ์์ด์ด, ๋์งธ ์ค์๋ ๋ ๋ฒ์งธ ๋ฌธ์์ด์ด, ์ ์งธ ์ค์๋ ์ธ ๋ฒ์งธ ๋ฌธ์์ด์ด ์ฃผ์ด์ง๋ค. ๊ฐ ๋ฌธ์์ด์ ์ํ๋ฒณ ์๋ฌธ์๋ก ์ด๋ฃจ์ด์ ธ ์๊ณ , ๊ธธ์ด๋ 100๋ณด๋ค ์๊ฑฐ๋ ๊ฐ๋ค.
์ถ๋ ฅ
์ฒซ ์ค์ ์ฒซ ๋ฒ์งธ ๋ฌธ์์ด๊ณผ ๋ ๋ฒ์งธ ๋ฌธ์์ด๊ณผ ์ธ ๋ฒ์งธ ๋ฌธ์์ด์ LCS์ ๊ธธ์ด๋ฅผ ์ถ๋ ฅํ๋ค.
ํ์ด
์ฒ์์๋ ๋ฌธ์์ด ์ผ์นํ๋ ์ต๋ substring ์ ๊ธธ์ด๋ฅผ ๊ตฌํ๋ ๊ฒ์ผ๋ก ์ฐฉ๊ฐํด์ ํ๋ ท๋ค.....
substring ์ด ์๋๋ผ, subsequence ๋ก ์ฐ์๋์ง ์์๋ ๋๋คโผ๏ธ
๊ทธ๋์ Dynamic Programming ์ผ๋ก ํ์๋ค.
DP[i][j][k] ๋ 1๋ฒ์งธ ๋ฌธ์์ด์ charAt(i) , 2๋ฒ์งธ ๋ฌธ์์ด์ charAt(j), 3๋ฒ์งธ ๋ฌธ์์ด์ charAt(k) ๊น์ง ์ผ์นํ ๋ฌธ์์ ๊ฐฏ์์ด๋ค.
charAt(i) == charAt(j) == charAt(k) ์ด๋ฉด,
DP[i][j][k] = DP[i-1][j-1][k-1] + 1 ์ด๊ณ
ํ๋๋ผ๋ ๊ฐ์ง ์์ผ๋ฉด,
DP[i][j][k] = max( DP[i-1][j][k], DP[i][j-1][k], DP[i][j][k-1] ) ์ด๋ค.
์ฝ๋
- ํ๋ฆฐ ์ฝ๋
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
public class Main{
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] str = new String[3];
for(int i = 0 ; i < 3; i++){
str[i] = br.readLine();
}
Arrays.sort(str, new Comparator<String>(){
@Override
public int compare(String o1, String o2) {
return o1.length() - o2.length();
}
});
int minLen = str[0].length();
int answer = 0;
boolean flag = false;
for(int i = minLen ; i >= 0 ; i--){
for(int j = 0; j < minLen; j++){
String s;
if(i + j > str[0].length())
s = str[0].substring(j, str[0].length());
else
s = str[0].substring(j, j+i);
if(str[1].contains(s) && str[2].contains(s)){
answer = i;
flag = true;
break;
}
}
if(flag)
break;
}
System.out.println(answer);
}
}
- ๋ง์ ์ฝ๋
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
public class Main_BOJ_1958_LCS3 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] str = new String[3];
int[] len = new int[3];
for(int i = 0 ; i < 3; i++){
str[i] = br.readLine();
len[i] = str[i].length();
}
int[][][] DP = new int[len[0]+1][len[1]+1][len[2]+1];
for(int i = 1 ; i <= len[0]; i++){
for(int j = 1 ; j <= len[1] ; j++){
for(int k = 1 ; k <= len[2] ; k++){
if(str[0].charAt(i-1) == str[1].charAt(j-1) && str[1].charAt(j-1) == str[2].charAt(k-1)){
DP[i][j][k] = DP[i-1][j-1][k-1] + 1;
}
else{
DP[i][j][k] = Math.max(DP[i-1][j][k], Math.max(DP[i][j-1][k], DP[i][j][k-1]));
}
}
}
}
System.out.println(DP[len[0]][len[1]][len[2]]);
}
}
'์๊ณ ๋ฆฌ์ฆ > ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ ๋ฐฑ์ค / BOJ 17070 ] ํ์ดํ ์ฎ๊ธฐ๊ธฐ 1 (0) | 2021.09.15 |
---|---|
[ ๋ฐฑ์ค / BOJ 17208 ] ์นด์ฐ๋ฒ๊ฑฐ ์๋ฐ์ (0) | 2021.09.15 |
[๋ฐฑ์ค / BOJ 12871] ๋ฌดํ ๋ฌธ์์ด (0) | 2021.09.14 |
[๋ฐฑ์ค / BOJ 17406] ๋ฐฐ์ด ๋๋ฆฌ๊ธฐ 4 (0) | 2021.09.14 |
[๋ฐฑ์ค / BOJ 18427] ํจ๊ป ๋ธ๋ก ์๊ธฐ (0) | 2021.09.14 |