题目:
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
源代码:
#include <stdio.h> #include <string.h> #include <ctype.h> #define MAXN 1000+5 #define MAXL 80+5 char dic[MAXN][MAXL];//保存原始的单词 char word[MAXL]; char re_dic[MAXN][MAXL];//保存转化为小写字母并排序的单词 int cnt=0; void word_insert();//获得单词,且有序的插入单词表 void re_order();//将re_dic中的每个单词内部排序 int main(){ int i,j,k,flag; // freopen("data","r",stdin); while(scanf("%s",word)&&word[0]!='#') word_insert(); re_order(); for(i=0;i<cnt;i++){ if(strlen(dic[i])==1){//注意,一个字母的单词也为ananagram printf("%s\n",dic[i]); continue; } flag=0; for(j=0;j<cnt;j++){ if(i==j) continue; if(strcmp(re_dic[i],re_dic[j])==0){//两个单词的re_dic相同,说明含有相同的字母组成,即为ananagram flag=1; break; } } if(!flag) printf("%s\n",dic[i]); } return 0; } void re_order(){ int i,j,k; char temp; for(i=0;i<cnt;i++)//将re_dic里的单词内部排序,便于以后比较 for(j=1;j<strlen(re_dic[i]);j++) for(k=j;k>0;k--) if(re_dic[i][k]<re_dic[i][k-1]){ temp=re_dic[i][k]; re_dic[i][k]=re_dic[i][k-1]; re_dic[i][k-1]=temp; } else break; } void word_insert(){ int i,j; if(cnt==0){ strcpy(dic[0],word); strcpy(re_dic[0],word); cnt++; return; } for(i=cnt-1;i>=0;i--)//按字典序插入新读入的单词 if(strcmp(dic[i],word)>0){ strcpy(dic[i+1],dic[i]); strcpy(re_dic[i+1],re_dic[i]); } else break; strcpy(dic[i+1],word); for(j=0;j<strlen(word);j++)//题目要求不区分大小写 if(isupper(word[j])) word[j]=tolower(word[j]); strcpy(re_dic[i+1],word); cnt++; return ; }
Ananagrams UVA 156,布布扣,bubuko.com