STL语法——映射:map 反片语(Ananagrams,UVa 156)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了STL语法——映射:map 反片语(Ananagrams,UVa 156)相关的知识,希望对你有一定的参考价值。
Description
Most crossword puzzle fans are used to anagrams--groups of words with the same letters in different orders--for example OPTS, SPOT, STOP, POTS and POST. Some words however do not have this attribute, no matter how you rearrange their letters, you cannot form another word. Such words are called ananagrams, an example is QUIZ. Obviously such definitions depend on the domain within which we are working; you might think that ATHENE is an ananagram, whereas any chemist would quickly produce ETHANE. One possible domain would be the entire English language, but this could lead to some problems. One could restrict the domain to, say, Music, in which case SCALE becomes a relative ananagram (LACES is not in the same domain) but NOTE is not since it can produce TONE. Write a program that will read in the dictionary of a restricted domain and determine the relative ananagrams. Note that single letter words are, ipso facto, relative ananagrams since they cannot be ``rearranged‘‘ at all. The dictionary will contain no more than 1000 words.
Input
Input will consist of a series of lines. No line will be more than 80 characters long, but may contain any number of words. Words consist of up to 20 upper and/or lower case letters, and will not be broken across lines. Spaces may appear freely around words, and at least one space separates multiple words on the same line. Note that words that contain the same letters but of differing case are considered to be anagrams of each other, thus tIeD and EdiT are anagrams. The file will be terminated by a line consisting of a single #.
Output
Output will consist of a series of lines. Each line will consist of a single word that is a relative ananagram in the input dictionary. Words must be output in lexicographic (case-sensitive) order. There will always be at least one relative ananagram.
Sample input
ladder came tape soon leader acme RIDE lone Dreis peat ScAlE orb eye Rides dealer NotE derail LaCeS drIed noel dire Disk mace Rob dries #
Sample output
Disk
NotE
derail
drIed
eye
ladder
soon
中文大意:
输入一些单词,找出所有满足如下条件的单词:该单词不能通过字母重排,得到输入文本中的另外一个单词。
在判断是否满足条件时,字母不分大小写,但在输入时应保留输入中的大小写,按字典序进行排列(所有大写字母在小写字母的前面)
分析:
题意:
从若干个单词中找出只出现过一次的单词,前提是不区分大小写,并且字母的顺序也不一定,例如:abc
和cba 就是属于同一个单词(因为题目是说值出现一次的单词,所以代码中必须有一个计数的变量)
思路:
判断两个单词是否可以通过重排列得到,把两个单词排序,然后比较两个单词是否相同,若相同则可以通过重新排列得到,所以对每个输入的单词进行标准化,即把单词中的字母转化为小写字母
(判断单词重排时,不区分大小写)然后对该单词进行排序,然后用map来储存标准化后的单词
1.写一个标准化函数(实现大写字母转换为小写(tolower()函数),单词排序。注意使用const是为了不改变s的初值)
2.两个vector容器(words,ans),一个map容器(cnt)
words存储所有的单词
map存储标准化后对应单词以及出现次数的值,相当于一个表格。
words经过查表map,把对应的符合值给ans
3.输出
代码:
1 # include <iostream>//输入输出流 2 # include <string> 3 # include <cctype> //包含 tolower 函数 4 # include <vector> //不定长数组 5 # include <map> //映射,在本例中36行有体现 6 # include <algorithm> //一些函数,sort等 7 using namespace std; 8 9 map<string, int> cnt; //定义映射 10 vector<string> words; //一个叫words的不定长数组 11 12 //将单词s进行标准化 13 string repr(const string& s)//声明变量s 14 { 15 string ans = s;//定义字符串类型数组,并使用第三方变量进行转换,并最终输出时还能保留大写部分 16 for (int i = 0; i < ans.length(); i++) 17 ans[i] = tolower(ans[i]);//全部转化成小写字母 18 sort(ans.begin(), ans.end());//从小到大按字典序进行排序 19 return ans; 20 } 21 22 int main() 23 { 24 int n = 0; 25 string s; 26 while (cin >> s) 27 { 28 if (s[0] == ‘#‘) break;//遇到‘#’结束循环 29 words.push_back(s);//存入vector 30 string r = repr(s);//给所有单词进行排列,上面13行用“引用”的原因,方便调用 31 if (!cnt.count(r)) cnt[r] = 0; //!cnt.count(r) 的值,不是0就是1 32 cnt[r]++; //count 是返回容器中r出现的次数,统计重新组成新单词的个数 33 } 34 vector<string> ans;//又重新复制了一份,用另外一个存储 35 for (int i = 0; i < words.size(); i++) 36 if (cnt[repr(words[i])] == 1)//此时用到了“映射”,如果不是映射的话,一个是字符串一个是int型,不能用等号的;找到唯一的单词 37 ans.push_back(words[i]); 38 sort(ans.begin(), ans.end());//排序 39 for (int i = 0; i < ans.size(); i++) 40 cout << ans[i] << "\\n"; 41 42 getchar(); 43 getchar(); 44 45 return 0; 46 }
运行结果:
革命尚未成功,同志们仍需努力......
以上是关于STL语法——映射:map 反片语(Ananagrams,UVa 156)的主要内容,如果未能解决你的问题,请参考以下文章