比赛 20120302 评测结果 AWWWWWWWWA
题目名称 有道搜索框 最终得分 20
用户昵称 苏轼 运行时间 0.000 s
代码语言 C++ 内存使用 0.00 MiB
提交时间 2012-03-02 21:26:32
显示代码纯文本
#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'];
			}
		}
		cout<<endl;
	}
	return 0;
}
void dfs(hehe *x)
{
	if (x==NULL)
		return;
	if (x->flag)
	{
		number++;
		for (int i=0;i<lw;i++)
		{
			cout<<c[i];
		}
		cout<<' '; 
	}
	if (number==8)
		return;
	for (int i=0;i<26;i++)
	{
		if (x->next[i]!=NULL)
		{
			c[lw]=f[i];
			lw++;
			dfs(x->next[i]);
			lw--;
		}
	}
}