浅谈\(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. Linux内建命令和外部命令

    Linux命令有内部命令(内建命令)和外部命令之分,内部命令和外部命令功能基本相同,但也有些细微差别. [内部命令 vs. 外部命令] (1)内部命令实际上是shell程序的一部分,其中包含的是一些比 ...

  2. 常用的SpringMVC注解

    1.@RequestMapping() 访问链接编写: 例如: (1).请求方法: 访问链接: (2).请求参数和请求头: 访问链接: 2.@PathVariable 例如: 访问链接: 结果显示: ...

  3. canvas之图形的变化(平移,缩放,旋转)

    1.保存与恢复canvas状态 ctx.save();暂时将当前的状态保存到堆中 ctx.restore();该方法用于将上一个保存的状态从堆中再次取出,恢复该状态的所有设置. <!DOCTYP ...

  4. tomcat安装图文教程

    tomcat安装图文教程 运维 memory 发布于June 1, 2013 标签: Windows, Tomcat 下载Tomcat安装文件,官方下载地址是:http://tomcat.apache ...

  5. Ansible 小手册系列 十六(Playbook Debug)

    debug模块在执行期间打印语句,并且可用于调试变量或表达式,而不必停止playbook. 打印自定义的信息 - debug: msg="System {{ inventory_hostna ...

  6. 小练习:补数 (Number Complement)

    1.eamples Input: Output: Explanation: The binary representation of (no leading zero bits), and its c ...

  7. dell c6220II lsi阵列卡

    1.如果在lsi阵列卡上有多个raid,那么需要在第一个创建的raid上装系统,或者说先创建装系统的raid,否则可能报 hard disk error(centos 6.6) 2.热插拔的后果:如果 ...

  8. JTable的应用

    最近项目中使用到一个table表格,表格的样子如下: 可以修改数量,以及折扣,对应的最终价会相应的变化. 随手写了份插件,命名为JTable,可以给热爱jquery 的友友们一个参考: 代码如下: / ...

  9. certbot以standalone方式新建密钥

    下载:wget https://dl.eff.org/certbot-auto 授权:chmod a+x ./certbot-auto 快捷命令 ./certbot-auto certonly --t ...

  10. Spring的AOP介绍

    AOP:(Aspect-Orlented-Programming)面向切面编程,和面向对象是互相补充的.面向对象是横着编程,面向切面则是竖着编程. 1 2 3 4 @Before("exec ...