记录编号 35838 评测结果 AAAAAAAAAA
题目名称 [Youdao2010] 有道搜索框 最终得分 100
用户昵称 Gravatar苏轼 是否通过 通过
代码语言 C++ 运行时间 0.354 s
提交时间 2012-03-04 18:01:38 内存使用 0.27 MiB
显示代码纯文本
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
struct hehe
{
	bool flag;
	hehe *next[26];
};
int n;
char c[21];
int lw,number=0;
hehe *root=new hehe;
char f[26]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
void dfs(hehe *x);
int main()
{
	freopen ("youdao.in","r",stdin);
	freopen ("youdao.out","w",stdout);
	cin>>n;
	root->flag=false;
	for (int i=0;i<26;i++)
	{
		root->flag=NULL;
		root->next[i]=NULL;
	}
	for (int i=0;i<n;i++)
	{
		char s[21];
		int lq;
		cin>>s;
		lq=strlen(s);
		hehe *curr=new hehe;
		curr=root;
		for (int j=0;j<lq;j++)
		{
			if (curr->next[s[j]-'a']==NULL)
			{
				hehe *q=new hehe;
				q->flag=false;
				for (int k=0;k<26;k++)
				{
					q->next[k]=NULL;
				}
				curr->next[s[j]-'a']=q;
				q=NULL;
				delete q;
			}
			curr=curr->next[s[j]-'a'];
			if (j==lq-1)
				curr->flag=true;
		}
		curr=NULL;
		delete curr;
	}
	cin>>n;
	for (int i=0;i<n;i++)
	{
		number=0;
		cin>>c;
		hehe *r=new hehe;
		r=root;
		lw=strlen(c);
		for (int j=0;j<lw;j++)
		{
			if (j==lw-1)
			{
				if (r->next[c[j]-'a']==NULL)
				{
					cout<<c;
					break;
				}
				r=r->next[c[j]-'a'];
				dfs(r);
			}
			else
			{
				if (r->next[c[j]-'a']==NULL)
				{
					cout<<c<<' ';
					break;
				}
				r=r->next[c[j]-'a'];
			}
		}
	}
	return 0;
}
void dfs(hehe *x)
{
	if (number==8)
		return;
	if (x==NULL)
		return;
	if (number==8)
		return;
	if (x->flag)
	{
		number++;
		for (int i=0;i<lw;i++)
		{
			cout<<c[i];
		}
		cout<<' '; 
	}
	for (int i=0;i<26;i++)
	{
		if (x->next[i]!=NULL)
		{
			c[lw]=f[i];
			lw++;
			dfs(x->next[i]);
			lw--;
			if (number==8)
				return;
		}
	}
}