• hoj 10338 / poj 2001 Shortest Prefixes (字典树)


    http://poj.org/problem?id=2001

    http://acm.hnu.cn/online/?action=problem&type=show&id=10338

    Shortest Prefixes 
    Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB
    Total submit users: 50, Accepted users: 48
    Problem 10338 : No special judgement
    Problem 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
    Problem Source
    Rocky Mountain 2004

    分析:

    利用字典树,将每个层的字母出现次数记下,若找到只出现一次的字母,则返回该位置,并输出到此处的字符串,若没有找到则返回本身

    代码实现:

     1 #include<iostream>
     2 using namespace std;
     3 char str[1010][21];
     4 //定义结构体
     5 struct Nod
     6 {
     7     Nod *next[26];
     8     int num;
     9     Nod()          //调用构造函数进行初始化(C++才有用)
    10     {
    11         memset(next,NULL,sizeof(next));
    12         num=0;
    13     }
    14 };
    15 //插入建立字典树
    16 void insert(Nod *head,char *tstr)    
    17 {
    18     Nod *h=head;
    19     int len=strlen(tstr),i;
    20     for(i=0;i<len;i++)
    21     {
    22         int id=tstr[i]-'a';
    23         if(h->next[id]==NULL)
    24             h->next[id]=new Nod;  //C++的动态申请,必须用new 若用C语言的malloc 将不会调用构造函数初始化 
    25         h=h->next[id];
    26         h->num++;     //计数器累加
    27     }
    28 }
    29 int find_tree(Nod *head,char *tstr)
    30 {
    31     Nod *h=head;
    32     int len=strlen(tstr),i,flag=0;
    33     for(i=0;i<len;i++)
    34     {
    35         int id=tstr[i]-'a';
    36         h=h->next[id];
    37         if(h->num==1)
    38             return i;   //若发现此处字母只出现一次则返回位置,说明其唯一性
    39     }
    40     return len-1;   //若没有找到唯一的位置则返回最后的位置,这是为了统一输出的方便性
    41 }
    42 //删除字典树,释放空间,在这个题目中,只有一组测试数据,所以,不进行空间的释放也不会超内存,但是为了养成良好的编程习惯,这里在程序结束时进行了释放
    43 void freedom(Nod *head)
    44 {
    45     Nod *h=head;
    46     int i;
    47     for(i=0;i<26;i++)
    48         if(h->next[i])
    49             freedom(h->next[i]);
    50     delete h;
    51 }
    52 int main()
    53 {
    54     int k=0;
    55     Nod *head=new Nod;
    56     while(gets(str[k]))
    57         insert(head,str[k++]);  
    58     int i,id;
    59     char str1[21];
    60     for(i=0;i<k;i++)
    61     {
    62         strcpy(str1,str[i]);
    63         id=find_tree(head,str[i]);
    64         str1[id+1]='\0';  //利用返回id截断字符串方便输出
    65         printf("%s %s\n",str[i],str1);
    66     }
    67     freedom(head);
    68     return 0;
    69 }
  • 相关阅读:
    3.2 playbook tags
    3.1 playbook语法实践
    3. playbook基础组件
    elasticsearch IK中文分词
    elasticsearch参数详解
    2. ansible常用模块
    1. ansible简介
    Python sphinx-build在Windows系统中生成Html文档
    Oracle PL/SQL Developer集成TFS进行团队脚本文件版本管理
    Gulp自动构建Web前端程序
  • 原文地址:https://www.cnblogs.com/crazyapple/p/2660628.html
Copyright © 2020-2023  润新知