zoukankan      html  css  js  c++  java
  • UVA156

    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

    题目大意:给一个字典,如果存在两个单词有相同的字母,即使是不同的顺序,则它们不是Ananagram。输入Ananagram。(要注意大小写)

     1 #include<cstdio>
     2 #include<string.h>
     3 #include<ctype.h>
     4 #include<stdlib.h>
     5 
     6 char s[1005][25];
     7 char sorted[1005][25];
     8 
     9 int cmp_char(const void*_a,const void*_b)//给每个单词中的字符排序
    10 {
    11     char * a=(char *)_a;
    12     char * b=(char *)_b;
    13     return *a-*b;
    14 }
    15 
    16 int cmp_string(const void*_a,const void*_b)//给所有单词排序(字典序)
    17 {
    18     char * a=(char *)_a;
    19     char * b=(char *)_b;
    20     return strcmp(a,b);
    21 }
    22 
    23 int main()
    24 {
    25     int n=0;
    26     int i,j,flag;
    27     while(scanf("%s",&s[n]) != EOF)
    28     {
    29         if(s[n][0]=='#')
    30             break;
    31         n++;
    32     }
    33     qsort(s,n,sizeof(s[0]),cmp_string);
    34     for(i=0;i<n;i++)
    35     {
    36         strcpy(sorted[i],s[i]);//sorted[i]与s[i]一一对应
    37     }
    38     for(i=0;i<n;i++)
    39     {
    40         for(j=0;j<strlen(s[i]);j++)
    41         {
    42             sorted[i][j]=toupper(sorted[i][j]);    //都转化为大写字母
    43         }
    44     }
    45     for(i=0;i<n;i++)
    46     {
    47         qsort(sorted[i],strlen(sorted[i]),sizeof(char),cmp_char);//将sorted数组中每个单词中的字符排序
    48     }
    49     
    50     for(i=0;i<n;i++)
    51     {
    52         flag=1;
    53         for(j=0;j<n;j++)
    54         {
    55             if(i!=j&&strcmp(sorted[i],sorted[j])==0)
    56             {
    57                 flag=0;
    58                 break;
    59             }
    60         }
    61         if(flag==1)
    62             printf("%s
    ",s[i]);//输出的是输入的单词
    63     }
    64     
    65     return 0;
    66 }
  • 相关阅读:
    php分享三十:php版本选择
    php分享二十九:命名空间
    高性能mysql读书笔记(一):Schema与数据类型优化
    php分享二十八:mysql运行中的问题排查
    php分享二十七:批量插入mysql
    php分享二十六:读写日志
    Python | 一行命令生成动态二维码
    Python-获取法定节假日
    GoLang-字符串
    基础知识
  • 原文地址:https://www.cnblogs.com/youdiankun/p/3683842.html
Copyright © 2011-2022 走看看