您的位置:首页 > 其它

HDU 1075 What Are You Talking About Trie

2015-03-06 20:11 387 查看
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1075

字典树的简单应用,具体实现如下:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
typedef char State[12];
typedef struct _trie{
	State word;
	struct _trie *next[26];
}trie, *Trie;
trie node[900000];
int sz = 0;
char buf[3010];
Trie built(){
	Trie T = &node[sz++];
	memset(T, 0, sizeof(trie));
	return T;
}
void insert(Trie T, char *src, char *word){
	char *p = src;
	while (*p != '\0'){
		if (T->next[*p - 'a'] == NULL) T->next[*p - 'a'] = built();
		T = T->next[*p - 'a'];
		p++;
	}
	strcpy(T->word, word);
}
char* search(Trie T, char *src){
	char *p = src;
	while (*p != '\0'){
		if (T == NULL || T->next[*p - 'a'] == NULL) return NULL;
		T = T->next[*p - 'a'];
		p++;
	}
	return (char *)(T->word);
}
int main(){
#ifdef LOCAL
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w+", stdout);
#endif
	State s1, s2;
	Trie root = built();
	while (gets(buf)){
		if (0 == strcmp(buf, "START")) continue;
		else if (0 == strcmp(buf, "END")) break;
		else{
			sscanf(buf, "%s %s", s1, s2);
			insert(root, s2, s1);
		}
	}
	while (gets(buf)){
		if (0 == strcmp(buf, "START")) continue;
		else if (0 == strcmp(buf, "END")) break;
		else{
			char *p = NULL;
			int i, j, n = strlen(buf);
			for (i = 0; i < n; i++){
				int k = 0;
				for (j = i; isalpha(buf[j]) != 0; j++){
					s1[k++] = buf[i++];
				}
				s1[k] = '\0';
				p = search(root, s1);
				if (p != NULL && *p != '\0') printf("%s", p);
				else printf("%s", s1);
				printf("%c", buf[i]);
			}
		}
		printf("\n");
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: