【poj 2001】Shortest Prefixes
来源:互联网 发布:windows ad域管理工具 编辑:程序博客网 时间:2024/06/05 04:25
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
Source
Rocky Mountain 2004
题意看看样例就明白了。
查找并输出字符串在串集里面唯一确定的最短前缀
代码只可以过样例QWQ
在poj上不知道怎样查错,先开个坑吧算是。
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn=1000+2;char s[maxn][25];int word[maxn];int child[maxn][maxn];int fa[maxn];int size;bool cnt[maxn];void add(char *s){ int i; for(i=1;*s;s++) { int r=*s-'a'; if(!child[i][r]) { child[i][r]=++size; fa[size]=i; } i=child[i][r]; word[i]++; } cnt[i]=1;}void find_shortest(char *s){ int i; for(i=1;*s;s++) { int r=*s-'a'; i=child[i][r]; printf("%c",*s); if(word[i]==1) return; }}int main(){ int tot=0; while(~scanf("%s",s[++tot])) { add(s[tot]); } for(int i=1;i<=tot;i++) { cout<<s[i]<<" "; find_shortest(s[i]); cout<<'\n'; } 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 2001 Shortest Prefixes
- poj 2001 Shortest Prefixes
- POJ-2001 Shortest Prefixes
- POJ 2001 Shortest Prefixes
- thinkphp3.2 I('get.id') 在 ningx代理apache下面错误 GET POST参数都变成_URL_ 解决方法 I函数
- XML文档中的字符引用和实体引用
- HDU
- hdu 4431 模拟
- ES6常用知识点概述
- 【poj 2001】Shortest Prefixes
- 悲观锁&乐观锁
- 读书笔记:构建基于Spark的推荐引擎
- 使用insmod安装驱动文件时,提示platform_driver_unregister:relocation 28 out of range
- java.util.ConcurrentModificationException异常 分析
- 如何让subline支持GB2312和GBK编码
- ubuntu 安装pip3遇到的问题及解决方案
- CodeVS1373 射命丸文【矩阵前缀和】
- Java笔记之面向对象