hdu1075What Are You Talking About (字典树)
into English. Can you help him?
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.
START
from fiwo
hello difh
mars riwosf
earth fnnvk
like fiiwj
END
START
difh, i'm fiwo riwosf.
i fiiwj fnnvk!
END
hello, i'm from mars.
i like earth! [hint]
Huge input, scanf is recommended.
[/hint]#include<stdio.h>
#include<string.h>
#include<malloc.h>
typedef struct nnn
{
int flag;
char *traslat;
struct nnn *next[26];
}node;
node *builde()
{
node *p=(node*)malloc(sizeof(node));
p->flag=0;
for(int i=0;i<26;i++)
p->next[i]=NULL;
return p;
}
node *root;
void insert(char str[],char tras[])
{
node *p=root;
for(int i=0;str[i]!='\0';i++)
{
if(p->next[str[i]-'a']==NULL)
p->next[str[i]-'a']=builde();
p=p->next[str[i]-'a'];
}
p->flag=1;
int len=strlen(tras)+1;
p->traslat=(char*)malloc(len*sizeof(char));
strcpy(p->traslat,tras);
}
void trasl(char str[])
{
node *p=root;
for(int i=0;str[i]!='\0';i++)
{
if(p->next[str[i]-'a']==NULL)
{
printf("%s",str); return ;
}
p=p->next[str[i]-'a'];
}
if(p->flag)
printf("%s",p->traslat);
else
printf("%s",str);
}
int main()
{
char tras[15],str[3005],str1[15];
root=builde();
while(scanf("%s",tras)>0&&strcmp(tras,"END")!=0)
{
if(strcmp("START",tras)==0)continue;
scanf("%s",str);
insert(str,tras);
}
getchar();
while(gets(str),strcmp(str,"END")!=0)
{
if(strcmp("START",str)==0)continue;
int len=strlen(str);
for(int i=0,j=0;i<=len; i++)
{
if(str[i]>='a'&&str[i]<='z')
str1[j++]=str[i];
else
{
str1[j]='\0';
if(j) trasl(str1);
if(i==len||str[i]=='\n')printf("\n");
else if(str[i]=='\t') printf("\t");
else printf("%c",str[i]);
j=0;
}
}
}
}
hdu1075What Are You Talking About (字典树)的更多相关文章
- 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)
前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...
- [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- 字典树+博弈 CF 455B A Lot of Games(接龙游戏)
题目链接 题意: A和B轮流在建造一个字,每次添加一个字符,要求是给定的n个串的某一个的前缀,不能添加字符的人输掉游戏,输掉的人先手下一轮的游戏.问A先手,经过k轮游戏,最后胜利的人是谁. 思路: 很 ...
- 萌新笔记——C++里创建 Trie字典树(中文词典)(一)(插入、遍历)
萌新做词典第一篇,做得不好,还请指正,谢谢大佬! 写了一个词典,用到了Trie字典树. 写这个词典的目的,一个是为了压缩一些数据,另一个是为了尝试搜索提示,就像在谷歌搜索的时候,打出某个关键字,会提示 ...
- 山东第一届省赛1001 Phone Number(字典树)
Phone Number Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 We know that if a phone numb ...
- 字典树 - A Poet Computer
The ACM team is working on an AI project called (Eih Eye Three) that allows computers to write poems ...
- trie字典树详解及应用
原文链接 http://www.cnblogs.com/freewater/archive/2012/09/11/2680480.html Trie树详解及其应用 一.知识简介 ...
- HDU1671 字典树
Phone List Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- *HDU1251 字典树
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
随机推荐
- ArrayList集合--C#
static void Main(string[] args) { //实例化出一个集合对象 ArrayList list = new ArrayList(); /*添加*/ //--添加单个元素 l ...
- BZOJ 2510: 弱题( 矩阵快速幂 )
每进行一次, 编号为x的数对x, 和(x+1)%N都有贡献 用矩阵快速幂, O(N3logK). 注意到是循环矩阵, 可以把矩阵乘法的复杂度降到O(N2). 所以总复杂度就是O(N2logK) --- ...
- 06-OC分类、协议、ARC
目录: 一.分类 二.扩展 三.协议 四.内存管理ARC 回到顶部 一.分类 1 分类就是类的补充和扩展,本质上是类的一部分,把一个类分成若干部分,每个部分就是分类. 2 语法 * 文件中的语法@in ...
- 回归基础从新认识——HTML+CSS
前言 这段时间工作没那么繁杂,索性就想说来套系统的学习,之前去面试的时候,有被问及些基础的知识,居然回答不上来,也不能说是回答不上吧,回答的不全面.前端群上问了那个机构比较好,选择了慕课网.看了一段时 ...
- CentOS 6.2 二进制安装apache2.4.3出现configure: error: APR-util not found. Please read the documentation的解决方
CentOS 6.2 二进制安装apache2.4.3出现configure: error: APR-util not found. Please read the documentation的解决方 ...
- excel通过转成xml格式模板,下载成excel文件
源代码: report ztest_down_excel. data: begin of i_file occurs , val() type c, end of i_file. data begin ...
- 搭建Windows SVN服务器及TortoiseSVN使用帮助和下载
搭建Windows SVN服务器: 用的SVN服务器通常为外部,例如Google Code的服务器,不过,做为一个程序开发人员,就算自己一个人写程序,也应该有一个SVN版本控制系统,以便对开发代码进行 ...
- javascript笔记整理(数据类型强制/隐式转换 )
A.数据类型强制转换 1.转换为数值类型 Number(参数) 把任何的类型转换为数值类型 A.如果是布尔值,false为0,true为1 var a=false;alert(Number(a)); ...
- Spring MVC 学习笔记 json格式的输入和输出
Spring mvc处理json需要使用jackson的类库,因此为支持json格式的输入输出需要先修改pom.xml增加jackson包的引用 <!-- json --> <dep ...
- [放松一下] 经典高清电影合集 170G BT种子下载
经典高清电影合集 170G BT种子下载 点击文件名下载 经典高清电影合集170G BT种子.torrent 下载方法 经典高清电影合集详情见目录: 1. 杀手47 2. 这个杀手不太冷 3. 放牛班 ...