Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

23-yuyu0830 #86

Merged
merged 1 commit into from
Aug 17, 2024
Merged

23-yuyu0830 #86

merged 1 commit into from
Aug 17, 2024

Conversation

yuyu0830
Copy link
Collaborator

@yuyu0830 yuyu0830 commented Aug 5, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

LCS 2

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

2์‹œ๊ฐ„

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

โ“ ๋ฌธ์ œ

image

๊ณจ๋“œ 4...? ์ด๊ฒŒ..?


โ— ํ’€์ด


LCS ๊ธธ์ด ๊ตฌํ•˜๊ธฐ


์˜ˆ์ „์— LCS ๋ฌธ์ œ๋ฅผ ํ‘ผ ์ ์ด ์žˆ์–ด์„œ ํ•ด๋‹น ํ’€์ด๋ฅผ ์ฐธ๊ณ ํ–ˆ๋‹ค. ๊ธฐ์กด ๋ฌธ์ œ๋Š” LCS์˜ ๊ธธ์ด๋งŒ ๊ตฌํ•˜๋ฉด ๋˜๊ธฐ์— 2์ฐจ์› DP ๋ฐฐ์—ด์„ ์ด์šฉํ•ด 1๋ฒˆ ๋ฌธ์ž์—ด์˜ i๋ฒˆ์งธ char, 2๋ฒˆ ๋ฌธ์ž์—ด์˜ j๋ฒˆ์งธ char๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” LCS์˜ ์ตœ๋Œ€ ๊ธธ์ด๋ฅผ ์ €์žฅํ•ด ์‚ฌ์šฉํ–ˆ๋‹ค.

str1[i]์™€ str2[j] ๊ฐ€ ๊ฐ™๋‹ค๋ฉด LCS๋ฅผ 1 ๋Š˜๋ฆด ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— str1[i] ์™€ str2[j] ์˜ ์ด์ „ ๋ฌธ์ž์—ด ์ค‘ ์ตœ๋Œ€ ๊ฐ’, ์ฆ‰ dp[i - 1][j - 1] ์˜ ๊ฐ’์— 1์„ ๋”ํ•˜๋ฉด ๋œ๋‹ค.

๋งŒ์•ฝ str1[i] ์™€ str2[j] ๊ฐ€ ๋‹ค๋ฅด๋‹ค๋ฉด str1[i] ํ˜น์€ str2[j] ๋‘˜ ์ค‘ ํ•˜๋‚˜๋ฅผ ์‚ฌ์šฉํ•œ ์ตœ๋Œ€ ๊ฐ’, ์ฆ‰ dp[i][j - 1], dp[i - 1][j] ์ค‘ ํฐ ๊ฐ’์„ ์“ฐ๋ฉด ๋œ๋‹ค.

i - 1, j - 1์„ ๋‹ค๋ฃจ๊ธฐ ๋•Œ๋ฌธ์— out of bound ์—๋Ÿฌ๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ๋ฐฐ์—ด์„ 1์”ฉ ๋ฏธ๋ฃฌ ๋’ค, i๋‚˜ j๊ฐ’์ด 0์ธ ๊ฒฝ์šฐ๋Š” ๊ฐ’์„ 0์œผ๋กœ ์ฑ„์› ๋‹ค.

์˜ˆ์‹œ๋ฅผ ๋ณด์ž

image

ํ‘œ๋ฅผ ์—ฌ๊ธฐ๊นŒ์ง€ ์ฑ„์› ๊ณ  ๋นจ๊ฐ„ ๋ฐ•์Šค๋ฅผ ์ฑ„์›Œ์•ผํ•  ์ฐจ๋ก€๋‹ค. 1๋ฒˆ์งธ ๋ฌธ์ž์—ด์˜ 5๋ฒˆ์งธ ๋ฌธ์ž k์™€ 2๋ฒˆ์งธ ๋ฌธ์ž์—ด์˜ 5๋ฒˆ์งธ ๋ฌธ์ž A๋ฅผ ๋น„๊ตํ•˜๊ณ  ์žˆ๋‹ค. ์ด ๋‘˜์€ ์„œ๋กœ ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ๊ฐ dp[i][j - 1]๊ณผ dp[i - 1][j] ์ค‘์— ํฐ ๊ฐ’์„ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

image

๋‹ค์Œ ์นธ์„ ๋ณด์ž

image

์ด๋ฒˆ์—” ๋น„๊ตํ•˜๋Š” ๋‘ ๋ฌธ์ž ๋ชจ๋‘ k๋กœ ๊ฐ™๊ธฐ ๋•Œ๋ฌธ์— dp[i - 1][j - 1]์˜ ๊ฐ’์— 1์„ ๋”ํ•˜๋ฉด ๋œ๋‹ค.

image

์ด๋Ÿฐ ์‹์œผ๋กœ ๋๊นŒ์ง€ ์ฑ„์›Œ๋‚˜๊ฐ€๋ฉด ๋œ๋‹ค. ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

image

์ด๋ ‡๊ฒŒ LCS์˜ ์ตœ๋Œ€ ๊ธธ์ด๋Š” 4์ž„์„ ์•Œ์•˜๋‹ค.

int main(){
    string a, b; cin >> a >> b;
    int arr[a.size() + 1][b.size() + 1] = {0, };
    for(int i = 0; i <= a.size(); i++){
        for (int j = 0; j <= b.size(); j++){
            if (i == 0 || j == 0) arr[i][j] = 0;
            else if (a[i-1] == b[j-1]) arr[i][j] = arr[i-1][j-1] + 1;
            else arr[i][j] = max(arr[i][j-1], arr[i-1][j]);
        }
    }
    cout << arr[a.size()][b.size()];
}

LCS ๊ตฌํ•˜๊ธฐ

LCS๋Š” ์—ฌ๋Ÿฌ๊ฐ€์ง€๊ฐ€ ๋  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ ์ค‘ ์•„๋ฌด๊ฑฐ๋‚˜ ํ•˜๋‚˜ ๊ตฌํ•˜๋ฉด ๋˜๋Š”๋ฐ ์—ฌ๊ธฐ์„œ ๋งŽ์ด ๋ฐฉํ™ฉํ–ˆ๋‹ค.. dp ๋ฐฐ์—ด๊ณผ ๊ฐ™์€ ํฌ๊ธฐ์˜ string ๋ฐฐ์—ด์„ ์„ ์–ธํ•˜๊ธฐ๋„ ํ•˜๊ณ  ์ด๋Ÿฐ์ €๋Ÿฐ ๋ฐฉ์‹์„ ์‹œ๋„ํ–ˆ๋Š”๋ฐ ์‹œ๊ฐ„์ดˆ๊ณผ ๋‚˜๊ณ  ์ง„์งœ ๋…ธํŠธ 3์žฅ ๋นฝ๋นฝํžˆ ์ฑ„์› ๋Š”๋ฐ๋„ ๋ชจ๋ฅด๊ฒ ์–ด์„œ ๋ธ”๋กœ๊ทธ ํ’€์ด ์ฐธ๊ณ ํ–ˆ๋‹ค.. (๋งํฌ)

๊ฒฐ๋ก ์€ ์•„๊นŒ ๊ตฌํ•œ ๋ฐฐ์—ด์„ ํ†ตํ•ด ์—ญ์œผ๋กœ ๋Œ์•„๊ฐˆ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. dp[i][j] ๋Š” str1[i] ์™€ str2[j] ๊ฐ€ ๊ฐ™์€ ๊ฒฝ์šฐ๋Š” dp[i - 1][j - 1] ์—์„œ ์™”๊ณ , ๋‹ค๋ฅธ ๊ฒฝ์šฐ์—๋Š” dp[i][j - 1] ๊ณผ dp[i - 1][j] ์ค‘์—์„œ ํฐ ๊ฐ’์„ ๊ณจ๋ผ์„œ ์™”๋‹ค. ์ด๋ฅผ ์—ญ์œผ๋กœ ํƒ์ƒ‰ํ•ด str1[i] ์™€ str2[j]๊ฐ€ ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” ์ถœ๋ ฅํ•˜๋ฉฐ ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ํƒ์ƒ‰์ด ๊ฐ€๋Šฅํ–ˆ๋‹ค.

image

#include <iostream>
#include <string>

using namespace std;

int arr[1001][1001] = {0, };
string str1, str2;

void print(int x, int y) {
    if (!arr[x][y]) return;

    if (str1[x - 1] == str2[y - 1]) {
        print(x - 1, y - 1);
        printf("%c", str1[x - 1]);
        return;
    }

    arr[x - 1][y] > arr[x][y - 1] ? print(x - 1, y) : print(x, y - 1);
}

int main() {
    cin >> str1 >> str2;

    int len1 = str1.size(), len2 = str2.size();

    for (int i = 0; i <= len1; i++) {
        for (int j = 0; j <= len2; j++) {
            if (!i || !j) arr[i][j] = 0;
            else if (str1[i - 1] == str2[j - 1]) arr[i][j] = arr[i - 1][j - 1] + 1;
            else arr[i][j] = max(arr[i][j - 1], arr[i - 1][j]);
        }
    }

    printf("%d\n", arr[len1][len2]);
    print(len1, len2);
}

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

์—ญ์‹œ ์—ฌ๋Ÿฌ ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ด์•ผ๊ฒ ๋„ค์š”.. ์–ด๋ ต๋‹ค ์–ด๋ ค์›Œ

Copy link
Collaborator

@InSange InSange left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋ฌธ์ž๊ฐ€ ๊ฐ™์„ ๋•Œ ๋”ฐ๋ผ๊ฐ€๋Š” ๋ฐฉ์‹์œผ๋กœ ๋ฌธ์ž์—ด์„ ์ €์žฅํ•ด์„œ ์ถœ๋ ฅํ•  ์ค„ ์•Œ์•˜๋Š”๋ฐ ์—ญ์ˆœ์œผ๋กœ ๊ฐ€๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๊ตฐ์š”. ์ด๋Ÿฐ ๋ฌธ์ œ๋Š” ๋งŽ์ด ํ’€์–ด๋ด์•ผ ์ ์‘ ๋  ๊ฒƒ ๊ฐ™๋„ค์š”. ์ž˜๋ณด๊ณ  ๊ฐ‘๋‹ˆ๋‹ค

Copy link
Collaborator

@seongwon030 seongwon030 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๊ธธ์ด๋งŒ ์ถœ๋ ฅํ•˜๋ฉด ๋˜๋Š” LCS ๋ณด๋‹ค ํ•œ ๋‹จ๊ณ„ ๋” ๋†’๋„ค์š”. ์—ญ์œผ๋กœ ํƒ์ƒ‰ํ•ด์„œ ๋ฌธ์ž์—ด์„ ๊ตฌํ•˜๋Š” ๋ฐฉ์‹์ด ์ธ์ƒ๊นŠ์—ˆ์Šต๋‹ˆ๋‹ค.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants