| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
字典树1A,贴个代码,不喜勿喷== 输入#include <cstdio>
#include <cstring>
struct Node
{
int next[26];
char str[12];
} dic[200000];
int index;
void insert(char* s,char* t)
{
int len=strlen(s);
int now=0;
for(int i=0;i<len;i++)
{
int next=s[i]-'a';
if(dic[now].next[next]==0)
dic[now].next[next]=++index;
now=dic[now].next[next];
}
memcpy(dic[now].str,t,strlen(t));
}
void query(char* s)
{
int len=strlen(s);
int now=0;
for(int i=0;i<len;i++)
{
int next=s[i]-'a';
if(dic[now].next[next]==0)
{
printf("eh\n");
return;
}
now=dic[now].next[next];
}
printf("%s\n",dic[now].str);
}
int main()
{
// freopen("in.txt","r",stdin);
char str[30];
while(gets(str),str[0])
{
int pos=0;
while(str[pos++]!=' ');
str[pos-1]=0;
insert(str+pos,str);
}
while(gets(str))
query(str);
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator