Astronomical Database

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+10;
using namespace std;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
int n,m,k,t,id,cnt;
struct node
{
    int to[36],num;
    char c[30];
}a[maxn];
char b[maxn];
void add(char*p)
{
    int now=0;
    for(int i=0;p[i];i++)
    {
        int x=(p[i]>=‘a‘&&p[i]<=‘z‘)?p[i]-‘a‘+10:p[i]-‘0‘;
        if(!a[now].to[x])a[now].to[x]=++id;
        now=a[now].to[x];
    }
    strcpy(a[now].c,p);
    a[now].num=1;
}
void dfs(int now)
{
    if(cnt>20)return;
    if(a[now].num)
    {
        if(cnt<=20)printf("  %s\n",a[now].c),cnt++;
        else return;
    }
    for(int i=0;i<=35;i++)
    {
        if(a[now].to[i])dfs(a[now].to[i]);
    }
}
void gao(char*p)
{
    printf("%s\n",p);
    int now=0,i;
    for(i=0;p[i];i++)
    {
        int x=(p[i]>=‘a‘&&p[i]<=‘z‘)?p[i]-‘a‘+10:p[i]-‘0‘;
        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[0]==‘+‘)
        {
            strcpy(b,b+1);
            add(b);
        }
        else
        {
            cnt=1;
            strcpy(b,b+1);
            gao(b);
        }
    }
    //system("Pause");
    return 0;
}
时间: 2024-10-13 11:47:24

Astronomical Database的相关文章

pbds(平板电视)?

比STL还STL?——平板电视 posted on 2018-08-07 14:08:04 | under 未分类 |  19 __gnu_pbds食用教程 ●QQ826755370 引入 某P党:“你们C++的STL库真恶心强大,好多数据结构和算法都不用手打.” C党1:“STL能省下的代码量又不多,平衡树多难调啊.” C党2:“欸?__gnu_pbds库就可以做到啊,它封装了hash,tree,trie,priority_queue这四种数据结构.” 正文 介绍 什么是__gnu_pbds?

oracle database resident connection pooling(驻留连接池)

oracle在11g中引入了database resident connection pooling(DRCP).在此之前,我们可以使用dedicated 或者share 方式来链接数据库,dedicated方式是oracle数据库默认的链接方式,无需过多的配置,而且关于dedicated的bug也是非常少的,因此,通常情况下,建议使用dedicated方式来链接数据库.但是,在服务器资源有限,并且同时连接数据库的用户量非常大时,dedicated方式就无能为力了.假设并发用户为5000,每个d

Oracle Database 12c Release 1下载安装(自身经历)

1.访问Oracle官网:https://www.oracle.com/index.html,下载Oracle Database 12c Release 1 (注意:File1和File2都要下载!!不然后期安装会报一堆错误,可参考:http://www.2cto.com/database/201503/386272.html) 2.将文件解压,把winx64_12102_database_2of2文件夹中database\stage\components目录下的所有文件夹,复制到winx64_

The SQL Server Service Broker for the current database is not enabled

把一个数据恢复至另一个服务器上,出现了一个异常: The SQL Server Service Broker for the current database is not enabled, and as a result query notifications are not supported.  Please enable the Service Broker for this database if you wish to use notifications. 截图如下: 解决方法: 参

Mac&#160;下locate命令使用问题WARNING: The locate database (/var/db/locate.database) does not exist.

想在Mac下使用locate时,提醒数据库没创建: WARNING: The locate database (/var/db/locate.database) does not exist. To create the database, run the following command: sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist Please be aware that the d

backup, file manipulation operations (such as ALTER DATABASE ADD FILE) and encryption changes on a database must be serialized.

昨天在检查YourSQLDba备份时,发现有台数据库做备份时出现了下面错误信息,如下所示: <Exec>   <ctx>yMaint.ShrinkLog</ctx>   <inf>Log Shrink</inf>   <Sql> --  ======================================================================== -- Shrink of log file E:\SQ

Windows 7 64bit上安装Oracle Database 12c [INS-30131] 错误的解决方法

Windows 7 64bit上安装Oracle Database 12c,出现以下错误: 解决方法: 第一步:控制面板>所有控制面板项>管理工具>服务>SERVER  启动 第二步:控制面板>所有控制面板项>管理工具>计算机管理>系统工具>共享文件夹>共享   右键单击“共享”>新建共享> 点击“下一步”>   单击“浏览”> 选择"本地磁盘(C:)">确定   单击“下一步”:     单击“

oracle 11g RMAN:Active Database Duplication for standby database 创建dg 命令解读

基于生产的duplicate 复制,如果事先没有手动配置pfile(设定内存,进程,dg相关配置参数,数据库相关路径参数)则会出现各种参数无法转换的问题: 因为duplicate 会从生产自动拷贝pfile,控制文件,密码文件过来,如果主库和备库环境不一样(数据库软件路径,数据文件存储路径) 如果没在duplicate的命令中完整指定新环境备库的各种参数涉及路径,及转换参数,就会默认使用从主库拷贝过来的spfile中的参数设置(会忽略oracle 自定义的的缺省配置:比如adr 缺省会放在ORA

Magento database maintenance

OverviewThis article examines the various ways to maintain an efficient Magento database, even when large in size. Magento does many things well, but maintaining an efficient database is not one of them. Having many products is a good reason to have