ural1542 Autocompletion
Autocompletion
Memory limit: 64 MB
Input
Output
Sample
| input | output |
|---|---|
5 |
kanojo |
分析:若对每个询问走一遍字典树是超时的;
对于询问建立字典树,然后将排好序的单词依次插入就得到了答案;
代码:
#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的更多相关文章
- 拷贝内容到eclipse中导致JSP的auto-completion不工作
刚才在编辑JSP文件,有一些东西我懒得敲了,就把一些代码里面拷贝到eclipse的editor中,结果你猜怎么,拷贝进去以后,jsp的auto-completion居然不工作了!(即<%%> ...
- 关于Jupyter Notebook无法自动补全(Autocompletion),报错TypeError: __init__() got an unexpected keyword argument 'column' 的解决方案
关于Jupyter Notebook无法自动补全(Autocompletion),报错TypeError: __init__() got an unexpected keyword argument ...
- Eclipse中Editor开启Auto-completion
Java Editor .abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ Java Script Editor 现在Eclipse限制使用最多 ...
- eclipse配置代码自动补全auto-completion
你如果使用的是JAVA EE的模式,就这样配置: 1. Window>Preferences>Java>Editor>Content Assist>Auto Activa ...
- URAL 1542. Autocompletion 字典树
给你最多10w个单词和相应的频率 接下来最多1w5千次询问 每次输入一个字符串让你从前面的单词中依照频率从大到小输出最多10个以该字符串为前缀的单词 開始把单词建成了字典树 然后每次询问找到全部满足条 ...
- Android学习路线
第一阶段:Java面向对象编程 1.Java基本数据类型与表达式,分支循环. 2.String和StringBuffer的使用.正则表达式. 3.面向对象的抽象,封装,继承,多态,类与对象,对象初始化 ...
- wxPython 自动提示文本框
1.原版和例子都在这里 在浏览器的地址栏,或者在百度.google 输入文字的时候,输入框的下面会把有关的项目都提示出来. wxPython 没有提供类似的控件,google 了一下,发现了一个,很好 ...
- 十个免费的web应用安全检测工具
Websites are getting more and more complex everyday and there are almost no static websites being bu ...
- 我的emacs配置
我的emacs配置文件 ;; .emacs ;; ============================== Basic Configure START ====================== ...
随机推荐
- js 手动轮播和自动轮播
$(function(){ //默认值 $("#carousel1").css("background-color","#FFF"); // ...
- URL scheme添加以及查找方式
2.1.1 添加URL Types URL Scheme是通过系统找到并跳转对应app的一类设置,通过向项目中的info.plist文件中加入URL types可使用第三方平台所注册的appkey信 ...
- 翻译-你必须知道的28个HTML5特征、窍门和技术
摘自by zhangxinxu from http://www.zhangxinxu.com 本文地址:http://www.zhangxinxu.com/wordpress/?p=1058 前端的发 ...
- <转>如何高效快速看懂Android源码
原网址:http://jingyan.baidu.com/article/574c5219ca78ed6c8d9dc12a.html 在Android系统上工作了一段时间,经常会遇到题目中的问题,下面 ...
- DOM操作-引用同级的元素
代码: ———————————————————————————————— <script type="text/javascript"> //获取 ...
- hdu_5793_A Boring Question(打表找规律)
题目链接:hdu_5793_A Boring Question 题意: 自己看吧,说不清楚了. 题解: 打表找规律 #include<cstdio> typedef long long l ...
- Boxes in a Line(移动盒子)
You have n boxes in a line on the table numbered 1 . . . n from left to right. Your task is to sim ...
- 第3章 Java语言基础----static
1.static只能声明成员变量,不能声明局部变量,如下图所示: 2.如果变量在类中用static中定义过,那么在方法中就可以直接赋值了:如果没有在类中定义,则不能在方法中使用,还得重新定义,如下图所 ...
- [转]php 在各种web服务器的运行模式
一.php在apache中运行模式 php在apache中一共有三种工作方式:CGI模式.FastCGI模式.Apache 模块DLL) 以下分别比较: 1. CGI模式与模块模式比较: php在ap ...
- MD5 32位加密算法源码(测试通过)(系转载 飞扬天下)
供自己学习使用 md5.h文件 #ifndef MD5_H #define MD5_H #include <string> #include <fstream> /* Type ...