首页 > 专题系列 > Java解POJ > POJ 2250 Compromise [解题报告] Java
2013
11-11

POJ 2250 Compromise [解题报告] Java

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.

输入:

The input 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.

输出:

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.

样例输入:

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
#

样例输出:

die einkommen der abgeordneten muessen dringend verbessert werden

解题代码:

import java.io.BufferedInputStream;   
import java.util.Scanner;   
 
public class Main{   
  
    private static final int MAXLEN = 101;   
    private static int[][] c = new int[MAXLEN][MAXLEN];   
    private static int[][] b = new int[MAXLEN][MAXLEN];   
  
    public static void main(String[] args) {   
        Scanner scan = new Scanner(new BufferedInputStream(System.in)); 

  
        while (scan.hasNext()) {   
            String s1 = "", s2 = "";   
            while (scan.hasNext()) {   
                String s = scan.nextLine();   
                if (s.startsWith("#")) {   
                    break;   
                }   
                s.trim();   
                s1 = s1 + s + " ";   
            }   
            while (scan.hasNext()) {   
                String s = scan.nextLine();   
                if (s.startsWith("#")) {   
                    break;   
                }   
                s.trim();   
                s2 = s2 + s + " ";   
            }   
            String[] ss1 = s1.trim().split(" ");   
            String[] ss2 = s2.trim().split(" ");   
            lcsLength(ss1, ss2);   
            printLCS(ss1, ss1.length, ss2.length);   
        }   
    }   
  
    public static void lcsLength(String[] x, String[] y) {   
        for (int i = 0; i <= x.length; i++) {   
            c[i][0] = 0;   
        }   
        for (int i = 0; i <= y.length; i++) {   
            c[0][i] = 0;   
        }   
        for (int i = 1; i <= x.length; i++) {   
            for (int j = 1; j <= y.length; j++) {   
                if (x[i - 1].equals(y[j - 1])) {   
                    c[i][j] = c[i - 1][j - 1] + 1;   
                    b[i][j] = 0;   
                } else if (c[i - 1][j] >= c[i][j - 1]) {   
                    c[i][j] = c[i - 1][j];   
                    b[i][j] = 1;   
                } else {   
                    c[i][j] = c[i][j - 1];   
                    b[i][j] = -1;   
                }   
            }   
        }   
    }   
  
    public static void printLCS(String[] x, int m, int n) {   
        int i = m;   
        int j = n;   
        if (i == 0 || j == 0) {   
            return;   
        }   
        if (b[i][j] == 0) {   
            printLCS(x, i - 1, j - 1);   
            System.out.print(x[i - 1] + " ");   
        } else if (b[i][j] == 1) {   
            printLCS(x, i - 1, j);   
        } else {   
            printLCS(x, i, j - 1);   
        }   
    }   
}

  1. Excellent Web-site! I required to ask if I might webpages and use a component of the net web website and use a number of factors for just about any faculty process. Please notify me through email regardless of whether that would be excellent. Many thanks

  2. 问题3是不是应该为1/4 .因为截取的三段,无论是否能组成三角形, x, y-x ,1-y,都应大于0,所以 x<y,基础应该是一个大三角形。小三角是大三角的 1/4.