hdu1251

hdu1251

trie模板题,用g++会MLE,用c++就好了

#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<set>
#include<map>
#include<stack>
#include<cstring>
#pragma GCC optimize(2)
#define inf 2147483647
#define ls rt<<1
#define rs rt<<1|1
#define lson ls,nl,mid,l,r
#define rson rs,mid+1,nr,l,r
#define N 1000010
#define For(i,a,b) for(int i=a;i<=b;++i)
#define p(a) putchar(a)
#define g() getchar()

using namespace std;
char c[20];
int t;
struct node{
    int cnt;
    node *next[26];
};
node *root=new node();
node *p;
void in(int &x){
    int y=1;char c=g();x=0;
    while(c<'0'||c>'9'){if(c=='-')y=-1;c=g();}
    while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=g();}
    x*=y;
}
void o(int x){
    if(x<0){p('-');x=-x;}
    if(x>9)o(x/10);
    p(x%10+'0');
}

void insert(){
    p=root;
    int l=strlen(c);
    For(i,0,l-1){
        t=c[i]-'a';
        if(p->next[t]==0)
            p->next[t]=new node();
        p=p->next[t];
        p->cnt++;
    }
}

int find(){
    p=root;
    int l=strlen(c);
    For(i,0,l-1){
        t=c[i]-'a';
        if(p->next[t]==0)
            return 0;
        p=p->next[t];
    }
    return p->cnt;
}

int main(){
    while(gets(c)&&c[0])
        insert();
    while(scanf("%s",c)!=EOF)
        o(find()),p('\n');
    return 0;
}

 

posted @ 2019-07-26 17:49  WeiAR  阅读(93)  评论(0编辑  收藏  举报