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;}

原创粉丝点击