POJ2001 Shortest Prefixes

时间:2023-02-23 10:53:47

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

Trie树真是神奇呢

/**/
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int chnum=26;
struct NODE{
int ct;//该节点被使用的次数
short a[26];//子节点
}n[15000];
int cnt;//总节点数
char c[2000][25];
void ins(char s[]){
int num=0,len=strlen(s);
int i,j;
for(i=0;i<len;i++)
{
if(n[num].a[s[i]-'a']==0){
cnt++;//增加节点
n[num].a[s[i]-'a']=cnt;
num=cnt;
}
else num=n[num].a[s[i]-'a'];//用已有结点 n[num].ct++;//该节点使用次数+1
}
return;
}
void Print(char s[]){
int num=0,len=strlen(s);
printf("%s ",s);
for(int i=0;i<len;i++){
num=n[num].a[s[i]-'a'];
printf("%c",s[i]);
if(n[num].ct==1) {
//printf("\n");//由于有些词是被完全包含在其他词里的,故不能在这里换行
break;}
}
printf("\n");
return;
}
int main(){
int i=0,j;
while(cin>>c[i] && c[i]!='\0')ins(c[i++]);
for(j=0;j<i;j++)Print(c[j]);
return 0;
}

POJ2001 Shortest Prefixes的更多相关文章

  1. poj2001 Shortest Prefixes&lpar;字典树&rpar;

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21642   Accepted: 926 ...

  2. poj2001 Shortest Prefixes &lpar;trie树&rpar;

    Description A prefix of a string is a substring starting at the beginning of the given string. The p ...

  3. poj2001 Shortest Prefixes (trie)

    读入建立一棵字母树,并且每到一个节点就增加这个节点的覆盖数. 然后再重新扫一遍,一旦碰到某个覆盖数为1就是这个单词的最短前缀了. 不知为何下面的程序一直有bug……不知是读入的问题? type nod ...

  4. POJ2001 Shortest Prefixes (Trie树)

    直接用Trie树即可. 每个节点统计经过该点的单词数,遍历时当经过的单词数为1时即为合法的前缀. type arr=record next:array['a'..'z'] of longint; w: ...

  5. 【POJ2001】Shortest Prefixes

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 18724   Accepted: 810 ...

  6. poj 2001&colon;Shortest Prefixes(字典树,经典题,求最短唯一前缀)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12731   Accepted: 544 ...

  7. POJ 2001:Shortest Prefixes

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 16782   Accepted: 728 ...

  8. OpenJudge&sol;Poj 2001 Shortest Prefixes

    1.链接地址: http://bailian.openjudge.cn/practice/2001 http://poj.org/problem?id=2001 2.题目: Shortest Pref ...

  9. poj 2001 Shortest Prefixes trie入门

    Shortest Prefixes 题意:输入不超过1000个字符串,每个字符串为小写字母,长度不超过20:之后输出每个字符串可以简写的最短前缀串: Sample Input carbohydrate ...

随机推荐

  1. &lbrack;Tool&rsqb; 使用StyleCop验证命名规则

    [Tool] 使用StyleCop验证命名规则 前言 微软的MSDN上,有提供了一份微软的命名方针,指引开发人员去建立风格一致的程序代码. http://msdn.microsoft.com/zh-t ...

  2. 表单美化-原生javascript和jQuery下拉列表(兼容IE6)

    效果: 思想:用其他标签配合脚本和隐藏的input并通过传值模拟表单元素中的select <!DOCTYPE HTML> <html lang="en-US"&g ...

  3. UVa 12563 Jin Ge Jin Qu hao【01背包】

    题意:给出t秒时间,n首歌分别的时间a[i],还给出一首长度为678的必须唱的劲歌金曲,问最多能够唱多少首歌(只要最后时间还剩余一秒,都可以将劲歌金曲唱完) 用dp[i]代表花费i时间时唱的歌的最大数 ...

  4. 【九度OJ】题目1078-二叉树遍历

    题目 这道题和后面的两道题,题目1201和题目1009,主要内容是对递归的实现,在逻辑上,递归是容易理解的,这种实现方式和我们思考的方式是相吻合的.但是在转换为计算机语言时,要明确告知计算机应该从哪里 ...

  5. &num;Leet Code&num; Convert Sorted Array to Binary Search Tree

    描述:递归 代码: class Solution: # @param num, a list of integers # @return a tree node def sortedArrayToBS ...

  6. Repeater绑定数据库,使用AspNetPager进行分页

    分页是Web中经常遇到的功能,分页主要有真分页和假分页. 所谓真分页是指:每一页显示多少数据,就从数据库读多少数据: 假分页是指:一次性从数据库读取所有数据,然后再进行分页. 这两种分页方式区别在于从 ...

  7. 不同频率下的pwm配置

    200k //PWM1 PWMPERDL1=0xb3; PWMPERDH1= 0x00; PWMCCNTL1=0x6B; PWMCCNTH1= ; PWMDBDY1=0x2B; //死区延时计时器 / ...

  8. &lbrack;Spark性能调优&rsqb; 第二章:彻底解密Spark的HashShuffle

    本課主題 Shuffle 是分布式系统的天敌 Spark HashShuffle介绍 Spark Consolidated HashShuffle介绍 Shuffle 是如何成为 Spark 性能杀手 ...

  9. 独立成分分析&lpar;ICA&rpar;的模拟实验&lpar;R语言&rpar;

    本笔记是ESL14.7节图14.42的模拟过程.第一部分将以ProDenICA法为例试图介绍ICA的整个计算过程:第二部分将比较ProDenICA.FastICA以及KernelICA这种方法,试图重 ...

  10. webpack 简单配置

    webpack.config.js const path = require('path'); const HtmlWebpackPlugin=require('html-webpack-plugin ...