poj2250

//============================================================================
// 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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值