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
?e
+earth
+egg
?e
+eagle
+earth
?ea
e
e
..earth
..egg
ea
..eagle
..earth

分析:简单的trie树;

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

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=1e6+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
int n,m,k,t,id,cnt;
struct node
{
int to[],num;
char c[];
}a[maxn];
char b[maxn];
void add(char*p)
{
int now=;
for(int i=;p[i];i++)
{
int x=(p[i]>='a'&&p[i]<='z')?p[i]-'a'+:p[i]-'';
if(!a[now].to[x])a[now].to[x]=++id;
now=a[now].to[x];
}
strcpy(a[now].c,p);
a[now].num=;
}
void dfs(int now)
{
if(cnt>)return;
if(a[now].num)
{
if(cnt<=)printf(" %s\n",a[now].c),cnt++;
else return;
}
for(int i=;i<=;i++)
{
if(a[now].to[i])dfs(a[now].to[i]);
}
}
void gao(char*p)
{
printf("%s\n",p);
int now=,i;
for(i=;p[i];i++)
{
int x=(p[i]>='a'&&p[i]<='z')?p[i]-'a'+:p[i]-'';
if(a[now].to[x])now=a[now].to[x];
else break;
}
if(!p[i])dfs(now);
}
int main()
{
int i,j;
add("sun");
while(~scanf("%s",b))
{
if(b[]=='+')
{
strcpy(b,b+);
add(b);
}
else
{
cnt=;
strcpy(b,b+);
gao(b);
}
}
//system("Pause");
return ;
}

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. 贪心+bfs 或者 并查集 Codeforces Round #268 (Div. 2) D

    http://codeforces.com/contest/469/problem/D 题目大意: 给你一个长度为n数组,给你两个集合A.B,再给你两个数字a和b.A集合中的每一个数字x都也能在a集合 ...

  2. oomph

    http://blog.csdn.net/u011004037/article/details/45679573 这么好个功能起了这么操蛋个名字害得老子一直不知道他干啥的

  3. JPA 系列教程6-单向多对多

    JPA中的@ManyToMany @ManyToMany注释表示模型类是多对多关系的一端. @JoinTable 描述了多对多关系的数据表关系. name 属性指定中间表名称 joinColumns ...

  4. runtime获取一个控件的所有属性

    控件的有些属性API并没有开放,可以通过runtime查看: unsigned int count; Ivar *ivarList = class_copyIvarList([UITextField ...

  5. Properties集合

    Map |--Hashtable |--Properties Properties集合特点: 1.该集合中的键和值都是字符串类型 2.集合中的数据可以保存在IO流中或者从IO流中获取数据. 通常该集合 ...

  6. 怎样将MySQL数据库上传到服务器

    首先,需要将本地的数据库导出来,作为一个数据文件,以备稍后上传到服务器用,在本地登陆phpmyadmin控制面板: 登陆成功后,在左侧选择需要操作的数据库: 选择后,页面会自动刷新,然后再在右边点击[ ...

  7. initWithFrame、initWithCoder、awakeFromNib的区别和调用次序 & UIViewController生命周期 查缺补漏

    当我们创建或者自定义一个UI控件时,就很可能会调用awakeFromNib.initWithCoder .initWithFrame这些方法.三者的具体区别如下: initWithFrame: 通过代 ...

  8. Jenkins email-ext邮件通知模板

    http://blog.csdn.net/houyefeng/article/details/51344337 示例 以html格式发送送如下内容:邮件内容为项目名称.构建次数.触发原因.构建日志前1 ...

  9. POJ 1845 Sumdiv (整数拆分+等比快速求和)

    当我们拆分完数据以后, A^B的所有约数之和为: sum = [1+p1+p1^2+...+p1^(a1*B)] * [1+p2+p2^2+...+p2^(a2*B)] *...*[1+pn+pn^2 ...

  10. android 多个shortCut快捷方式实现以及对58同城快捷方式的实现思路的研究

    这几天,项目中有个新需求,需要按照模块添加不同的快捷方式到桌面上,从而方便用户的使用.特意进行了研究并分析了下58上面桌面快捷方式的实现. 首先多个shortcut的实现: <activity ...