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): 14107 Accepted Submission(s): 4546
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.
from fiwo
hello difh
mars riwosf
earth fnnvk
like fiiwj
END
START
difh, i'm fiwo riwosf.
i fiiwj fnnvk!
END
i like earth!
Huge input, scanf is recommended.
/*hdu 1075 字典树写法*/
//#define LOCAL
#include<cstdio>
#include<cstring>
#include<cstdlib>
typedef struct node
{
int id;
struct node *child[];
}Trie;
void Insert(char *s,Trie *root,int v)
{
Trie *cur=root,*curnew;
int i,pos;
for(i=;s[i]!='\0';i++){
pos=s[i]-'a';
if(cur->child[pos]==NULL)
{
curnew= new Trie;
curnew->id=;
for(int j=;j<;j++)
curnew->child[j]=NULL;
cur->child[pos]=curnew;
}
cur=cur->child[pos];
}
cur->id=v;
}
int query(char *s,Trie *root)
{
Trie *cur=root;
int i,pos;
for(i=;s[i]!='\0';i++){
pos=s[i]-'a';
if(cur->child[pos]!=NULL)
cur=cur->child[pos];
else return ; //输出原字符串
}
return cur->id;
}
char aa[][],bb[];
char str[];
int main()
{
#ifdef LOCAL
freopen("test.in","r",stdin);
#endif
Trie *root= new Trie ;
root->id=;
int st;
for(int i=;i<;i++)
root->child[i]=NULL;
scanf("%s",aa[]);
int i=;
while(scanf("%s",aa[i]),strcmp(aa[i],"END"))
{
scanf("%s",bb);
Insert(bb,root,i); //对应标号
i++;
}
scanf("%s",aa[]);
getchar();
while(gets(str),strcmp(str,"END"))
{
for(st=i=;str[i]!='\0';i++)
{
if(str[i]<'a'||str[i]>'z'){
if(i>st){
strncpy(aa[],str+st,i-st);
aa[][i-st]='\0';
printf("%s",aa[query(aa[],root)]);
}
st=i+;
printf("%c",str[i]);
}
}
puts("");
}
return ;
}
当然可以用map,在这里就不在补充啦!喵喵!O(∩_∩)O哈哈~
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 (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
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1075 Problem Description Ignatius is so lucky that he ...
- hdu 1075 (map)
http://acm.hdu.edu.cn/showproblem.php?pid=1075 What Are You Talking About Time Limit: 10000/5000 MS ...
- hdu 1075 What Are You Talking About
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1075 题意:比较简单,易懂,这里不做说明. 解法:第一种方法:用map映射,耗时1000+ms:第二种 ...
- HDU 1057 What Are You Talking About trie树 简单
http://acm.hdu.edu.cn/showproblem.php?pid=1075 题意 : 给一个单词表然后给一些单词,要求翻译单词表中有的单词,没有则直接输出原单词. 翻译文段部分get ...
- 字典树 HDU 1075 What Are You Talking About
http://acm.hdu.edu.cn/showproblem.php?pid=1075 ;}
- HDU 1075-What Are You Talking About(Trie)
题意: 给你一个字典 一个英文单词对应一个火星单词 给你一段火星文翻译成英文 字典上的没有的不翻译 分析: 没有给数据规模 字典树用链表 #include <map> #include & ...
- HDU 11488 Hyper Prefix Sets (字符串-Trie树)
H Hyper Prefix Sets Prefix goodness of a set string is length of longest common prefix*number of str ...
随机推荐
- 【面向打野编程】——KMP算法入门
一.问题 咱们先不管什么KMP,来看看怎么匹配两个字符串. 问题:给定两个字符串,求第二个字符串是否包含于第一个字符串中. 为了具体化,我们以 ABCAXABCABCABX 与 ABCABCABX为例 ...
- VS生成事件
源自:http://www.cnblogs.com/FreeDong/p/3406737.html 如果说磨刀不误砍柴工,同样用好Visual Studio,会大大增加咱.NET程序猿效率.本文说的就 ...
- XPah学习
资料1: 来源:http://www.cnblogs.com/ChengDong/archive/2012/06/28/2567744.html 示例Xml: <?xml version=&qu ...
- XAML基础
1.标记扩展 将一个对象的属性值依赖在其他其他对象的某个属性上 用法:标记属性的一般用法是:Attribute = Value,使用标记拓展,Value字符串是由一对花括号及其括起来的内容组成,XAM ...
- git学习笔记07-冲突了怎么办-那就解决冲突呗
比如一个人自己创建了分支feature1进行修改提交之后提交,另一个人在master上修改然后提交. master分支和feature1分支各自都分别有新的提交,变成了这样: 这种情况下,Git无法执 ...
- iOS - Swift 与 Objective-C 互相操作
前言 在 Swift 语言中,我们可以使用 Objective-C.C 语言编写代码,我们可以导入任意用 Objective-C 写的 Cocoa 平台框架.Objective-C 框架或 C 类库. ...
- iOS - UIRefreshControl 刷新数据
前言 NS_CLASS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED @interface UIRefreshControl : UIControl 1.UIRefresh ...
- Redis基础知识之————php-Redis 常用命令专题
Keys del,delete - 删除键 dump - 返回存储在指定键值的序列化版本. exists - 确定键是否存在 expire,setTimeout,pexpire - 设置键的生存时间( ...
- JAVA调用C语言写的SO文件
JAVA调用C语言写的SO文件 因为工作需要写一份SO文件,作为手机硬件IC读卡和APK交互的桥梁,也就是中间件,看了网上有说到JNI接口技术实现,这里转载了一个实例 // 用JNI实现 // 实例: ...
- IIS_Mvc发布
网站发布步骤: 这部分是转载文章 在此标明出处,以前有文章是转的没标明的请谅解,因为有些已经无法找到出处,或者与其它原因. 如有冒犯请联系本人,或删除,或标明出处. 因为好的文章,以前只想收藏,但连接 ...