HDU 1075 What Are You Talking About(字典树)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1075

题意:根据词典翻译语句。

思路:裸的字典树。每个节点存以该节点为结尾的对应的单词。

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <string>
#include <vector>

using namespace std;

#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define ceil(x, y) (((x) + (y) - 1) / (y))

const int SIZE = 30;
const int N = 3000 * 10 * 26 + 10;
const int INF = 0x7f7f7f7f;
const int MAX_WORD = 15;

struct Trie {
	char word[MAX_WORD];
	int val[SIZE];
};

int sz;
Trie pn[N];

int newnode() {
	memset(pn[sz].val, 0, sizeof(pn[sz].val));
	strcpy(pn[sz].word, "#");
	return sz++;
}

void init() {
	sz = 0;
	newnode();
}

void insert(char *s, char *t) {
	int u = 0;
	for (int i = 0; s[i]; i++) {
		int idx = s[i] - 'a';
		if (!pn[u].val[idx])
			pn[u].val[idx] = newnode();
		u = pn[u].val[idx];
	}
	strcpy(pn[u].word, t);
}

char *findstr(char *s, int st, int ed) {
	int u = 0;
	for (int i = st; i <= ed; i++) {
		int idx = s[i] - 'a';
		if (!pn[u].val[idx])
			return NULL;
		u = pn[u].val[idx];
	}
	return pn[u].word;
}

int main() {
	char s[3005], t[MAX_WORD];
	init();
	while (scanf("%s", s) && s[0] != 'E') {
		if (s[0] == 'S')
			continue;
		scanf("%s", t);
		insert(t, s);
	}
	getchar();
	while (gets(s) && s[0] != 'E') {
		if (s[0] == 'S')
			continue;
		int len = strlen(s);
		int l = -1;
		for (int i = 0; i < len; i++) {
			if (s[i] >= 'a' && s[i] <= 'z') {
				if (l == -1)
					l = i;
				else if (i == len - 1) {
					char *p = findstr(s, l, i);
					if (p != NULL && p[0] != '#')
						printf("%s", p);
					else {
						for (int j = l; j <= i; j++)
							printf("%c", s[j]);
					}
				}
			}
			else {
				if (l == -1)
					printf("%c", s[i]);
				else {
					char *p = findstr(s, l, i - 1);
					if (p != NULL && p[0] != '#')
						printf("%s", p);
					else {
						for (int j = l; j < i; j++)
							printf("%c", s[j]);
					}
					printf("%c", s[i]);
					l = -1;
				}
			}
		}
		printf("\n");
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-05 20:34:22

HDU 1075 What Are You Talking About(字典树)的相关文章

hdu 1075 What Are You Talking About 字典树 trie

What Are You Talking About Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others) Total Submission(s): 14703    Accepted Submission(s): 4724 Problem Description Ignatius is so lucky that he met a Martian yesterday. But

HDU 4825 Xor Sum(二进制的字典树,数组模拟)

题目 //居然可以用字典树...//用cin,cout等输入输出会超时 //这是从别处复制来的 #include<cstdio> #include<algorithm> #include<cstring> using namespace std; int node[3011111][2]; int tag,m,n,cas=0,T; long long one[64],allone,tmp; //0/1树的加数据操作,增加一个32的数 //其中如果当前位是0,则加左儿子,

hdu 3172 Virtual Friends (并查集 + 字典树)

题目: 链接:点击打开链接 题意: 输入n,给出n行数据,每行有两个字符串,输出关系网络中朋友的个数,n行. 思路: 代码: #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> using namespace std; const int N = 22; const int M = 200020; struct node { int c; node *chil

HDU 4825 Xor Sum(经典01字典树+贪心)

Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) Total Submission(s): 1555    Accepted Submission(s): 657 Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Ze

HDU 1800 Flying to the Mars(字典树)

Flying to the Mars Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 12767    Accepted Submission(s): 4048 Problem Description In the year 8888, the Earth is ruled by the PPF Empire . As the popu

hdu 1671&amp;&amp;poj 3630 Phone List 【字典树】

题目链接:http://acm.acmcoder.com/showproblem.php?pid=1671 题意:问是否存在一个串是另一个串的前缀. 解法:建字典树,插入的串的结尾设置标志位,如果以后访问到,则存在一个串是另一个串的前缀.注意释放内存,不然超内存:(太弱,释放内存调了好久... 代码: #include <stdio.h> #include <iostream> #include <string.h> #include <algorithm>

HDU 1800 Flying to the Mars 字典树,STL中的map ,哈希树

http://acm.hdu.edu.cn/showproblem.php?pid=1800 字典树 #include<iostream> #include<string.h> #include<stdio.h> using namespace std; struct node { int sum; node *next[10]; node() { sum=0; memset(next, NULL, sizeof(next)); }; }; int ans; char

01字典树

Hdu 4825 从高位到地位建立字典树,贪心查询. #include <bits/stdc++.h> using namespace std; const int maxn = 100000+5; typedef long long ll; ll bin[35]; int n,m; ll a[maxn],x; struct trie{ int cnt; int ch[maxn*35][2]; ll val[maxn*35]; void init(){ cnt=1; memset(ch[0],

HDU 1075 map or 字典树

What Are You Talking About Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others)Total Submission(s): 12773    Accepted Submission(s): 4069 Problem Description Ignatius is so lucky that he met a Martian yesterday. But