题目大意就是给出一个字典,找出其中不能通过打乱字母顺序后组成新单词的 那些单词(稍微有点绕)
我写的相当繁琐,晚上回去要研究一下stl写法
题目:
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 drIed noel dire Disk mace Rob dries #
Sample output
Disk NotE derail drIed eye ladder soon
单纯的数组代码:
1 #include <iostream> 2 #include <cstdio> 3 #include <string.h> 4 #include <stdlib.h> 5 #include <ctype.h> 6 using namespace std; 7 8 9 10 const int Wmax=25; 11 const int Dmax=1010; 12 char dict[Dmax][Wmax]; 13 char sorted[Dmax][Wmax]; 14 int dlen=0; 15 16 int word_cmp(const void* _a,const void* _b) 17 { 18 char* a =(char*) _a; 19 char* b = (char*) _b; 20 21 return *a-*b; 22 23 } 24 25 int str_cmp(const void* _a,const void* _b) 26 { 27 char* a=(char*) _a; 28 char* b = (char*) _b; 29 30 return strcmp(a,b); 31 } 32 33 int main() 34 { 35 cin>>dict[dlen]; 36 strcpy(sorted[dlen],dict[dlen]); 37 38 while(strcmp(dict[dlen],"#")!=0) 39 { 40 dlen++; 41 cin>>dict[dlen]; 42 strcpy(sorted[dlen],dict[dlen]); 43 44 } 45 qsort(dict,dlen,sizeof(dict[0]),str_cmp); 46 qsort(sorted,dlen,sizeof(sorted[0]),str_cmp); 47 48 for(int j=0;j<dlen;j++) 49 for(int i=0;i<strlen(sorted[j]);i++) 50 { 51 if(isupper(sorted[j][i])) 52 sorted[j][i]=tolower(sorted[j][i]); 53 } 54 for(int i=0;i<dlen;i++) 55 qsort(sorted[i],strlen(sorted[i]),sizeof(char),word_cmp); 56 57 58 59 for(int i=0;i<dlen;i++) 60 { 61 int flag=0; 62 for(int j=0;j<dlen;j++) 63 { 64 if(!strcmp(sorted[i],sorted[j]))flag++; 65 } 66 if(flag==1) 67 printf("%s ",dict[i]); 68 69 } 70 71 return 0; 72 }