hdu 1075:What Are You Talking About(字典树,经典题,字典翻译)
What Are You Talking About
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/204800 K (Java/Others)
Total Submission(s): 12617 Accepted Submission(s): 4031
Huge input, scanf is recommended.
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <malloc.h>
using namespace std; struct Tire{
Tire *next[];
char *trans; //定义一个指向一维数组的指针
Tire() //构造函数
{
int i;
for(i=;i<;i++){
next[i]=NULL;
}
trans = NULL;
}
}; Tire root; void Insert(char trans[],char word[]) //将单词word插入到字典树中,并在最后加上翻译trans
{
Tire *p = &root;
int i;
for(i=;trans[i];i++){
int n = trans[i]-'a';
if(p->next[n]==NULL)
p->next[n] = new Tire;
p = p->next[n];
}
p->trans = (char*)malloc(sizeof(char)*);
strcpy(p->trans,word);
}
void Find(char str[]) //找到对应的翻译并输出,没找到则输出原来的字符串
{
Tire *p = &root;
int i;
for(i=;str[i];i++){
int n = str[i]-'a';
if(p->next[n]==NULL){
printf("%s",str);
return ;
}
p = p->next[n];
}
if(p->trans==NULL)
printf("%s",str);
else
printf("%s",p->trans);
} int main()
{
char word[],trans[];
scanf("%s",word);
while(scanf("%s",word)!=EOF){ //输入字典
if(strcmp(word,"END")==) //遇到结束标记
break;
scanf("%s",trans);
Insert(trans,word); //将单词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 ;
}
Freecode : www.cnblogs.com/yym2013
hdu 1075:What Are You Talking About(字典树,经典题,字典翻译)的更多相关文章
- HDU 1251 统计难题(字典树模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意:给出一些单词,然后有多次询问,每次输出以该单词为前缀的单词的数量. 思路: 字典树入门题. #inc ...
- 字典树模板题(统计难题 HDU - 1251)
https://vjudge.net/problem/HDU-1251 标准的字典树模板题: 也注意一下输入方法: #include<iostream> #include<cstdi ...
- 字典树&&01字典树专题&&对字典树的理解
对于字典树和01字典树的一点理解: 首先,字典树建树的过程就是按照每个数的前缀来的,如果你要存储一个全小写字母字符串,那么这个树每一个节点最多26个节点,这样的话,如果要找特定的单词的话,按照建树的方 ...
- CH 1601 - 前缀统计 - [字典树模板题]
题目链接:传送门 描述给定 $N$ 个字符串 $S_1,S_2,\cdots,S_N$,接下来进行 $M$ 次询问,每次询问给定一个字符串 $T$,求 $S_1 \sim S_N$ 中有多少个字符串是 ...
- hdu1305 字典树水题
题意: 给你一些字符串,然后问你他们中有没有一个串是另一个串的前缀. 思路: 字典树水题,(这种水题如果数据不大(这个题目不知道大不大,题目没说估计不大),hash下也行,把每个 ...
- hdu 1251 统计难题 (字典树入门题)
/******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...
- HDU 1247 - Hat’s Words - [字典树水题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247 Problem DescriptionA hat’s word is a word in the ...
- HDU 2072 - 单词数 - [(有点小坑的)字典树模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2072 Problem Descriptionlily的好朋友xiaoou333最近很空,他想了一件没有 ...
- HDU 4825 Xor Sum(01字典树入门题)
http://acm.hdu.edu.cn/showproblem.php?pid=4825 题意: 给出一些数,然后给出多个询问,每个询问要从之前给出的数中选择异或起来后值最大的数. 思路:将给出的 ...
- HDU 1251 统计难题(字典树 裸题 链表做法)
Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己 ...
随机推荐
- Android实用代码模块集锦
1. 精确获取屏幕尺寸(例如:3.5.4.0.5.0寸屏幕) 1 2 3 4 5 6 public static double getScreenPhysicalSize(Activity ctx) ...
- maven简单配置
maven-3.3.9下载 Maven是一个项目管理和综合工具.Maven提供了开发人员构建一个完整的生命周期框架.开发团队可以自动完成项目的基础工具建设,Maven使用标准的目录结构和默认构建生命周 ...
- 嵌套div中margin-top转移问题的解决办法
在这两个浏览器中,有两个嵌套关系的div,如果外层div的父元素padding值为0,那么内层div的margin-top或者margin-bottom的值会“转移”给外层div. <!DOCT ...
- CentOS 下安装xdebug
在CentOS 6.x 的系统中,是集成xdebug 的, yum install PHP-pecl-xdebug 如果是CentOS.5 也可能通过安装安装 epel 来安装 rpm -ivh ht ...
- 多节点 devstack 部署
1, 网络配置 每个节点 /etc/network/interfaces auto eth0 iface eth0 inet static address 192.168.42.11 netmask ...
- 【SpringBoot】SpringBoot 入门示例
参考资料: http://www.tuicool.com/articles/mqeee2A http://www.cnblogs.com/suncj/p/4065589.html http://spr ...
- SQL merge into 表合并
Merge关键字是一个神奇的DML关键字.它在SQL Server 2008被引入,它能将Insert,Update,Delete简单的并为一句.MSDN对于Merge的解释非常的短小精悍:”根据与源 ...
- uniq 重复行统计
uniq 命令 文字 uniq 是LINUX命令 用途 报告或删除文件中重复的行. 语法 uniq [ -c | -d | -u ] [ -f Fields ] [ -s Characters ] ...
- PHP带重试功能的curl
2016年1月13日 10:48:10 星期三 /** * @param string $url 访问链接 * @param string $target 需要重试的标准: 返回结果中是否包含$tar ...
- static cross compile gtk-2.16.6+gtk-directfb+arm-linux (arm-linux-gcc-3.4.4+glib-2.3.5)
----------------------------------------------------------------------- In Ubuntu 10.4 Desktop and & ...