码迷,mamicode.com
首页 > 其他好文 > 详细

字典树acmhdu1075

时间:2015-12-22 10:20:05      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:

/*

直接用字典树,输入输出的格式和重要。坑死了。

直接用scanf也可以,但我用自定义的输入会快一些。

欢迎交流1370632684@qq.com

*/

#include<stdio.h>
#include<string.h>
const int maxx=15;
const int maxx1=5000;
/*
单词查找需要用字典树处理,直接循环查找会超时。
*/
//
const int sonnum=30,base=‘a‘;
struct Trie
{
bool terminal;
struct Trie *son[sonnum];
char translate[maxx];
};
Trie *NewTrie()
{
int i;
Trie *temp=new Trie;
temp->terminal=false;
for(i=0;i<sonnum;i++)
temp->son[i]=NULL;
return temp;
}
void Insert(Trie *pnt,char*s,char *s1,int len)
{
int i;
Trie *temp=pnt;
for(i=0;i<len;i++)
{
if(temp->son[s[i]-base]==NULL)
{
temp->son[s[i]-base]=new Trie();
temp->son[s[i]-base]->terminal=false;
}
temp=temp->son[s[i]-base];
}
temp->terminal=true;
//copy;
strcpy(temp->translate,s1);

}
Trie* Find(Trie*pnt,char*s,int len)
{
int i;
Trie *temp=pnt;
for(i=0;i<len;i++)
{
if(temp->son[s[i]-base]!=NULL)temp=temp->son[s[i]-base];
else
return NULL;
}
if(temp->terminal==true)
return temp;
else
return NULL;
}
//
struct dictionary
{
char english[maxx];
char mar[maxx];
}dic;

char each_line[maxx1];
char change[maxx];
void changegets(char x[maxx1])
{
char ch;
int i;
i=0;
while((ch=getchar())!=‘\n‘)
{
x[i++]=ch;
}
x[i]=0;
}
void input(char x[maxx])
{
int i;
i=0;
char ch = getchar();
while (ch<‘a‘&&ch>‘z‘)
ch = getchar();
while (ch >= ‘a‘ && ch <= ‘z‘||(ch==‘E‘||ch==‘N‘||ch==‘D‘))
{
x[i++]=ch;
ch = getchar();
}
x[i]=0;
}

int main()
{
int i,j;
int len;
char getUselessMessage[maxx];
gets(getUselessMessage);
Trie *p;
p=NewTrie();
Trie *preturn;
while(true)
{
//scanf("%s",dic.english);
input(dic.english);
if(dic.english[0]==‘E‘&&dic.english[1]==‘N‘&&dic.english[2]==‘D‘)
break;
//scanf("%s",dic.mar);
input(dic.mar);
len=strlen(dic.mar);
Insert(p,dic.mar,dic.english,len);
}
//字典存储;
scanf("%s",getUselessMessage);
getchar();
while(true)
{
gets(each_line);
if(each_line[0]==‘E‘&&each_line[1]==‘N‘&&each_line[2]==‘D‘)
break;
i=0;
while(each_line[i]!=0)
{
if(each_line[i]<=‘z‘&&each_line[i]>=‘a‘)
{
j=0;
while(each_line[i]<=‘z‘&&each_line[i]>=‘a‘)
{
change[j++]=each_line[i++];
}
change[j]=0;
preturn=Find(p,change,j);
if(preturn==NULL)
printf("%s",change);
else
printf("%s",preturn->translate);
}
else
printf("%c",each_line[i++]);
}
printf("\n");
}
//逐行翻译;
return 0;
}

字典树acmhdu1075

标签:

原文地址:http://www.cnblogs.com/onlybetter/p/5065616.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!