uva 156 Ananagrams 解题报告
来源:互联网 发布:服务器与数据库的区别 编辑:程序博客网 时间:2024/05/17 04:58
Ananagrams
Ananagrams
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 drIednoel dire Disk mace Rob dries#
sample output
DiskNotEderaildrIedeyeladdersoon
入门经典STL部分例题。题目本身算法较简单,主要为学习map。
涉及到对map的理解与用法,诸如关键字的对应关系,count()函数,[]运算符等。
同时加深了对sort的理解。
#include <iostream>#include <cstdio>#include <string>#include <vector>#include <algorithm>#include <map>using namespace std;string change(string c){ int len=(int)c.length(); for(int i=0;i<len;i++) c[i]=tolower(c[i]); //tolower,isalpha等函数在<iostream>与<cctype>中都有 sort(c.begin(),c.end()); //传入的地址由begin()和end()算出 //将构成字符串的字符按字典序(字母对应ascii码从小到大)排列 return c;}int main(){ string s; vector<string> all; vector<string> ans; map<string,int> m; while(cin>>s) { if(s[0]=='#') break; all.push_back(s); string c=change(s); //count()返回值只有1或0,对应传入的关键字是否已出现 if(!m.count(c)) m[c]=0; m[c]++; } int size=(int)all.size(); for(int i=0;i<size;i++) { if(m[change(all[i])]==1) ans.push_back(all[i]); } sort(ans.begin(),ans.end()); //将不定长数组中的多个字符串按照字典序排列 int num=(int)ans.size(); for(int i=0;i<num;i++) cout<< ans[i] << "\n"; return 0;}
- UVa 156 - Ananagrams解题报告
- uva 156 Ananagrams 解题报告
- 【Jason's_ACM_解题报告】Ananagrams
- UVA 156 解题报告
- 【ACM菜逼解题报告】Ananagrams
- UVa 156 - Ananagrams
- uva 156 Ananagrams
- UVA 156 - Ananagrams
- UVA 156 - Ananagrams
- UVa 156 - Ananagrams
- uva 156 - Ananagrams
- uva- 156-Ananagrams
- UVa 156 - Ananagrams
- UVA 156 - Ananagrams
- UVa 156 - Ananagrams
- UVa 156 - Ananagrams
- UVA 156 Ananagrams
- uva 156 - Ananagrams
- 北大暑期学习之并查集
- Xcode命令Archive导出4个ipa包的含义
- Header First HTML and CSS章后bullet points总结之Chapter 10
- 如何将磁盘从GPT格式转换成MBR格式——或MBR2GPT
- 怎样打印linux/mac环境变量
- uva 156 Ananagrams 解题报告
- 北大暑期学习之动态规划
- js 匀速/缓动动画 简单封装
- pl/sql developer调试定位到出错过程
- 原型模式——文档复制
- 北大暑期学习之数学问题
- 如何成为一名优秀的iOS开发工程师
- 北大暑期学习之搜索
- 北大暑期学习之线段树与树状数组