UVA156 Ananagrams

来源:互联网 发布:mac版本支持远程控制 编辑:程序博客网 时间:2024/05/16 04:52


 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 arelative 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这题题目是把这些单词中,不存在字母完全相同的单词,按字典序输出。要进行单词中字母的排序,以及字符串的排序。但要记得保留大小写没转换前的字符串,用来输出。我这题是福祉了三份,一份字母排序,字符串也排序。一份就字母排序。。通过第二份来找原来那份。。AC代码:
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int Comp(const void *p1,const void *p2) {return strcmp((char*)p1,(char*)p2);}int main() {char str[1010][100];char temp[1010][100];char temp2[1010][100];char temp3[1010][100];int i;int count = 0;for ( i = 0 ; ; i++ ) {scanf("%s",str[i]) ;strcpy (temp[i] , str[i] );strcpy (temp2[i] , str[i]);for (int j = 0 ; j < strlen(temp[i]) ;j++ ) {if (temp[i][j] >= 'A' && temp[i][j] <='Z') {temp[i][j] += 32 ;temp2[i][j] += 32 ;}}if ( str[i][0] == '#' ) break;}for (int j = 0; j < i; j++) {sort(temp2[j], temp2[j] + strlen(temp2[j]));sort(temp[j], temp[j] + strlen(temp[j]));qsort(temp,i,sizeof(temp[0]),Comp);}for ( int j = 0 ; j < i ; j++) {if (j == 0) {if (strcmp ( temp[0], temp[1]) != 0) {for ( int k = 0 ;k < i ; k++ ) {if (strcmp (temp2[k],temp[j]) == 0 ) {strcpy(temp3[count++] ,str[k]);break;}}}continue;}else if (j == i - 1) {if (strcmp ( temp[j], temp[j-1]) != 0 ) {for ( int k = 0 ;k < i ; k++ ) {if (strcmp (temp2[k],temp[j]) == 0 ) {strcpy( temp3[count++] , str[k]);break;}}}continue;}else {if (strcmp ( temp[j], temp[j-1]) != 0 && strcmp ( temp[j] ,temp[j+1]) != 0) {for ( int k = 0 ;k < i ; k++ ) {if (strcmp (temp2[k],temp[j]) == 0 ) {strcpy( temp3[count++] , str[k]);break;}}}}}qsort(temp3,count,sizeof(temp3[0]),Comp);for (int j = 0 ; j < count  ;j++) {printf("%s\n",temp3[j]);}return 0;}


0 0