首页 > 其他 > 详细

HDU 1251 统计难题

时间:2014-02-22 00:39:43      阅读:335      评论:0      收藏:0      [点我收藏+]

字典树~

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <cstdio>
#include <cstring>
using namespace std;
int cnt,n;
char s[12];
struct Node{int sum; int son[26];}trie[500000];
void insert(char *s){
    for(int l=strlen(s),x=0,i=0;i<l;i++){
        if(!trie[x].son[s[i]-‘a‘])trie[x].son[s[i]-‘a‘]=++cnt;
        x=trie[x].son[s[i]-‘a‘];
        trie[x].sum++;
    }
}
int find(char *s){
    for(int l=strlen(s),x=0,i=0;i<l;i++){
        if(!trie[x].son[s[i]-‘a‘])return 0;
        x=trie[x].son[s[i]-‘a‘];
        if(i==l-1)return trie[x].sum;
    }
}
int main(){
    while(gets(s)){
        if(strlen(s)==0)break;
        insert(s);
    }
    while(gets(s))printf("%d\n",find(s));
    return 0;
}

HDU 1251 统计难题

原文:http://www.cnblogs.com/forever97/p/3559382.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!