trie
View Code
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
#define maxl 15
#define maxn 10005
struct Node
{
Node *next[10];
bool have;
bool end;
} trie[maxn * maxl];
int n;
char st[maxl];
int ncount;
bool ins(Node *proot, char *st)
{
if (proot->end)
return false;
if (st[0] == '\0')
{
proot->end = true;
return !proot->have;
}
if (!proot->next[st[0] - '0'])
{
proot->next[st[0] - '0'] = trie + ncount;
ncount++;
proot->have = true;
ins(proot->next[st[0] - '0'], st + 1);
return true;
}
return ins(proot->next[st[0] - '0'], st + 1);
}
void finish(int a)
{
int b = n - a - 1;
for (int i = 0; i < b; i++)
scanf("%s", st);
}
bool work()
{
ncount = 1;
memset(trie, 0, sizeof(trie));
for (int i = 0; i < n; i++)
{
scanf("%s", st);
if (!ins(trie, st))
{
finish(i);
return false;
}
}
return true;
}
int main()
{
//freopen("t.txt", "r", stdin);
int t;
scanf("%d", &t);
while (t--)
{
scanf("%d", &n);
if (work())
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
#define maxl 15
#define maxn 10005
struct Node
{
Node *next[10];
bool have;
bool end;
} trie[maxn * maxl];
int n;
char st[maxl];
int ncount;
bool ins(Node *proot, char *st)
{
if (proot->end)
return false;
if (st[0] == '\0')
{
proot->end = true;
return !proot->have;
}
if (!proot->next[st[0] - '0'])
{
proot->next[st[0] - '0'] = trie + ncount;
ncount++;
proot->have = true;
ins(proot->next[st[0] - '0'], st + 1);
return true;
}
return ins(proot->next[st[0] - '0'], st + 1);
}
void finish(int a)
{
int b = n - a - 1;
for (int i = 0; i < b; i++)
scanf("%s", st);
}
bool work()
{
ncount = 1;
memset(trie, 0, sizeof(trie));
for (int i = 0; i < n; i++)
{
scanf("%s", st);
if (!ins(trie, st))
{
finish(i);
return false;
}
}
return true;
}
int main()
{
//freopen("t.txt", "r", stdin);
int t;
scanf("%d", &t);
while (t--)
{
scanf("%d", &n);
if (work())
printf("YES\n");
else
printf("NO\n");
}
return 0;
}