首页 > 专题系列 > Java解POJ > POJ 2001 Shortest Prefixes [解题报告] Java
2013
11-10

POJ 2001 Shortest Prefixes [解题报告] Java

Shortest Prefixes

问题描述 :

A prefix of a string is a substring starting at the beginning of the given string. The prefixes of “carbon” are: “c”, “ca”, “car”, “carb”, “carbo”, and “carbon”. Note that the empty string is not considered a prefix in this problem, but every non-empty string is considered to be a prefix of itself. In everyday language, we tend to abbreviate words by prefixes. For example, “carbohydrate” is commonly abbreviated by “carb”. In this problem, given a set of words, you will find for each word the shortest prefix that uniquely identifies the word it represents.

In the sample input below, “carbohydrate” can be abbreviated to “carboh”, but it cannot be abbreviated to “carbo” (or anything shorter) because there are other words in the list that begin with “carbo”.

An exact match will override a prefix match. For example, the prefix “car” matches the given word “car” exactly. Therefore, it is understood without ambiguity that “car” is an abbreviation for “car” , not for “carriage” or any of the other words in the list that begins with “car”.

输入:

The input contains at least two, but no more than 1000 lines. Each line contains one word consisting of 1 to 20 lower case letters.

输出:

The output contains the same number of lines as the input. Each line of the output contains the word from the corresponding line of the input, followed by one blank space, and the shortest prefix that uniquely (without ambiguity) identifies this word.

样例输入:

carbohydrate
cart
carburetor
caramel
caribou
carbonic
cartilage
carbon
carriage
carton
car
carbonate

样例输出:

carbohydrate carboh
cart cart
carburetor carbu
caramel cara
caribou cari
carbonic carboni
cartilage carti
carbon carbon
carriage carr
carton carto
car car
carbonate carbona

解题代码:

import java.util.*;

public class Main {
 static int MAX = 1010;
 static TrieNode root = new TrieNode();
 static char[][] result = new char[MAX][22];
 static char[][] s = new char[MAX][22];

 public static void main(String[] args) {
  Scanner cin = new Scanner(System.in);
  TrieNode my = new TrieNode();
  int n=0;
  char[] ss = new char[21];
  while(cin.hasNext()) {
    ss = cin.nextLine().toCharArray();
    s[n++] = ss;
    insert(ss);
  }

  int i;
  for(i=0;i< n;i++) {
   check(i);
   System.out.println(new String(s[i])+" "+ new String(result[i]).trim());
  }
 }

private static void insert(char[] str) {
 TrieNode p = root;
 int i,len = str.length;
 for(i=0;i< len;i++){
   if(p.num[str[i]-'a'] == null) {
   p.num[str[i]-'a'] = new TrieNode();
   p = p.num[str[i]-'a'];
  }else{
   p = p.num[str[i]-'a'];
  }
  p.value++;
 }
}

private static void check(int ind) {
 TrieNode p = root;
 int i;
 for(i=0;i< s[ind].length;i++) {
  if(p.value == 0) {
   return;
  }
  p = p.num[s[ind][i]-'a'];
  result[ind][i] = s[ind][i];
 }
}

}
class TrieNode {
 int value;
 TrieNode[] num = new TrieNode[26];
 public TrieNode() {
  value=-1;
  int i;
  for(i=0;i< 26;i++)
    num[i] = null;
  }
}

  1. 题目需要求解的是最小值,而且没有考虑可能存在环,比如
    0 0 0 0 0
    1 1 1 1 0
    1 0 0 0 0
    1 0 1 0 1
    1 0 0 0 0
    会陷入死循环

  2. 给你一组数据吧:29 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 1000。此时的数据量还是很小的,耗时却不短。这种方法确实可以,当然或许还有其他的优化方案,但是优化只能针对某些数据,不太可能在所有情况下都能在可接受的时间内求解出答案。