What Are You Talking About

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others)

Total Submission(s): 20751    Accepted Submission(s): 6881

Problem Description
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?
 
Input
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.
 
Output
In this problem, you have to output the translation of the history book.
 
Sample Input
START
from fiwo
hello difh
mars riwosf
earth fnnvk
like fiiwj
END
START
difh, i'm fiwo riwosf.
i fiiwj fnnvk!
END
 
Sample Output
hello, i'm from mars.
i like earth!
Hint
Huge input, scanf is recommended.
 
题目大意:
相当于就是给了原文和对应的译文,然后给出例句,输出真正想要说的内容。都是以START开始和END结束。

解题思路:
用map做的话很简单,要是需要的话后面在补上。这里我是用字典树做的,缺点就是容易超内存。麻烦的地方时就是树需要自己建。下面的代码里面注释写的已经很详细了,所以就不再说了。



源代码:
<span style="font-size:18px;">
#include<iostream>
#include<algorithm>
#include<string>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std; const int MAXN = 26;
const int MAXM = 15;
const int MAXS = 3005; struct Trie//名字不用起多,按书上的写太累赘
{
char s[MAXM];
Trie *next[MAXN];
};
Trie *root;//紧随其后,声明一个 void init()
{
root = (Trie*)malloc(sizeof(Trie));
strcpy(root->s, "");
for (int i = 0; i<MAXN; i++)
{
root->next[i] = NULL;
}
} void CreateTire(char str[], char ss[])//不许要传整棵树了
{
//这里需要p来对roor进行操作,因为会有迭代过程
//如果root参与运算的话,root=root->next[i],root的值就被改变了
Trie *p = root, *temp;
int len = strlen(ss);
for (int i = 0; i<len; i++)
{
int id = ss[i] - 'a';
if (p->next[id] == NULL)//说明还没有访问过
{
//对临时temp进行初始话,同root的初始化一样
temp = (Trie*)malloc(sizeof(Trie));
strcpy(temp->s, "");
for (int j = 0; j<MAXN; j++)
{
temp->next[j] = NULL;
}
p->next[id] = temp;
p = p->next[id];
}
else//访问过继续向下走
{
p = p->next[id];
}//到最后了,把需要翻译的字符串拷贝进来
if (i == len - 1)
{
strcpy(p->s, str);
}
}
//建树成功
} string FindTrie(char ss[])//写string,不要写成char*,会返回地址
{
string ans = "";
Trie *p = root;
int len = strlen(ss);
for (int i = 0; i<len; i++)
{
int id = ss[i] - 'a';
if (p->next[id] == NULL)
{
ans = "";//没有的话就把空串拷贝进去
return ans;
}
else
{
p = p->next[id];//有的话继续向下
}
}
//假设ss对应的没有保存,因为ss可能是另外一个的前缀,这个时候p->next[id]也不为空
//但是拷贝进来的还是空串,不用担心
ans = (string)p->s;
return ans;
} void getData()
{
char s[MAXM];
scanf("%s", s);//先把start读进来
while (~scanf("%s", s))
{
if (s[0] == 'E')//读到end退出
break;
char ss[MAXM];
scanf("%s", ss);
CreateTire(s, ss);
}
} void slove()
{
string s;//因为整行读取还是string方便
cin >> s;
getline(cin, s);
while (getline(cin, s))
{
if (s[0] == 'E')
break;
int st;//开始结束的下标
st = 0;
string res = "";
s += ".";//末尾加一个不是小写字母的字符方便处理
int len = s.length();
for (int i = 0; i<len; i++)
{
if (s[i]>'z'||s[i]<'a')//不属于小写字母,逻辑或的关系
{
char temp[MAXM];
int index = 0;
for (int j = st; j<i; j++)
{
temp[index] = s[j];
index++;
}
temp[index] = '\0';//'\0'结尾
string ss = FindTrie(temp);
if (ss=="")//是空串
{
res += (string)temp;//保留原值追加
}
else
{
res += ss;
}
//寻找下一个小写字母开头的位置赋值st
st = i;
while (s[i]>'z'||s[i]<'a'&&i<len-1)
{
i++;
}
index = 0;
//把中间不是小写字符的字符串原值保留
for (int j = st; j < i; j++)
{
temp[index] = s[j];
index++;
}
temp[index] = '\0';
res += (string)temp;
st = i; }
}
cout << res << endl;
}
delete(root);
}
int main()
{
init();
getData();
slove();
return 0;
}
</span>


HDU1075-What Are You Talking About的更多相关文章

  1. HDU1075

    题目大意: 给你一本火星词典,每个火星单词对应一个英文单词. 然后给你一篇火星文章,要求你翻译成英文. 要求如下: 如果这个火星单词用英文单词可以表示,就翻译成英文,如果没有这个单词,就原样输出.遇到 ...

  2. HDU1075 - What Are You Talking About(Trie树)

    题目大意 给定一些火星文单词以及对应的英语单词,然后给你一些火星文,要求你翻译成对应的英文 题解 第一次写Trie树! 把所有火星文单词插入到Trie树中,并且每个火星文单词结尾的节点记录相应英文单词 ...

  3. HDU1075 字典树板子题

    题意 :给出两组字符串 一一映射,给出一种组成的文字,要求映射成另外一种思路:使用字典树,把映射的另外一个字符存在字典树的单词节点处  例如 abc   123 则把123存在abc节点中的c处即可 ...

  4. (map)What Are You Talking About hdu1075

    What Are You Talking About Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K ...

  5. What Are You Talking About HDU1075

    一开始我也想用map  但是处理不好其他字符.. 看了题解   多多学习! 很巧妙  就是粗暴的一个字符一个字符的来 分为小写字母和非小写字母两个部分  一但单词结束的时候就开始判断. #includ ...

  6. HDU1075 字典树 + 字符串映射

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1075 ,字典树的字符串映射. 题意是给你每个火星文单词对应的英语,然后让你把一篇火星文文章给翻译成英语 ...

  7. HDU1075 What Are You Talking About(map)

    传送门 题目大意:一个单词对应另一个单词 翻译一段文字 题解:stl map走一波 代码: #include<iostream> #include<map> #include& ...

  8. ACM训练计划建议(写给本校acmer,欢迎围观和指正)

    ACM训练计划建议 From:freecode#  Date:2015/5/20 前言: 老师要我们整理一份训练计划给下一届的学弟学妹们,整理出来了,费了不少笔墨,就也将它放到博客园上供大家参考. 菜 ...

  9. Trie树入门及训练

    什么叫Trie树? Trie树即字典树. 又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本 ...

  10. OJ题目分类

    POJ题目分类 | POJ题目分类 | HDU题目分类 | ZOJ题目分类 | SOJ题目分类 | HOJ题目分类 | FOJ题目分类 | 模拟题: POJ1006 POJ1008 POJ1013 P ...

随机推荐

  1. MongoDB查询分析

    MongoDB 查询分析可以确保我们建立的索引是否有效,是查询语句性能分析的重要工具.MongoDB 查询分析常用函数有:explain() 和 hint(). 1. explain(): 提供查询信 ...

  2. java设计模式-工厂模式(springweb为例子)

    一般而言,工厂模式分为3种,简单工厂模式,工厂方法模式,抽象工厂模式.这三种工厂模式逐层深入吧. 一,从springWeb.jar包使用抽象工厂模式的一个例子聊起 之前对spring各种痴迷,所以在需 ...

  3. 用户关注微信公众号后,获取该用户的openID存数据库失败

    关注微信公众号后将关注人的openID存入数据库失败,而openID换成字符串写死却可以存入数据库: $wxid=$postObj->FromUserName; $data['wx_openid ...

  4. Ionic3 启动页以及应用图标

    将新的启动页和应用图标图片(最好是高清png)上传到根目录 resources 使用命令自动生成,通过CMD进入项目所在文件夹,分别执行 ionic cordova resources android ...

  5. Ionic3 遇到的一些错误-Error: Cannot find module 'reflect-metadata'

    E:\Projects\ionic\myApp5>ionic serve Error: Cannot find module 'reflect-metadata' 解决方法: npm insta ...

  6. Javaweb项目开发的前后端解耦的必要性

    JavaWeb项目为何我们要放弃jsp?为何要前后端解耦?为何要动静分离? 使用jsp的痛点: 1.jsp上动态资源和静态资源全部耦合在一起,服务器压力大,因为服务器会收到各种静态资源的http请求, ...

  7. java与OC比较

    转自:http://blog.sina.com.cn/s/blog_93742d0d010165qi.html 1.Cocoa是什么?Cocoa是使用OC语言编写的工具包,里面有大量的类库.结构体,说 ...

  8. 免费好用的阿里云云盾证书服务(https证书)申请步骤

    推荐一个免费的阿里云产品:云盾证书(https证书) 为了能让非专业人士看懂,同样尽量用直白的话,一般来说:当你个人需要建立网站,或者公司要建立官网.商城,通常需要先购买服务器或云主机,虚拟空间,然后 ...

  9. 借助 frp 随时随地访问自己的树莓派

    前言 看了知乎上的一个「树莓派」是什么以及普通人怎么玩? 的高票回答,双十一时间,果断买了一个树莓派 3. 周一(11.13) 到的货.我目前只想实现一个简单的功能 -- 想从任意位置访问我的树莓派. ...

  10. 网页加速特技之 AMP

    据统计,40%的人会放弃使用加载时间超过3秒的网站.对于加载慢的页面我也是没耐心等待的,同类型网站那么多,为什么不选择加载速度更快体验更好的呢.为了解决网页加载慢的问题,Google联合数十家技术机构 ...