UVa156 - Ananagrams

题目地址:点击打开链接

C++代码:

 

#include <cstdio>

#include <algorithm>

#include <cstring>

#include <cctype>

using namespace std;

struct MyStruct

{

	char word[30];

	char changed_s[30];

	void change()

	{

		strcpy(changed_s,word);

		int i;

		int len=strlen(word);

		for(i=0;i<len;++i)

			if(isupper(changed_s[i]))

				*(changed_s+i)=tolower(changed_s[i]);



	}

	void sort_changed_s()

	{

		sort(changed_s,changed_s+strlen(changed_s));

	}

};

bool cmp_MS(MyStruct A,MyStruct B)

{

	return strcmp(A.changed_s,B.changed_s)>=0?false:true;

}

const int maxsize=10000;

MyStruct MS[maxsize];

struct MyStruct1

{

	char st[30];

};

MyStruct1 str[maxsize];

bool cmp(MyStruct1 A,MyStruct1 B)

{

	return strcmp(A.st,B.st)>=0?false:true;

}

int main()

{

	char word[30];

	while(true)

	{

		if(scanf("%s",word)==EOF)

			return 0;

		int n=1;

		strcpy(MS[0].word,word);

		MS[0].change();

		MS[0].sort_changed_s();

		while(true)

		{

			scanf("%s",word);

			if(word[0]=='#')

				break;

			strcpy(MS[n].word,word);

			MS[n].change();

			MS[n++].sort_changed_s();

		}

		sort(MS,MS+n,cmp_MS);

		strcpy(word,MS[0].changed_s);

		int i;

		int num=0;

		int k=0;

		if(n==1)

			printf("%s\n",MS[0].word);

		else

		{

			for(i=1;i<n;++i)

			{

				if(strcmp(MS[i].changed_s,word)==0)

				{

					++num;

					continue;

				}

				else

				{

					if(num==0)

						strcpy(str[k++].st,MS[i-1].word);

					else

						num=0;

					strcpy(word,MS[i].changed_s);

				}

			}

			if(num==0)

				strcpy(str[k++].st,MS[i-1].word);

			sort(str,str+k,cmp);

			for(i=0;i<k;++i)

				printf("%s\n",str[i].st);

		}

	}

	return 0;

}


 

 

你可能感兴趣的:(uva)