zoukankan      html  css  js  c++  java
  • 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;
    }
    

      

  • 相关阅读:
    SGC强制最低128位加密,公钥支持ECC加密算法的SSL证书
    python学习笔记(一)
    eclipse中启动 Eclipse 弹出“Failed to load the JNI shared library jvm.dll”错误
    外键建立失败
    scala函数式编程(一)
    idea环境下建立maven工程并运行scala程序
    scala中option、None、some对象
    Java与mysql数据库编程中遇见“Before start of result set at com.mysql.jdbc.SQLError.createSQLException” 的解决办法
    hive表的存储路径查找以及表的大小
    red hat7 系统可以ping通ip地址但是不能ping通域名
  • 原文地址:https://www.cnblogs.com/inmoonlight/p/5701524.html
Copyright © 2011-2022 走看看