hdu 1075 What Are You Talking About【字典树】
2016-02-08 15:55
453 查看
What Are You Talking About
[b]Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/204800 K (Java/Others)Total Submission(s): 18471 Accepted Submission(s): 6036
[/b]
[align=left]Problem Description[/align]
Ignatius is so lucky that he met a Martian yesterday. But he didn't know the language the Martians use. The Martian gives him a history book of Mars and a dictionary when it leaves. Now Ignatius want to translate the history book
into English. Can you help him?
[align=left]Input[/align]
The problem has only one test case, the test case consists of two parts, the dictionary part and the book part. The dictionary part starts with a single line contains a string "START", this string should be ignored, then some lines
follow, each line contains two strings, the first one is a word in English, the second one is the corresponding word in Martian's language. A line with a single string "END" indicates the end of the directory part, and this string should be ignored. The book
part starts with a single line contains a string "START", this string should be ignored, then an article written in Martian's language. You should translate the article into English with the dictionary. If you find the word in the dictionary you should translate
it and write the new word into your translation, if you can't find the word in the dictionary you do not have to translate it, and just copy the old word to your translation. Space(' '), tab('\t'), enter('\n') and all the punctuation should not be translated.
A line with a single string "END" indicates the end of the book part, and that's also the end of the input. All the words are in the lowercase, and each word will contain at most 10 characters, and each line will contain at most 3000 characters.
[align=left]Output[/align]
In this problem, you have to output the translation of the history book.
[align=left]Sample Input[/align]
START
from fiwo
hello difh
mars riwosf
earth fnnvk
like fiiwj
END
START
difh, i'm fiwo riwosf.
i fiiwj fnnvk!
END
[align=left]Sample Output[/align]
hello, i'm from mars.
i like earth!
Hint
Huge input, scanf is recommended.
一道挺好的学习字典树的题、这里我在做题过程中遇到了这样的几个问题,也许大家会跟我犯一样的毛病:
1、PE、在有gets和scanf同时使用的时候,由于某些原因自动输出一个换行符、getchar可解决之。
2、TLE、MLE、尝试换成C++提交、尝试开大数组,这里测试数据是很令人心酸的。(也别过分离谱的开大数组)
3、WA、这里我提供一组数据,也许你就WA在这里:
START
from fiwo
hello difh
mars riwosf
earth fnnvk
like fiiwj
END
START
difh, i'm fiwo riwosf.
i fiiwj fnnvk!fn f fnn
END
这里先详解处理输入部分:
while(~scanf("%s",b))
{
if(strcmp(b,"START")==0)continue;
if(strcmp(b,"END")==0)break;
scanf("%s",a);
creat(a);//用a来找b
}
getchar();
while(gets(c))
{
if(strcmp(c,"START")==0)continue;
if(strcmp(c,"END")==0)break;
memset(str,'\0',sizeof(str));
for(int i=0;i<strlen(c);i++)
{
if(islower(c[i]))//如果是小写字母,
{
int cont=0;
int k=i;
while(k<strlen(c)&&islower(c[k]))//用str字符串数组截取下来这个单词、
{
str[cont]=c[k];
k++;cont++;
}
str[cont]='\0';//这里别忘记了、
if(find(str)==0)//如果没能找到映射部分单词
{
for(int j=i;j<=k-1;j++)//原样输出
{
printf("%c",c[j]);
}
}
i=k-1;//i向后移动
}
else//如果不是小写字母,原样输出、
{
printf("%c",c[i]);
}
}
printf("\n");
}然后强调一下刚才说的第三点如何处理、字典树的作用是查找前缀、如果只找到了前缀就输出的话,是一定会wa的、所以我们这里在初始化的时候,加上一个标记变量,在creat的时候的最后一个字母处给标记赋值,当find的时候 ,如果查到的地方的标记有赋值,那么就输出,否则就相当于没有找到、
同时,在给标记赋值的同时,给节点strcpy一下映射的单词、
这里贴上处理代码:
void creat(char *str)
{
int len=strlen(str);
tree *p=&root,*q;
for(int i=0;i<len;i++)
{
int id=str[i]-'a';
if(p->next[id]==NULL)
{
q=(tree *)malloc(sizeof(root));
for(int j=0;j<26;j++)
{
q->next[j]=NULL;
}
p->next[id]=q;
}
p=p->next[id];
if(i==len-1)//如果creat到了最后一个字母;
{
p->flag=1;//标记上为1
strcpy(p->la,b);//strcpy一下b
}
}
}
int find(char *str)
{
int len=strlen(str);
tree *p=&root;
for(int i=0;i<len;i++)
{
int id=str[i]-'a';
p=p->next[id];
if(p==NULL)
return 0;
}
if(p->flag==1)//光找到前缀是不行的。
printf("%s",p->la);
else
return 0;
return 1;
}然后是完整的AC代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<stdlib.h>
using namespace std;
#define maxn 26//只包含小写字母、
typedef struct tree
{
int flag;
tree *next[maxn];
char la[30];
}tree;
tree root;
char b[10000000];
char c[10000000];
char str[1000];
void creat(char *str)
{
int len=strlen(str);
tree *p=&root,*q;
for(int i=0;i<len;i++)
{
int id=str[i]-'a';
if(p->next[id]==NULL)
{
q=(tree *)malloc(sizeof(root));
for(int j=0;j<26;j++)
{
q->next[j]=NULL;
}
p->next[id]=q;
}
p=p->next[id];
if(i==len-1)
{
p->flag=1;
strcpy(p->la,b);
}
}
}
int find(char *str)
{
int len=strlen(str);
tree *p=&root;
for(int i=0;i<len;i++)
{
int id=str[i]-'a';
p=p->next[id];
if(p==NULL)
return 0;
}
if(p->flag==1)
printf("%s",p->la);
else
return 0;
return 1;
}
int main()
{
char a[20];
for(int i=0;i<26;i++)
{
root.flag=0;
root.next[i]=NULL;
}
while(~scanf("%s",b))
{
if(strcmp(b,"START")==0)continue;
if(strcmp(b,"END")==0)break;
scanf("%s",a);
creat(a);
}
getchar();
while(gets(c))
{
if(strcmp(c,"START")==0)continue;
if(strcmp(c,"END")==0)break;
memset(str,'\0',sizeof(str));
for(int i=0;i<strlen(c);i++)
{
if(islower(c[i]))
{
int cont=0;
int k=i;
while(k<strlen(c)&&islower(c[k]))
{
str[cont]=c[k];
k++;cont++;
}
str[cont]='\0';
if(find(str)==0)
{
for(int j=i;j<=k-1;j++)
{
printf("%c",c[j]);
}
}
i=k-1;
}
else
{
printf("%c",c[i]);
}
}
printf("\n");
}
}
/*
START
from fiwo
hello difh
mars riwosf
earth fnnvk
like fiiwj
END
START
difh, i'm fiwo riwosf.
i fiiwj fnnvk!fn f fnn
END
*/
相关文章推荐
- 字典树(入门) 之 hdu 1075
- hdu/hdoj 1075 What Are You Talking About
- Hdu 1075 What Are You Talking About
- Python成长之路第二篇(2)_列表元组内置函数用法
- 解密ThreadLocal
- HDU-1023 Train Problem II 出栈序列问题
- Python成长之路第二篇(3)_字典的置函数用法
- Linux netstat命令详解
- Android报错:Your content must have a ListView whose id attribute is 'android.R.id.list'
- hust 1628 LowerBound 莫队算法
- java学习--jvm内存初学
- 【zigbee】开启及清除NV_RESTORE信息的方法
- 垃圾回收机制和数据结构栈链表
- 1012. 数字分类
- 体系结构复习2——指令级并行(分支预測和VLIW)
- [bzoj3203][Sdoi2013]保护出题人
- hdu1789 Doing Homework again(贪心)
- 3.1、随机森林之随机森林实例
- hust 1626 Cutting rope 概率论
- python小白-day5 json&pickle