zoukankan      html  css  js  c++  java
  • 最短前缀

    Shortest Prefixes

    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 out

    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 Code

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80


    #include<string>
    #include<iostream>
    大专栏  最短前缀>using namespace std;
    struct trie
    {
    int count;
    bool isWord;
    trie* next[26];
    trie()
    {
    count = 0;
    isWord = false;
    for (int i = 0; i<26; i++)
    {
    next[i] = NULL;
    }
    }
    }root;

    void (char* str)
    {

    trie*p = &root;
    while (*str)
    {
    int id = *str - 'a';
    if (p->next[id] == NULL)
    {
    p->next[id] = new trie();
    }
    p->next[id]->count++; //对每个遍历到的或新建的节点,重复次数加1
    p = p->next[id];
    str++;
    }
    p->isWord = true; //遍历结束后,标记以此为结尾的是一个单词
    }

    string search(char* str)
    {

    trie*p = &root;
    string s = "";
    while (*str)
    {
    int id = *str - 'a';

    s+=*str; //添加输出的字符
    if (p->next[id]->count>1) //如果count>1,则表明这是大家共同的节点,不能以此为结尾构造前缀==
    {
    p = p->next[id];
    str++;
    }
    else //如果count=1,则表明没有其他的单词以此为结尾了,可以作为前缀
    {
    return s;
    }
    }
    if (p->isWord)//如果直到遍历结束,还没找到前缀,则以此单词作为前缀
    {
    return s;
    }
    }

    char input[1005][25];
    int main()
    {

    int nCount = 0;
    while (gets(input[nCount])) //此处是学的别人的输入方法,在一边输入,一边记录字符串个数
    {
    insert(input[nCount]);
    nCount++;
    }

    for (int i = 0; i<nCount; i++)
    {
    string str = search(input[i]);
    cout << input[i] << " " << str << endl; //string输出,最好用cout,简单
    }
    return 0;
    }
  • 相关阅读:
    状态机的常见问题
    基于quartus的高级时序分析
    FPGA中的时钟域问题
    quartus中的时序约束常用方法
    时序约束与时序分析
    FPGA的PCB设计
    AXI4的主机协议代码分析
    selenium 笔记 场景判断
    Codeforces Round #676 (Div. 2) XORwice、Putting Bricks in the Wall、Palindromifier
    Trap HDU
  • 原文地址:https://www.cnblogs.com/lijianming180/p/12268263.html
Copyright © 2011-2022 走看看