达成成就:手撕字典树
看代码知题意,水题
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>
#include <stack>
#define INF 0x3f3f3f3f
#define IMAX 2147483646
#define LINF 0x3f3f3f3f3f3f3f3f
#define ll long long
#define ull unsigned long long
#define uint unsigned int
using namespace std;
struct T {
int ch[11], val, end;
}t[111111];
int tot,len,now;
bool insert(char* num) {
len = strlen(num);
now = 0;
for (int i = 0; i < len; i++) {
if (t[now].ch[num[i] - '0'] == -1) {
t[now].ch[num[i] - '0'] = tot++;
t[tot - 1].val = num[i] - '0';
for (int i = 0; i < 10; i++)
t[tot - 1].ch[i] = -1;
t[tot - 1].end = 0;
}
now = t[now].ch[num[i] - '0'];
if (t[now].end == 1 && i != len - 1)
return false;
if (i == len - 1) {
t[now].end = 1;
for (int i = 0; i < 10; i++)
if (t[now].ch[i] != -1)
return false;
}
}
return true;
}
int tt,n,flag;
char num[15];
int main() {
scanf("%d", &tt);
while (tt--) {
for (int i = 0; i < 10; i++)
t[0].ch[i] = -1;
t[0].end = 0;
tot = 1;
scanf("%d", &n);
flag = 1;
for (int i = 0; i < n; i++) {
memset(num, 0, sizeof(num));
scanf("%s", num);
if (!insert(num)) flag = 0;
}
printf("%s\n", flag ? "YES" : "NO");
}
return 0;
}