HDU 1113

不复杂,只是写起来很麻烦……


#include "stdio.h"
#include "string.h"
#include "stdlib.h"

typedef struct word{
	char s[7];
	int len;

	char* has[100];
	int k;
}Word, *pWord;

void sort(char* s, int len){
	int i, j;
	char t;
	for(i=1; i<len; i++){
		t = s[i];
		for(j=i-1; j>=0; j--){
			if(s[j] > t)
				s[j+1] = s[j];
			else
				break;
		}
		s[j+1] = t;
	}
}

Word words[100];
int p;

char data[500][7];
int num;

int insert(int find, int* wh){
	char st[7];
	int i, k;
	int len;

	len = strlen(data[num]);
	strcpy(st, data[num]);
	sort(st, len);

	for(i=0; i<p; i++){
		if(words[i].len != len) continue;
		for(k=0; k<words[i].len; k++){
			if(st[k] != words[i].s[k])
				break;
		}

		if(k == words[i].len){
			if(find){
				*wh = i;
				return 1;
			}
			words[i].has[words[i].k++] = data[num];
			return 1;
		}
	}
	
	if(find) return 0;

	strcpy(words[p].s, st);
	words[p].len = len;
	words[p].has[0] = data[num];
	words[p].k = 1;
	p++;

	return 0;
}

void main(){
	int i, j;
	int wh;
	char* t;

	p = 0;
	num = 0;

	freopen("in.txt", "r", stdin);

	while(scanf("%s", data[num]), data[num][0]!='X'){
		insert(0, 0);
		num++;
	}
	
	while(scanf("%s", data[num]), data[num][0]!='X'){
		if(insert(1, &wh)){
			for(i=1; i<words[wh].k; i++){
				t = words[wh].has[i];
				for(j=i-1; j>=0; j--){
					if(strcmp(words[wh].has[j], t) > 0)
						words[wh].has[j+1] = words[wh].has[j];
					else
						break;
				}
				words[wh].has[j+1] = t;
			}
			for(i=0; i<words[wh].k; i++)
				printf("%s\n", words[wh].has[i]);
		}else
			printf("NOT A VALID WORD\n");
		printf("******\n");
		num++;
	}
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值