hdu 1075 字典树

#include <iostream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
using namespace std;

struct treenode{
	bool is_over;
	char english[12];
	treenode *next[26];
	treenode(){
		is_over = false;
		memset( english, 0, sizeof( english ) );
		memset( next, 0, sizeof( next ) );
	}
}*root = new treenode();

char book[3001];

void add( char *martian, char *english ){
	treenode *next = root;
	while( *martian ){
		if( next->next[(*martian)-'a'] == NULL ){
			next->next[(*martian)-'a'] = new treenode();
		}
		next = next->next[(*martian)-'a'];
		martian++;
	}
	next->is_over = true;
	strcpy( next->english, english );
}

bool search( char *martian ){
	treenode *next = root;
	while( *martian ){
		if( next->next[(*martian)-'a'] == NULL ){
			return false;
		}
		next = next->next[(*martian)-'a'];
		martian++;
	}
	if( next->is_over ){
		printf( "%s", next->english );
		return true;
	}
	return false;
}

int main(){
	char martian[13], english[13], temp[130], *temp1, temp2;
	scanf( "%s", temp );
	while( scanf( "%s%s", english, martian ), strcmp( english, "END" ) != 0 ){
		add( martian, english );
	}
	getchar();
	
	while( gets(book), strcmp( book, "END" ) != 0 ){
		char *iter = book;
		while( *iter ){
			int i = 0;
			temp1 = iter;
			while( isalpha( *iter ) ){
				temp1[i] = *iter;
				iter++;
				i++;
			}
			temp2 = temp1[i];
			temp1[i] = '\0';
			if( search( temp1 ) == false ){
				printf( "%s", temp1 );
			}
			temp1[i] = temp2;
			while( !isalpha( *iter ) ){
				if( *iter == NULL ){
					break;
				}
				printf( "%c", *iter );
				iter++;
			}
		}
		puts(""); 
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值