156 - Ananagrams

#include <iostream>
#include <cstdio>
#include <string>
#include <cctype>
#include <sstream>
#include <algorithm>

using namespace std;
const int maxn=85;

struct Words
{
	string changedWord;
	string word;
}words[maxn*maxn];
string anagrams[maxn*maxn];

void change(int m)
{
	words[m].changedWord="";
	for(int i=0;i<words[m].word.size();i++)
	{
		words[m].changedWord+=tolower(words[m].word[i]);
	}
	sort(words[m].changedWord.begin(),words[m].changedWord.end());
}

bool cmp_words(const Words &a,const Words &b)
	//	Compilation error
	//sort中比较函数定义时,传入的参数必须为const常量
{
	return a.changedWord<b.changedWord;
}

int main()
{
	/*
	freopen("156.in","r",stdin);
	freopen("156.out","w",stdout);
	*/
	string line;
	int u=0;
	while(getline(cin,line))
	{
		if(line=="#")
			break;
		istringstream sin(line);
		string a;
		while(sin>>a)
		{
			words[u].word=a;
			change(u);
			u++;
		}
	}
	sort(words,words+u,cmp_words);
	int num=1;
	int v=0;
	for(int i=0;i<u;i++)
	{
		if(words[i].changedWord==words[i+1].changedWord)
			num++;
		else
		{
			if(num==1)
			{
				anagrams[v++]=words[i].word;
			}
			else
				num=1;
		}
	}
	sort(anagrams,anagrams+v);
	for(int i=0;i<v;i++)
		cout<<anagrams[i]<<endl;
	return 0;
}


你可能感兴趣的:(String,struct,include)