zoukankan      html  css  js  c++  java
  • poj-2503

    嘻嘻,这个是自己的思想打的代码,独一无二,居然过了。首先构造字典树的节点,包括一个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;
    }
  • 相关阅读:
    TransmitFile
    xml
    鼠标划过表格行变色-简洁实现
    关于表变量
    显式接口成员实现
    华为致新员工书
    C#实现的堆栈
    Gridview中合并单元格,某字段的内容相同时如何只显示一个,屏蔽相同列或行的内容(转)
    ASP.NET 验证控件
    动态SQL EXEC
  • 原文地址:https://www.cnblogs.com/hhhhhhhhhhhhhhhhhhhhhhhhhhh/p/3890395.html
Copyright © 2011-2022 走看看