我是靠谱客的博主 娇气冷风,这篇文章主要介绍STL——反片语,现在分享给大家,希望可以做个参考。

题目:反片语
    输入一些单词,找到所有满足如下条件的单词,该单词不能通过字母重排,得到输入文本另外一些单词。在判断是否满足条件时,字母不分大小写,但在输出时应保留输入中的大小写,按字典序进行排序(所有大写字母在所有小写字母的前面)。

复制代码
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
41
42
43
44
45
46
47
48
/*题目:反片语 输入一些单词,找到所有满足如下条件的单词,该单词不能通过字母重排,得到输入文本的 另外一些单词。在判断是否满足条件时,字母不分大小写,但在输出时应保留输入中的大小写, 按字典序进行排序(所有大写字母在所有小写字母的前面)。 */ #include<iostream> #include<map> #include<string> #include<vector> #include<cctype> #include<algorithm> using namespace std; map<string,int> cnt; vector<string> words; //将单词s标准化 //注:const string& s引用s的地址作为参数,const使其成为不可改变的常量,说明了这个地址只读不允许更改 string repr(const string& s){ string ans=s; for(int i=0;i<ans.length();i++){ ans[i]=tolower(ans[i]);//转为小写 } sort(ans.begin(),ans.end());//按字典序升序,如:abcd return ans; } int main(){ int n=0; string s; while(cin>>s){ if(s[0]=='#') break;//#作为结束标志 words.push_back(s); string r=repr(s); if(!cnt.count(r))//标准单词r如果不存在 cnt[r]=0; cnt[r]++;//标准单词r个数加1 } vector<string> ans; for(int i=0;i<words.size();i++){ if(cnt[repr(words[i])]==1) ans.push_back(words[i]); } sort(ans.begin(),ans.end()); for(int i=0;i<ans.size();i++){ cout<<ans[i]<<endl; } return 0; }

      运行结果:

MLXG和mlxg重复,Clearlove和LoveClear重复;

输出先按首字母大写从前往后排序,在按首字母小写从前往后排序。

最后

以上就是娇气冷风最近收集整理的关于STL——反片语的全部内容,更多相关STL——反片语内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(66)

评论列表共有 0 条评论

立即
投稿
返回
顶部