UVA156 Ananagrams
来源:互联网 发布:淘宝购物车js代码 编辑:程序博客网 时间:2024/06/04 19:54
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 drIednoel dire Disk mace Rob dries#
Sample Output
DiskNotEderaildrIedeyeladdersoon
#include <iostream>#include <cstdio>#include <algorithm>#include <set>#include <string>#include <sstream>#include <vector>#include <map>using namespace std;string change(const string &s){ string ans=s; for(int i=0;i<ans.length();i++) ans[i]=tolower(ans[i]); sort(ans.begin(),ans.end()); return ans;}int main(){ map<string,int> cnt; vector<string> words; string s; while(cin>>s) { if(s[0]=='#') break; words.push_back(s); string t=change(s); if(!cnt.count(t)) cnt[t]=0; cnt[t]++; //若找不到则映射0,之后加1,最后输出cnt为一的就是没有重复的单词 } set<string> ans; //最后把所有没有重复的单词放在一起,用set可以自动按字典顺序排序 for(int i=0;i<words.size();i++) { if(cnt[change(words[i])]==1) ans.insert(words[i]); } for(set<string>::iterator it=ans.begin();it!=ans.end();it++) { cout<<*it<<endl; } return 0;}
- UVa156 Ananagrams
- UVA156 Ananagrams
- Ananagrams uva156
- UVA156 - Ananagrams
- UVA156-Ananagrams
- UVA156-Ananagrams
- UVA156 Ananagrams
- UVA156 Ananagrams
- Ananagrams Uva156
- Ananagrams UVa156
- uva156 Ananagrams
- UVa156 Ananagrams,map
- Uva156——Ananagrams
- 反片语 Ananagrams,UVa156
- UVA156 Ananagrams(字符串处理)
- uva156 Ananagrams (stl map的使用)
- UVA156-5.4-Ananagrams-映射(map)
- map函数的应用:UVa156-Ananagrams
- 算法竞赛入门经典--古老的密码
- mac上远程连接windows
- nested exception is java.lang.IllegalArgumentException
- 十二个开源UML工具
- HELLO WORLD---IoT
- UVA156 Ananagrams
- GitHub 优秀的 Android 开源项目
- 程序员常用网站
- 优秀开源项目集锦
- Hadoop2.7与Spark1.6的集群搭建
- ssd: single shot multibox detector配置问题
- 以太网 串行管理接口 SMI MDIO
- Android ImageView的setImageLevel和level-list使用简介
- Segment切换页