zoukankan      html  css  js  c++  java
  • HDU 1251 Trie树模板题

    1、HDU 1251 统计难题  Trie树模板题,或者map

    2、总结:用C++过了,G++就爆内存。。

    题意:查找给定前缀的单词数量。

    #include<iostream>
    #include<cstring>
    #include<cmath>
    #include<queue>
    #include<algorithm>
    #include<cstdio>
    #define max(a,b) a>b?a:b
    #define F(i,a,b) for(int i=a;i<=b;i++)
    #define mes(a,b) memset(a,b,sizeof(a))
    #define INF 0x3f3f3f3f
    #define LL long long
    using namespace std;
    const int N=10010,MAX=1000100;
    
    struct Node
    {
        int count;
        Node *child[26];
        Node(){
            mes(child,NULL);
            count=0;
        }
    };
    
    Node *root=new Node,*current;
    void insert(char *str)
    {
        current=root;
        for(int i=0;str[i];++i){
            int m=str[i]-'a';
            if(current->child[m]==NULL){
                current->child[m]=new Node;
            }
            current=current->child[m];
            current->count++;
        }
    }
    
    int search(char *str)
    {
        current=root;
        for(int i=0;str[i];++i){
            int m=str[i]-'a';
            if(current->child[m]==NULL)
                return 0;
            current=current->child[m];
        }
        return current->count;
    }
    
    int main()
    {
        char str[20];
        while(gets(str),*str)
            insert(str);
        while(gets(str))
            printf("%d
    ",search(str));
    
        return 0;
    }
    View Code
  • 相关阅读:
    LeetCode-5. Longest Palindromic Substring(M)
    Python if else简洁写法,列表推导式,三目运算符写法
    Java GC机制
    int与integer的区别
    Java内存分配机制
    HashMap原理
    哈希表算法
    哈希
    java 三大框架面试题
    Java反射机制
  • 原文地址:https://www.cnblogs.com/sbfhy/p/5877426.html
Copyright © 2011-2022 走看看