POJ---2001 Shortest Prefixes[字典树---判断唯一前缀]
2012-09-08 18:02
471 查看
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
Sample Output
Source
Rocky Mountain 2004
字典树判断字符的唯一性
code:
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 9754 | Accepted: 4130 |
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
字典树判断字符的唯一性
code:
#include<iostream> #include<cstring> using namespace std; char str[10100][22]; struct node { int cnt; node *next[26]; }*p; void build(char str[],int k,node *head) { while(k<strlen(str)) { if(head->next[str[k]-'a']!=NULL) { head->next[str[k]-'a']->cnt+=1; head=head->next[str[k]-'a']; } else { head->next[str[k]-'a']=new node; head=head->next[str[k]-'a']; head->cnt=1; for(int i=0;i<26;i++) head->next[i]=NULL; } k++; } } void search(char str[],int k,node *head) { while(k<strlen(str)) { if(head->next[str[k]-'a']!=NULL) { printf("%c",str[k]); if(head->next[str[k]-'a']->cnt==1) return; } head=head->next[str[k]-'a']; k++; } } void del(node *head) { if(head==NULL) return; for(int i=0;i<26;i++) { del(head->next[i]); head->next[i]=NULL; } delete(head); return; } int main() { p=new node; for(int i=0;i<26;i++) p->next[i]=NULL; int cnt=0; while(~scanf("%s",str[cnt])) { build(str[cnt],0,p); cnt++; } for(int i=0;i<cnt;i++) { printf("%s ",str[i]); search(str[i],0,p); printf("\n"); } del(p); return 0; }
相关文章推荐
- poj 2001 Shortest Prefixes 【字典树】【找每一个字符串在字符串集里面的 最短且可唯一标识 的前缀】
- poj 2001:Shortest Prefixes(字典树,经典题,求最短唯一前缀)
- POJ 2001 Shortest Prefixes 字典树经典题,求最短唯一前缀)
- POJ - 2001 Shortest Prefixes(字典树重复前缀查询)
- poj--3630--Phone List(字典树+前缀判断)
- poj 3630 Phone List 【字典树】【判断一个字符串集里面 是否存在一个字符串是另一个字符串前缀】
- poj--3630--Phone List(字典树+前缀判断)
- 字典树---2001 POJ Shortest Prefixes(找最短前缀)
- POJ2001 -- 字典树和字符串的前缀
- 字典树---2001 POJ Shortest Prefixes(找最短前缀)
- poj 2001/1056 Trie树(求单词表的最短前缀/判断立即码)
- POJ 1056 IMMEDIATE DECODABILITY(字典树,判断有没有一个是另一个的前缀)
- poj2001Shortest Prefixes【字典树找最短唯一前缀】
- poj 2001 Shortest Prefixes(字典树)
- Shortest Prefixes POJ - 2001 (字典树模板题~)
- [字典树] poj 2001 Shortest Prefixes
- POJ 2001 Shortest Prefixes(字典树)
- poj 2001 Shortest Prefixes 字典树
- POJ 2001 Shortest Prefixes (字典树,简单)
- POJ 2001 trie字典树