[POJ3630]Phone List (Tire)

题意

trie字典树模板

LOJ有中文翻译https://loj.ac/problem/10049

思路

TIRE

代码

之前在LOJ上做过

直接交了

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<map>
#include<queue>
#include<stack>
#include<cstring>
#include<string>
using namespace std;
#define N 100000+5
int T,n,tot;
int ch[N][15];
bool bo[N];
char s[15];
bool insert(char *s)
{
    int len = strlen(s);
    int u = 1;
    bool fl = false;
    for (int i = 0; i < len; i++)
    {
        int c = s[i] - 48;
        if (!ch[u][c]) {
            ch[u][c] = ++tot;
        }
        else {
            if (i == len-1)
            {
                fl = true;
            }
        }
        u = ch[u][c];
        if (bo[u]) fl = true;
    }
    bo[u] = true;
    return fl;
}
int main()
{
    scanf("%d", &T);
    while (T--)
    {
        scanf("%d",&n);
        tot = 1;
        memset(ch, 0, sizeof(ch));
        memset(bo, false, sizeof(bo));
        bool ans = false;
        for (int i = 1; i <= n; i++)
        {
            scanf("%s", s);
            if (insert(s)) ans = true;
        }
        if (!ans) puts("YES");
        else puts("NO");
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/lincold/p/10228914.html