Ananagrams
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
Disk
NotE
derail
drIed
eye
ladder
soon
题目给的很明确,排除重复,按字典序输出未重复的单词。
就是麻烦了点(至少以我现在的水平)
先开一个二维数组a储存原来的单词,之后再看一个二维数组b进行转化大小写,排序,比较
然后将b所得的结果映射到a上
思路很糙,但是也想不出别的简单的方法了
贴上代码,以后留着看看
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int struprs(char a[])
{
int L=strlen(a);
int i,j;
for(i=0;i<L;i++)
if(a[i]>='a'&&a[i]<='z')
a[i]-=32;
return 0;
}
int main()
{
char a[100][30];
char c[100][30];
char w[30];
char x,y;
int i=0,j,k;
int N,m,n,L;
while(scanf("%s",a[i])!=EOF)
{
if(strcmp(a[i],"#")==0)
break;
strcpy(c[i],a[i]);
/*将a复制到c中*/
struprs(c[i]);
/*转化大小写*/
L=strlen(c[i]);
/*计算长度*/
for(k=0;k<L;k++)
for(j=0;j<L-k-1;j++)
if(c[i][j]>c[i][j+1])
{
x=c[i][j];
c[i][j]=c[i][j+1];
c[i][j+1]=x;
}
/*对单词C进行大小写转化和排序之后*/
i++;
}
N=i; /*一共有多少个单词*/
for(i=0,k=0;i<N;i++)
{
y=0;
for(j=1;j<N-i;j++)
if(strcmp(c[i],c[i+j])==0) /*如果找到了重复的单词的话*/
{
y=1;/*就告诉找到了,之后将对应的a复制成为123*/
strcpy(a[i+j],"123");
}
if(y==1)
{
strcpy(a[i],"123");
}
}
for(i=0;i<N;i++)
for(j=0;j<N-i-1;j++) /*冒泡排序,排列a的顺序*/
if(strcmp(a[j],a[j+1])>0)
{
strcpy(w,a[j]);
strcpy(a[j],a[j+1]);
strcpy(a[j+1],w);
}
for(i=0;i<N;i++)
if(strcmp(a[i],"123")!=0)/*输出没有重复的单词!*/
puts(a[i]);
return 0;
}