首页 > 其他 > 详细

hdu1251

时间:2019-07-26 18:21:02      阅读:95      评论:0      收藏:0      [点我收藏+]

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;
}

 

hdu1251

原文:https://www.cnblogs.com/war1111/p/11251939.html

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