您的位置:首页 > 其它

poj 2001 Shortest Prefixes

2014-12-29 08:40 288 查看
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.
 

题目大意:给出若干个单词,求出每个单词与其他单词不冲突的最短前缀表示。(当前缀为单词本身的时候就不存在冲突的问题了)

//===========================================================================

打这题是为了trie树练手的,不过貌似也可以排序下搞掉,以前就尝试过了不知道为什么WA了。。太弱了。。。

先建立trie数,递归处理每个trie树上的节点以下有几个单词记录为c[i]。

然后在for一遍所有单词,对于每个单词沿着trie树走一遍。第一次走到c[i]=1的点就是最短前缀表示了。(如果走到头了就是单词本身、、)
 
AC CODE
 
program pku_2001;

var trie:array[0..20000,1..26] of longint;

   
fa:array[0..20000] of longint;

   
a,b:array[1..1000] of longint;

   
c:array[0..20000] of longint;

   
s:array[1..10000] of string;

   
i,j,tot,n,len,tmp,now:longint;

   
ans:string;

//============================================================================

procedure qsort(l,r:longint);

var i,j,k,tt:longint;

begin

  k:=a[(l+r) shr 1]; i:=l; j:=r;

  repeat

    while
a[i]>k do inc(i);

    while
a[j]<k do dec(j);

    if
i<=j then

    begin

     
tt:=a[i]; a[i]:=a[j]; a[j]:=tt;

     
tt:=b[i]; b[i]:=b[j]; b[j]:=tt;

     
inc(i); dec(j);

    end;

  until i>j;

  if l<j then qsort(l,j);

  if i<r then qsort(i,r);

end;

//============================================================================

begin n:=0;

  while not(eof) do

  begin inc(n); a
:=0;

   
readln(s
); now:=0; len:=length(s
);

    for i:=1 to
len do

     
if s
[i] in ['a'..'z'] then

     
begin inc(a
);

       
tmp:=ord(s
[i])-ord('a')+1;

       
if trie[now,tmp]=0 then

       
begin

         
inc(tot); fa[tot]:=now;

         
trie[now,tmp]:=tot;

         
now:=tot;

       
end else now:=trie[now,tmp];

     
end; b
:=now;

  end;
qsort(1,n);     
//按节点深度排序,找出递归的起点。

  for i:=0 to tot do c[i]:=0;

  for i:=1 to n do

  begin j:=b[i];

    repeat

     
inc(c[j]);

     
j:=fa[j];

    until
j=0;

  end;

  for i:=1 to n do

  begin

    now:=0;
ans:=''; len:=length(s[i]);

    for j:=1 to
len do

     
if s[i][j] in ['a'..'z'] then

     
begin

       
tmp:=ord(s[i][j])-ord('a')+1;

       
now:=trie[now,tmp];

       
ans:=ans+s[i][j];

       
if c[now]=1 then break;

     
end; writeln(s[i],' ',ans);

  end;

end.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: