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>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<stack>
#include<iostream>
#include<list>
#include<set>
#include<bitset>
#include<vector>
#include<cmath>

#define INF 0x7fffffff
#define eps 1e-8
#define LL long long
#define PI 3.141592654
#define CLR(a,b) memset(a,b,sizeof(a))
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define FOR_(i,a,b) for(int i=a;i>=b;i--)
#define pb push_back
#define mp make_pair
#define ft first
#define sd second
#define sf scanf
#define pf printf
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(),(v).end()
#define acfun std::ios::sync_with_stdio(false)

#define SIZE 1000 +1
using namespace std;

int a[SIZE],b[SIZE];
int dp[SIZE][SIZE];
int path[SIZE][SIZE];
map<string,int>word;
map<int,string>exword;
bool flag;
void print(int i,int j)
{
    if(i>0&&j>0)
    {
        if(path[i][j]==0)
        {
            print(i-1,j-1);
            if(flag)
                cout<<" ";
            else
                flag=1;
            cout<<exword[a[i-1]];
        }
        else if(path[i][j]==1)
            print(i-1,j);
        else if(path[i][j]==-1)
            print(i,j-1);
    }
}
int main()
{
    acfun;
    while(1)
    {
        string tmp;
        word.clear();
        int cot=1;
        int la=0,lb=0;
        flag=0;
        while(1)
        {
            cin>>tmp;
            if(cin.eof())return 0;
            if(tmp=="#")break;
            if(word[tmp]==0)
            {
                word[tmp]=cot++;
                exword[cot-1]=tmp;
            }
            a[la++]=word[tmp];
        }
        while(1)
        {
            cin>>tmp;
            if(tmp=="#")break;
            if(word[tmp]==0)
            {
                word[tmp]=cot++;
                exword[cot-1]=tmp;
            }
            b[lb++]=word[tmp];
        }
        FOR(i,0,la)
        FOR(j,0,lb)
        {
            if(a[i]==b[j])
            {
                dp[i+1][j+1]=dp[i][j]+1;
                path[i+1][j+1]=0;
            }
            else
            {
                if(dp[i+1][j]>=dp[i][j+1])
                {
                    dp[i+1][j+1]=dp[i+1][j];
                    path[i+1][j+1]=-1;
                }
                else
                {
                    dp[i+1][j+1]=dp[i][j+1];
                    path[i+1][j+1]=1;
                }
            }
        }
        //cout<<dp[la][lb]<<endl;
        print(la,lb);
        cout<<endl;
    }
}
时间: 2024-08-07 09:31:44

POJ 2250 Compromise (UVA 531)的相关文章

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 1384 Piggy-Bank(完全背包)

http://poj.org/problem?id=1384 Piggy-Bank Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7900 Accepted: 3813 Description Before ACM can do anything, a budget must be prepared and the necessary financial support obtained. The main income f

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 - 1006 Biorhythms (中国剩余定理)

Description Some people believe that there are three cycles in a person's life that start the day he or she is born. These three cycles are the physical, emotional, and intellectual cycles, and they have periods of lengths 23, 28, and 33 days, respec

Poj 1182种类(带权)并查集

题目链接 食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44316 Accepted: 12934 Description 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种. 有人用两种说法对这N个动物所构成的食物链关系进行描述: 第一种说法是"1 X Y",表示X和Y是

poj 2431 Expedition (贪心+优先队列)

Expedition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6890   Accepted: 2065 Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being rather poor drivers, the cows unfortunately managed to

poj The Clocks(暴搜)

mnesia在频繁操作数据的过程可能会报错:** WARNING ** Mnesia is overloaded: {dump_log, write_threshold},可以看出,mnesia应该是过载了.这个警告在mnesia dump操作会发生这个问题,表类型为disc_only_copies .disc_copies都可能会发生. 如何重现这个问题,例子的场景是多个进程同时在不断地mnesia:dirty_write/2 mnesia过载分析 1.抛出警告是在mnesia 增加dump

POJ 3367 Expressions(数据结构-二叉树)

Expressions Description Arithmetic expressions are usually written with the operators in between the two operands (which is called infix notation). For example, (x+y)*(z-w) is an arithmetic expression in infix notation. However, it is easier to write