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;
    }

  • 相关阅读:
    DB2 9 基础根基(730 测验)认证指南,第 2 部门: 安适性(1)
    DB2 9 根底(730 考验)认证指南,第 3 部门: 访问 DB2 数据(7)
    IT人 软件工程师的务实职业生涯规划
    《Delphi 算法与数据结构》学习与感悟[8]: 单向链表的添加、删除与遍历
    《Delphi 算法与数据结构》简介及下载
    《Delphi 算法与数据结构》学习与感悟[1]: 通过 "顺序查找" 与 "二分查找" 说明算法的重要性
    《Delphi 算法与数据结构》学习与感悟[2]: 数据对齐
    学习 TTreeView [14] StateIndex(状态图标)、OverlayIndex(叠加图标)
    《Delphi 算法与数据结构》学习与感悟[3]: 获取一个字节中非空位的个数
    《Delphi 算法与数据结构》学习与感悟[6]: 一个简单的"单向链表"
  • 原文地址:https://www.cnblogs.com/csu-lmw/p/9124467.html
Copyright © 2011-2022 走看看