• UVa 156 Ananagrams


    Description

    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

    解析:题意为把每个单词转换为小写字母形式,对于每个单词,如果它的字母重排后得到的单词没有在所有输入的单词中出现过,则称这种单词的转化之前的单词为ananagrams。按字典序输出所有的ananagrams。根据题意进行模拟即可。

    #include <iostream>
    #include <string>
    #include <algorithm>
    using namespace std;
    
    struct S{
        string s1, s2;    //s1为原单词,s2为转化后单词
        bool f;    //是否重复
        bool operator < (const S& b)const
        {
            return s1<b.s1;
        }
    };
    
    S s[1005];
    
    int main()
    {
        int cnt = 0;
        while(cin>>s[cnt].s1, s[cnt].s1[0] != '#'){
            s[cnt].s2.resize(s[cnt].s1.size());
            transform(s[cnt].s1.begin(), s[cnt].s1.end(), s[cnt].s2.begin(), ::tolower);
            sort(s[cnt].s2.begin(), s[cnt].s2.end());
            s[cnt].f = false;
            ++cnt;
        }
        for(int i = 0; i < cnt; ++i){
            for(int j = i+1; j < cnt; ++j){
                if(s[i].s2 == s[j].s2)
                    s[i].f = s[j].f = true;
            }
        }
        sort(s, s+cnt);
        for(int i = 0; i < cnt; ++i){
            if(!s[i].f)
                cout<<s[i].s1<<endl;
        }
        return 0;
    }
    

      

  • 相关阅读:
    Java程序设计:最大连续1的个数(LeetCode:485)
    LeetCode刷题经验2
    LeetCode刷题经验
    Java程序设计:区域和检索—数组不可变(LeetCode:303)
    Java程序设计:在排序数组中查找元素的第一个和最后一个位置(LeetCode:34)
    解决安装python3后yum不能使用情况
    使用shell做数据库备份的时候,遇到了以下问题,原因未知
    使用windows系统编写shell代码,在linux执行后的报错
    Apache URL重写规则
    PHP中的环境变量$_ENV, $_SERVER 及getenv
  • 原文地址:https://www.cnblogs.com/inmoonlight/p/5701524.html
Copyright © 2020-2023  润新知