uva 156 Ananagrams

这道题的大意是要判断两个字符串里面的字符如果大小写不敏感打乱顺序后能不能互相转换,其实判断的方法很简单,将两个字符串里面所有字符全部转为小写,然后对字符排序,如果两个原始字符串得到的转换后的字符串是一样的,说明两个字符串可以互相转换,反之则不行,注意最后要求用字典序打印结果,先一步一步得出每一个字符串是不是题目要求的类型,然后将符合条件的字符串插入到集合里面,自然就是按字典序列排序的了,最后用一个集合的迭代器来循环打印结果就行了。

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

#define		MAX_LEN			100
#define		WORD_NUM		5000
#define		WORD_LEN		30

set result;

struct node
{
	char key[WORD_LEN];
	char value[WORD_LEN];
};

struct node words[WORD_NUM];


int cmp(const void *a, const void *b)
{
	char *pa = (char*)a;
	char *pb = (char*)b;

	return *pa-*pb;
}

int cmp_node(const void *a, const void *b)
{
	struct node *pa = (struct node*)a;
	struct node *pb = (struct node*)b;

	return strcmp(pa->value, pb->value);
}


void generate_value(struct node *p)
{
	int i;

	strcpy(p->value, p->key);
	for(i=0; ivalue); i++)
		if(p->value[i]>='A' && p->value[i]<='Z')
			p->value[i] += ('a'-'A');
	qsort((void*)(p->value), strlen(p->value), sizeof(char), cmp);
}


int main(void)
{
	char buffer[MAX_LEN];
	int i;
	int start, end;
	int count;
	string str;
	set::iterator it;

	count = 0;
	while(1)
	{
		gets(buffer);
		if(strcmp(buffer,"#") == 0)
			break;

		for(start = 0; start%s\n", words[i].key, words[i].value);
	*/

	qsort((void*)words, count, sizeof(struct node), cmp_node);
	if(strcmp(words[0].value,words[1].value))
	{
		str.assign(words[0].key, strlen(words[0].key));
		result.insert(str);
	}
	for(i=1; i


 

你可能感兴趣的:(acm)