//============================================================================
// Name : 2250.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <vector>
using namespace std;
typedef struct node {
int count;
struct node* pre;
string str;
} Node;
string strend("#");
int first = 1;
void output(Node* node) {
if (node->pre)
output(node->pre);
if (node->str != strend) {
if (first) {
cout << node->str;
first = 0;
} else {
cout << " " << node->str;
}
}
}
int main() {
ios::sync_with_stdio(false);
int state = 0;
string str;
while (cin >> str) {
state = 0;
vector<string> a, b;
a.push_back(str);
while (true) {
cin >> str;
if (str == strend) {
state++;
if (state == 2)
break;
} else if (state == 0) {
a.push_back(str);
} else if (state == 1) {
b.push_back(str);
}
}
int m = a.size() + 1;
int n = b.size() + 1;
Node res[m][n];
for (int i = 0; i < m; i++) {
res[i][0].count = 0;
res[i][0].pre = NULL;
res[i][0].str = strend;
}
for (int i = 0; i < n; i++) {
res[0][i].count = 0;
res[0][i].pre = NULL;
res[0][i].str = strend;
}
for (int i = 1; i < m; i++)
for (int j = 1; j < n; j++) {
if (a[i - 1] == b[j - 1]) {
res[i][j].count = res[i - 1][j - 1].count + 1;
res[i][j].str = a[i - 1];
res[i][j].pre = &res[i - 1][j - 1];
} else {
if (res[i - 1][j].count < res[i][j - 1].count) {
res[i][j].count = res[i][j - 1].count;
res[i][j].str = strend;
res[i][j].pre = &res[i][j - 1];
} else {
res[i][j].count = res[i - 1][j].count;
res[i][j].str = strend;
res[i][j].pre = &res[i - 1][j];
}
}
}
first = 1;
output(&res[m - 1][n - 1]);
cout << endl<< endl;
}
return 0;
}
poj2250
最新推荐文章于 2017-02-26 09:41:39 发布