HDU 1075 What Are You Talking About (Trie)
What Are You Talking About
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/204800 K (Java/Others)
Total Submission(s): 16042 Accepted Submission(s): 5198
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!
解析:将词典中的原始单词组织成Trie,相应的相应单词作为原始单词的附加信息放到Trie树中原始单词的最后一个字符结点中。
AC代码:
//#include <bits/stdc++.h> //hdu的C++,不支持此头文件。G++支持
#include <cstdio>
#include <cstring>
#include <cctype> using namespace std; const int maxw = 12;
const int maxnode = 100000 * maxw + 10;
const int sigma_size = 26; struct Trie{
int ch[maxnode][sigma_size];
char val[maxnode][maxw];
int sz; void clear(){ sz = 1; memset(ch[0], 0, sizeof(ch[0])); }
int idx(char c){ return c - 'a'; } void insert(const char *s, const char *v){
int u = 0, n = strlen(s);
for(int i=0; i<n; i++){
int c = idx(s[i]);
if(!ch[u][c]){
memset(ch[sz], 0, sizeof(ch[sz]));
ch[u][c] = sz++;
}
u = ch[u][c];
}
strcpy(val[u], v);
} char* find(const char *s){
int u = 0, n = strlen(s);
int i;
char ans[maxw];
for(i=0; i<n; i++){
int c = idx(s[i]);
if(!ch[u][c]) return "";
u = ch[u][c];
}
return val[u];
}
}; Trie trie; int main(){
#ifdef sxk
freopen("in.txt", "r", stdin);
#endif // sxk trie.clear();
char s[maxw], ss[maxw], text[3002];
scanf("%s", s);
while(scanf("%s", s)){
if(s[0] == 'E' && s[1] == 'N' && s[2] == 'D') break;
scanf("%s", ss);
trie.insert(ss, s);
}
scanf("%s", s);
getchar();
while(gets(text)){
if(text[0] == 'E' && text[1] == 'N' && text[2] == 'D') break;
int n = strlen(text);
for(int i=0; i<n; ){
char foo[maxw];
int cnt = 0;
while(i < n && isalpha(text[i])){ foo[cnt ++] = text[i ++]; }
foo[cnt] = '\0';
if(!cnt){ putchar(text[i ++]); continue; }
if(strcmp(trie.find(foo), "")) printf("%s", trie.find(foo));
else printf("%s", foo);
}
puts("");
}
return 0;
}
HDU 1075 What Are You Talking About (Trie)的更多相关文章
- HDU 1075 What Are You Talking About(Trie的应用)
What Are You Talking About Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/204800 K ...
- HDU 1075 What Are You Talking About (strings)
What Are You Talking About Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/204800 K ...
- hdu 1075 What Are You Talking About(map)
题意:单词翻译 思路:map #include<iostream> #include<stdio.h> #include<string.h> #include< ...
- 【python】Leetcode每日一题-前缀树(Trie)
[python]Leetcode每日一题-前缀树(Trie) [题目描述] Trie(发音类似 "try")或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的 ...
- HDU 1052 Tian Ji -- The Horse Racing(贪心)(2004 Asia Regional Shanghai)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1052 Problem Description Here is a famous story in Ch ...
- HDU 1087:Super Jumping! Jumping! Jumping!(LIS)
Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- hdu 1251 统计难题 (字典树(Trie)<PS:C++提交不得爆内存>)
统计难题Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submis ...
- HDU 1671 Phone List (Trie)
pid=1671">Phone List Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- HDU 1358 Period (kmp求循环节)(经典)
<题目链接> 题目大意: 意思是,从第1个字母到第2字母组成的字符串可由某一周期性的字串(“a”) 的两次组成,也就是aa有两个a组成: 第三行自然就是aabaab可有两个aab组成: 第 ...
随机推荐
- xss bypass
重要的4个规则: 1 &符号不应该出现在HTML的大部分节点中. 2 尖括号<>是不应该出现在标签内的,除非为引号引用. 3 在text节点里面,<左尖括号有很大的危害. 4 ...
- Kali之Metasploit Framework环境配置
运行Metasploit Framework 依照Kali Linux网络服务策略,Kali没有自动启动的网络服务,包括数据库服务在内.所以为了让Metasploit以支持数据库的方式运行有些必要的步 ...
- http://my.oschina.net/lenglingx/blog/205269
http://my.oschina.net/lenglingx/blog/205269 http://www.2cto.com/os/201402/281465.html 单点登录原理: http:/ ...
- phantomjs 抓取房产信息
抓取https://sf.taobao.com/item_list.htm信息 driver=webdriver.PhantomJS(service_args=['--ssl-protocol=any ...
- 使用git-svn迁移SVN至GitLab
使用git-svn迁移SVN至GitLab 1.安装git和git-svn 后面的步骤中对git版本有一定要求,通过yum安装的git版本较低,这里进行编译安装 [root@DevTest ~]# y ...
- nginx安装第三方模块的方法
nginx第三方模块安装方法: ./configure --prefix=/你的安装目录 --add-module=/第三方模块目录 以安装fair模块实例 下载fair安装包并解压 1.在未安装ng ...
- attributes vs properties --记于jquery attr不能正确更新input的value值后
最近做的前端页面是个单页面应用,需要经常给个input赋值什么的. 我常用的方式是$('#id').attr('value','XXXX'),一直可以正常使用.今天突然发现一个问题,某个要赋值的inp ...
- JMeter 四:建立高级web测试计划
发送带有Header的请求 参考:http://jmeter.apache.org/usermanual/build-adv-web-test-plan.html#header_manager Jme ...
- Firefly 流程架构
print '----startmaster------' 1print '----appmain------' 2 print '----netserver------' 3 print '---- ...
- GUID转换成16位字符串或19位唯一字符串
整理几个经常使用GUID转换成16位字符串或19位唯一字符串方法: /// <summary> /// 依据GUID获取16位的唯一字符串 /// Author : 付义方 /// < ...