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
#include
#include
#include
#include
#include
using namespace std;
mapma;
vectorwords;
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] = '\0';
			s[m] = '\0';
			sort(s, s + m);
			words.push_back(a);
			if (!ma.count(s))ma[s] = 0;//记录s出现次数
			ma[s]++;
			m = 0;
		}
	}
	vectorprint;
	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;
}

你可能感兴趣的:(UVA 156 Ananagrams(map映射))