记录编号 |
340504 |
评测结果 |
AAAAAAAAAA |
题目名称 |
[Youdao2010] 有道搜索框 |
最终得分 |
100 |
用户昵称 |
ONCE AGAIN |
是否通过 |
通过 |
代码语言 |
C++ |
运行时间 |
3.524 s |
提交时间 |
2016-11-06 19:18:02 |
内存使用 |
107.10 MiB |
显示代码纯文本
#include<stdio.h>
#include<cstring>
#define Turn(x) (x-'a'+1)
using namespace std;
struct Trie_Tree{
int ch[1000000][27],tot;
int word[1000000],isfind,len;
char s[50];
Trie_Tree(){memset(ch,0,sizeof(ch));memset(word,0,sizeof(word));tot = 0;}
inline void insert(char *s,int len){
int now = 0;
for(int i = 1;i<=len;++i){
if(!ch[now][Turn(s[i])])
ch[now][Turn(s[i])] = ++tot;
now = ch[now][Turn(s[i])];
}
++word[now];
}
inline void Print(){for(int i = 1;s[i];++i)printf("%c",s[i]);printf(" ");}
void find(int x,int now){
if(!x||isfind == 8)return;
if(now+1<=len)find(ch[x][Turn(s[now+1])],now+1);
else{
if(word[x]&&isfind<8)Print(),isfind++;
if(isfind == 8)return;
for(int j = 1;j<=26;++j){
if(!ch[x][j])continue;
s[now+1] = j+'a'-1;
find(ch[x][j],now+1);
s[now+1] = '\0';
}
}
}
bool find(char *tt,int l){
if(!ch[0][Turn(tt[1])])return false;
memcpy(s,tt,sizeof(s));len = l;isfind = 0;
find(ch[0][Turn(s[1])],1);
return isfind;
}
}t;
char ch[50] = "\0";int N,M;
int main(){
freopen("youdao.in","r",stdin);
freopen("youdao.out","w",stdout);
scanf("%d",&N);
for(int i = 1;i<=N;i++){
scanf("%s",ch+1);
t.insert(ch,strlen(ch+1));
}
scanf("%d",&M);
for(int i = 1;i<=M;i++){
memset(ch,0,sizeof ch);
scanf("%s",ch+1);
if(!t.find(ch,strlen(ch+1)))puts(ch+1);
else printf("\n");
}
return 0;
}