您的位置:首页 > 其它

hihocoder 1014 Trie树

2016-09-23 21:55 344 查看
#include <iostream>

#include <cstdio>

#include<limits>

#include <map>

#include <cstring>

#include <string>

using namespace std;

const int N =100005;

struct tree

{

    int num;

    tree *next[26];

    tree()

    {

        num=0;

        for(int i=0; i<26; i++)

        {

            next[i]=NULL;

        }

    }

} t;

void In(char str[])

{

    tree *p = &t;

    int cnt;

    for(int i = 0; str[i]; i++)

    {

        cnt = str[i]-'a';

        if(p->next[cnt]==NULL)

        {

            p->next[cnt] = new tree();

        }

        p->next[cnt]->num++;

        p=p->next[cnt];

    }

}

int Find(char str[])

{

    tree *p = &t;

    int cnt, i;

    for(i=0; str[i]; i++)

    {

        cnt = str[i] - 'a';

        if(p->next[cnt]==NULL)

        {

            return 0;

        }

        else

        {

            p = p->next[cnt];

        }

    }

    return p->num;

}

int main()

{

    int n, scan;

    char str
;

    scanf("%d", &scan);

    while(scan--)

    {

        scanf("%s",  str);

        In(str);

    }

    scanf("%d", &n);

    while(n--)

    {

        scanf("%s", str);

        printf("%d\n",  Find(str));

    }

    return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: