HDU 1075 What Are You Talking About (map解法+Trie解法)

HDU 1075 What Are You Talking About (map解法+Trie解法)

ACM

题目地址:

HDU 1075 What Are You Talking About

题意:

给出一个“翻译-原文”的对应表,然后给出句子,要把句子中的原文都翻译出来。

分析:

可以用map赤裸裸地做,但是比较花费时间,虽然这题时间给了5s,map解法是能过的。

不过Trie解法500+ms,果然Trie字典树才是正解啊。

Trie入门题。

另外发现ios_base::sync_with_stdio(0)这句话是关闭IO同步的,如果把cin和gets混用就不同步...坑了好久..

代码:

(map解法)

/*
*  Author:      illuz <iilluzen[at]gmail.com>
*  File:        1075_map.cpp
*  Create Date: 2014-09-23 13:48:38
*  Descripton:
*/

#include <cstdio>
#include <cstring>
#include <cctype>
#include <map>
#include <iostream>
#include <algorithm>
using namespace std;

#define repf(i,a,b) for(int i=(a);i<=(b);i++)
typedef long long ll;

string a, b;
char s[3010];
map<string, string> mp;

int main() {
	// ios_base::sync_with_stdio(0);
	cin >> a;
	while (cin >> a) {
		if (a == "END")
			break;
		cin >> b;
		mp[b] = a;
	}
	cin >> a;
	getchar();
	while (1) {
		gets(s);
		if (!strcmp(s, "END"))
			break;
		int len = strlen(s);
		a = "";
		repf (i, 0, len - 1) {
			if (islower(s[i])) {
				a += s[i];
			} else {
				if (mp.find(a) != mp.end())
					cout << mp[a];
				else
					cout << a;
				a = "";
				cout << s[i];
			}
		}
		cout << endl;
	}
	return 0;
}

(Trie解法)

/*
*  Author:      illuz <iilluzen[at]gmail.com>
*  File:        1075_trie.cpp
*  Create Date: 2014-09-23 14:25:31
*  Descripton:  trie
*/

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

#define repf(i,a,b) for(int i=(a);i<=(b);i++)
typedef long long ll;

const int N = 3010;
const int SIZE = 26;

// pointer trie
struct Node {
	char* val;
	Node *next[SIZE];
};

struct PTrie {
	Node *root;
	PTrie() { root = newNode(); }
	void init() { del(root); root = newNode(); }
	inline int idx(char c) { return c - 'a'; }

	Node *newNode() {
		Node *u = new Node;
		repf (i, 0, SIZE - 1) {
			u->next[i] = NULL;
		}
		u->val = NULL;
		return u;
	}

	void insert(char *s, char *v) {
		Node *u = root;
		int len = strlen(s);
		repf (i, 0, len - 1) {
			int c = idx(s[i]);
			if (u->next[c] == NULL)
				u->next[c] = newNode();
			u = u->next[c];
		}
		u->val = new char[11];
		strcpy(u->val, v);
	}

	void find(char *s) {
		Node*u = root;
		int len = strlen(s);
		repf (i, 0, len - 1) {
			int c = idx(s[i]);
			if (u->next[c] == NULL) {
				printf("%s", s);
				return;
			}
			u = u->next[c];
		}
		if (u->val)
			printf("%s", u->val);
		else
			printf("%s", s);
	}

	void del(Node *rt) {
		if (rt == NULL)
			return;
		else {
			repf (i, 0, SIZE - 1)
				if (rt->next[i])
					del(rt->next[i]);
		}
		delete rt->val;
		delete rt;
	}
} trie;

char a[11], b[11];
char str[N], rec[N];

int main() {
	// ios_base::sync_with_stdio(0);
	scanf("%s", a);
	while (scanf("%s %s\n", a, b) && strcmp(a, "END")) {
		//cout << a << b << endl;
		trie.insert(b, a);
	}
	while (gets(str) && strcmp(str, "END")) {
		int len = strlen(str);
		int idx = 0;
		repf (i, 0, len - 1) {
			if (islower(str[i])) {
				rec[idx++] = str[i];
			} else {
				rec[idx] = 0;
				trie.find(rec);
				idx = 0;
				printf("%c", str[i]);
			}
		}
		puts("");
	}
}
时间: 2024-10-12 01:35:27

HDU 1075 What Are You Talking About (map解法+Trie解法)的相关文章

hdu 1075 What Are You Talking About(map)

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

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 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

HDU 1075 What Are You Talking About Trie题解

翻译火星语,不过火星语也是使用英文单词的,就是把一个单词对应到另外一个单词. 可以使用map, 使用二分,方法很多. 不过最快的应该都是Trie解法了. 把火星语挂在Trie树中,然后在叶子节点增加一个string容器,装英语单词. 查找的时候,找到了出现在Trie中的火星语,就返回string就可以了. #include <stdio.h> #include <string> #include <string.h> using namespace std; const

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): 20680    Accepted Submission(s): 6852 Problem Description Ignatius is so lucky that he met a Martian yesterday. But

hdu 1075 字典树

// hdu 1075 字典树 // // 题目大意: // // 给你一个字典,即有两个字符串,一个是英文,一个是火星文,然后 // 输入一段火星文,要你翻译成英文. // // 解题思路: // // 字典树,查字典嘛,有就输出查到的,没有原样输出.将火星文插入到 // 字典树中,然后在字典输中查找.找到了,输出对应的英文,否则,原样输 // 出. // // 感悟: // // 题目确实很简单,但是,没告诉数据范围啊,导致我一直RE,原来单词 // 可能对应很长的英文啊,找人家ac的开数组

hdu 4941 Magical Forest(STL之map应用)2014多校训练第7场

Magical Forest                                                                       Time Limit: 24000/12000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description There is a forest can be seen as N * M grid. In this fore

POJ - 2418 Hardwood Species(map,trie,BST)

1.输入若干行树名,输入结束后,按字典序输出树名及其所占百分比. 2.多种方法:map,trie,BST 3. map: #include<iostream> #include<stdio.h> #include<string> #include<map> using namespace std; int main(){ map<string,int>h; string s; int n; n=0; while(getline(cin,s)){

bzoj 2754 [SCOI2012]喵星球上的点名 (AC自动机+map维护Trie树)

题目大意:略 由于字符集大,要用map维护Trie树 并不能用AC自动机的Trie图优化,不然内存会炸 所以我用AC自动机暴跳fail水过的 显然根据喵星人建AC自动机是不行的,所以要根据问题建 然而这题有一些很艮的地方: 1.如果一个喵的名和姓都被点到,那他只被点到了一次 2.询问的串可能相同 3.如果map中并不包含某个元素,但你强行用数组表示它,那么它会返回0,然后这个元素会被强行插入map并赋值成0 1 #include <map> 2 #include <queue> 3