Autocompletion
Time limit: 2.0 second
Memory limit: 64 MB
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+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,q[maxn],to[maxn],id; bool flag; char ans[15001][11][21]; struct node { char b[21]; int num; bool operator<(const node&p)const { if(num==p.num)return strcmp(b,p.b)<0; else return num>p.num; } }a[maxn]; struct node1 { int nxt[26],num; }b[maxn]; char c[21]; void add(char*c,int tot) { int now=0; for(int i=0;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=0; for(int i=0;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]<=9) { 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,1,n)scanf("%s %d",a[i].b,&a[i].num); sort(a+1,a+n+1); scanf("%d",&m); rep(i,1,m)scanf("%s",c),add(c,i); rep(i,1,n)gao(a[i].b); rep(i,1,m) { if(flag)printf(" ");else flag=true; rep(j,0,q[to[i]]-1)printf("%s ",ans[to[i]][j]); } //system("Pause"); return 0; }