浅谈\(Trie\):https://www.cnblogs.com/AKMer/p/10444829.html

题目传送门:http://poj.org/problem?id=2503

\(Trie\)树模板题,就是要你实现一个字典查找的功能。读入十分的恶心。

时间复杂度:\(O(len*n)\)

空间复杂度:\(O(len)\)

代码如下:

#include <cstdio>
#include <cstring>
using namespace std; const int maxn=1e5+5; char s[maxn<<1],s1[maxn],s2[maxn]; struct Trie {
int tot;
char s[maxn*10][15];
int son[maxn*10][26]; void ins() {
int pos=1,len=strlen(s2+1);
for(int i=1;i<=len;i++) {
if(son[pos][s2[i]-'a'])pos=son[pos][s2[i]-'a'];
else pos=son[pos][s2[i]-'a']=++tot;
}
len=strlen(s1+1);
for(int i=1;i<=len;i++)
s[pos][i]=s1[i];
} void find() {
int pos=1,len=strlen(s1+1);
for(int i=1;i<=len;i++) {
if(son[pos][s1[i]-'a'])pos=son[pos][s1[i]-'a'];
else {puts("eh");return;}
}
if(!strlen(s[pos]+1))puts("eh");
else printf("%s\n",s[pos]+1);
}
}T; int main() {
T.tot=1;
while(1) {
int n,pos;
scanf("%[^\n]",s+1);getchar();
if((n=strlen(s+1))==0)break;
for(int i=1;i<=n;i++)
if(s[i]==' ') {pos=i;break;}
for(int i=1;i<pos;i++)s1[i]=s[i];
for(int i=pos+1;i<=n;i++)s2[i-pos]=s[i];
T.ins(),memset(s,0,(n+1)*4);
}
while(~scanf("%s",s1+1))T.find();
return 0;
}

POJ2503:Babelfish的更多相关文章

  1. Poj 2503 / OpenJudge 2503 Babelfish

    1.Link: http://poj.org/problem?id=2503 http://bailian.openjudge.cn/practice/2503/ 2.Content: Babelfi ...

  2. java web 开发三剑客 -------电子书

    Internet,人们通常称为因特网,是当今世界上覆盖面最大和应用最广泛的网络.根据英语构词法,Internet是Inter + net,Inter-作为前缀在英语中表示“在一起,交互”,由此可知In ...

  3. 所有selenium相关的库

    通过爬虫 获取 官方文档库 如果想获取 相应的库 修改对应配置即可 代码如下 from urllib.parse import urljoin import requests from lxml im ...

  4. POJ2503——Babelfish(map映射+string字符串)

    Babelfish DescriptionYou have just moved from Waterloo to a big city. The people here speak an incom ...

  5. Babelfish 分类: 哈希 2015-08-04 09:25 2人阅读 评论(0) 收藏

    Babelfish Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 36398 Accepted: 15554 Descripti ...

  6. POJ2503——Babelfish

    Description You have just moved from Waterloo to a big city. The people here speak an incomprehensib ...

  7. POJ2503 Babelfish

    题目链接. 分析: 应当用字典树,但stl的map做很简单. #include <iostream> #include <cstdio> #include <cstdli ...

  8. 题解报告:poj 2503 Babelfish(map)

    Description You have just moved from Waterloo to a big city. The people here speak an incomprehensib ...

  9. POJ2503 Babelfish map或者hash_map

    POJ2503 这是一道水题,用Map轻松AC. 不过,可以拿来测一下字符串散列, 毕竟,很多情况下map无法解决的映射问题需要用到字符串散列. 自己生成一个质数, 随便搞一下. #include&l ...

随机推荐

  1. bzoj 1798 双标记区间修改线段树

    #include<bits/stdc++.h> using namespace std; #define MAXN 100000 #define M ((L+R)>>1) #d ...

  2. Pandas:时间数据的季节分析

    最近在做论文的数据处理,涉及到不同年份不同季节的分析.另外还要求不同季节的数据可以单独分析. 其实思路还是比较简单的,那就在原始数据中增加一栏:季节 2013-05-21 Aotizhongxin 1 ...

  3. Java环境搭建---(基础)

    首先下载eclipse开发工具,下载地址:http://www.eclipse.org/downloads/,界面如下: 选择eclipse juno(4.2)的版本进入界面 点击Downloads, ...

  4. opencv:图像的基本变换

    0.概述 图像变换的基本原理都是找到原图和目标图的像素位置的映射关系,这个可以用坐标系来思考,在opencv中, 图像的坐标系是从左上角开始(0,0),向右是x增加方向(cols),向下时y增加方向( ...

  5. nodejs利用express操作mysql增删改查

    如果不知道怎么连接数据库的请看http://www.cnblogs.com/complete94/p/6714757.html 我当大家都知道怎么连接数据库了,那么 我们开始吧 var express ...

  6. bzoj3000

    题解: n!k进制的位数 首先考虑n!十进制的位数 =log10(n!) 然后用阶乘近似公式 继而换底 得到答案 代码: #include<bits/stdc++.h> using nam ...

  7. 学会使用postman模拟http请求(转)

    原文链接:http://www.cnblogs.com/mafly/p/postman.html 这个东西关键时刻还是有点用的.因为有些时候你和前端工作不协调的话,自己动手,丰衣足食.确定自己没问题是 ...

  8. .net 枚举(Enum)使用总结

    在实际问题中,有些变量的取值被限定在一个有限的范围内.例如,一个星期内只有七天,一年只有十二个月,性别只有男跟女等等.如果把这些量说明为整型.字符型或其它类型显然是不妥当的.为此,C#提供了一种称为“ ...

  9. Linux:网络配置

    网络配置 1.网卡配置 vim /etc/sysconfig/network-scripts/ifcfg-eth0 1DEVICE=eth0 2 HWADDR=00:0C:29:93:41:2B 3 ...

  10. CString与输入输出流对象问题。

    在C++ 编程出现:cin>>Id;没有与这些操作匹配的">>"运算符: 你要看你的Id的数据类型,如果是CString等字符串,要用cin.getline ...