poj 2250 Compromise(动归)
来源:互联网 发布:arp设计软件 编辑:程序博客网 时间:2024/05/16 14:36
#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
这道题就是把长度换为相同单词的集合
如有语言表述不准确的地方,欢迎指正~谢谢大家
- poj 2250 Compromise(动归)
- poj(1260,动归)
- poj 2250 Compromise (LCS)
- poj 2250 Compromise(LCS)
- poj2479,poj 2593 动归
- POJ 1018 Communication System(搜索/贪心/动归)
- poj 1157 little shop of flowers(动归)
- 【精】POJ 3267 The Cow Lexicon(区间动归)
- POJ 2750 Potted Flower (线段树+动归)
- POJ -1037-A decorative fence(动归 +计数)
- poj 2250 Compromise(DP)
- POJ 2250 Compromise (UVA 531)
- poj 2250 Compromise(裸LCS DP)
- poj-2250Compromise(LCS+标记数组)
- poj 2250 Compromise
- poj 2250 Compromise
- Poj 2250 Compromise
- poj 2250 Compromise
- Matlab三维枝干图
- 一些IT类的图书推荐
- Android 类Path橡皮筋的效果
- jni学习系列之一
- Delphi读取INI
- poj 2250 Compromise(动归)
- 内存中的堆栈
- 英文Ubuntu12.04配置中文输入法
- MyEclipse x.x各版本终极优化配置指南
- 基于Axis的WebService简单实例
- SAP系统常用变量
- HooK SSDT
- php 使用include,require是提示 T_INCLUDE等错误
- 如何修改VS2005创建MFC工程的默认模板