题意:求这些电话号码是否存在一个是另一个的前缀,字典树的建立,不然数据太大
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN = 10010;
const int N = 11;
int n,t,cnt;
char number[MAXN][N];
struct Node{
bool flag;
struct Node *child[N];
}node[MAXN*N];
Node *root;
Node* newNode(){
node[cnt].flag = false;
for (int i = 0; i < N; i++)
node[cnt].child[i] = NULL;
return &node[cnt++];
}
bool insert(char *str){
Node *s = root;
int len = strlen(str);
for (int i = 0; i < len; i++){
int num = str[i] - '0';
if (s->child[num] == NULL)
s->child[num] = newNode();
s = s->child[num];
if (s->flag == true)
return false;
}
for (int i = 0; i < N; i++)
if (s->child[i]) //如果他的下一个节点标记了,代表他是别人的前缀
return false;
s->flag = true;
return true;
}
int main(){
scanf("%d",&t);
while (t--){
int flag = 1;
cnt = 0;
root = newNode();
scanf("%d",&n);
for (int i = 0; i < n; i++){
scanf("%s",number[i]);
if (flag)
if (!insert(number[i]))
flag = 0;
}
if (flag)
printf("YES\n");
else printf("NO\n");
}
return 0;
}