|
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include <memory.h>
using namespace std;
struct trieNode
{
int next[26];
bool isWord;
char word[22];
trieNode()
{
memset(next,-1,sizeof(next));
isWord=false;
}
};
int N=200000;
trieNode tt[200000];
int len;
bool first;
int num;
void insert(char * tar,char * w)
{
trieNode * p= &tt[0];
int id ;
while(*tar)
{
id=*tar-'a';
if(p->next[id]==-1)
{
p->next[id]=len;
len++;
}
p=&tt[p->next[id]];
tar++;
}
p->isWord=true;
strcpy(p->word,w);
}
void DFS(int n)
{
if(num >= 8)
return ;
int i;
trieNode * p= &tt[n];
if(p->isWord==true)
{
if(first)
{
printf("%s",p->word);
first=false;
num++;
}
else
{
printf(" %s",p->word);
num++;
}
}
for(i=0;i<26;++i)
{
if(p->next[i]!=-1)
{
DFS(p->next[i]);
}
}
}
bool search(char *tar)
{
trieNode * p= &tt[0];
int id;
int n;
while (* tar)
{
id=*tar -'a';
if(p->next[id]==-1)
return false;
n=p->next[id];
p=&tt[p->next[id]];
tar++;
}
DFS(n);
return true;
}
int main()
{
int n,q;
int i;
char str[25];
bool b;
len=1;
scanf("%d",&n);
for(i=0;i<n;++i)
{
scanf("%s",&str);
insert(str,str);
}
scanf("%d",&q);
for(i=0;i<q;++i)
{
b=true;
first=true;
num=0;
scanf("%s",&str);
b=search(str);
if(b==false)
printf("%s\n",str);
else
printf("\n");
}
return 0;
}
|