zoukankan      html  css  js  c++  java
  • HNUSTOJ 1601:名字缩写

    1601: 名字缩写

    时间限制: 1 Sec  内存限制: 128 MB
    提交: 288  解决: 80
    [提交][状态][讨论版]

    题目描述

     Noname老师有一个班的学生名字要写,但是他太懒了,想少写几个字母。很快他发现这是可行的,例如下面的学生名单:

    Davidson
    Davis
    Dixon
    Smith
    可以缩写为
    David
    Davis
    Di
    S
    David 指明Davidson外,不可能是其他三位同学名字的前缀。S仅能代表Smith。在确保能无歧义指明同学的前提下,Noname老师总是希望使用最少的字母。

    输入

     给定一系列名字,每个名字一行(不超过100行),名字仅含英文字母,名字长度不超过40,这些名字按字母升序排列, 任意两个名字不相同而且一个名字不会正好是另一个名字的前缀。

    输出

     每行输入对应一行输出,内容为空格分开原来的名字和缩写后的名字。

    样例输入

    Adams
    Andersen
    Anderson
    Carson
    Carter
    Carville
    Cooper
    Coply
    Smith
    Smythe
    Sorensen
    Sorenson
    Wynn
    

    样例输出

    Adams Ad
    Andersen Anderse
    Anderson Anderso
    Carson Cars
    Carter Cart
    Carville Carv
    Cooper Coo
    Coply Cop
    Smith Smi
    Smythe Smy
    Sorensen Sorense
    Sorenson Sorenso
    Wynn W
    

    提示

    来源

    2014湖南科技大学校赛

    可以用字典树,但这题数据量较小,可以尝试STL里的map来记录前缀数量就可以;
    代码如下:
    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
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<string>
    #include<map>
     
    using namespace std;
    const int N = 100 + 5;
    char name[N][45];
    map<string,int> mp;
    void Add(char *ch){
        int len = strlen(ch);
        for(int i=1;i<= len;i++){
            char c = ch[i];
            ch[i] = '';
            mp[ch]++;
            ch[i] = c;
        }
    }
    void solve(int x){
        char ch[45];
        strcpy(ch,name[x]);
        int len = strlen(ch);
        for(int i=1;i<=len;i++){
            char c = ch[i];
            ch[i] = '';
            if(mp[ch]==1){printf("%s %s ",name[x],ch);return;}
            ch[i] = c;
        }
    }
    int main(){
        std::ios::sync_with_stdio(false);
        int cnt = 0;
        while(scanf("%s",name[cnt])==1){
            Add(name[cnt++]);
        }
        for(int i=0;i<cnt;i++){
            solve(i);
        }
    }

  • 相关阅读:
    ASP.NET Core多环境配置文件问题
    .NET Core中Object Pool的简单使用
    Refit在ASP.NET Core中的实践
    HttpClientFactory与Steeltoe结合来完成服务发现
    用HttpClientFactory来实现简单的熔断降级
    看看.NET Core几个Options的简单使用
    再探Circuit Breaker之使用Polly
    谈谈Circuit Breaker在.NET Core中的简单应用
    在.NET Core中使用简单的插件化机制
    谈谈ASP.NET Core中的ResponseCaching
  • 原文地址:https://www.cnblogs.com/Pretty9/p/7347694.html
Copyright © 2011-2022 走看看