您的位置:首页 > 其它

POJ 2001 - Shortest Prefixes《字典树,求每个单词的最简化》

2016-06-13 07:30 447 查看
C - Shortest Prefixes
Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d
& %I64u
Submit Status Practice POJ
2001

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


这个题主要是对最简化的理解-.-开始一直不理解。。。

只要那个单词有一个是他自己特有的元素。那他就是最简的了-.-

car是因为它就三个=.=再多也没有了

题解:

先把单词添加入树,并求出每个树出现的次数,

再查找最简树。。。。

代码:

#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
int n,wei;
struct node{
char hh[22];
int ll;
int hao;
char qianzui[22];
}ch[1100];
struct trie{
struct trie *hai[26];
int op;
}di[26000];
struct trie *root;
bool cmp(node xx,node yy)
{
int ll=min(xx.ll,yy.ll);
for (int i=0;i<=xx.ll;i++)
{
if (xx.hh[i]!=yy.hh[i])
return xx.hh[i]<yy.hh[i];
}
}
bool cm(node xx,node yy)
{
return xx.hao<yy.hao;
}
void jia(int ii)
{

char xx[22];
strcpy(xx,ch[ii].hh);
struct trie *kk;
kk=root;
int lp=-1;
int ll=strlen(xx);
for (int i=0;i<ll;i++)
{
if (!kk->hai[xx[i]-'a'])
{
kk->hai[xx[i]-'a']=&di[wei++];
memset(kk->hai[xx[i]-'a'],0,sizeof(trie));
kk->hai[xx[i]-'a']->op=0;
}
kk=kk->hai[xx[i]-'a'];
kk->op++;
}
}
void zhao(int ii)
{

char xx[22];
strcpy(xx,ch[ii].hh);
struct trie *kk;
kk=root;
int lp=0;
int ll=strlen(xx);
for (int i=0;i<ll;i++)
{
if (!lp)
printf("%c",xx[i]);
kk=kk->hai[xx[i]-'a'];
if (kk->op==1)
lp=1;
}
printf("\n");
}
int main()
{
n=0;
while (gets(ch
.hh),ch
.hh[0])
{
ch
.ll=strlen(ch
.hh);
ch
.hao=n;
n++;
}
sort(ch,ch+n,cmp);
wei=0;
root=&di[wei++];
memset(root,0,sizeof(trie));
bool fafe=true;
for (int i=0;i<n;i++)
{
jia(i);
}
sort(ch,ch+n,cm);
for (int i=0;i<n;i++)
{
printf("%s ",ch[i].hh);
zhao(i);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: