๐Ÿ™‡โ€โ™€๏ธ[Gold IV] LCS 2 - 9252

๋ฌธ์ œ ๋งํฌ

์„ฑ๋Šฅ ์š”์•ฝ

๋ฉ”๋ชจ๋ฆฌ: 5984 KB, ์‹œ๊ฐ„: 4 ms

๋ถ„๋ฅ˜

๋‹ค์ด๋‚˜๋ฏน ํ”„๋กœ๊ทธ๋ž˜๋ฐ

์ œ์ถœ ์ผ์ž

2024๋…„ 2์›” 22์ผ 15:31:44

๋ฌธ์ œ ์„ค๋ช…

LCS(Longest Common Subsequence, ์ตœ์žฅ ๊ณตํ†ต ๋ถ€๋ถ„ ์ˆ˜์—ด)๋ฌธ์ œ๋Š” ๋‘ ์ˆ˜์—ด์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ๋ชจ๋‘์˜ ๋ถ€๋ถ„ ์ˆ˜์—ด์ด ๋˜๋Š” ์ˆ˜์—ด ์ค‘ ๊ฐ€์žฅ ๊ธด ๊ฒƒ์„ ์ฐพ๋Š” ๋ฌธ์ œ์ด๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, ACAYKP์™€ CAPCAK์˜ LCS๋Š” ACAK๊ฐ€ ๋œ๋‹ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„๊ณผ ๋‘˜์งธ ์ค„์— ๋‘ ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์ง„๋‹ค. ๋ฌธ์ž์—ด์€ ์•ŒํŒŒ๋ฒณ ๋Œ€๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ, ์ตœ๋Œ€ 1000๊ธ€์ž๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค.

์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„ ๋‘ ๋ฌธ์ž์—ด์˜ LCS์˜ ๊ธธ์ด๋ฅผ, ๋‘˜์งธ ์ค„์— LCS๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

LCS๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐ€์ง€์ธ ๊ฒฝ์šฐ์—๋Š” ์•„๋ฌด๊ฑฐ๋‚˜ ์ถœ๋ ฅํ•˜๊ณ , LCS์˜ ๊ธธ์ด๊ฐ€ 0์ธ ๊ฒฝ์šฐ์—๋Š” ๋‘˜์งธ ์ค„์„ ์ถœ๋ ฅํ•˜์ง€ ์•Š๋Š”๋‹ค.

๐Ÿš€ํ’€์ด

LCS

LCS๊ฐ€ ์ตœ๋Œ€ ๊ธธ์ด๋งŒ ์•Œ ์ˆ˜ ์žˆ๋Š”๋ฐ cache๊ฐ’์„ ๋ฐ”ํƒ•์œผ๋กœ ๊ฐ’์„ ์—ญ ์ถ”์ ํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•œ๋‹ค.

string str1, str2;
cin >> str1 >> str2;

for (int i = 1; i <= str1.length(); ++i)
{
    for (int j = 1; j <= str2.length(); ++j)
    {
        if (str1[i - 1] == str2[j - 1])
        {
            cache[i][j] = cache[i - 1][j - 1] + 1;
        }
        else
        {
            cache[i][j] = max(cache[i - 1][j], cache[i][j - 1]);
        }
    }
}

cout << cache[str1.length()][str2.length()] << '\n';

์ด๋ ‡๊ฒŒ cache๋ฅผ ์ฑ„์›Œ์ฃผ๊ณ  ์ตœ๋Œ€ ๊ธธ์ด๋ฅผ ๊ตฌํ•œ๋‹ค.

if (cache[str1.length()][str2.length()] == 0)
    return;
else
{
    int f = str1.length();
    int s = str2.length();
    // ๊ทธ ์ˆ˜์—ด์„ ์ฐพ์•„์•ผ๋จ
    string str;

    // ์ˆ˜์—ด์„ ์—ญ ์ถ”์ ํ•˜๊ธฐ
    int idx = str2.size();
    for (int i = str1.size(); i > 0; --i)
    {
        for (int j = idx; j > 0; --j)
        {
            if (cache[i][j] == cache[i - 1][j])
            {
                idx = j;
                break;
            }
            else if (cache[i][j] == cache[i][j - 1])
                continue;
            else
                str = str1[i - 1] + str;
        }
    }

    cout << str;
}

cache๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์—ญ ์ถ”์ ์„ ์‹œ์ž‘ํ•˜๋Š”๋ฐ

LCS

cache๋Š” ๋ฌธ์ œ์— ์˜ˆ์‹œ๋ฅผ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜๋ฉด ์ด๋ ‡๊ฒŒ๋œ๋‹ค.

์ด๋•Œ 4๋ถ€๋ถ„๋ถ€ํ„ฐ ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๋Š”๋ฐ cache[i][j] == cache[i - 1][j]๋ผ๋ฉด idx๋ฅผ j๋กœ ์น˜ํ™˜.
cache[i][j] == cache[i][j - 1] ๋ผ๋ฉด ๊ทธ๋ƒฅ ๋„˜๊ธฐ๊ณ 

์ตœ์ข…์ ์œผ๋กœ ๊ทธ๊ฒŒ ์•„๋‹ˆ๋ฉด str = str1[i - 1] + str;๋กœ ํ•˜์—ฌ ์ตœ์ข… ๊ฐ’์„ ์™„์„ฑํ•œ๋‹ค.

๐Ÿš€์ „์ฒด ์ฝ”๋“œ

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include<iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <math.h>

using namespace std;

// LCS (Longest Common Subsequence) 2

vector<vector<int>> cache(1001, vector<int>(1001));
void LCS()
{
	string str1, str2;
	cin >> str1 >> str2;

	for (int i = 1; i <= str1.length(); ++i)
	{
		for (int j = 1; j <= str2.length(); ++j)
		{
			if (str1[i - 1] == str2[j - 1])
			{
				cache[i][j] = cache[i - 1][j - 1] + 1;
			}
			else
			{
				cache[i][j] = max(cache[i - 1][j], cache[i][j - 1]);
			}
		}
	}

	cout << cache[str1.length()][str2.length()] << '\n';

	if (cache[str1.length()][str2.length()] == 0)
		return;
	else
	{
		int f = str1.length();
		int s = str2.length();
		// ๊ทธ ์ˆ˜์—ด์„ ์ฐพ์•„์•ผ๋จ
		string str;

		// ์ˆ˜์—ด์„ ์—ญ ์ถ”์ ํ•˜๊ธฐ
		int idx = str2.size();
		for (int i = str1.size(); i > 0; --i)
		{
			for (int j = idx; j > 0; --j)
			{
				if (cache[i][j] == cache[i - 1][j])
				{
					idx = j;
					break;
				}
				else if (cache[i][j] == cache[i][j - 1])
					continue;
				else
					str = str1[i - 1] + str;
			}
		}

		cout << str;
	}
}

void solve()
{
	LCS();
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	freopen("input.txt", "rt", stdin);

	solve();

	return 0;
}

ํƒœ๊ทธ:

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ: