HDU 1075 What Are You Talking About

题意:

      给一个映射的字典,然后给加密后的文本,求根据给定的映射的字典还原文本。

解法:

     其实用STL的map就可以了,但这里我用了Tire tree弄了个伪map

AC代码如下:

/* Author: ACb0y Date: 2010年12月4日20:39:40 Type: Map, Tire tree ProblemID: 1075 HDU What Are You Talking About Result: 3280822 2010-12-04 21:09:44 Accepted 1075 500MS 63268K 1935 B G++ ACb0y */ #include <stdio.h> #include <malloc.h> #include <string.h> struct node { node * child[26]; char * pstr; }; node * root; void insert(char * a, char * b) { int length = strlen(b); node * pnew = NULL; node * cur = root; for (int i = 0; i < length; ++i) { int pos = b[i] - 'a'; if (cur->child[pos] == NULL) { pnew = (node *)malloc(sizeof(node)); memset(pnew->child, 0, sizeof(node * [26])); pnew->pstr = NULL; cur->child[pos] = pnew; cur = cur->child[pos]; } else { cur = cur->child[pos]; } } cur->pstr = (char *)malloc(strlen(a) + 1); strcpy(cur->pstr, a); } char * find(char * str) { int length = strlen(str); node * cur = root; for (int i = 0;i < length; ++i) { int pos = str[i] - 'a'; if (cur->child[pos] == NULL) { cur = NULL; break; } else { cur = cur->child[pos]; } } return cur == NULL ? NULL : cur->pstr; } int main() { #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); #endif char str[3005]; char a[15]; char b[15]; char stack[15]; root = (node *)malloc(sizeof(node)); memset(root->child, 0, sizeof(node * [26])); root->pstr = NULL; gets(str); while (gets(str)) { if (strcmp(str, "END") == 0) { break; } else { sscanf(str, "%s %s", a, b); insert(a, b); } } gets(str); while (gets(str)) { if (strcmp(str, "END") == 0) { break; } else { int length = strlen(str); int top = 0; for (int i = 0; i < length; ++i) { if (str[i] >= 'a' && str[i] <= 'z') { stack[top++] = str[i]; } else { int c = 0; if (top == 0) { printf("%c", str[i]); continue; } stack[top] = 0; top = 0; char * pstr = find(stack); if (pstr == NULL) { printf("%s", stack); } else { printf("%s", pstr); } printf("%c", str[i]); } } printf("/n"); } } return 0; }

你可能感兴趣的:(c,加密,tree,null,insert,2010)