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 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 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.
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 #
Disk NotE derail drIed eye ladder soon
找出不重复的单词按字典序排序,排序区分大小写,重复是所有字母一样顺序不同无所谓不区分大小写。
很蛋疼我把标记数组定义在堆区就是主函数外面就对了,而定义在主函数内也就是栈区就runtime error了,数组大小都是一样的
奇怪╮(╯▽╰)╭求指教
#include <stdio.h>
#include <string.h>
#define max 1001
char word[max][100],key[max][100],s[100],s1[100];
int a[max];
void change()
{
int l=strlen(s),i,j;
for (i=0;i<l;i++)
if (s[i]<='Z') s1[i]=s[i]+32;
else s1[i]=s[i];
for (i=0;i<l-1;i++)
for (j=i+1;j<l;j++)
if (s1[i]>s1[j]) {s1[l]=s1[i];s1[i]=s1[j];s1[j]=s1[l];}
s1[l]='\0';
};
int pk(char s1[],char s2[])
{int l1=strlen(s1),l2=strlen(s2),i;
l1=l1<l2 ? l1:l2;
for (i=0;i<l1;i++)
{if (s1[i]>s2[i]) return 1;
if (s1[i]<s2[i]) return 0;
}
};
void main ()
{int sum=0,i,j,f;
while (scanf("%s",&s))
{if (s[0]=='#') break;
change();
f=1;
for (i=1;i<=sum;i++)
if (strcmp(key[i],s1)==0) {f=0;a[i]=0;break;}
if (f) {++sum;a[sum]=1;strcpy(word[i],s);strcpy(key[i],s1);}
}
for (i=1;i<sum;i++)
for (j=i+1;j<=sum;j++)
if (pk(word[i],word[j]))
{strcpy(s,word[i]);strcpy(word[i],word[j]);strcpy(word[j],s);
a[0]=a[i];a[i]=a[j];a[j]=a[0];
}
for (i=1;i<=sum;i++)
if (a[i]) puts(word[i]);
}