HDU 1075 What Are You Talking About (MAP solution + trie solution)
ACM
Question address:
HDU 1075 what are you talking about
Question:
Give a corresponding table of "Translation-original", then give sentences, and translate all the original texts in the sentence.
Analysis:
You can use map to do it naked, but it takes a lot of time. Although the time for this question is 5 s, the MAP solution can be used.
However, the trie solution is 500 + MS, And the trie dictionary tree is a positive solution.
Trie entry question.
In addition, we found that ios_base: sync_with_stdio (0) is used to disable Io synchronization. If we mix CIN and gets, it will not be synchronized ..
Code:
(MAP solution)
/** 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];elsecout << a;a = "";cout << s[i];}}cout << endl;}return 0;}
(Trie solution)
/** 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 triestruct 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);elseprintf("%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("");}}
HDU 1075 What Are You Talking About (MAP solution + trie solution)