程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> POJ_2001_Shortest Prefixes(字典樹)

POJ_2001_Shortest Prefixes(字典樹)

編輯:關於C++
Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 14535   Accepted: 6277

Description

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".

Input

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.

Output

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.

Sample Input

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

Sample Output

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

Source

Rocky Mountain 2004

題意:求給出的每個單詞的“標志前綴”,即此前綴只能在此單詞中有,而其他單詞不擁有,所以叫做標志前綴。
分析:很明顯的運用字典樹求解。
題目鏈接:http://poj.org/problem?id=2001
代碼清單:
#include
#include
#include
#include
const int MAX = 27 ;
const int MAXS = 21 ;
const int MAXN = 1005 ;

struct trie{
    int point;
    trie *next[MAX];
};

trie *root=new trie;
char str[MAXN][MAXS];
int number=0;
void createTrie(char *s){
    trie *p=root,*q;
    int len=strlen(s),pos;
    for(int i=0;inext[pos]==NULL){
            q=new trie;
            q->point=1;
            for(int j=0;jnext[j]=NULL;
            p->next[pos]=q;
            p=p->next[pos];
        }
        else{
            p->next[pos]->point++;
            p=p->next[pos];
        }
    }
}

void findTrie(char *s){
    trie *p=root;
    int len=strlen(s),pos;
    for(int i=0;inext[pos]->point==1)
            break;
        p=p->next[pos];
    }
    printf("\n");
}

void delTrie(trie *Root){
    for(int i=0;inext[i]!=NULL)
            delTrie(Root->next[i]);
    }
    free(Root);
}

int main(){
    //freopen("liuchu.txt","r",stdin);
    for(int i=0;inext[i]=NULL;
    while(scanf("%s",str[number])!=EOF){
        createTrie(str[number]);
        number++;
    }
    for(int i=0;i


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved