poj 2503 哈希 Map 字典树
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 36967 | Accepted: 15749 |
Description
Input
word. No foreign word appears more than once in the dictionary. The message is a sequence of words in the foreign language, one word on each line. Each word in the input is a sequence of at most 10 lowercase letters.
Output
Sample Input
dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay atcay
ittenkay
oopslay
Sample Output
cat
eh
loops
Hint
Source
//16096K 2625MS
#include<iostream>
#include<cstdio>
#include<map>
#include<cstring> using namespace std; int main()
{
char s[100],s1[11];
string ss;
char c;
map<string,string>Q;
int num;
while(gets(s)&&s[0]!='\0') //读串比读多个字符快
{
int len=strlen(s);
int i;
for( i=0;i<len;i++)
{
if(s[i]==' ')
{s[i]='\0';
break;
}
}
ss=s+i+1;
Q[ss]=s;
}
while(~scanf("%s",s1))
{
if(Q[s1].size())
cout<<Q[s1]<<endl;
else
printf("eh\n");
}
}
// while(~scanf("%c",&c)) // 超时
// {
// if(c=='\n')
// break;
// num=0;
// while(c!=' ')
// {
// s[num++]=c;
// scanf("%c",&c);
// }
// s[num]='\0'; //够成字符串
// num=0;
// scanf("%c",&c); //防止上一个空格被读入
//
// while(c!='\n')
// {
// ss[num++]=c;
// scanf("%c",&c);
// }
// ss[num]='\0';
// Q[ss]=s;
// }
//字典树 26240K <span id="transmark"></span>735MS
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib> using namespace std; char ss[100],s[100],c[100010][100];
int num=0;
struct node
{
int flag;
node *next[26];
}*head;
node * Creat()
{
node *p;
p=new node;
p->flag=0;
for(int i=0;i<26;i++)
p->next[i]=NULL;
return p;
}
int Build_Tree()
{
node *p=head;
int len=strlen(s);
for(int i=0;i<len;i++)
{
int a=s[i]-'a';
if(!p->next[a])
{
p->next[a]=Creat();
//p->next[a]->flag=num;
}
p=p->next[a];
}
p->flag=num;
}
int Find(char s1[])
{
int len=strlen(s1);
node *p=head;
for(int i=0;i<len;i++)
{
int a=s1[i]-'a';
if(!p->next[a])
{
return 0;
}
p=p->next[a];
}
return p->flag;
}
int main()
{
num=1;
head=Creat();
while(gets(ss)&&ss[0]!='\0')
{
sscanf(ss,"%s %s",c[num],s);
Build_Tree();
num++;
}
char s1[100];
while(~scanf("%s",s1))
{
int flag=Find(s1);
//cout<<flag;
if(flag)
printf("%s\n",c[flag]);
else
printf("eh\n");
}
}
poj 2503 哈希 Map 字典树的更多相关文章
- POJ 2503 Babelfish(map,字典树,快排+二分,hash)
题意:先构造一个词典,然后输入外文单词,输出相应的英语单词. 这道题有4种方法可以做: 1.map 2.字典树 3.快排+二分 4.hash表 参考博客:[解题报告]POJ_2503 字典树,MAP ...
- POJ 3376 Finding Palindromes EX-KMP+字典树
题意: 给你n个串串,每个串串可以选择和n个字符串拼接(可以自己和自己拼接),问有多少个拼接后的字符串是回文. 所有的串串长度不超过2e6: 题解: 这题由于是在POJ上,所以string也用不了,会 ...
- [POJ] #1002# 487-3279 : 桶排序/字典树(Trie树)/快速排序
一. 题目 487-3279 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 274040 Accepted: 48891 ...
- POJ 3764 (异或+字典树)
早就听过用字典树求异或最大值,然而没做过.发现一碰到异或的题就GG,而且因为以前做过的一道类似的题(事实上并不类似)限制了思路,蠢啊= =. 题意:一棵带权的树,求任意两点间路径异或的最大值. 题解: ...
- poj 3764 The xor-longest Path(字典树)
题目链接:poj 3764 The xor-longest Path 题目大意:给定一棵树,每条边上有一个权值.找出一条路径,使得路径上权值的亦或和最大. 解题思路:dfs一遍,预处理出每一个节点到根 ...
- 哈希与字典树与KMP
hash讲解 主要记录hash的公式: ; i<=len; i++) { Hash[i]=(Hash[i-]*)%mod)%mod; } 求hash的公式是这个,怎么求一小段的hash值呢? ; ...
- J - What Are You Talking About(map,字典树)
题意:上部分是单词表,下部分是句子,翻译句子.START开始,END结束. 思路:简单字典树. Ignatius is so lucky that he met a Martian yesterday ...
- poj 2001:Shortest Prefixes(字典树,经典题,求最短唯一前缀)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12731 Accepted: 544 ...
- POJ 3764 The xor-longest Path ( 字典树求异或最值 && 异或自反性质 && 好题好思想)
题意 : 给出一颗无向边构成的树,每一条边都有一个边权,叫你选出一条路,使得此路所有的边的异或值最大. 分析 : 暴力是不可能暴力的,这辈子不可能暴力,那么来冷静分析一下如何去做.假设现在答案的异或值 ...
随机推荐
- ATP自造8Gb内存颗粒供DDR3使用
随着整个行业已经全面转向DDR4内存,不少厂商都陆续停产了DDR3,并准备好了迎接DDR5,但对于很多特殊用户,尤其是网络和嵌入式领域,仍然对DDR3有着强劲且持续的需求. 工业内存存储厂商ATP E ...
- parted---磁盘分区
parted命令是由GNU组织开发的一款功能强大的磁盘分区和分区大小调整工具,与fdisk不同,它支持调整分区的大小.作为一种设计用于Linux的工具,它没有构建成处理与fdisk关联的多种分区类型, ...
- vue使用axios中 this 指向问题
1.解决办法 在vue中使用axios做网络请求的时候,会遇到this不指向vue,而为undefined,可以使用箭头函数"=>"来解决.如下: methods: { lo ...
- 恐怖的奴隶主(bob)
题目 试题3:恐怖的奴隶主(bob) 源代码:bob.cpp 输入文件:bob.in 输出文件:bob.out 时间限制:1s 空间限制:512MB 题目描述 小L热衷于undercards. 在un ...
- js中数组增删查改unshift、push、pop、shift、slice、indexOf、concat、join
js中数组增删查改unshift.push.pop.shift.slice.indexOf.concat.join
- 【ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined) D】Tree
[链接] 我是链接,点我呀:) [题意] 让你在树上找一个序列. 这个序列中a[1]=R 然后a[2],a[3]..a[d]它们满足a[2]是a[1]的祖先,a[3]是a[2]的祖先... 且w[a[ ...
- 国庆 day 6 上午
1. 角谷猜想(kakutani.pas/c/cpp)(kakutani.in/out)时间限制:1s/空间限制:256M[题目描述] 某个名字末尾是 654321 的小 A 同学是个大家眼中公认的学 ...
- Attach、Detach和DeleteObject
原文:Attach.Detach和DeleteObject,想飞的梦想 1.CWnd Attatch和Detach的关系 首先,要明白Windows对象和MFC对象的区别. MFC对象实际上并没有把整 ...
- Qt之图形(转换)
简述 QTransform类指定坐标系的2D转换,可以指定平移.缩放.扭曲(剪切).旋转或投影坐标系.绘制图形时,通常会使用. QTransform与QMatrix的不同之处在于,它是一个真正的3x3 ...
- 基于请求响应的MVC框架调用分析
一.使用Servlet来处理请求响应 当client提交数据之后.接着发送请求,请求被封装成对象,server接收到请求,依据请求的URL.来推断将请求对象交由哪个Servlet处理. 在servle ...