poj 2001 Shortest Prefixes 字典树
来源:互联网 发布:逆战自瞄源码 编辑:程序博客网 时间:2024/05/01 14:00
点击打开链接题目链接
Shortest Prefixes
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 13405 Accepted: 5786
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".
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
carbohydratecartcarburetorcaramelcariboucarboniccartilagecarboncarriagecartoncarcarbonate
Sample Output
carbohydrate carbohcart cartcarburetor carbucaramel caracaribou caricarbonic carbonicartilage carticarbon carboncarriage carrcarton cartocar carcarbonate carbona
输出能唯一代表字符串的前缀 如果没有 输出原字符串
#include<stdio.h>#include<string.h>#include<stdlib.h>#include<iostream>using namespace std;struct node{ int next[26]; int count;}trie[100010];char str[10010][25];int head;void tree_add(char *str){ int s=0; int i,l,id; l=strlen(str); for(i=0;i<l;i++) { id=str[i]-'a'; if(trie[s].next[id]==-1) { trie[s].next[id]=++head; s=head; } else { s=trie[s].next[id]; trie[s].count++; } } // trie[s].count++;}void tree_search(char *str){ int s=0; int i,l,id; l=strlen(str); for(i=0;i<l;i++) { id=str[i]-'a'; if(trie[s].count>1) printf("%c",str[i]); s=trie[s].next[id]; }}int main(){ head=0; int i,j; for(i=0;i<100010;i++) { trie[i].count=1; for(j=0;j<26;j++) { trie[i].next[j]=-1; } } trie[0].count=10000; int n=0; int flag=0; while(gets(str[n])!=NULL) { if(strcmp(str[n],"")==0) break; tree_add(str[n++]); } for(i=0;i<n;i++) { printf("%s ",str[i]); tree_search(str[i]); printf("\n"); }}
0 0
- POJ 2001:Shortest Prefixes(字典树)
- poj 2001 Shortest Prefixes(字典树)
- poj 2001 -- Shortest Prefixes ( 字典树 )
- POJ 2001 Shortest Prefixes ( 字典树 )
- POJ 2001 Shortest Prefixes 字典树Trie
- POJ 2001 Shortest Prefixes(字典树Trie)
- 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 字典树
- poj 2001 Shortest Prefixes(字典树)
- poj 2001 Shortest Prefixes(字典树)
- POJ - 2001 - Shortest Prefixes (字典树!!)
- js中获取后台的集合数据
- Elasticsearch模块功能之-映射(Mapper)
- tomcat中http与https协议socket工厂
- HDU 1074 Doing Homework
- hdu 4585 STL map 的运用
- poj 2001 Shortest Prefixes 字典树
- hdoj 2091 空心三角形
- 参悟PID算法之理解算法与基本实现
- 盧克淸 (賤買貴賣)
- POJ 3281 Dining(网络最大流)
- Linux 程序设计学习笔记----进程管理与程序开发(上)
- poj 1195 Mobile phones (二维树状数组)
- System eth2 找不到 , 配置centos 静态IP 暂未解决问题
- poj 1204 Word Puzzles 字典树 + 暴搜