HDOJ 1671 Phone List(静态表trie树入门)

#include <iostream>

using namespace std;



struct node {

    bool isword;

    int child[10];

} trie[100010] ;



int size = 0;

bool flag = false;



void Insert(char* word, int i, int rt)

{

    if (!word[i])

    {

        trie[rt].isword = true;

        return ;

    }



    int m = word[i] - '0';

    if (trie[rt].child[m])

    {

        if (trie[trie[rt].child[m]].isword || !word[i+1])

            flag = true;

        Insert(word, i + 1, trie[rt].child[m]);

    }

    else

    {

        trie[rt].child[m] = ++size;

        Insert(word, i + 1, size);

    }

}

int main()

{

    int cases;

    scanf("%d", &cases);

    while (cases--)

    {

        size = 0;

        flag = false;

        memset(trie, 0, sizeof(trie));



        int n;

        char word[12];

        scanf("%d", &n);

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

        {

            scanf("%s", word);

            Insert(word, 0, 0);

        }

        if (flag)

            printf("NO\n");

        else

            printf("YES\n");

    }

    return 0;

}

你可能感兴趣的:(list)