POJ 2250

  1 #include <iostream>
  2 #include <stack>
  3 #define MAXN 150
  4 #include <string>
  5
  6 using namespace std;
  7
  8
  9 int dp[MAXN][MAXN];
 10 int mark[MAXN][MAXN];
 11 string s_1[MAXN];
 12 string s_2[MAXN];
 13 stack <string> coll;
 14
 15 int main()
 16 {
 17     int i;
 18     int j;
 19     string tem;
 20     int n_1;
 21     int n_2;
 22     //freopen("acm.acm","r",stdin);
 23     while(cin>>tem)
 24     {
 25         s_1[0] = tem;
 26         i = 1;
 27         while(cin>>tem)
 28         {
 29             if(tem == "#")
 30             {
 31                 break;
 32             }
 33             s_1[i ++] = tem;
 34         }
 35         n_1 = i;
 36         i = 0;
 37         while(cin>>tem)
 38         {
 39             if(tem == "#")
 40             {
 41                 break;
 42             }
 43             s_2[i ++] = tem;
 44         }
 45         n_2 = i;
 46         dp[0][0] = 0;
 47         for(i = 0; i <= n_1; ++ i)
 48         {
 49             dp[i][1] = 0;
 50         }
 51         for(i = 0; i <= n_2; ++ i)
 52         {
 53             dp[1][i] = 0;
 54         }
 55         memset(mark,-1,sizeof(mark));
 56         for(i = 0; i < n_1; ++ i)
 57         {
 58             for(j = 0; j < n_2; ++ j)
 59             {
 60                 if(s_1[i] == s_2[j])
 61                 {
 62                     dp[i+1][j+1] = dp[i][j] + 1;
 63                     mark[i][j] = 1;    //相等来自i-1和j-1
 64                 }
 65                 else
 66                 {
 67                     if(dp[i+1][j] > dp[i][j+1])
 68                     {
 69                         dp[i+1][j+1] = dp[i+1][j];
 70                         mark[i][j] = 2;  // j-1方向
 71                     }
 72                     else
 73                     {
 74                         dp[i+1][j+1] = dp[i][j+1];
 75                         mark[i][j] = 3; // i-1方向
 76                     }
 77                 }
 78             }
 79         }
 80
 81         i = n_1-1;
 82         j = n_2-1;
 83
 84         while(mark[i][j] != -1 && j >= 0 && i >= 0)
 85         {
 86             if(mark[i][j] == 1)
 87             {
 88                 coll.push(s_1[i]);
 89                 -- i;
 90                 -- j;
 91             }
 92             else if(mark[i][j] == 2)
 93             {
 94                 -- j;
 95             }
 96             else if(mark[i][j] == 3)
 97             {
 98                 -- i;
 99             }
100         }
101
102
103         while(!coll.empty())
104         {
105             cout<<coll.top()<<" ";
106             coll.pop();
107         }
108         cout<<endl;
109     }
110
111 }
时间: 2024-10-11 07:50:54

POJ 2250的相关文章

POJ 2250 Compromise (DP,最长公共子序列)

Compromise Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6440 Accepted: 2882 Special Judge Description In a few months the European Currency Union will become a reality. However, to join the club, the Maastricht criteria must be fulfille

周赛 POJ 2250 Compromise

Description In a few months the European Currency Union will become a reality. However, to join the club, the Maastricht criteria must be fulfilled, and this is not a trivial task for the countries (maybe except for Luxembourg). To enforce that Germa

poj 2250 Compromise dp lcs 路径输出

点击打开链接题目链接 Compromise Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6520   Accepted: 2922   Special Judge Description In a few months the European Currency Union will become a reality. However, to join the club, the Maastricht criteria

LIS POJ 2250 Compromise

题目传送门 1 /* 2 LIS模板题:题目看错了,是求单词的最长上升子序列! 3 编程好累:) 4 */ 5 #include <cstdio> 6 #include <iostream> 7 #include <cstring> 8 #include <algorithm> 9 #include <string> 10 using namespace std; 11 12 const int MAXN = 1e2 + 10; 13 const

POJ 2250 Compromise (线性dp LCS +递归路径)

Compromise Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6735   Accepted: 3009   Special Judge Description In a few months the European Currency Union will become a reality. However, to join the club, the Maastricht criteria must be fu

POJ 2250 Compromise(最长公共子序列LCS)

http://poj.org/problem?id=2250 题意: 给你两段由空格分隔的语句, 要你求该两段语句的最长公共子序列. 且随便输出一个解即可. 注意每个单词需要看成我们一般处理字符串子序列的一个单独字符. 即每个单词是一个整体. 分析: 与往常计算最长公共子序列一样的方式即可. 然后用DFS输出序列即可.本题与POJ1458提供的解法本质一样. http://blog.csdn.net/u013480600/article/details/40741333 AC代码: #inclu

POJ 2250 Compromise(最长公共子序列)

题意:求两段文本的最长公共文本: 思路:最长公共子序列+打印公共序列: #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int dp[505][505],num1,num2; char s[505][505],s1[505][505],s2[505][505]; void lcs(int a,int b) { if(a==0||b==0) return; if(s[a

poj 2250 Compromise (LCS)

题目大意:给出两段文字,求出最长的公共单词串. 直接是以前的代码改一点就A了. #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; char s1[35][100],s2[35][100],s[35][100]; int len1,len2,dp[105][105],mark[105][105],l; void LCS() { int i,j; memset(d

POJ 2250 Compromise (UVA 531)

LCS问题,基础DP. 让我很忧伤的WA了很多次.只是一个LCS问题,需要记录一下路径. 自己的想办法记录path出错,最后只好用标记. 没有什么优化,二维数组,递归打印,cin.eof() 来识别 end of file 标识. 至于单词用map 映射的.其实也用不着,直接二维string或者 二维char 然后strcmp 也行. Special Judge 交 UVA 531 奇怪的PE了... 然后改成 flag 标记 输出 空格.终于都AC了. #include<cstdio> #i