POJ 1318 Word Amalgamation (模拟)

30 篇文章 0 订阅

字典长度已知,直接开个结构体数组存单词,顺便保存单词长度,搜索的时候可以迅速检测不需要的单词,然后开一个30的数组保存每个单词出现的个数

最后要对字典排序(当时没看题忘排序了,WA了一次)


#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct words
{
	char word[101];
	int length;
	int check[30];
}dictionary[100000];

int cmp(void const* a,void const *b)
{
	struct words A=*(struct words *)a;
	struct words B=*(struct words *)b;
	return strcmp(A.word,B.word);
}

int checkal(int s1[],int s2[])
{
	int i=0;
	for(i=0;i<30;++i)
	{
		if(s1[i]!=s2[i])
		{
			return 0;
		}
	}
	return 1;
}
int main()
{
	int i=0,j=0,count=0;
	char data[101]={0};
	int check[30]={0};
	char flag=0;
	while(scanf("%s",dictionary[i].word)!=EOF&&dictionary[i].word[0]!='X')
	{
		for(j=0;j<30;j++)
		{
			dictionary[i].check[j]=0;
		}
		for(j=0;dictionary[i].word[j];++j)
		{
			++dictionary[i].check[dictionary[i].word[j]-'a'];
		}
		dictionary[i++].length=j;
	}
	qsort(dictionary,i,sizeof(struct words),cmp);
	count=i;
	while(scanf("%s",data)&&data[0]!='X')
	{
		flag=0;
		for(i=0;i<30;++i)
		{
			check[i]=0;
		}
		for(i=0;data[i];++i)
		{
			++check[data[i]-'a'];
		}
		for(j=0;j<count;++j)
		{
			if(dictionary[j].length==i&&checkal(dictionary[j].check,check))
			{
				flag=1;
				puts(dictionary[j].word);
			}
		}
		if(!flag)
		{
			puts("NOT A VALID WORD");
		}
		puts("******");
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值