반응형

문제 출처 :


https://www.acmicpc.net/problem/3793



알고리즘 분석 :


문제 해결에 필요한 사항

1. LCS :: http://www.crocus.co.kr/787


기본적인 LCS 문제이다.


위의 LCS링크를 통해 LCS의 개념을 익히고, 그대로 적용하여 문제를 풀어보자.



그외 다른  LCS 문제들은 다음 링크에 존재한다. :: http://www.crocus.co.kr/search/lcs






소스 코드 : 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <cstdio>
#include <string.h>
#include <memory.h>
using namespace std;
 
char str1[1001];
char str2[1001];
int lcs[1001][1001];
 
int main()
{
    while(scanf("%s %s", str1, str2) != EOF)
    {
        memset(lcs, 0sizeof(lcs));
        int len1 = strlen(str1);
        int len2 = strlen(str2);
 
        for (int i = 0; i <= len1; i++)
        {
            for (int j = 0; j <= len2; j++)
            {
                if (i == || j == 0)
                {
                    lcs[i][j] = 0;
                    continue;
                }
 
                if (str1[i - 1== str2[j - 1])
                    lcs[i][j] = lcs[i - 1][j - 1+ 1;
 
                else
                {
                    if (lcs[i - 1][j] > lcs[i][j - 1])
                        lcs[i][j] = lcs[i - 1][j];
                    else
                        lcs[i][j] = lcs[i][j - 1];
                }
            }
        }
 
 
        cout << lcs[len1][len2] << endl;
    }
    return 0;
}
 
//                                                       This source code Copyright belongs to Crocus
//                                                        If you want to see more? click here >>
Crocus


반응형

'Applied > 알고리즘 문제풀이' 카테고리의 다른 글

[2261번] 가장 가까운 두 점  (0) 2017.04.21
[11945번] 뜨거운 붕어빵  (0) 2017.04.21
[2941번] 크로아티아 알파벳  (0) 2017.04.21
[3943번] 헤일스톤 수열  (0) 2017.04.21
[2563번] 색종이  (0) 2017.04.21