poj 2503 Trie树

  典型的Trie树, 算是复习一下字符串吧, 就是输入有点恶心,代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
const int maxn = 500000+100;
struct Trie{
    bool isword;
    int next[26];
    char words[15];
    Trie(){
        memset(next, -1, sizeof(next));
        isword = false;
    }
}Root;
Trie node[maxn];
int n;

void Insert(char *tar){
    Trie *head = &Root;
    char *s = tar;
    while(*tar!=‘ ‘) tar++;
    *tar=‘\0‘;
    tar++;
    while(*tar!=‘\0‘){
        int id = *tar - ‘a‘;
        if(head->next[id] == -1){
            head->next[id] = n++;
        }
        head = &node[head->next[id]];
        tar++;
    }
    head->isword = true;
    //printf("s = %s\n", s);
    strcpy(head->words, s);
}

char *Search(char *tar)
{
    Trie *head = &Root;
    while(*tar){
        int id = *tar - ‘a‘;
        if(head->next[id] == -1) return NULL;
        head = &node[head->next[id]];
        tar++;
    }
    if(head->isword) return head->words;
    else return NULL;
}

char str[100];

int main(){
    n = 0;
    while(gets(str)){
        bool kongge = false;
        int len = strlen(str);
        for(int i=0; i<len; i++) {
            if(str[i] == ‘ ‘){
                    kongge = true;
                    break;
            }
        }
        if(str[0]<‘a‘||str[0]>‘z‘) continue;
        if(kongge) Insert(str);
        else {
            char *p = Search(str);
            if(p == NULL) printf("eh\n");
            else printf("%s\n", p);
        }
    }
    return 0;
}
时间: 2024-08-04 20:52:15

poj 2503 Trie树的相关文章

POJ 2503 字典树

这题记得以前是我们周赛的题,然后用的是map,也暴过了. 因为这两天要给大一的讲字典树,所以练练几道的代码,以防给大一搞晕了-- #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<map> #include<queue> #include<set> #include<cmath> #include

poj 1056 Trie树判断哈夫曼编码是否合法

理解了Trie树然后就能1A   其实估计这个题随便做做就能A掉,可能不需要高级数据. 先贴吉林大学的代码模板 /*==================================================*| Trie树(k叉) | INIT: init(); | 注: tree[i][tk]>0时表示单词存在, 当然也可赋予它更多含义; \*==================================================*/ const int tk = 26,

poj 2945 trie树统计字符串出现次数

用记录附加信息的val数组记录次数即可. PS:trie树还有种动态写法,使用指针和动态分配内存代替了连续的ch数组,更加节省内存. Reference:http://blog.csdn.net/architect19/article/details/8966247 1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 using namespace std; 5 #define maxnode

POJ 2503 Trie

链接: http://poj.org/problem?id=2503 题意: 给定一些字符串以及它在外星语言中的对应翻译,现在有若外星语言中的串,要把它们翻译成英语 题解: 这道题map,hash,trie都是可以做的 然而我用g++提交发现map和trie都超时了,换成c++就全都过了 map用了1141ms,trie只要485ms 代码: 31 vector<string> word; 32 int pi =1; 33 34 struct Node { 35 int next[26]; 3

POJ 2503 Babelfish (Trie树 或 map)

Babelfish Time Limit: 3000MS        Memory Limit: 65536K Total Submissions: 34278        Accepted: 14706 Description You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately

poj 2503 Babelfish (map,trie 树)

链接:poj 2503 题意:输入 语言A及翻译为语言B的词典,之后再输入语言B的单词,判断是否能从词典中找到, 若能找到,将其翻译为语言A,否则输出"eh". 思路:这题肯定得先将词典对应语言存起来,但是如果直接暴力找输入的单词是否出现过,必然会TLE 因为单词都是一对一的关系,可以用map实现 当然,trie树是用空间换时间,对于字符串的查找,在时间上有着相当的优势,因此也可以用trie树 注:sscanf函数,从一个字符串中读进与指定格式相符的数据. map实现:938MS #i

POJ 2513 Colored Sticks (Trie树+并查集+欧拉路)

Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 31490   Accepted: 8320 Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some color. Is it possible to align the sticks in a st

字典树模板题 POJ 2503

1 #include <cstdio> 2 #include <cstring> 3 4 char en[11],fr[11]; 5 int st; 6 struct Tire{ 7 int next[26]; 8 char eng[11]; 9 }node[200005]; 10 void insert(char *s,int cur) 11 { 12 if(*s){ 13 if(!node[cur].next[*s-'a']) 14 node[cur].next[*s-'a']

poj 2503:Babelfish(字典树,经典题,字典翻译)

Babelfish Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 30816   Accepted: 13283 Description You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have