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

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

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

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

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

代码如下:

  1. #include <cstdio>
  2. #include <cstring>
  3. using namespace std;
  4. const int maxn=1e5+5;
  5. char s[maxn<<1],s1[maxn],s2[maxn];
  6. struct Trie {
  7. int tot;
  8. char s[maxn*10][15];
  9. int son[maxn*10][26];
  10. void ins() {
  11. int pos=1,len=strlen(s2+1);
  12. for(int i=1;i<=len;i++) {
  13. if(son[pos][s2[i]-'a'])pos=son[pos][s2[i]-'a'];
  14. else pos=son[pos][s2[i]-'a']=++tot;
  15. }
  16. len=strlen(s1+1);
  17. for(int i=1;i<=len;i++)
  18. s[pos][i]=s1[i];
  19. }
  20. void find() {
  21. int pos=1,len=strlen(s1+1);
  22. for(int i=1;i<=len;i++) {
  23. if(son[pos][s1[i]-'a'])pos=son[pos][s1[i]-'a'];
  24. else {puts("eh");return;}
  25. }
  26. if(!strlen(s[pos]+1))puts("eh");
  27. else printf("%s\n",s[pos]+1);
  28. }
  29. }T;
  30. int main() {
  31. T.tot=1;
  32. while(1) {
  33. int n,pos;
  34. scanf("%[^\n]",s+1);getchar();
  35. if((n=strlen(s+1))==0)break;
  36. for(int i=1;i<=n;i++)
  37. if(s[i]==' ') {pos=i;break;}
  38. for(int i=1;i<pos;i++)s1[i]=s[i];
  39. for(int i=pos+1;i<=n;i++)s2[i-pos]=s[i];
  40. T.ins(),memset(s,0,(n+1)*4);
  41. }
  42. while(~scanf("%s",s1+1))T.find();
  43. return 0;
  44. }

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. 你真的掌握 LVS、Nginx 及 HAProxy 的工作原理吗

    你真的掌握 LVS.Nginx 及 HAProxy 的工作原理吗 当前大多数的互联网系统都使用了服务器集群技术,集群是将相同服务部署在多台服务器上构成一个集群整体对外提供服务,这些集群可以是 Web ...

  2. 关于mybatis mapper.xml中的if判断

    场景: 页面上有搜索框进行调节查询,不同搜索框中的内容可以为空. 过程: 点击搜索,前端把参数传给后台,这是后台要把为空的参数过滤掉. 做法: 通常我们在dao层即mapper.xml中进行过滤判断操 ...

  3. nyoj151——中国剩余定理

    生理周期 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 142220   Accepted: 45744 Descripti ...

  4. no persistent classes found for query class: FROM com.vrv.paw.domain.User

    在整合Spring+Hibernate时报该错误,sessionFactory配置如下: <bean id="sessionFactory" class="org. ...

  5. 使用libimobiledevice + ifuse提取iOS沙盒文件

    简介 libimobiledevice:一个开源包,可以让Linux支持连接iPhone/iPod Touch等iOS设备. Git仓库: https://github.com/libimobiled ...

  6. 三、dbms_pipe(类似UNIX系统的管道)

    1.概述 说明:Oracle管道类似UNIX系统的管道,但不采用OS机制实现,管道信息被缓存到SGA中,当关闭例程时会丢失管道信息,建立公用管道所有数据库用户都可访问,私有管道只能由建立这访问.作用: ...

  7. 【javascript基础】运算符优先级

    优先级 运算类型 关联性 运算符 1 成员运算符 从左到右 . [] new 从右到左 new 2 函数调用运算符 从左到右 () 3 自增运算符 n/a ++ 自减运算符 n/a -- 4 逻辑非运 ...

  8. javascript: 对象2

    数字对象Number Number 对象表示数值日期,整数或浮点数.一般情况下,你不需要担心 Number 对象,因为浏览器自动将数字文 本转换为数字类的实例. 语法 创建一个 Number 对象: ...

  9. flask中过滤器的使用

    过滤器 过滤器的本质就是函数.有时候我们不仅仅只是需要输出变量的值,我们还需要修改变量的显示,甚至格式化.运算等等,而在模板中是不能直接调用 Python 中的某些方法,那么这就用到了过滤器. 使用方 ...

  10. 求小于等于k长度的最大区间和

    题意 给出一个序列,求长度小于等于k的最大区间和并输出起点和终点 1<=n<=100000 1<=k<=n   题解:先算出前缀和,利用单调队列的性质,在单调队列中存储sum[ ...