zoukankan      html  css  js  c++  java
  • POJ2001 Shortest Prefixes

    描述

    传送门:我是传送门

    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”. 

    输入

    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.

    输出

    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.

    样例

    输入

    carbohydrate
    cart
    carburetor
    caramel
    caribou
    carbonic
    cartilage
    carbon
    carriage
    carton
    car
    carbonate

    输出

    carbohydrate carboh
    cart cart
    carburetor carbu
    caramel cara
    caribou cari
    carbonic carboni
    cartilage carti
    carbon carbon
    carriage carr
    carton carto
    car car
    carbonate carbona

    思路

    题目大意

    是在输出原字符串的同时找出原字符串的唯一最短前缀

    (:)(唯一:这个前缀只在这个单词出现过)
    如果没找到的话则输出原字符串

    类似 HDU1251 统计难题 略微升级版

    使用 num[N]num[N] 数组保存当前前缀出现次数

    num[i]num[i] 代表以当前字符串为前缀的单词数量

    若 num[i]>1num[i]>1 则说明当前前缀不止出现过一次,也就是说还需要往后找才可以找到唯一的前缀

    代码

     1 /*
     2  * =========================================================================
     3  *
     4  *       Filename:  poj2001.cpp
     5  *
     6  *           Link:  
     7  *
     8  *        Version:  1.0
     9  *        Created:  2018/08/29 14时09分22秒
    10  *       Revision:  none
    11  *       Compiler:  g++
    12  *
    13  *         Author:  杜宁元 (https://duny31030.top/), duny31030@126.com
    14  *   Organization:  QLU_浪在ACM
    15  *
    16  * =========================================================================
    17  */
    18 #include <stdio.h>
    19 #include <iostream>
    20 #include <string.h>
    21 #include <algorithm>
    22 using namespace std;
    23 #define clr(a, x) memset(a, x, sizeof(a))
    24 #define rep(i,a,n) for(int i=a;i<=n;i++)
    25 #define pre(i,a,n) for(int i=a;i>=n;i--)
    26 #define ll long long
    27 #define max3(a,b,c) fmax(a,fmax(b,c))
    28 #define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    29 const double eps = 1e-6;
    30 const int INF = 0x3f3f3f3f;
    31 const int mod = 1e9 + 7;
    32 const int N = 5e5+10;
    33 struct node
    34 {
    35     int next[27];
    36 }trie[N];
    37 int num[N];
    38 char s[N][30];
    39 int tot = 0;
    40 int size = 0;
    41 void add(char a[])
    42 {
    43     int i,now = 0;
    44     int len = strlen(a);
    45     for(i = 0;i < len;i++)
    46     {
    47         int tmp = a[i]-'a'+1;
    48         int next = trie[now].next[tmp];
    49         if(next == 0)
    50             trie[now].next[tmp] = ++tot;
    51         now = trie[now].next[tmp];
    52         num[now]++;
    53     }
    54 }
    55 void query(char a[])
    56 {
    57     int len = strlen(a);
    58     int now = 0,i;
    59     for(i = 0;i < len;i++)
    60     {
    61         if(num[now] == 1)
    62             break;
    63         int tmp = a[i]-'a'+1;
    64         printf("%c",a[i]);
    65         now = trie[now].next[tmp];
    66     }
    67 }
    68 
    69 int main()
    70 {
    71     ios
    72 #ifdef ONLINE_JUDGE 
    73 #else 
    74         freopen("in.txt","r",stdin);
    75     // freopen("out.txt","w",stdout); 
    76 #endif
    77     while(scanf("%s",s[size++]) != EOF)
    78         add(s[size-1]);
    79     rep(i,0,size-1)
    80     {
    81         printf("%s ",s[i]);
    82         query(s[i]);
    83         printf("
    ");
    84     }
    85     fclose(stdin);
    86     // fclose(stdout);
    87     return 0;
    88 }
  • 相关阅读:
    phpquery中文手册
    Fiddler工具使用介绍一
    Xshell出现要继续使用此程序必须应用到最新的更新或使用新版本
    PHP Ajax 跨域问题最佳解决方案
    charles和Fiddler感觉哪个更好用
    趣味小应用:用HTML5消磨时光 狼人:
    开发人员必知的20+HTML5技巧 狼人:
    云计算时代:LAMP何去何从? 狼人:
    Adobe改善Flash隐私功能 狼人:
    LAMP网站架构方案分析 狼人:
  • 原文地址:https://www.cnblogs.com/duny31030/p/14304869.html
Copyright © 2011-2022 走看看