例题5-4 反片语
输入一些单词(以“#”为结束标志),找出所有满足如下条件的单词:该单词不能通过字母的重排,得到输入文本中的另一个单词。在判断是否满足条件是不分大小写,但是在输出时应保留输入时的大小写,按字典序进行排列(所有大写字母在所有小写字母前面)。
Sample input
ladder came tape soon leader acme RIDE lone Dreis peat ScAlE orb eye Rides dealer NotE derail LaCeS drIednoel dire Disk mace Rob dries#
Sample output
DiskNotEderaildrIedeyeladdersoon 分析 这道题的解法很多,最简化的方式就是使用map容器。想到使用“标准化”。 整体思路: 1.写一个标准化函数(实现大写字母转换为小写(tolower()函数),单词排序。注意使用const是为了不改变s的初值) 2.两个vector容器(words,ans),一个map容器(cnt) words存储所有的单词 map存储标准化后对应单词以及出现次数的值,相当于一个表格。 words经过查表map,把对应的符合值给ans 3.输出 代码:
#include#include #include #include #include #include