hdu 1075 What Are You Talking About(字典树)
2014-10-29 20:50
465 查看
题目链接:hdu 1075 What Are You Talking About
题目大意:给定若干个字符串的映射,要求输出给定字符串的原串,即可以用映射串代替的均要替换。
解题思路:对被映射串建立字典树,然后单词节点记录的是对应串映射串位置。然后对于需要翻译的字符串,逐个处理
处单词,在字典树中查找,如果有遍历到单词节点,则输出对应的映射串。
题目大意:给定若干个字符串的映射,要求输出给定字符串的原串,即可以用映射串代替的均要替换。
解题思路:对被映射串建立字典树,然后单词节点记录的是对应串映射串位置。然后对于需要翻译的字符串,逐个处理
处单词,在字典树中查找,如果有遍历到单词节点,则输出对应的映射串。
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 5000000; const int sigma_size = 26; struct Tire { int sz; int g[maxn][sigma_size]; int val[maxn]; void init (); int idx(char ch); void insert(char* s, int x); int find(char* s); }S; char r[500000][15], op[3005], word[15]; void solve (int& p) { if (p == 0) return; word[p++] = '\0'; int x = S.find(word); if (x) printf("%s", r[x]); else printf("%s", word); p = 0; } int main () { while (scanf("%s", op) == 1) { S.init(); int n = 1; while (scanf("%s", op), strcmp(op, "END")) { strcpy(r , op); scanf("%s", op); S.insert(op, n++); } scanf("%s", op); getchar(); while (gets(op), strcmp(op, "END")) { int len = strlen(op), p = 0; for (int i = 0; i < len; i++) { if (op[i] >= 'a' && op[i] <= 'z') word[p++] = op[i]; else { solve(p); printf("%c", op[i]); } } solve(p); printf("\n"); } } return 0; } void Tire::init() { sz = 1; val[0] = 0; memset(g[0], 0, sizeof(g[0])); } int Tire::idx(char ch) { return ch - 'a'; } int Tire::find(char* s) { int u = 0, n = strlen(s); for (int i = 0; i < n; i++) { int v = idx(s[i]); if (g[u][v] == 0) return 0; u = g[u][v]; } return val[u]; } void Tire::insert(char* s, int x) { int u = 0, n = strlen(s); for (int i = 0; i < n; i++) { int v = idx(s[i]); if (g[u][v] == 0) { val[sz] = 0; memset(g[sz], 0, sizeof(g[sz])); g[u][v] = sz++; } u = g[u][v]; } val[u] = x; }
相关文章推荐
- hdu 1075 What Are You Talking About (字典树)
- hdu 1075 What Are You Talking About(字典树)
- HDU 1075 What Are You Talking About(字典树)
- hdu 1075 What Are You Talking About(字典树)
- hdu-1075-What Are You Talking About-(字典树)
- hdu 1075 What Are You Talking About(字典树)
- HDU1075 What Are You Talking About 解题报告--字典树
- 杭电hdu 1075 What Are You Talking About 字典树的应用
- 【字典树】hdu 1075 What Are You Talking About
- (HDU)1075 - What Are You Talking About【字典树】or【字符串map】
- hdu 1075 What Are You Talking About (字典树)
- HDU-1075 What Are You Talking About 字典树
- hdu 1075 What Are You Talking About 字典树 trie
- hdu 1075 What Are You Talking About (字典树)
- HDU 1075 What Are You Talking About (字典树)
- 【HDU 1075】What Are You Talking About(字典树)
- HDU ACM 1075 What Are You Talking About(字典树)
- HDU 1075 What Are You Talking About 经典字典树
- HDU 1075 What Are You Talking About (字典树)
- hdu 1075 What Are You Talking About 字典树