题目描述:输入一些单词,找出所有满足如下条件的单词:该单词不能通过字母重排,得到输入文本中的另一个单词。在判断是否满足条件时,字母不区分大小写,但在输出时应该保留输入中的大小写,按字典序进行排列。
思路:判断两个单词是否可以通过重排列得到,把两个单词排序,然后比较两个单词是否相同,若相同则可以通过重排列得到。所以对每个输入的单词进行标准化,即把单词中的字母转换为小写字母(判断单词重排时,不区分大小写),然后对该单词进行排序。然后用map存储标准化后的单词,key值为重排后的单词,value为文本输入重排变换相同的单词个数。则把所有value为1的单词按照字典序排序,然后输出得到结果。
代码如下所示(vs2012运行通过):
复制代码
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
49
50
51
52
53
54
55
56
57// 156.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <iostream> #include <fstream> #include <vector> #include <map> #include <string> #include <algorithm> using namespace std; string standard(string a) { string str = a; for(int i=0;i<str.size();i++) { str[i] = tolower(str[i]); } sort(str.begin(),str.end()); return str; } int _tmain(int argc, _TCHAR* argv[]) { //ifstream in("data.txt"); map<string,int> res; vector<string> word; string str; while(cin>>str) { if(str[0]=='#') break; word.push_back(str); if(!res.count(standard(str))) { res[standard(str)]=0; } res[standard(str)]++; } vector<string> word_res; for(int i=0;i<word.size();i++) { if(res[standard(word[i])]==1) { word_res.push_back(word[i]); } } sort(word_res.begin(),word_res.end()); for(int i=0;i<word_res.size();i++) { cout<<word_res[i]<<endl; } return 0; }
最后
以上就是辛勤蜜蜂最近收集整理的关于反片语的全部内容,更多相关反片语内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复