• ACM题目————STL练习之Ananagrams


    Description

    Download as PDF

    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

    题目很简单,就是说给定一些字符串(以#结束),把这些字符串排序(不区分大小写),然后输出出现过两次以上的字符串(按字典顺序,也就是按ASII码表)。
    //Asimple
    #include <iostream>
    #include <vector>
    #include <string>
    #include <map>
    
    using namespace std;
    
    typedef multimap<string,string>::iterator mss_iter;
    multimap<string,string> m;
    string str;
    
    int main()
    {
        while ( cin >> str )
        {
            if( str == "#" ) break ;
            string str_copy(str);
            for(int i=0; i<str_copy.size(); i++)
                str_copy[i] = tolower(str_copy[i]);
            sort(str_copy.begin(),str_copy.end());//排序
            m.insert(make_pair(str_copy,str));//将排序后的字符串和原字符串放进一个容器
        }
        vector<string> S;
        for( mss_iter it=m.begin(); it!=m.end(); it++)
            if( m.count( it->first) == 1 )
                S.push_back( it->second) ;
        sort(S.begin(),S.end());
        for( int i=0; i<S.size(); i++)
            cout << S[i] << endl ;
    
        return 0;
    }
    


    低调做人,高调做事。
  • 相关阅读:
    网站描述description如何编写
    网站关键词布局设置,这样添加关键词排名很容易上来!
    长尾关键词挖掘工具和使用方法
    小站点如何做好长尾词库(600个长尾词排名的经验分享!)
    如何利用seo技术霸屏你的行业关键词排名
    利用seo技术排名热点新闻词引流(日IP增加2万+)
    yagmail模块的使用
    python--接口自动化
    Python--unittest参数化
    Python--日志模块
  • 原文地址:https://www.cnblogs.com/Asimple/p/5523507.html
Copyright © 2020-2023  润新知