Autocompletion

Time limit: 2.0 second
Memory limit: 64 MB
The Japanese are infinitely in love with machinery that surrounds them. They follow closely all technical innovations and try to use the most modern and clever of them. Den and Sergey have an ingenious plan: they want to create a text editor that will win the Japanese over. The most important feature of the editor will be the autocompletion function. If a user has typed first several letters of a word, then the editor will automatically suggest the most probable endings.
Den and Sergey have collected a lot of Japanese texts. For each Japanese word they counted the number of times it was found in the texts. For the first several letters entered by a user, the editor must show no more than ten words starting with these letters that are most commonly used. These words will be arranged in the order of decreasing encounter frequencies.
Help Sergey and Den to turn over the market of text editors.

Input

The first line contains the number of words found in the texts N (1 ≤ N ≤ 105). Each of the following N lines contains a word wi and an integer ni separated with a space, where wi is a nonempty sequence of lowercase Latin letters no longer than 15 symbols, and ni (1 ≤ ni ≤ 106) is the number of times this word is encountered in the texts. The (N + 2)th line contains a number M(1 ≤ M ≤ 15000). In each of the next M lines there is a word ui (a nonempty sequence of lowercase Latin letters no longer than 15 symbols), which is the beginning of a word entered by a user.

Output

For each of the M lines, output the most commonly used Japanese words starting with ui in the order of decreasing encounter frequency. If some words have equal frequencies, sort them lexicographically. If there are more than ten different words starting with the given sequence, output the first ten of them. The lists of words for each ui must be separated by an empty line.

Sample

input output
5
kare 10
kanojo 20
karetachi 1
korosu 7
sakura 3
3
k
ka
kar
kanojo
kare
korosu
karetachi kanojo
kare
karetachi kare
karetachi

分析:若对每个询问走一遍字典树是超时的;

   对于询问建立字典树,然后将排好序的单词依次插入就得到了答案;

代码:

#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,q[maxn],to[maxn],id;
bool flag;
char ans[][][];
struct node
{
char b[];
int num;
bool operator<(const node&p)const
{
if(num==p.num)return strcmp(b,p.b)<;
else return num>p.num;
}
}a[maxn];
struct node1
{
int nxt[],num;
}b[maxn];
char c[];
void add(char*c,int tot)
{
int now=;
for(int i=;c[i];i++)
{
int x=c[i]-'a';
if(!b[now].nxt[x])b[now].nxt[x]=++id;
now=b[now].nxt[x];
}
if(!b[now].num)b[now].num=tot,to[tot]=tot;
else to[tot]=b[now].num;
}
void gao(char*c)
{
int now=;
for(int i=;c[i];i++)
{
int x=c[i]-'a';
if(b[now].nxt[x])
{
int y=b[now].nxt[x];
if(b[y].num&&q[b[y].num]<=)
{
strcpy(ans[b[y].num][q[b[y].num]++],c);
}
now=b[now].nxt[x];
}
else break;
}
}
int main()
{
int i,j;
scanf("%d",&n);
rep(i,,n)scanf("%s %d",a[i].b,&a[i].num);
sort(a+,a+n+);
scanf("%d",&m);
rep(i,,m)scanf("%s",c),add(c,i);
rep(i,,n)gao(a[i].b);
rep(i,,m)
{
if(flag)printf("\n");else flag=true;
rep(j,,q[to[i]]-)printf("%s\n",ans[to[i]][j]);
}
//system("Pause");
return ;
}

ural1542 Autocompletion的更多相关文章

  1. 拷贝内容到eclipse中导致JSP的auto-completion不工作

    刚才在编辑JSP文件,有一些东西我懒得敲了,就把一些代码里面拷贝到eclipse的editor中,结果你猜怎么,拷贝进去以后,jsp的auto-completion居然不工作了!(即<%%> ...

  2. 关于Jupyter Notebook无法自动补全(Autocompletion),报错TypeError: __init__() got an unexpected keyword argument 'column' 的解决方案

    关于Jupyter Notebook无法自动补全(Autocompletion),报错TypeError: __init__() got an unexpected keyword argument ...

  3. Eclipse中Editor开启Auto-completion

    Java Editor .abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ Java Script Editor 现在Eclipse限制使用最多 ...

  4. eclipse配置代码自动补全auto-completion

    你如果使用的是JAVA EE的模式,就这样配置: 1. Window>Preferences>Java>Editor>Content Assist>Auto Activa ...

  5. URAL 1542. Autocompletion 字典树

    给你最多10w个单词和相应的频率 接下来最多1w5千次询问 每次输入一个字符串让你从前面的单词中依照频率从大到小输出最多10个以该字符串为前缀的单词 開始把单词建成了字典树 然后每次询问找到全部满足条 ...

  6. Android学习路线

    第一阶段:Java面向对象编程 1.Java基本数据类型与表达式,分支循环. 2.String和StringBuffer的使用.正则表达式. 3.面向对象的抽象,封装,继承,多态,类与对象,对象初始化 ...

  7. wxPython 自动提示文本框

    1.原版和例子都在这里 在浏览器的地址栏,或者在百度.google 输入文字的时候,输入框的下面会把有关的项目都提示出来. wxPython 没有提供类似的控件,google 了一下,发现了一个,很好 ...

  8. 十个免费的web应用安全检测工具

    Websites are getting more and more complex everyday and there are almost no static websites being bu ...

  9. 我的emacs配置

    我的emacs配置文件 ;; .emacs ;; ============================== Basic Configure START ====================== ...

随机推荐

  1. Oracle教程-常用命令(二)

    oracle sql*plus常用命令 一.sys用户和system用户Oracle安装会自动的生成sys用户和system用户(1).sys用户是超级用户,具有最高权限,具有sysdba角色,有cr ...

  2. windows做时间服务器,linux和windows时间同步

    找了很多的资料,都没有windows做时间服务,linux同步windows的时间的,最后自己找了一些软件,终于搞定了,写出来给大家共享,以免大家多走弯路 首先在http://www.meinberg ...

  3. hdu_5768_Lucky7(中国剩余定理+容斥)

    题目链接:hdu_5768_Lucky7 题意: 给你一个区间,问你这个区间内是7的倍数,并且满足%a[i]不等于w[i]的数的个数 乍一看以为是数位DP,仔细看看条件,发现要用中国剩余定理,然后容斥 ...

  4. 用ios做的一个简单的记事本

    #import "ViewController.h" @interface ViewController ()@property (weak, nonatomic) IBOutle ...

  5. 关于微信分享功能开发的一些bug

    wx.onMenuShareTimeline({//onMenuShareTimeline title: (h('#mainForm').children('.content').inf('value ...

  6. linux CTRL+Z

    关于 linux 系统中使用ctrl+Z 的使用 (1) CTRL+Z停止进程并放入后台 (2) jobs 显示当前暂停的进程 (3) bg %N 使第N个任务在后台运行(%前有空格) ,N 为任务号 ...

  7. Heartbeat+DRBD+MySQL高可用方案【转】

    转自Heartbeat+DRBD+MySQL高可用方案 - yayun - 博客园 http://www.cnblogs.com/gomysql/p/3674030.html 1.方案简介 本方案采用 ...

  8. MySQL 索引 总结

    1.索引的种类(六种) 普通索引,唯一索引,全文索引,单列索引,多列索引,空间索引 2.优缺点及注意事项 优点:有了索引,对于记录数量很多的表,可以提高查询速度. 缺点:索引是占用空间的,索引会影响u ...

  9. 【转】linux ls -l的详解

    原文:http://blog.csdn.net/sjzs5590/article/details/8254527 以root的家目录为例: 可以看到,用ls -l命令查看某一个目录会得到一个7个字段的 ...

  10. Openlays 3 绘制基本图形

    <body> <div id="menu"> <label>几何图形类型:</label> <select id=" ...