POJ2001 Shortest Prefixes【字典树】

Shortest Prefixes

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 21228   Accepted: 9103

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

问题链接POJ2001 Shortest Prefixes

问题描述

  给出若干单词,求各个单词的最短前缀。

问题分析

  构建字典树,进行相应的计算即可,与计算前缀数量的题相比,函数query()需要改写。

程序说明:(略)

参考链接:(略)

题记:(略)

AC的C++语言程序如下:

/* POJ2001 Shortest Prefixes */

#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;

#include <iostream>
#include <vector>
#include <stdio.h>
#include <string.h>

using namespace std;

const int N = 1000;
const int LEN = 20;
const int SIZE = 26;
const char SCHAR = 'a';

struct Node {
    int cnt;
    int child[SIZE];
} trie[N * LEN];
int ncnt;

void insert(string& s)
{
    int p = 0;
    for (int i = 0; s[i]; i++) {
        trie[p].cnt++;
        int k = s[i] - SCHAR;
        if(trie[p].child[k] == 0)
            trie[p].child[k] = ++ncnt;
        p = trie[p].child[k];
    }
    trie[p].cnt++;
}

int query(string& s)
{
    int p = 0;
    for (int i = 0; s[i]; i++) {
        p = trie[p].child[s[i] - SCHAR];
        if(trie[p].cnt <= 1) {
            return i;
        }
    }
    return -1;
}

int main()
{
    string s[N + 1];

    ncnt = 0;
    memset(trie, 0, sizeof(trie));

    int tot = 0;
    while (getline(cin, s[tot]) && s[tot][0])
        insert(s[tot++]);

    for (int i = 0; i < tot; i++) {
        int len = query(s[i]);
        cout << s[i] << " " << ((len < 0) ?  s[i] : s[i].substr(0, len + 1)) << endl;
    }

    return 0;
}

AC的C++语言程序如下:

/* POJ2001 Shortest Prefixes */

#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;

const int N = 1000;
const int LEN = 20;
const int SIZE = 26;
const char SCHAR = 'a';

char s[N + 1][LEN + 1];
int ncnt, trie[N * LEN][SIZE], acnt[N * LEN];

void insert(char s[])
{
    int p = 0;
    for(int i=0; s[i]; i++) {
        int k = s[i] - SCHAR;
        if(trie[p][k] == 0)
            trie[p][k] = ++ncnt;
        p = trie[p][k];
        acnt[p]++;
    }
}

void query(char s[])
{
    int p = 0;
    for(int i=0; s[i]; i++)
    {
        if(acnt[p] == 1)
            break;
        int k = s[i] - SCHAR;
        printf("%c", s[i]);
        p = trie[p][k];
   }
}

int main()
{
    int tot = 0;
    while(~scanf("%s", s[tot]))
        insert(s[tot++]);

    ncnt = 0;
    for(int i = 0; i < tot; i++) {
        printf("%s ", s[i]);
        query(s[i]);
        printf("\n");
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/tigerisland45/article/details/81427816