poj2250 - Compromise
来源:互联网 发布:网络诈骗1500判刑多久 编辑:程序博客网 时间:2024/04/28 12:02
想看更多的解题报告:http://blog.csdn.net/wangjian8006/article/details/7870410
转载请注明出处:http://blog.csdn.net/wangjian8006
题目大意:给出两篇文章,文章里面由一个一个单词构成,每个单词由空格隔开,文章以#作为结束,输出两篇文章的最长的相似句子,句子的答案可能有多种情况,每个输出之后有个换行符。
解题思路:题目类型和最长公共子序列是一样的,只不过这里将最长子序列的字符改成了一个单词。但是输出的时候需要注意,就是要将每次的决策给记录下来,方便输出的时候用,每次输出的时候是当他们相等的时候决策就输出。
#include <iostream>#include <string.h>#include <memory.h>using namespace std;#define MAXV 110#define MAXN 35char stemp[MAXN];char s1[MAXV][MAXN];char s2[MAXV][MAXN];int len1,len2,dp[MAXV][MAXV],p[MAXV][MAXV],pos[MAXV],sum;int max(int i,int j,int a,int b,int c){if(a>b && a>c){p[i][j]=1;//将其决策记录下来return a;}if(b>a && b>c){p[i][j]=2;return b;}p[i][j]=3;return c;}int same(int x,int y){if(!strcmp(s1[x],s2[y])) return 1;return 0;}void print(int i,int j){if(p[i][j]==1){pos[sum++]=i-1;print(i-1,j-1);}else if(p[i][j]==2)print(i-1,j);else if(p[i][j]==3)print(i,j-1);}int main(){int i,j,t;//Input//while(~scanf("%s",stemp)){len1=len2=0;strcpy(s1[len1++],stemp);while(1){scanf("%s",stemp);if(!strcmp(stemp,"#")){break;}strcpy(s1[len1++],stemp);}while(1){scanf("%s",stemp);if(!strcmp(stemp,"#")){break;}strcpy(s2[len2++],stemp);}// DP //memset(dp,0,sizeof(dp));memset(p,0,sizeof(p));for(i=1;i<=len1;i++)for(j=1;j<=len2;j++){dp[i][j]=max(i,j,dp[i-1][j-1]+same(i-1,j-1),dp[i-1][j],dp[i][j-1]);}//Output//sum=0;print(len1,len2);for(i=sum-1;i>=0;i--) printf("%s ",s1[pos[i]]);printf("\n");}return 0;}
- poj2250 - Compromise
- POJ2250:Compromise(LCS)
- POJ2250 Compromise,map、dp
- POJ2250:Compromise(LCS)
- POJ2250——Compromise
- POJ2250 Compromise(dp)
- POJ2250 Compromise (LCS)
- POJ2250 Compromise (LCS)
- POJ2250(compromise) 解题报告
- poj2250 Compromise dp lcs 记录路径
- POJ2250 Compromise DP最长公共子序列
- 【POJ2250】Compromise (最长公共子序列,DP)
- POJ2250 & UVA 531 Compromise(字符串、 最长公共子序列)
- POJ2250---Compromise(dp,最长公共子序列,输出路径)
- poj2250
- POJ2250
- poj2250
- poj2250 Compromise--最长公共子序列+打印序列+单词非字符
- java线程系列---线程范围内共享变量(一)
- Bash ------ Keyword Variable Memo
- 寒冰老师的力作——vsFTP服务器·磁盘配额管理
- Android之SurfaceView学习
- 有空应该多看看书
- poj2250 - Compromise
- VC CFile Open save
- Bash ------ Special Characters Memo
- 关于windows7访问windows2003共享的问题
- Ubuntu下安装bochs出现错误的解决办法
- Linux的标准文件系统----EXT2
- MapXtreme 2005 相关代码知识(三)
- 基于C#Winform的简易聊天程序两端通信(0)
- Spring transaction事务之roll back回滚