码迷,mamicode.com
首页 > 其他好文 > 详细

poj-2503

时间:2014-08-04 17:28:17      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   使用   os   io   for   ar   

嘻嘻,这个是自己的思想打的代码,独一无二,居然过了。首先构造字典树的节点,包括一个root,我把要保存的对应信息,如dog ogdoy中的dog保存在一个数组中,这个数组原本是一个单词结束标志,注意string的使用,我的代码内存快爆掉了,刚刚过的。

#include<cstdio>
#include<iostream>
#include<queue>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
char str[50];
char str1[11];
char str2[11];
string temp;
const int maxn = 1000100;
struct trie
{
    int ch[maxn][26],sz,root;
    string inf[maxn];
    int newnode(){
        inf[sz] = "eh";
        memset(ch[sz],0,sizeof(ch[sz]));
        return sz++;
    }
    void init(){
        sz = 0;
        root = newnode();
    }
void insert(char *str1,char* str2){
    int length = strlen(str1);
    int now = root;
    for(int i = 0;i < length;i++){
        int &temp = ch[now][str1[i] - a];
        if(!temp){
            temp = newnode();
        }
        now = temp;
    }
    inf[now] = str2;
}

void query(char *str)
{
    int len = strlen(str);
    int now = root;
    for(int i = 0;i < len;i++){
        now = ch[now][str[i]-a];
        int tmp = now;
        if(!tmp)
        {
            temp = "eh";
            return;
        }
       else
       {
           now = tmp;
       }
    }
    temp = inf[now];
}
}tr;

int main(){
    tr.init();
  while(gets(str)&&str[0]){
        sscanf(str,"%s%s",str2,str1);
        tr.insert(str1,str2);
    }

    while(scanf("%s",str) != EOF){
        tr.query(str);
       cout << temp << endl;
    }
    return 0;
}

 

poj-2503,布布扣,bubuko.com

poj-2503

标签:style   blog   color   使用   os   io   for   ar   

原文地址:http://www.cnblogs.com/hhhhhhhhhhhhhhhhhhhhhhhhhhh/p/3890395.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!