hdu 1075(字典树)
What Are You Talking About
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/204800 K (Java/Others)
Total Submission(s): 21658 Accepted Submission(s): 7228
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?
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.
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<cstdlib>
#include<string>
#define eps 0.000000001
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const int N=+;
struct tire{
char str[N];
int flag;
struct tire *next[];
};
tire *root;
tire *init(){
tire *p=(tire *)malloc(sizeof(tire));
for(int i=;i<;i++){
p->next[i]=NULL;
}
// p->terminal=false;
p->flag=;
return p;
}
void insert(char *a,char *b){
int i=;
tire *p=root;
while(b[i]!='\0'){
if(p->next[b[i]-'a']==){
p->next[b[i]-'a']=init();
}
p=p->next[b[i]-'a'];
//p->num++;
i++;
}
p->flag=;
strcpy(p->str,a);
}
void find(char *str){
int i=;
tire *p=root;
while(str[i]!='\0'&&p->next[str[i]-'a']){
p=p->next[str[i]-'a'];
i++;
}
if(str[i]=='\0'&&p->flag){
printf("%s",p->str);
}
else
printf("%s",str); }
int main()
{
char word[],trans[];
char s[];
scanf("%s",word);
root=init();
while(scanf("%s",word)!=EOF){ //输入字典
if(strcmp(word,"END")==) //遇到结束标记
break;
scanf("%s",s);
insert(word,s); //将单词word插入到字典树中,并在最后加入其翻译
}
scanf("%s",word);
getchar();
char str[];
while(gets(str)){
if(strcmp(str,"END")==)
break;
int i,j=;
char t[]={};
for(i=;str[i];i++){
if('a'<=str[i] && str[i]<='z'){ //检测到的是小写字母
t[j++] = str[i];
}
else{
t[j] = '\0';
if(t[]!='\0'){ //不是空的
find(t); //找到对应的翻译并输出,没找到则输出原来的字符串
t[]='\0',j=; //初始化t
}
printf("%c",str[i]);
}
}
printf("\n");
} return ;
}
/*
int main(){
char str1[N],str2[N],str3[N],str4[N],s[N];
gets(str1);
root=init();
while(scanf("%s",str2)!=EOF){
if(strcmp(str2,"END")==0)break;
scanf("%s",str3);
insert(str2,str3);
}
//getchar();
gets(str4);
getchar();
char a[N];
//getchar();
while(gets(s)){
//find(s);
int k=0;
if(strcmp(s,"END")==0)break;
int len=strlen(s);
int flag=0;
for(int i=0;i<len;i++){
if(s[i]>='a'&&s[i]<='z'){
a[k++]=s[i];
}
else{
a[k]='\0';
if(a[0]!='\0'){
find(a);
k=0;
a[0]='\0';
}
printf("%c",s[i]);
} }
cout<<endl;
}
}*/
Huge input, scanf is recommended.
hdu 1075(字典树)的更多相关文章
- hdu 1075 字典树
#include<stdio.h> #include<iostream> struct node { int num,i; node *a[27]; char s[20];// ...
- HDU 5687 字典树插入查找删除
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5687 2016百度之星资格赛C题,直接套用字典树,顺便巩固了一下自己对字典树的理解 #include< ...
- HDU 5384 字典树、AC自动机
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5384 用字典树.AC自动机两种做法都可以做 #include<stdio.h> #includ ...
- hdu 2112(字典树+最短路)
HDU Today Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdu 2072(字典树模板,set,map均可做)
地址:http://acm.hdu.edu.cn/showproblem.php?pid=2072 lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词 ...
- Chip Factory HDU - 5536 字典树(删除节点|增加节点)
题意: t组样例,对于每一组样例第一行输入一个n,下面在输入n个数 你需要从这n个数里面找出来三个数(设为x,y,z),找出来(x+y)^z(同样也可以(y+z)^1)的最大值 ("^&qu ...
- hdu 1251 字典树的应用
这道题看了大神的模板,直接用字典树提交的会爆内存,用stl 里的map有简单有快 #include <iostream> #include <map> #include < ...
- hdu 2896 字典树解法
#include <iostream> #include <cstring> #include <cstdio> #include <cstdlib> ...
- Repository HDU - 2846 字典树
题意:给出很多很多很多很多个 单词 类似搜索引擎一下 输入一个单词 判断有一个字符串包含这个单词 思路:字典树变体,把每个单词的后缀都扔字典树里面,这里要注意dd是一个单词 但是把d 和dd都放字典树 ...
随机推荐
- Google开源技术protobuf
1.protobuf简介 protobuf是google提供的一个开源序列化框架,类似于XML,JSON这样的数据表示语言,其最大的特点是基于二进制,因此比传统的XML表示高效短小得多.虽然是二进制数 ...
- IO文件读取
/** *按字节读取文件 */@Testpublic void readerByte() { File file = new File("D:\\BindCheckControllerTes ...
- fuel一键部署
1. 所需物理主机的要求如下 内存:8GB+,推荐16GB: 磁盘:50GB+: 物理机OS:ubuntu-desktop-amd64 14.04(推荐) 或windows64位 物理机安装软件:安装 ...
- 46.颜色+品牌下钻分析时按最深层metric进行排序
主要知识点: 在做下钻分析时的排序 需求,以颜色进行bucket,这里bucket里面的doc以其各品牌的平均价格排序, GET /tvs/sales/_search { " ...
- 1.scrapy爬取的数据保存到es中
先建立es的mapping,也就是建立在es中建立一个空的Index,代码如下:执行后就会在es建lagou 这个index. from datetime import datetime fr ...
- Django-cookie与session操作
添加cookie: def login(req): if req.method=="POST": uf = UserInfoForm(req.POST) if uf.is_vali ...
- 《ABCD组》第八次作业:ALPHA冲刺
<ABCD组>第八次作业:ALPHA冲刺 项目 内容 这个作业属于哪个课程 http://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://ww ...
- SQLServer中的Cross Apply、Outer Apply
https://www.2cto.com/database/201304/206330.html
- SCI 论文金句
SCI 不会写?其实英语基础好一点,文献多看一点,多写写自然就能自己写出来了.当然,你肯定会说英语真的好难,好吧,就知道你们懒得学英语了.我给你们整理了一套万能模板,涵盖了论文不同部分的常用句型. 摘 ...
- hdu2009 求数列的和【C++】
求数列的和 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...