Astronomical Database

Time limit: 2.0 second
Memory limit: 64 MB
After the Hubble telescope had been orbited the number of known stars increased. Imagine how it will grow in the future when the mankind masters the hyperspace jump!
The farsighted astronomers want to get ready for that moment right now. They create a control system for a database of all known stars. The database will be multiuser and astronomers all over the world will be able to fill it with useful data. To improve the software's usability it's necessary to implement the popup prompting: when a new character is typed, the software must suggest the list of stars whose names start with the already typed characters. You are to help astronomers with their cosmic problem and to develop a prototype of the algorithm that will be used in the database control system in the future.

Input

Each input line consists of one operation. The first character denotes the type of the operation. The rest of the characters (small Latin letters or digits) are the operation's argument.
The operation types are as follows:
'+' — add a star's name to the database. The argument of this operation is the name of the star that is to be added to the database. As the database is multiuser, the information concerning one star may be added several times. When the program is launched, the database contains a single word "sun".
'?' — find all the names that start with the characters given in the operation's argument.
There are not more than 10000 operations in the input and all the arguments contain not less than one and not more than 20 characters.

Output

For a given '+' operation you should output nothing.
For each '?' operation output a respond to the query: the argument of the operation and then a list of stars' names that start with the given characters and that are presented in the database at the moment of the query.
The stars' names must be given in the lexicographical order, one in each line without repetitions.
If the length of the resulting list exceeds 20, then you should output the first 20 names only. Each name must be preceded by two spaces as it is shown in the sample (the spaces are replaced with dots in order to make the sample more illustrative).

Sample

input output
  1. ?e
  2. +earth
  3. +egg
  4. ?e
  5. +eagle
  6. +earth
  7. ?ea
  1. e
  2. e
  3. ..earth
  4. ..egg
  5. ea
  6. ..eagle
  7. ..earth

分析:简单的trie树;

   注意一开始就有sun,还有样例的.其实是空格,英语渣了。。。

代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <climits>
  7. #include <cstring>
  8. #include <string>
  9. #include <set>
  10. #include <map>
  11. #include <queue>
  12. #include <stack>
  13. #include <vector>
  14. #include <list>
  15. #define rep(i,m,n) for(i=m;i<=n;i++)
  16. #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
  17. #define mod 1000000007
  18. #define inf 0x3f3f3f3f
  19. #define vi vector<int>
  20. #define pb push_back
  21. #define mp make_pair
  22. #define fi first
  23. #define se second
  24. #define ll long long
  25. #define pi acos(-1.0)
  26. #define pii pair<int,int>
  27. #define Lson L, mid, rt<<1
  28. #define Rson mid+1, R, rt<<1|1
  29. const int maxn=1e6+;
  30. using namespace std;
  31. ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
  32. ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
  33. int n,m,k,t,id,cnt;
  34. struct node
  35. {
  36. int to[],num;
  37. char c[];
  38. }a[maxn];
  39. char b[maxn];
  40. void add(char*p)
  41. {
  42. int now=;
  43. for(int i=;p[i];i++)
  44. {
  45. int x=(p[i]>='a'&&p[i]<='z')?p[i]-'a'+:p[i]-'';
  46. if(!a[now].to[x])a[now].to[x]=++id;
  47. now=a[now].to[x];
  48. }
  49. strcpy(a[now].c,p);
  50. a[now].num=;
  51. }
  52. void dfs(int now)
  53. {
  54. if(cnt>)return;
  55. if(a[now].num)
  56. {
  57. if(cnt<=)printf(" %s\n",a[now].c),cnt++;
  58. else return;
  59. }
  60. for(int i=;i<=;i++)
  61. {
  62. if(a[now].to[i])dfs(a[now].to[i]);
  63. }
  64. }
  65. void gao(char*p)
  66. {
  67. printf("%s\n",p);
  68. int now=,i;
  69. for(i=;p[i];i++)
  70. {
  71. int x=(p[i]>='a'&&p[i]<='z')?p[i]-'a'+:p[i]-'';
  72. if(a[now].to[x])now=a[now].to[x];
  73. else break;
  74. }
  75. if(!p[i])dfs(now);
  76. }
  77. int main()
  78. {
  79. int i,j;
  80. add("sun");
  81. while(~scanf("%s",b))
  82. {
  83. if(b[]=='+')
  84. {
  85. strcpy(b,b+);
  86. add(b);
  87. }
  88. else
  89. {
  90. cnt=;
  91. strcpy(b,b+);
  92. gao(b);
  93. }
  94. }
  95. //system("Pause");
  96. return ;
  97. }

ural1414 Astronomical Database的更多相关文章

  1. 1414. Astronomical Database(STL)

    1414 破题 又逼着用stl 卡内存 trie树太耗了 水不过去 用set存字符串 set可以自己按一定顺序存 且没有重复的 再用lower_bound二分查找字符串的第一次出现 接着往后找就行了 ...

  2. [洛谷日报第39期]比STL还STL?——pbds

    [洛谷日报第39期]比STL还STL?——pbds   洛谷科技 发布时间:18-08-3116:37 __gnu_pbds食用教程 引入 某P党:“你们C++的STL库真强(e)大(xin),好多数 ...

  3. Database Replay和Consolidated Database replay

    简介 在数据库的迁移和升级场景中,我们经常会遇到一个问题:在做压力测试时,如何模拟真实的业务压力,解决这个问题的方法有很多,比如:应用方开发模拟程序或者使用压力测试工具模拟,如load runner, ...

  4. Oracle Database 12c Data Redaction介绍

    什么是Data Redaction Data Redaction是Oracle Database 12c的高级安全选项之中的一个新功能,Oracle中国在介绍这个功能的时候,翻译为“数据编纂”,在EM ...

  5. 使用技术手段限制DBA的危险操作—Oracle Database Vault

    概述 众所周知,在业务高峰期,某些针对Oracle数据库的操作具有很高的风险,比如修改表结构.修改实例参数等等,如果没有充分评估和了解这些操作所带来的影响,这些操作很可能会导致故障,轻则导致应用错误, ...

  6. 【Oracle 集群】ORACLE DATABASE 11G RAC 知识图文详细教程之集群概念介绍(一)

    集群概念介绍(一)) 白宁超 2015年7月16日 概述:写下本文档的初衷和动力,来源于上篇的<oracle基本操作手册>.oracle基本操作手册是作者研一假期对oracle基础知识学习 ...

  7. ERROR 1010 (HY000): Error dropping database (can't rmdir './test/', errno: 17)

    在删除数据库的时候报标题所示错误 mysql> drop database test; ERROR (HY000): Error dropping database (can't rmdir ' ...

  8. Oracle 集群】ORACLE DATABASE 11G RAC 知识图文详细教程之ORACLE集群概念和原理(二)

    ORACLE集群概念和原理(二) 概述:写下本文档的初衷和动力,来源于上篇的<oracle基本操作手册>.oracle基本操作手册是作者研一假期对oracle基础知识学习的汇总.然后形成体 ...

  9. 【Oracle 集群】ORACLE DATABASE 11G RAC 知识图文详细教程之RAC 工作原理和相关组件(三)

    RAC 工作原理和相关组件(三) 概述:写下本文档的初衷和动力,来源于上篇的<oracle基本操作手册>.oracle基本操作手册是作者研一假期对oracle基础知识学习的汇总.然后形成体 ...

随机推荐

  1. [转]Flash、Flex、AS3.0框架及类库资源收集之十全大补

    原文地址:http://www.d5power.com/portal.php?mod=view&aid=27 APIs.Libs.Components1.as3ebaylibhttp://co ...

  2. As3.0 视频缓冲、下载总结

    来源:http://www.cuplayer.com/player/PlayerCodeAs/2012/0913404.html 利用NetStream的以下属性: bufferTime — 缓冲区大 ...

  3. windows下使用git管理github项目

    1. 下载安装msysgithttp://code.google.com/p/msysgit/downloads/list2. 注册github账号3. 生成ssh公钥和私钥ssh-keygen -C ...

  4. 对象之function

    开头啰嗦几句:程序之中,万事万物皆对象,这就是面向对象编程的核心思想,比如我们的js中的function,其实也是一个对象,是对象就可以有属性,在jq的源码中经常看到类似这样的方法: jQuery.E ...

  5. docker容器和镜像区别

    这篇文章希望能够帮助读者深入理解Docker的命令,还有容器(container)和镜像(image)之间的区别,并深入探讨容器和运行中的容器之间的区别. 当我对Docker技术还是一知半解的时候,我 ...

  6. 使用HAXM加速Android虚拟机

    Android虚拟机在支持Intel VT技术的CPU上,可以使用HAXM(Hardware Accelerated Execution Manager)得到硬件加速支持,使得虚拟机运行速度得到极大提 ...

  7. CAPSPageMenu分页交互

    最近在开发过程中,我的前任在处理类似于新闻多板块的界面,在一个视图控制器里加载多个UITableView以显示不同类型的信息,并可通过头部按钮和左右滑动来切换不同的tableView这样的界面中,采取 ...

  8. poi设置excel表格边框、字体等

    POI中可能会用到一些需要设置EXCEL单元格格式的操作小结: 先获取工作薄对象: HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb ...

  9. Android 项目开发

    可以使用mapview.getMapCenter()获取当前可视范围中心点的坐标,然后计算出数据库中的点与中心点的距离值,如果该距离在触发显示的范围内(比如100米),就显示该点到地图上.百度地图的S ...

  10. PID控制学习笔记(二)

    不管是基本的PID控制还是变形的PID控制算法,其核心都是对输入信号(设定值信号.测量信号或者偏差信号等)做基本的比例.积分.微分运算,最终提供给被控过程良好的调节信号. 在过程控制仪表,特别是在数字 ...