博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
算法入门经典-第五章 例题5-4 反片语
阅读量:4662 次
发布时间:2019-06-09

本文共 989 字,大约阅读时间需要 3 分钟。

例题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
using namespace std;map
cnt;vector
words;//将单词s标准化 string repr(const string& s)//这个设计很巧妙 ,不改变原值 {string ans=s;for(int i=0;i
>s){if(s[0]=='#')break;words.push_back(s);string r=repr(s);//cout<
<
ans;for(int i=0;i

  

转载于:https://www.cnblogs.com/is-Tina/p/7350507.html

你可能感兴趣的文章