uva 156 Ananagrams

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

#include <stdio.h>
#include <string>
#include <string.h>
#include <set>
#include <algorithm>
#include <iostream>
using namespace std;

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

set<string> 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; i<strlen(p->value); 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<string>::iterator it;

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

		for(start = 0; start<strlen(buffer); )
		{
			if(buffer[start] == ' ')
			{
				start ++;
				continue;
			}
			else
			{
				for(end=start+1; end<strlen(buffer); end++)
					if(buffer[end] == ' ')
						break;

				memcpy(words[count].key, buffer+start, end-start);
				words[count].key[end-start] = '\0';

				generate_value(words+count);

				count ++;
				start = end;
			}

		}
		
	}
	/*
	printf("count=%d\n", count);
	for(i=0; i<count; i++)
		printf("%s->%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<count; i++)
	{
		if(strcmp(words[i].value,words[i-1].value) && strcmp(words[i].value,words[i+1].value))
		{
			str.assign(words[i].key, strlen(words[i].key));
			result.insert(str);
		}
	}

	for(it=result.begin(); it!=result.end(); it++)
		cout << *it << endl;

	return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值