Ananagrams(sort+map) UVA - 156 统计字谜

Ananagrams(sort+map) UVA - 156 统计字谜

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

个人分析:

这个题目题意简单说就是一段短文,里面的单词是特别的,那么就输出。何为特别?就是别的单词不能通过排列组合出来 比如came mace这一组came可以通过排列组合变为mace 但是有一点要注意:与大小写无关 所以我们直接将所有的单词先变为小写 具体代码如下:

string sort_lower(string s)
{
    for(int i=0;i<s.length();i++)
    {
        s[i]=tolower(s[i]);
    }
    sort(s.begin(),s.end());
    return s;
}

以上就是把每个单词变为小写的形式
我们把所有变小的单词放入map中去(为了统计出现个数)如果出现超过1次,那么这个单词就不是特殊的了 也就不输出 但是我们单词都变小了 后面输出要求是原来的单词 并且要按照字母表来排序输出 后面排序类似变小单词一样 用一个sort排序就好 那么对于原本的单词,显然,我们可以用vector来动态存储,具体代码如下:(在开头申明了一个名为words的vector数组)

string str;
    mp.clear();
    while(cin>>str&&str[0]!='#')
    {
        words.push_back(str);
        string ss=sort_lower(str);
        mp[ss]++;
    }

对下面代码的解释:
因为后面输出需要按字典序进行排列,所以我们需要一个专门用来判断是否输出的vector数组 这里我命名vec
mp[sort_lower(words[i])]==1
注意这段代码!!!
因为之前我们放入mp放进去的是排好了的并且全是小写 所以这里还要调用一次变为小写单词的方法sort_lower() 如果访问的单词只出现了一次(特殊的)那么我们就放入vec里面去

 vector<string> vec;
    for(int i=0;i<words.size();i++)
    {
        if(mp[sort_lower(words[i])]==1)
        {
            vec.push_back(words[i]);
        }
    }

个人感受:

这个题目呢,英文比较多,表面看似很复杂哦,但是别怕,把难题想通了会很有成就感的。有些英文单词不认识~~然后就看的别人博客的解题思路,发现其实就是统计单词型的题目,后面经过优化后就是如下代码了。然后出错的地方一是while循环扩大了,二是对map里面判断单词出现次数的时候没有调用变小写的方法 没有考虑之前map里面放入的全是排好序的小写单词
具体代码如下:
AC

#include<iostream>
#include<cstring>
#include<map>
#include<vector>
#include<cstdio>
#include<algorithm>
using namespace std;
map<string,int>mp;
vector<string> words;
string sort_lower(string s)
{
    for(int i=0;i<s.length();i++)
    {
        s[i]=tolower(s[i]);
    }
    sort(s.begin(),s.end());
    return s;
}
int main()
{
    string str;
    mp.clear();
    while(cin>>str&&str[0]!='#')
    {
        words.push_back(str);
        string ss=sort_lower(str);
        mp[ss]++;
    }
    vector<string> vec;
    for(int i=0;i<words.size();i++)
    {
        if(mp[sort_lower(words[i])]==1)
        {
            vec.push_back(words[i]);
        }
    }
    sort(vec.begin(),vec.end());
    for(int i=0;i<vec.size();i++)
    {
        cout<<vec[i]<<endl;
    }


    return 0;
}

学如逆水行舟,不进则退

猜你喜欢

转载自blog.csdn.net/weixin_42429718/article/details/87470164