hihocoder 1014


Trie树模板题
时隔几个月终于又get了Trie树好理解又简洁的模板

/**/
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

typedef long long LL;
typedef unsigned long long ULL;
using namespace std;

bool Sqrt(LL n) { return (LL)sqrt(n) * sqrt(n) == n; }
const double PI = acos(-1.0), ESP = 1e-10;
const LL INF = 99999999999999;
const int inf = 999999999, N = 4e5 + 24;
int n, m, h, root, len, cnt[N], trie[N][28];
char s[12];

void insert()
{
    root = 0;
    len = strlen(s);
    for(int i = 0; i < len; i++) {
        int id = s[i] - 'a';
        if(!trie[root][id]) trie[root][id] = ++h;
        cnt[trie[root][id]]++; //Don't forget to change the counter.
        root = trie[root][id];
    }
}

void search()
{
    root = 0;
    len = strlen(s);
    for(int i = 0; i < len; i++) {
        int id = s[i] - 'a';
        if(!trie[root][id]) { puts("0"); return ;}
        root = trie[root][id];
    }
    printf("%d\n", cnt[root]);
}

int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    scanf("%d", &n);
    while(n--) {
        scanf("%s", s);
        insert();
    }
    scanf("%d", &m);
    while(m--) {
        scanf("%s", s);
        search();
    }

    return 0;
}
/*
    input:
    output:
    modeling:
    methods:
    complexity:
    summary:
*/

你可能感兴趣的:(hihocoder 1014)