백준 9251 c++

magicdrill·2024년 3월 21일
0

백준 문제풀이

목록 보기
189/655

백준 9251 c++

#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

string input_string(int low, int high)
{
	string temp;

	while (1)
	{
		cin >> temp;
		if (temp.length() >= low && temp.length() <= high)
		{
			break;
		}
		else
		{
			;
		}
	}

	return temp;
}

int find_result(string s1, string s2)
{
	int i, j;
	int m = s1.length(), n = s2.length();
	int result = 0;
	int**DP = new int*[m + 1];
	for (i = 0; i < m +1; i++)
	{
		DP[i] = new int[n + 1];
	}
	for (i = 0; i < m + 1; i++)
	{
		for (j = 0; j < n + 1; j++)
		{
			DP[i][j] = 0;
		}
	}

	for (i = 1; i < m+1; i++)
	{
		for (j = 1; j < n+1; j++)
		{
			if (s1[i-1] == s2[j-1])//같은 문자이면
			{
				DP[i][j] = DP[i - 1][j - 1] + 1;
			}
			else //다른 문자이면
			{
				DP[i][j] = max(DP[i][j-1], DP[i-1][j]);
			}
		}
	}
	/*for (i = 0; i < n + 1; i++)
	{
		for (j = 0; j < m + 1; j++)
		{
			cout << DP[i][j] << " ";
		}
		cout << "\n";
	}*/
	result = DP[m][n];

	for (i = 0; i < m +1; i++)
	{
		delete[] DP[i];
	}
	delete[] DP;

	return result;
}

int main(void)
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	string s1;
	string s2;

	cin >> s1 >> s2;
	//s1 = input_string(1, 1000);
	//s2 = input_string(1, 1000);
	cout << find_result(s1, s2) << "\n";

	return 0;
}

0개의 댓글