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): 20680 Accepted Submission(s): 6852
Huge input, scanf is recommended.
题目链接:HDU 1075
结构体Trie中用flag=1表示当前点是否是某个单词的结尾处(防止出现单词a为单词b的前缀但是却被b覆盖的情况,比如diff对应bbb,但是此时diff的前缀dif显然是不存在的),若为结尾,则flag赋值为1,然后把对应的翻译单词赋值给这个节点的s[]。
查询的是否看查完之后的flag是否为1,如果为1则存在该单词,否则flag为0或根本查不到,则返回NULL
一开始智障了把每个点flag都设为1狂WA……
代码:
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <sstream>
#include <cstring>
#include <bitset>
#include <string>
#include <deque>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=26;
const int M=100;
struct Trie
{
Trie *nxt[N];
int flag;
char s[M];
Trie()
{
CLR(nxt,0);
CLR(s,0);
flag=0;
}
};
Trie *L=new Trie();
void update(char s[],char earth[])
{
int len=strlen(s);
int indx;
Trie *cur=L;
for (int i=0; i<len; ++i)
{
indx=s[i]-'a';
if(cur->nxt[indx])
cur=cur->nxt[indx];
else
{
Trie *one=new Trie();
cur->nxt[indx]=one;
cur=one;
}
}
strcpy(cur->s,earth);
cur->flag=1;
}
char* Find(char s[])
{
int len=strlen(s);
int indx;
Trie *cur=L;
for (int i=0; i<len; ++i)
{
indx=s[i]-'a';
if(cur->nxt[indx]==NULL)
return NULL;
cur=cur->nxt[indx];
}
return cur->flag?cur->s:NULL;
}
char from[M],to[M];
char tot[1000010];
char temp[M];
int main(void)
{
int i;
scanf("%s",from);
getchar();
while (~scanf("%s",to)&&strcmp(to,"END"))
{
scanf("%s",from);
update(from,to);
}
getchar(); gets(from);
while (gets(tot)&&strcmp(tot,"END"))
{
int len=strlen(tot);
int k=0;
CLR(temp,0);
for (i=0; i<len; ++i)
{
if(islower(tot[i]))
temp[k++]=tot[i];
else
{
char *one=Find(temp);
if(k)
printf("%s",one==NULL?temp:one);
putchar(tot[i]);
CLR(temp,0);
k=0;
}
}
if(k)
{
char *one=Find(temp);
printf("%s",one==NULL?temp:one);
}
putchar('\n');
}
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
题目链接: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 ...
- 【HDU - 5790 】Prefix(主席树+Trie树)
BUPT2017 wintertraining(15) #7C 题意 求[min((Z+L)%N,(Z+R)%N)+1,max((Z+L)%N,(Z+R)%N)+1]中不同前缀的个数,Z是上次询问的结 ...
随机推荐
- Android之ScrollView
1.ScrollView和HorizontalScrollView是为控件或者布局添加滚动条 2.上述两个控件只能有一个孩子,但是它并不是传统意义上的容器 3.上述两个控件可以互相嵌套 4.滚动条的位 ...
- 禁随意改root密码
lsattr 主要参数如表. -a 显示所有文件和目录,包括以"."为名称开头字符的额外内建,现行目录"."与上层目录"..". -d 显示 ...
- mysql中char,varchar与text类型的区别和选用
关于char,varchar与text平时没有太在意,一般来说,可能现在大家都是用varchar.但是当要存储的内容比较大时,究竟是选择varchar还是text呢?不知道...... 于是去查阅了一 ...
- 移除IIS默认的响应头
在IIS+ASP.NET的运行环境,默认情况下会输出以下的响应头(Response Headers): 那如何移除这些响应头呢?下面我们来一个一个移除. 1. 移除Server 借助IIS URL R ...
- angular语法:Controller As
这个东东我觉得很好哟. 数据可以在同一个页面的不同的controller之间自由穿梭... 当然, https://thinkster.io/a-better-way-to-learn-angular ...
- javascript 复习代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- 理解HTTP session原理及应用
转自:http://www.2cto.com/kf/201206/135471.html 一.术语session在我的经验里,session这个词被滥用的程度大概仅次于transaction,更加有趣 ...
- Android快捷开关实现(转)
在Android源码中,提供的快捷开关相对是比较少的,Android4.0系统默认提供的桌面快捷开关AppWidget上只有5种开关(分别是Wifi开关.蓝牙开关.GPS开关.同步开关.亮度设置开关) ...
- ScrollView与ListView的冲突
众所周知ListView与ScrollView都具有滚动能力,对于这样的View控件,当ScrollView与ListView相互嵌套会成为一种问题: 问题一:ScrollView与ListView嵌 ...
- 输入框提示文字js
<input style="margin-right: 0px; padding-right: 0px;" class="text" required=& ...