zoukankan      html  css  js  c++  java
  • UVA 531

      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("
    ");
    				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;
    }


  • 相关阅读:
    HDU 4920 Matrix multiplication
    UVALive 5545 Glass Beads
    POJ 2230 Watchcow
    hdu 1878 欧拉回路
    hdu 3018 Ant Trip
    2015 Multi-University Training Contest 1 hdu 5296 Annoying problem
    深入理解php内核 编写扩展 I:介绍PHP和Zend
    PHP数组实际占用内存大小的分析
    深入理解php底层:php生命周期
    PHP实现的进度条效果详解
  • 原文地址:https://www.cnblogs.com/james1207/p/3295229.html
Copyright © 2011-2022 走看看