这题的难度和前一篇的裸模版差不多。
题目大意就是看字符串是否是其他字符串的前缀,
其实就是在字典树中查看最后一个节点的count数是否为1.
若为1,则代表该字符串唯一。
若不唯一,则代表该字符串被其他字符串完全包含,不就是前缀么!
水过。。。
#include<iostream>
#include<string>
#include<cstdio>
#define MAX 10
using namespace std;
char s[11111][11];
int allocp;
struct TireNode
{
int nCount;
TireNode *next[MAX];
};
TireNode Memeroy[1111111];
void InitTire( TireNode **root )
{
*root=NULL;
}
TireNode *CreateTire()
{
int i;
TireNode *p=&Memeroy[allocp++];
p->nCount=1;
for( int i=0;i<MAX;i++ )
p->next[i]=NULL;
return p;
}
void InsertTire( TireNode **root,char *s )
{
int i=0,k;
TireNode *p;
if( !(p=*root) )
p=*root=CreateTire();
while( s[i] )
{
k=s[i++]-'0';
if( p->next[k] )
p->next[k]->nCount++;
else
p->next[k]=CreateTire();
p=p->next[k];
}
}
bool SearchTire( TireNode **root,char *s )
{
int i=0,k;
TireNode *p=*root;
int cnt=0;
while( s[i] )
{
k=s[i++]-'0';
cnt=p->next[k]->nCount;
p=p->next[k];
}
if( cnt==1 )
return false;
else
return true;
}
int main()
{
int T;
scanf( "%d",&T );
while( T-- )
{
allocp=0;
TireNode *root;
root=NULL;
int len=0;
scanf( "%d",&len );
for( int i=0;i<len;i++ )
{
scanf( "%s",&s[i] );
InsertTire(&root,s[i]);
}
bool found=true;
for( int i=0;i<len;i++ )
{
if( SearchTire(&root,s[i]) )
{
found=false;
break;
}
}
if( found==false )
printf( "NO\n" );
else
printf( "YES\n" );
}
return 0;
}