编程珠玑(第二版)2.1节第三个问题之变位词的实现

今天翻了翻编程珠玑这本书,整体而言还是比较有趣的。可以说给了读者另一种认识编程的角度吧。

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
using namespace std;

const int maxn = 1100;
vector<string> A[maxn];

map<string, int> hash;

int main()
{
	hash.clear();
	
	string str[110];
	int n = 0, k = 0;
	
	while(cin>>str[n] && str[n] != "END")
	{
		string t = str[n];
		sort(t.begin(), t.end());
		if(!hash[t]) hash[t] = ++k;
		n++;
	}
	
	for(int i = 0; i < n; i++)
	{
		string t = str[i];
		sort(t.begin(), t.end());
		A[hash[t]].push_back(str[i]);
	}
	
	for(int i = 0; i < n; i++)
	{
		for(int j = 0; j < A[i].size(); j++)
		{
			cout<<A[i][j]<<" ";
		}
		cout<<endl;
	}
	
	system("pause");
	return 0;
}


/*
input:
	
ptos
pots
stop
tops
XXXX
END

output:
ptos pots stop tops
XXXX
*/