写了个三叉的字典树,找了个题测试了一下,POJ 1056过了,但是在3630题那就TLE,原因是用指针写的申请内存和回收内存太废时间了。用数组模拟了一下系统堆,果断过。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
#include<cmath>
#include<string>
using namespace std;
struct Node
{
char ch;
int count;
int lson, rson, subtree;
}Node[1000010];
int flag, top;
typedef int Nodelink;
Nodelink create(char ch)
{
Nodelink p = top++;
Node[p].ch = ch;
Node[p].lson = 0;
Node[p].rson = 0;
Node[p].subtree = 0;
Node[p].count = 0;
return p;
}
void insert(char str[], Nodelink root)
{
int i;
Nodelink p = root;
for(i = 0; i < strlen(str); i++)
{
while(Node[p].ch != str[i])
{
if(str[i] < Node[p].ch)
{
if(!Node[p].lson)Node[p].lson = create(str[i]);
p = Node[p].lson;
}
else
{
if(!Node[p].rson)Node[p].rson = create(str[i]);
p = Node[p].rson;
}
}
if(Node[p].subtree == 0)Node[p].subtree = create('\0');
p = Node[p].subtree;
if(Node[p].count)flag = 0;
}
if(p < top-1)flag = 0;
Node[p].count ++;
}
int main()
{
char str[15];
int st;
scanf("%d", &st);
while(st--)
{
int n;
scanf("%d", &n);
top = 1;
create('\0');
flag = 1;
int i;
for(i = 0; i < n; i++)
{
scanf("%s", str);
if(flag)insert(str, 1);
}
if(flag)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}