UVALive3046 ZOJ2346 Shortest Prefixes【字典树】

Shortest Prefixes


Time Limit: 2 Seconds      Memory Limit: 65536 KB


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

This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.

Input

Each input block 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 file contains the same number of lines as the input file. Each line of the output file contains the word from the corresponding line of the input file, followed by one blank space, and the shortest prefix that uniquely (without ambiguity) identifies this word.

Sample Input

1

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

 

Regionals 2004 >> North America - Rocky Mountain

 

问题链接UVALive3046 ZOJ2346 Shortest Prefixes

问题描述

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

问题分析

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

程序说明

  本题与参考链接的题是同一题,只是输入输出略有不同。

参考链接POJ2001 Shortest Prefixes【字典树】

题记:(略)

 

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

/* UVALive3046 ZOJ2346 Shortest Prefixes */

#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()
{
    int t;
    string s[N + 1];

    cin >> t;
    getline(cin, s[0]);
    getline(cin, s[0]);
    while (t--) {
        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;
        }

        if(t)
            cout << endl;
    }

    return 0;
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值