HDU 1075-What Are You Talking About(Trie)

题意:

给你一个字典 一个英文单词对应一个火星单词 给你一段火星文翻译成英文 字典上的没有的不翻译

分析:

没有给数据规模 字典树用链表

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define read freopen("in.txt", "r", stdin)
const ll  INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int maxn = 1e5+100;
const int mod =  1000000007;
struct trie{
    string tran;
    trie *next[26];
};
trie *root;
void init(){
    root=new trie;
    root->tran[0]=‘*‘;
    for(int i=0;i<26;++i)
        root->next[i]=NULL;
}
void build(string eng,string mar){
    int l=mar.length();
    trie *p=root;
    for(int i=0;i<l;++i){
        int tmp=mar[i]-‘a‘;
        if(p->next[tmp]==NULL){
            p->next[tmp]=new trie;
            p=p->next[tmp];
            p->tran[0]=‘*‘;
            for(int j=0;j<26;++j)
                p->next[j]=NULL;
        }
        else{
            p=p->next[tmp];
        }
    }
    p->tran=eng;//存对应的英文单词
}
void dele(trie *root){
    for(int i=0;i<26;++i){
        if(root->next[i]!=NULL)
            dele(root->next[i]);
    }
    delete(root);
}
string query(string mar){
    int l=mar.length();
    trie *p=root;
    for(int i=0;i<l;++i){
        int tmp=mar[i]-‘a‘;
        if(p->next[tmp]==NULL)return mar;//没查到
        p=p->next[tmp];
    }
    if(p->tran[0]==‘*‘)return mar;
    return p->tran;
}
int main()
{
    init();
    string  s1,s2;
    while(cin>>s1){
        if(s1=="START")continue;
        if(s1=="END")break;
        cin>>s2;
        build(s1,s2);
    }
    getchar();
    while(getline(cin,s1)){
        if(s1=="START")continue;
        if(s1=="END")break;
        string str1="",str2="";
        for(int i=0;i<s1.length();++i){
            if(s1[i]>=‘a‘&&s1[i]<=‘z‘){
                str1+=s1[i];
            }
            else{
                str2+=query(str1);
                str1="";
                str2+=s1[i];
            }
        }
        cout<<str2<<endl;
    }
   // dele(root);
return 0;
}
时间: 2024-08-10 19:56:34

HDU 1075-What Are You Talking About(Trie)的相关文章

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

HDU 1075 What Are You Talking About (Trie树)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1075 map可以过...我上的字典树,小bug有点尴尬,题目没有明确给出数据范围也是无奈. 贡献了几次RE 一次WA.尴尬.discuss里面有个说注意前缀的到是给了点tip.总体来说不错 代码: 1 #define _CRT_SECURE_NO_WARNINGS 2 #include <functional> 3 #include <algorithm> 4 #include <

HDU 1075 What Are You Talking About (strings)

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

HDU ACM 1247-Hat’s Words-字典树(Trie)

分析:字典树解决,注意节点里面只需要保存该点是否构成一个单词,和匹配类型的题有所区别:另外要注意重读打印.字典树效率高. #include<iostream> using namespace std; struct Tri { bool v; Tri* child[26]; }; Tri* root; void Init() { root->v=false; for(int i=0;i<26;i++) { root->child[i]=NULL; } } void Creat

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_

hdu 1507 Uncle Tom&#39;s Inherited Land*(二分)

Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1853    Accepted Submission(s): 769 Special Judge Problem Description Your old uncle Tom inherited a piece of land fr

HDU 5024 Wang Xifeng&#39;s Little Plot (搜索)

Wang Xifeng's Little Plot Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 157    Accepted Submission(s): 105 Problem Description <Dream of the Red Chamber>(also <The Story of the Stone>)

hdu 4956 Poor Hanamichi BestCoder Round #5(数学题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4956 Poor Hanamichi Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7    Accepted Submission(s): 4 Problem Description Hanamichi is taking part in

HDU 4001 To Miss Our Children Time (动态规划)

To Miss Our Children Time Problem Description Do you remember our children time? When we are children, we are interesting in almost everything around ourselves. A little thing or a simple game will brings us lots of happy time! LLL is a nostalgic boy