• 名字缩写(map+字符串前缀)


    题目描述

     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


    AC_Code
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 const int maxn=105;
     4 char name[maxn][45];
     5 map<string,int> mp;
     6 
     7 void Add(char *ch){
     8     int len = strlen(ch);
     9     for(int i=1;i<= len;i++){
    10         char c = ch[i];
    11         ch[i] = '';
    12         mp[ch]++;
    13         ch[i] = c;
    14     }
    15 }
    16 
    17 void solve(int x){
    18 
    19     char ch[45];
    20     strcpy(ch,name[x]);
    21     int len = strlen(ch);
    22     for(int i=1;i<=len;i++){
    23         char c = ch[i];
    24         ch[i] = '';
    25         if(mp[ch]==1){printf("%s %s
    ",name[x],ch);return;}
    26         ch[i] = c;
    27     }
    28 }
    29 
    30 int main(){
    31 
    32     std::ios::sync_with_stdio(false);
    33     int cnt = 0;
    34     while(scanf("%s",name[cnt])==1){
    35         Add(name[cnt++]);
    36     }
    37 
    38     for(int i=0;i<cnt;i++){
    39         solve(i);
    40     }
    41     return 0;
    42 }
     
  • 相关阅读:
    区块链
    区块链
    区块链
    区块链
    区块链 – 介绍
    区块链 教程
    Matplotlib 直方图
    Matplotlib 饼图
    Matplotlib 柱状图
    Matplotlib 多个图形
  • 原文地址:https://www.cnblogs.com/wsy107316/p/12304542.html
Copyright © 2020-2023  润新知