POJ 2001 Shortest Prefixes(字典树)
来源:互联网 发布:prod.env.js 编辑:程序博客网 时间:2024/03/29 14:51
题目链接:http://poj.org/problem?id=2001
Shortest Prefixes
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 17746 Accepted: 7718
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
【中文题意】问你每个单词与其他单词都不相同的最小的前缀是什么。
【思路分析】创建一棵字典树,然后每个单词插入时对每一位都做记录,然后改一下查找函数的功能就OK了。
【AC代码】
#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>using namespace std;#define N 1000005#define trsize 26char a[1005][30];struct Tire{ int ch[N][trsize]; int isEnd[N]; int size; void init() { size=1; memset(ch[0],0,sizeof(ch[0])); memset(isEnd,0,sizeof(isEnd)); } int index(char c) { return c-'a'; } void inser(char s[]) { int i,rt,len=strlen(s); for(int i=rt=0; i<len; i++) { int c=index(s[i]); if(!ch[rt][c]) { memset(ch[size],0,sizeof(ch[size])); ch[rt][c]=size++; } rt=ch[rt][c]; isEnd[rt]++; } } void fin(char s[]) { int i,rt,len=strlen(s); for(int i=rt=0; i<len; i++) { if(isEnd[rt]==1)//这个字符只出现了一次,所以作为最后一位前缀 { break; } printf("%c",s[i]); int c=index(s[i]); rt=ch[rt][c]; } printf("\n"); }} trie;int main(){ trie.init(); int len=0; while(cin>>a[len])//插入单词 { trie.inser(a[len]); len++; } for(int i=0; i<len; i++)//查找最小不相同前缀 { printf("%s ",a[i]); trie.fin(a[i]); } return 0;}
- 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(字典树)
- POJ 2001 Shortest Prefixes(字典树)
- poj 2001 Shortest Prefixes(字典树)
- poj 2001Shortest Prefixes(字典树)
- poj 2001 Shortest Prefixes(字典树)
- POJ 2001 Shortest Prefixes ( 字典树 )
- POJ 2001 Shortest Prefixes 字典树Trie
- QT开发环境搭建
- PAT A 1001
- Angular-xeditable 入坑记
- MAVEN的安装及用maven命令生成一个普通的java项目
- 什么是maven?
- POJ 2001 Shortest Prefixes(字典树)
- 变量与常量高级
- 一天一条Linux指令-nohup
- Fiddler 教程
- Visual Studio 2010打开项目时提示“此项目针对的是.Net Framework,version=4.0”
- 软件测试(第2版)_Paul学习01——简介
- 一个程序在内存中的分析
- 电脑蓝屏0x0000007B
- FlyRefresh——让人眼前一亮的下拉刷新