[poj 2001] Shortest Prefixes(tire树的简单应用) 标签: pojstring 2017-05-12 15:29 52人阅读

Shortest Prefixes
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 18363 Accepted: 7962
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

中文:
最短前缀
时间限制:1000MS内存限制:30000K
总提交:18363接受:7962
描述

字符串的前缀是从给定字符串开头开始的子字符串。 “碳”的前缀是:“c”,“ca”,“car”,“carb”,“carbo”和“carbon”。请注意,空字符串在此问题中不被视为前缀,但每个非空字符串都被认为是其本身的前缀。在日常语言中,我们倾向于用前缀缩写词。例如,“碳水化合物”通常被缩写为“碳水化合物”。在这个问题中,给定一组单词,你会发现每个单词是唯一标识它所代表的单词的最短的前缀。

在下面的示例输入中,“碳水化合物”可以缩写为“碳”,但不能简称为“碳”(或任何更短),因为列表中以“碳”开头的其他单词。

完全匹配将覆盖前缀匹配。例如,前缀“car”恰好匹配给定的单词“car”。因此,无歧义地理解,“汽车”是“汽车”的缩写,而不是用于“车”或以“汽车”开始的列表中的任何其他单词。
输入

输入至少包含两行,但不超过1000行。每行包含一个由1到20个小写字母组成的单词。
产量

输出包含与输入相同数量的行。输出的每行包含来自输​​入的相应行的单词,后跟一个空格,唯一(无模糊)的最短前缀标识该单词。

思路
此题利用tire树(字典树)
先将每个单词建入数中 利用val数组 对进入树的每个字母 val[u]++;
在对于每个单词在数中搜索 最后如果val[i]==1
代表只有这个自己单词走过
则可以输出前面的出现的单词 即为最短前缀

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace  std;
int  sz=1;
int ch[100000][27];  //ch这里一点要写int 不是char 不然就RE了
char s[100000][27]; 
int val[100000];
void insert(char *s){//建数函数
    int u=0;
    int len=strlen(s);
    for(int i=0;i<len;i++){
        int c=s[i]-'a';
        if(!ch[u][c])
        {
            ch[u][c]=sz;  
            val[sz++]=0;  
        }
        u=ch[u][c];  
        val[u]++;  //核心:只要这个字母存在一次就加一,记录下来 
    }
} 
void output(char *s)  //打印函数
{  
    int len=strlen(s),u=0;  
    for(int i=0;i<len;i++)  
    {  
        int c=s[i]-'a';  
        if(val[u]==1)   return;  
        //这个点只有自己这个单词访问过(为1)就结束打印
        printf("%c",s[i]);  
        u=ch[u][c];  //往下找 
    }  
}  
int main(){
    int t=0;
    sz=1;  
    while(scanf("%s",s[t])!=EOF)  
    {  
        insert(s[t]);  //建数
        t++;  
    }  
    for(int i=0;i<=t-1;i++)  
    {
        printf("%s ",s[i]);
        output(s[i]);//打印
        cout<<endl;
    }
    return 0;
} 
原文地址:https://www.cnblogs.com/xljxlj/p/7183669.html