zoukankan      html  css  js  c++  java
  • UVA 156 Ananagrams(map映射)

            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. Therewillalways 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重排后可组成acme则不输出该单词。
    思路:使用map与vector容器,将每个单词中的字母全部转为小写并将字母按字典序排序后用map记录出现次数,用一个vector储存所有单词,再用另一个vector储存并输出符合条件的单词。
    #include<iostream>
    #include<map>
    #include<cctype>
    #include<string>
    #include<vector>
    #include<algorithm>
    using namespace std;
    map<string, int>ma;
    vector<string>words;
    int main()
    {
    	char c, s[25],a[25];
    	int m = 0;
    	while (~(c = getchar()))
    	{
    		if (c == '#')
    			break;
    		if (isalpha(c))
    		{
    			a[m] = c;
    			s[m++] = tolower(c);//tolower将大写转小写
    		}
    		else if (m != 0)
    		{
    			a[m] = '';
    			s[m] = '';
    			sort(s, s + m);
    			words.push_back(a);
    			if (!ma.count(s))ma[s] = 0;//记录s出现次数
    			ma[s]++;
    			m = 0;
    		}
    	}
    	vector<string>print;
    	for (int i = 0; i < words.size(); i++)
    	{
    		string b=words[i];
    		for (m = 0; m < b.length(); m++)
    		{
    			b[m] = tolower(b[m]);
    		}
    		sort(b.begin(), b.end());
    		
    		if (ma[b] == 1)
    			print.push_back(words[i]);
    	}
    	sort(print.begin(), print.end());
    	for (int i = 0; i < print.size(); i++)
    	{
    		cout << print[i] << endl;
    	}
    	return 0;
    }

  • 相关阅读:
    Ansible
    Ansible
    MySQL
    JS计算时间差(天,时,分钟,秒)
    cookie,localStorage,sessionStorage的区别
    css布局 -双飞翼布局&圣杯布局
    Python批量爬取网站图片
    vue-cli3中引入图片的几种方式和注意事项
    git常用命令
    vue-cli3的打包并在本地查看
  • 原文地址:https://www.cnblogs.com/csu-lmw/p/9124467.html
Copyright © 2011-2022 走看看