首页 > 专题系列 > Java解POJ > POJ 2629 Common permutation [解题报告] Java
2013
11-11

POJ 2629 Common permutation [解题报告] Java

Common permutation

问题描述 :

Given two strings of lowercase letters, a and b, print the longest string x of lowercase letters such that there is a permutation of x that is a subsequence of a and there is a permutation of x that is a subsequence of b.

输入:

Input consists of pairs of lines. The first line of a pair contains a and the second contains b. Each string is on a separate line and consists of at most 1,000 lowercase letters.

输出:

For each subsequent pair of input lines, output a line containing x. If several x satisfy the criteria above, choose the first one in alphabetical order.

样例输入:

pretty
women
walking
down
the
street

样例输出:

e
nw
et

解题代码:

//* @author  [email protected]
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
            String s=""; int[] l1,l2;
        while(stdin.ready()){
            l1=new int[26];l2=new int[26];
            s=stdin.readLine().trim();
            for(int i=0;i< s.length();i++){
                l1[(int)(s.charAt(i)-'a')]++;
            }
            s=stdin.readLine().trim();
            for(int i=0;i< s.length();i++){
                l2[(int)(s.charAt(i)-'a')]++;
            }
            for(int i=0;i< 26;i++){
                l1[i]=Math.min(l1[i], l2[i]);
            }
            for(int i=0;i< 26;i++){
                for(int j=0;j< l1[i];j++){
                    System.out.print((char)(i+'a'));
                }
            }
            System.out.print("\n");
        }
    }
}

  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