More actions
No edit summary |
(Repair pages found by live-compare batch 0001) |
||
| Line 15: | Line 15: | ||
#include<cstring> | #include<cstring> | ||
using namespace std; | using namespace std; | ||
char f | char f[1010] = {0,}, s[1010] = {0,},ans[1010] = {0,}; | ||
int len | int len[1010][1010] = { 0, }; | ||
int main(){ | int main(){ | ||
| Line 25: | Line 25: | ||
for(int i = 1; i<=x; i++){ | for(int i = 1; i<=x; i++){ | ||
for(int j = 1; j<=y; j++){ | for(int j = 1; j<=y; j++){ | ||
if(f | if(f[i-1] == s[j-1]){ | ||
len | len[i][j] = len[i-1][j-1] + 1; | ||
} | } | ||
else{ | else{ | ||
len | len[i][j] = max(len[i-1][j], len[i][j-1]); | ||
} | } | ||
} | } | ||
} | } | ||
int num = len | int num = len[x][y]; | ||
cout<<num<<endl; | cout<<num<<endl; | ||
ans | ans[num] = '\0'; | ||
num--; | num--; | ||
int i = x, j = y; | int i = x, j = y; | ||
while (i >= 1 && j >= 1 && num >= 0) { | while (i >= 1 && j >= 1 && num >= 0) { | ||
if (f | if (f[i-1] == s[j - 1]) { | ||
//cout<< ">"; | //cout<< ">"; | ||
//cout<< i << " " << j << endl; | //cout<< i << " " << j << endl; | ||
ans | ans[num] = f[i - 1]; | ||
num--; | num--; | ||
i--; | i--; | ||
| Line 49: | Line 49: | ||
else { | else { | ||
//cout<< i << " " << j << endl; | //cout<< i << " " << j << endl; | ||
if (len | if (len[i - 1][j] >= len[i][j - 1]) { | ||
i--; | i--; | ||
} | } | ||
| Line 66: | Line 66: | ||
#include <vector> | #include <vector> | ||
int lcs | int lcs[1001][1001] = { 0, }; | ||
char len1 | char len1[1001] = { 0, }, len2[1001] = { 0, }; | ||
int main() | int main() | ||
| Line 80: | Line 80: | ||
for (int j = 1; j <= m; j++) | for (int j = 1; j <= m; j++) | ||
{ | { | ||
if (len1 | if (len1[i - 1] == len2[j - 1]) | ||
lcs | lcs[i][j] = lcs[i - 1][j - 1] + 1; | ||
else | else | ||
lcs | lcs[i][j] = std::max(lcs[i][j - 1], lcs[i - 1][j]); | ||
} | } | ||
} | } | ||
//lcs 길이 출력 | //lcs 길이 출력 | ||
std::cout << lcs | std::cout << lcs[n][m] << std::endl; | ||
std::vector<char> res; | std::vector<char> res; | ||
int x = n - 1, y = m - 1; | int x = n - 1, y = m - 1; | ||
| Line 95: | Line 95: | ||
while (x >= 0 && y >= 0) | while (x >= 0 && y >= 0) | ||
{ | { | ||
if (len1 | if (len1[x] == len2[y]) { | ||
res.push_back(len1 | res.push_back(len1[x]); | ||
x--, y--; | x--, y--; | ||
} | } | ||
else lcs | else lcs[x + 1][y] >= lcs[x][y + 1] ? y-- : x--; | ||
} | } | ||
//lcs 출력 | //lcs 출력 | ||
std::reverse(res.begin(), res.end()); | std::reverse(res.begin(), res.end()); | ||
for (int i = 0; i < res.size(); i++) std::cout << res | for (int i = 0; i < res.size(); i++) std::cout << res[i]; | ||
return 0; | return 0; | ||
| Line 118: | Line 118: | ||
== 곽정흠 == | == 곽정흠 == | ||
Latest revision as of 14:46, 26 March 2026
오늘의 문제
참가자
- 박인서
코드
15이원준
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<cstring>
using namespace std;
char f[1010] = {0,}, s[1010] = {0,},ans[1010] = {0,};
int len[1010][1010] = { 0, };
int main(){
cin>> f >> s;
int x, y;
x = strlen(f);
y = strlen(s);
for(int i = 1; i<=x; i++){
for(int j = 1; j<=y; j++){
if(f[i-1] == s[j-1]){
len[i][j] = len[i-1][j-1] + 1;
}
else{
len[i][j] = max(len[i-1][j], len[i][j-1]);
}
}
}
int num = len[x][y];
cout<<num<<endl;
ans[num] = '\0';
num--;
int i = x, j = y;
while (i >= 1 && j >= 1 && num >= 0) {
if (f[i-1] == s[j - 1]) {
//cout<< ">";
//cout<< i << " " << j << endl;
ans[num] = f[i - 1];
num--;
i--;
j--;
}
else {
//cout<< i << " " << j << endl;
if (len[i - 1][j] >= len[i][j - 1]) {
i--;
}
else {
j--;
}
}
}
cout << ans << endl;
}
박인서
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
int lcs[1001][1001] = { 0, };
char len1[1001] = { 0, }, len2[1001] = { 0, };
int main()
{
//입력
std::cin >> len1 >> len2;
//lcs 탐색
int n = strlen(len1), m = strlen(len2);
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
if (len1[i - 1] == len2[j - 1])
lcs[i][j] = lcs[i - 1][j - 1] + 1;
else
lcs[i][j] = std::max(lcs[i][j - 1], lcs[i - 1][j]);
}
}
//lcs 길이 출력
std::cout << lcs[n][m] << std::endl;
std::vector<char> res;
int x = n - 1, y = m - 1;
//경로 되추적
while (x >= 0 && y >= 0)
{
if (len1[x] == len2[y]) {
res.push_back(len1[x]);
x--, y--;
}
else lcs[x + 1][y] >= lcs[x][y + 1] ? y-- : x--;
}
//lcs 출력
std::reverse(res.begin(), res.end());
for (int i = 0; i < res.size(); i++) std::cout << res[i];
return 0;
}
곽정흠
아이디어
15이원준
박인서
- 설명으로 대신합니다.