uva 156 Ananagrams(检索+sort排序)
来源:互联网 发布:谁也知夜夜与她那内情 编辑:程序博客网 时间:2024/06/05 19:56
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
解题思路:每次读入word时,就将单词转化成小写的,再进行字典序排序(对于单词),记得保留原先的单词,因为输出时输出的为原先的单词。比较是只要比较转化后的单词就可以了。注意输出时是按照原先单词的字典序,这时候有区分大小写。
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define N 1005#define M 25struct word{char before[M];char change[M];int bo;};word s[N];char str[M];void swap(word &a){int l = strlen(a.before);for (int i = 0; i <= l; i++){if (a.before[i] >= 'A' && a.before[i] <= 'Z')a.change[i] = a.before[i] + 32;elsea.change[i] = a.before[i];}sort(a.change, a.change + l);}int judge(int n){for (int i = 0; i < n; i++){if (strcmp(s[i].change, s[n].change) == 0){s[i].bo++;return 0;}}s[n].bo = 0;return 1;}int cmp(const word &a, const word &b){return strcmp(a.before, b.before) < 0;}int main(){int n = 0;// Read.do{memset(str, 0, sizeof(str));scanf("%s", s[n].before);swap(s[n]);if (strcmp(s[n].before, "#") == 0)break;s[n].bo = 0;if(judge(n))n++;}while (1);sort(s, s + n, cmp);// Printf.for (int i = 0; i < n; i++){if (s[i].bo)continue;printf("%s\n", s[i].before);}return 0;}
- uva 156 Ananagrams(检索+sort排序)
- UVA 156 (暑假-排序、检索(2)-A-Ananagrams)
- UVA 156 --- Ananagrams 排序去重
- UVA 156Ananagrams (数据结构)
- uva 156(排序、检索)
- uva 156 Ananagrams(字符串模拟搜索)
- uva 156 Ananagrams(字符串+STL应用)
- UVA -156 Ananagrams(反片语)
- 反片语(Ananagrams,Uva 156)
- Ananagrams UVA 156(反片语)
- UVA 156 Ananagrams(stl,map,set)
- UVA 156 Ananagrams(map、set)
- UVA 156 Ananagrams (map应用)
- UVa 156 - Ananagrams
- uva 156 Ananagrams
- UVA 156 - Ananagrams
- UVA 156 - Ananagrams
- UVa 156 - Ananagrams
- JAVA IO
- 在DateTimePicker中Format 设置
- MySQL 数据类型
- 迷宫问题
- pku3013
- uva 156 Ananagrams(检索+sort排序)
- hdu 2594
- 为手机定制ROM
- java 怎么判断socket已经断开
- unbuntu版本
- hdu 3810 DP
- 关于Oracle Clusterware 进程
- 【分享】一款手机真机屏幕同步抓取软件
- Javascript中Function,Object,Prototypes,__proto__等概念详解