poj 2250 Compromise(动归)

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

using namespace std;
vector<string> p1, p2;
vector<string> p[105][105];
char sample[35];
string word;
int numOfMark = 1;
void deal();
int main(){
	freopen("1.txt","r",stdin);
	//freopen("2.txt","w",stdout);
	p1.push_back("");
	p2.push_back("");
	while(scanf("%s",sample)!=EOF){
		word = sample;
		if(word == "#"){
			if(numOfMark &1){
				++numOfMark;
				continue;
			}
			else{
				deal();
				p1.clear();
				p2.clear();
				p1.push_back("");
				p2.push_back("");
				for(int q = 0;  q<105; ++q){
					for(int w = 0; w < 105; ++w){
						p[q][w].clear();
					}
				}
			numOfMark = 1;
				continue;
			}
		}
		if(numOfMark &1){  //奇数
			p1.push_back(word);
		}
		else{
			p2.push_back(word);
		}
	}
	return 0;
}
void deal(){
	int len1 = p1.size() , len2 = p2.size(), templen1, templen2;
	for(int i = 1; i < len1; ++i){
		for(int j = 1; j < len2; ++j){
			if(p1[i] == p2[j]){
				p[i][j] = p[i-1][j-1];
				p[i][j].push_back(p1[i]);
			}
			else{
				templen1 = p[i-1][j].size();
				templen2 = p[i][j-1].size();
				if(templen1 > templen2){
					p[i][j] = p[i-1][j];
				}
				else{
					p[i][j] = p[i][j-1];
				}
			}
		}
	}
	int same = p[len1-1][len2-1].size(),limit1= len1-1, limit2= len2-1;
	for(int i = 0; i < same; ++i){
		if(i ){
			printf(" ");
		}
		printf("%s",p[limit1][limit2][i].c_str());
	}
	printf("\n");
	printf("\n");
	return;
}

			





		


这是一道LCS的变形题,要求两个序列(单词)的最长公共子序列;

首先要知道怎么求LCS(Largest common subsequence)

设有两个序列s1,s2;长度分别为l1,l2;设p[i][j](0 <= i < l1, 0 <= j < l2)是s1的前i个字符与s2的前j个字符的公共子串长度

那么p[i][j] = p[i-1][j-1]+1   当s1[i] == s2[j];

                    max(p[i][j-1],p[i-1][j]} else

这道题就是把长度换为相同单词的集合

 

如有语言表述不准确的地方,欢迎指正~谢谢大家

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值