UVA531- Compromise

题意:找出两个文本的最长公共子序列,输出序列

思路:最长公共子序列(LCSL),使用标记数组,递归输出最长公共子序列。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAXN = 105;

char s1[MAXN][MAXN], s2[MAXN][MAXN], s[MAXN];
int d[MAXN][MAXN], path[MAXN][MAXN];
int cnt1, cnt2, flag;

void init() {
    cnt1 = 0;
    strcpy(s1[cnt1++], s);
    while (scanf("%s", s1[cnt1])) {
        if (strcmp(s1[cnt1], "#") == 0)
            break;
        cnt1++;
    }
    cnt2 = 0;
    while (scanf("%s", s2[cnt2])) {
        if (strcmp(s2[cnt2], "#") == 0)
            break;
        cnt2++;
    }
    flag = 0;
}

void LCSL(int l1, int l2) {
    memset(d, 0,sizeof(d));
    for (int i = 1; i <= l1; i++) {
        for (int j = 1; j <= l2; j++) {
            if (strcmp(s1[i - 1], s2[j - 1]) == 0) {
                d[i][j] = d[i - 1][j - 1] + 1;
                path[i][j] = 1;
            }
            else {
                if (d[i - 1][j] > d[i][j - 1]) {
                    d[i][j] = d[i - 1][j];
                    path[i][j] = 0;
                }
                else {
                    d[i][j] = d[i][j - 1];
                    path[i][j] = -1;
                }
            }
        }
    }
}

void outPut(int i, int j) {
    if (!i || !j)
        return;
    if (path[i][j] == 1) {
        outPut(i - 1, j - 1);
        if (flag)
            printf(" ");
        else
            flag = 1;
        printf("%s", s1[i - 1]);
    }
    else if (path[i][j] == 0)
        outPut(i - 1, j);
    else
        outPut(i, j - 1);
}

int main() {
    while (scanf("%s", s) != EOF) {
        init();
        LCSL(cnt1, cnt2);
        outPut(cnt1, cnt2);
        printf("\n");
    }
    return 0;
}
时间: 2024-10-10 01:31:15

UVA531- Compromise的相关文章

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

TOJ 1139.Compromise

2015-06-03 问题简述: 大概就是输入两段文本(用小写英文字母表示),分别用#表示一段话的结束输入,输出这两个文本的最长公共子序列. 简单的LCS问题,但是输入的是一段话了,而且公共部分比较是字符串的比较. 原题链接:http://acm.tju.edu.cn/toj/showp.php?pid=1139 解题思路: 简单的最长公共子序列问题,只不过过程中比较的是两个字符串,故使用二维字符数组保存输入文本. 输入 x[1...m][], y[1...n][] ,c[i,j]代表两个文本的

POJ2250:Compromise(LCS) 解题心得

原题: 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 G

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

UVa 531 - Compromise

题目:给你两个文章,求里面最多的按顺序出现的单词. 分析:dp,LCS(最大公共子序列).直接求最大公共子序列,每个单词当做一个元素即可: 注意记录路径:如果匹配成功记录前驱,否则取前面取得的最大值的前驱(这里合成一个数字): 设状态f(i,j)为串S[0..i-1]与串T[0..j-1]的最大公共子序列. 有状态转移方程: f(i,j)= f(i-1,j-1)                                  {S[i-1]与T[j-1]相同} f(i,j)= max(f(i-

[dp] poj 1015 Jury Compromise

题目链接: http://poj.org/problem?id=1015 Jury Compromise Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 24438   Accepted: 6352   Special Judge Description In Frobnia, a far-away country, the verdicts in court trials are determined by a jury

周赛 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

uva 531&#183;Compromise(LCS---路径打印)

题目: 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 Germany will

K - Jury Compromise POJ 1015 (动态规划 --难)

K - Jury Compromise Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1015 Description In Frobnia, a far-away country, the verdicts in court trials are determined by a jury consisting of members of the ge

POJ 2250 Compromise

Compromise Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6654   Accepted: 2976   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