比赛 |
20120302 |
评测结果 |
AAAAAAAAAA |
题目名称 |
有道搜索框 |
最终得分 |
100 |
用户昵称 |
Makazeu |
运行时间 |
0.000 s |
代码语言 |
C++ |
内存使用 |
0.00 MiB |
提交时间 |
2012-03-02 18:27:22 |
显示代码纯文本
- #include <cstdio>
- #include <cstdlib>
- #include <cstring>
- #include <iostream>
- #define readfile(str) freopen(str,"r",stdin)
- #define writefile(str) freopen(str,"w",stdout)
- using namespace std;
-
- const int base='a';
- const int SonNum=26;
- const int MAXN=10001;
- char Word[MAXN][21];
- class Trie
- {
- public:
- bool isStr;
- int pos;
- class Trie *Son[SonNum];
- };
-
- Trie *Root;
- int Top=0;
- int P=0;
- int N,M;
-
- Trie *NewTrie()
- {
- Trie *temp=new Trie;
- temp->isStr=false;
- temp->pos=0;
- for(int i=0;i<SonNum;i++)
- temp->Son[i]=NULL;
- return temp;
- }
-
- void Insert(Trie *pnt,char *s,int len)
- {
- Trie *temp=pnt;
- for(int i=0;i<len;i++)
- {
- if(temp->Son[s[i]-base]==NULL)
- temp->Son[s[i]-base]=NewTrie();
- temp=temp->Son[s[i]-base];
- }
- temp->isStr=true;
- temp->pos=Top;
- }
-
- void dfs(Trie *pnt)
- {
- Trie *temp=pnt;
- if(temp->isStr)
- {
- if(P==0)
- printf("%s",Word[temp->pos]);
- else
- printf(" %s",Word[temp->pos]);
- P++;
- }
- if(P==8)
- return;
- for(int i=0;i<SonNum;i++)
- {
- if(P==8)
- return;
- if(temp->Son[i]!=NULL)
- dfs(temp->Son[i]);
- }
- }
-
- void Search(Trie *pnt,char *s,int len)
- {
- Trie *temp=pnt;
- for(int i=0;i<len;i++)
- {
- if(temp->Son[s[i]-base]==NULL)
- {
- printf("%s",s);
- return;
- }
- temp=temp->Son[s[i]-base];
- }
-
- P=0;
- dfs(temp);
- }
-
- bool check(Trie *pnt,char *s,int len)
- {
- Trie *temp=pnt;
- for (int i=0;i<len;i++)
- {
- if(temp->Son[s[i]-base]==NULL)
- return false;
- temp=temp->Son[s[i]-base];
- }
- return temp->isStr;
- }
-
- void init()
- {
- Root=NewTrie();
- char str[20];
- int len;
- scanf("%d\n",&N);
- for(int i=1;i<=N;i++)
- {
- memset(str,'\0',sizeof(str));
- scanf("%s\n",str);
- len=strlen(str);
- if(check(Root,str,len))
- continue;
- Top++;
- for(int i=0;i<len;i++)
- Word[Top][i]=str[i];
- Insert(Root,str,len);
- }
- scanf("%d\n",&M);
- for(int i=1;i<=M;i++)
- {
- memset(str,'\0',sizeof(str));
- scanf("%s\n",str);
- Search(Root,str,strlen(str));
- if(i!=M)
- printf("\n");
- }
- }
-
- int main()
- {
- readfile("youdao.in"),writefile("youdao.out");
- init();
- return 0;
- }