Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 43623 | Accepted: 18403 |
Description
Input
Output
Sample Input
dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay
atcay
ittenkay
oopslay
Sample Output
cat
eh
loops
Hint
-
字典樹
- 基本操作
- 查找、插入和刪除
性質
基本操作
題意:前面一組數據是英問和火星文對應的譯碼錶 接着輸入的是火星文,
要求根據譯碼錶吧火星文翻譯成英文,如果找不到則輸出 eh
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Node
{
char low[11];
Node *next[26];
};
Node *root,*p,*q;
void Init(Node *root)
{
root->low[0]='#';
for(int i=0;i<26;i++)
root->next[i]=NULL;
}
void BuildTire(char *s2,char *s1)
{
int i,v;
for(i=0,p=root;i<strlen(s2);i++)
{
v=s2[i]-'a';
if(p->next[v]==NULL)
{
q=(struct Node*)malloc(sizeof(Node));
Init(q);
p->next[v]=q;
}
p=p->next[v];
}
strcpy(p->low,s1);
}
Node *Insearch(char *str)
{
int i,v;
for(i=0,p=root;i<strlen(str);i++)
{
v=str[i]-'a';
if(p->next[v]==NULL)
break;
p=p->next[v];
}
return p;
}
int main()
{
char str[23],s1[11],s2[11];
root=(struct Node *)malloc(sizeof(Node));
Init(root);
while(gets(str) && str[0]!='\0')//以回車結束
{
sscanf(str,"%s%s",s1,s2);//格式話字符串
BuildTire(s2,s1);
}
while(gets(str) && str[0]!='\0')
{
Node *r=Insearch(str);
if(r->low[0]!='#')
printf("%s\n",p->low);
else
printf("eh\n");
}
return 0;
}