Shortest Prefixes
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 |
[Submit] [Go Back] [Status] [Discuss]
输入一堆单词输出唯一前缀第一次自己的字典树wa比较多这么水的题不应该。蒟蒻算是种下自己的字典树了Orz。
ACcode:
#pragma warning(disable:4786)//使命名长度不受限制
#pragma comment(linker, "/STACK:102400000,102400000")//手工开栈
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <stack>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define rd(x) scanf("%d",&x)
#define rd2(x,y) scanf("%d%d",&x,&y)
#define rds(x) scanf("%s",x)
#define rdc(x) scanf("%c",&x)
#define ll long long int
#define maxn 100005
#define mod 1000000007
#define INF 0x3f3f3f3f //int 最大值
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)
#define MT(x,i) memset(x,i,sizeof(x))
#define PI acos(-1.0)
#define E exp(1)
#define MAX 26
using namespace std;
struct Trie{
Trie *next[MAX];
int v;
};
Trie *root;
char str[1010][1010];
void createTire(char *str){
int len=strlen(str);
Trie *p=root,*q;
FOR(i,0,len-1){
int id=str[i]-'a';
if(p->next[id]==NULL){
q=(Trie *)malloc(sizeof(Trie));
q->v=-9;
FOR(j,0,MAX-1)
q->next[j]=NULL;
p->next[id]=q;
p=p->next[id];
}
else{
p->next[id]->v=100;
p=p->next[id];
}
}
p->v=-1;
}
void findTrie(char *str){
printf("%s ",str);
int len=strlen(str);
Trie *p=root;
int i;
for( i=0;i<len;++i){
int id=str[i]-'a';
p=p->next[id];
if(p->v==NULL)
break;
if(p->v==-9)
break;
putchar(str[i]);
}
if(i!=len)
putchar(str[i]);
printf("\0");
printf("\n");
//cout<<len<<" "<<i<<'\12';
return ;
}
void deal(Trie *t){
if(!t)
return;
FOR(i,0,MAX-1)
if(t->next[i])
deal(t->next[i]);
free(t);
}
void init(){
root=(Trie *)malloc(sizeof(Trie));
FOR(i,0,MAX-1)
root->next[i]=NULL;
}
int main(){
int num=0;
init();
while(scanf("%s",&str[++num])!=EOF)
createTire(str[num]);
FOR(i,1,num)
findTrie(str[i]);
deal(root);
return 0;
}
/*
carbohydrate
cart
carburetor
caramel
caribou
carbonic
cartilage
carbon
carriage
carton
car
carbonate
caaaaaa
aaaaax
x
a
c
*/