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

hdu1251 字典树

时间:2015-07-31 16:14:02      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <stack>
#include <set>
#include <map>
#include <vector>

using namespace std;
#define INF 0x2fffffff
#define LL long long
#define MAX(a,b) ((a)>(b))?(a):(b)
#define MIN(a,b) ((a)<(b))?(a):(b)
#define  MAXNODE 1000000
#define id(a) (a-‘a‘)
struct trie{
    int ch[MAXNODE][26];
    int val[MAXNODE];
    int sz;
    int insert(char *s){
        int n = strlen(s);
        int u = 0;
        for(int i = 0;i < n;i++){
            int c = id(s[i]);
            if(!ch[u][c]){
                ch[u][c] = sz++; 
            }
            u = ch[u][c];
            val[u]++;
        }

    }
    int query(char *s){
        int n = strlen(s);
        int u = 0;
        for(int i = 0;i < n;i++){
            int c = id(s[i]);
            if(!ch[u][c]){
                return 0;
            }
            u = ch[u][c];
        }
        return val[u];
    }
};
trie a;
int main(){
    char s[105];
    memset(a.ch[0],0,sizeof(a.ch[0]));
    memset(a.val,0,sizeof(a.val));
    a.sz = 1;
    while(gets(s),s[0]!=‘\0‘){
        a.insert(s);
    }    
    while(scanf("%s",s)!=EOF){
        cout << a.query(s) << endl;
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

hdu1251 字典树

标签:

原文地址:http://blog.csdn.net/qq_24667639/article/details/47171143

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