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] =  '\0' ;
         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] =  '\0' ;
         if (mp[ch]==1){ printf ( "%s %s\n" ,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);
     }
}

转载于:https://www.cnblogs.com/Pretty9/p/7347694.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值