编程手记 156 - Ananagrams-UVA


 最近一直在刷UVA的题,虽然现在自己的水平不是很高,只能做一些水题,但是也收货了很多,所以决定刷完一些题目之后,写一些心得和总结,聊胜于无,以后无聊的时候可以翻出来看看,今天先说一个刚刷完的水题吧。

 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;
}




总结:交上去WA了2次,第一次是因为不知道uva不支持strupr这个转换函数,二次是数组开小了,开大一点就AC了
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值