·天新网首页·加入收藏·设为首页·网站导航
数码笔记本手机摄像机相机MP3MP4GPS
硬件台式机网络服务器主板CPU硬盘显卡
办公投影打印传真
家电电视影院空调
游戏网游单机动漫
汽车新车购车试驾
下载驱动源码
学院开发设计
考试公务员高考考研
业界互联网通信探索
您现在的位置:天新网 > 软件开发
UVA 531 - Compromise(dp + LCS打印路径)
http://www.21tx.com 2014年01月20日 CSDN 帆帆帆帆帆帆帆帆帆帆

Compromise

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 fulfill the criteria, our government has so many wonderful options (raise taxes, sell stocks, revalue the gold reserves,...) that it is really hard to choose what to do.

Therefore the German government requires a program for the following task:

Two politicians each enter their proposal of what to do. The computer then outputs the longest common subsequence of words that occurs in both proposals. As you can see, this is a totally fair compromise (after all, a common sequence of words is something what both people have in mind).

Your country needs this program, so your job is to write it for us.

Input Specification

The input file will contain several test cases.

Each test case consists of two texts. Each text is given as a sequence of lower-case words, separated by whitespace, but with no punctuation. Words will be less than 30 characters long. Both texts will contain less than 100 words and will be terminated by a line containing a single '#'.

Input is terminated by end of file.

Output Specification

For each test case, print the longest common subsequence of words occuring in the two texts. If there is more than one such sequence, any one is acceptable. Separate the words by one blank. After the last word, output a newline character.

Sample Input

die einkommen der landwirte
sind fuer die abgeordneten ein buch mit sieben siegeln
um dem abzuhelfen
muessen dringend alle subventionsgesetze verbessert werden
#
die steuern auf vermoegen und einkommen
sollten nach meinung der abgeordneten
nachdruecklich erhoben werden
dazu muessen die kontrollbefugnisse der finanzbehoerden
dringend verbessert werden
#

Sample Output

die einkommen der abgeordneten muessen dringend verbessert werden

题意:给定两个文本,要求输出最多相同的单词序列。

思路:LCS问题。要打印路径。可以用一个VIS数组在过程中记录下状态转移方式。最后在根据这去遍历输出。

代码:

#include <stdio.h>
#include <string.h>
#include <ctype.h>
  
char a[105][35], b[105][35], c, cc, flag;
int dp[105][105], vis[105][105], aa, bb, aaa , bbb, i, j;
  
void print(int i, int j) {
    if (!i || !j)
        return;
    if (vis[i][j] == 1) {
        print(i - 1, j - 1);
        if (flag)
            printf(" ");
        else
            flag = 1;
        printf("%s", a[i - 1]);
    }
    else if(vis[i][j] == 0)
        print(i - 1, j);
    else
        print(i, j - 1);
}
int main() {
    flag = aa = bb = aaa = bbb = 0;
    memset(dp, 0, sizeof(dp));
    memset(vis, 0, sizeof(vis));
    memset(a, 0, sizeof(a));
    memset(b, 0, sizeof(b));
    while (~scanf("%s", a[aa])) {
        if (a[aa][0] == '#') {
            while (~scanf("%s", b[bb]) && b[bb][0] != '#')
                bb ++;
            for (i = 1; i <= aa; i ++)
                for (j = 1; j <= bb; j ++) {
                    if (strcmp(a[i - 1], b[j - 1]) == 0) {
                        dp[i][j] = dp[i - 1][j - 1] + 1;
                        vis[i][j] = 1;
                    }
                    else {
                        if (dp[i - 1][j] > dp[i][j - 1]) {
                            dp[i][j] = dp[i - 1][j];
                            vis[i][j] = 0;
                        }
                        else {
                            dp[i][j] = dp[i][j - 1];
                            vis[i][j] = -1;
                        }
                    }
                }
                print(aa, bb);
                printf("n");
                flag = aa = bb = aaa = bbb = 0;
                memset(dp, 0, sizeof(dp));
                memset(vis, 0, sizeof(vis));
                memset(a, 0, sizeof(a));
                memset(b, 0, sizeof(b));
                continue;
        }
        aa ++;
    }
    return 0;
}

上一篇: UVA 10029 - Edit Step Ladders(记忆化搜索)
下一篇: UVA 620 Cellular Structure (dp)

关于我们 | 联系我们 | 加入我们 | 广告服务 | 投诉意见 | 网站导航
Copyright © 2000-2011 21tx.com, All Rights Reserved.
晨新科技 版权所有 Created by TXSite.net