poj 2001 Shortest Prefixes
2014-12-29 08:36
447 查看
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.
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.
相关文章推荐
- POJ 2001 Shortest Prefixes
- poj 2001 Shortest Prefixes
- POJ 2001 - Shortest Prefixes (Trie树)
- poj 2001 Shortest Prefixes ——字典树入门
- POJ2001 - Shortest Prefixes - 字典树
- POJ2001 Shortest Prefixes trie树模板
- poj 2001 Shortest Prefixes
- poj 2001 Shortest Prefixes 字典树的输出问题的解释~~ 2012-4-14
- poj 2001 Shortest Prefixes Trie
- [字典树] poj 2001 Shortest Prefixes
- poj 2001 Shortest Prefixes Trie树
- OpenJudge/Poj 2001 Shortest Prefixes
- POJ_2001_Shortest Prefixes
- POJ 2001 Shortest Prefixes
- poj 2001 Shortest Prefixes 【字典树】【找每一个字符串在字符串集里面的 最短且可唯一标识 的前缀】
- POJ【2001】Shortest Prefixes----字典树
- poj 2001 Shortest Prefixes 字典树(模版题)
- POJ 2001 Shortest Prefixes【字典树】
- POJ 2001 Shortest Prefixes【第一棵字典树】
- poj 2001 Shortest Prefixes Trie树