Ananagrams-UVA 156 白书第五章
来源:互联网 发布:主从 mysql spring 编辑:程序博客网 时间:2024/06/05 12:42
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
思路:先把单词从字符串中提取出来存入vector中,然后复制一份把大写改为小写,并且对每个单词内字母排序,然后对比vector里面的单词看出现的次数,看是不是只出现一次还是出现多次。。
#include<iostream>#include<algorithm>#include<string>#include<vector>#include<cctype>using namespace std;bool cmp1(char x,char y){ return x<y;}bool cmp2(string x,string y){ return x<y;}bool is_equal(string x,string y){ sort(x.begin(),x.end(),cmp1); sort(y.begin(),y.end(),cmp1); if(x==y) return true; else return false;}int main(){ string s; vector<string> words; int k=0,i=0,j; string s1; while(getline(cin,s)&&s!="#") { for(i=0;i<s.size();i++) { if(s[i]!=' ') { s1=s1+s[i]; if(i==(s.size()-1)) { words.push_back(s1); s1=""; k++; } } else { if(i>=1&&s[i-1]!=' ') { words.push_back(s1); k++; s1=""; } } } } vector<string> word(words.begin(),words.end()); for(i=0;i<words.size();i++) { for(j=0;j<words[i].size();j++) { words[i][j]=tolower(words[i][j]); } } vector<string> word1; for(i=0;i<words.size();i++) { int t=0; for(j=0;j<words.size();j++) { if(is_equal(words[i],words[j])) t++; } if(t==1) { word1.push_back(word[i]); } } sort(word1.begin(),word1.end(),cmp2); for(i=0;i<word1.size();i++) cout<<word1[i]<<endl; return 0;}
- Ananagrams-UVA 156 白书第五章
- 经典第五章 例 5-4 UVA 156 Ananagrams(反片语)【map的应用】
- 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
- UVa - 156 - Ananagrams
- uva 156 Ananagrams
- UVa 156 - Ananagrams
- oracle 削除データの復帰方法(flash back)
- cocos2dx 2.1.4 程序运行脉络解析3—解析CCEGLView
- 关于C++中的友元函数的总结
- poj 1226|| hdu 1238 Substrings(KMP)
- 双系统修复ubuntu grub2引导的方法
- Ananagrams-UVA 156 白书第五章
- Mac mini+Parallels 8.0+FreeBSD-8.4-RELEASE-amd64-disc1 全纪录
- Leetcode: Longest Valid Parentheses
- iptables常用命令
- 新建个博客
- ubuntu下解压缩包命令
- 虚函数、纯虚函数详解
- Stacks of Flapjacks 翻煎饼-UVA120 白书第五章
- 庞果网-数组排序