#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5+2e4+11;
const int dep = 666;
const int len = 30;
struct AAA{
string str;
int cnt;
}a[maxn];
bool cmp(AAA a,AAA b){
if(a.cnt!=b.cnt) return a.cnt>b.cnt;
// return a.str<b.str;
return lexicographical_compare(a.str.begin(),a.str.end(),b.str.begin(),b.str.end());
}
struct TRIE{
int son[dep][len];
bool edp[maxn];//is endPoint?
vector<int> vec[maxn];//idx of str
int tot,root;
int node(){
for(int i = 0; i < len; i++) son[tot][i]=-1;
edp[tot]=0;
return tot++;
}
void init(){
memset(son,-1,sizeof son);
memset(vec,0,sizeof vec);
tot=0;
root=node();
}
void insert(char str[]){
int now=root;
int llen = strlen(str);
for(int i = 0; i < llen; i++){
int idx=str[i]-'a';
if(son[now][idx]==-1) son[now][idx]=node();
now=son[now][idx];
}
edp[now]=1;
}
// bool asprefix(char str[]){
// int llen=strlen(str);
// int now=root;
// bool reach=0;
// for(int i = 0; i < llen; i++){
// int idx=str[i]-'a';
// if(son[now][idx]==-1&&reach) return 1;
// if(son[now][idx]==-1&&!reach) return 0;
// now=son[now][idx];
// if(edp[now]) reach=1;
// }
// return reach;
// }
void insert(string str,int k){
int now=root;
int llen=str.length();
for(int i = 0; i < llen; i++){
int idx = str[i]-'a';
if(son[now][idx]==-1) break;
now=son[now][idx];
if(edp[now]&&vec[now].size()<10) vec[now].push_back(k);
}
}
int getid(char str[]){
int now=root;
int llen=strlen(str);
for(int i = 0; i < llen; i++){
int idx=str[i]-'a';
now=son[now][idx];
}
return now;
}
}trie;
char s[maxn][30];
int main(){
ios::sync_with_stdio(false);
int n,m;
while(cin>>n){
for(int i = 1; i <= n; i++){cin>>a[i].str>>a[i].cnt;}
cin>>m;
trie.init();
for(int i = 1; i <= m; i++){
cin>>s[i];
trie.insert(s[i]);
}
sort(a+1,a+n,cmp);
for(int i = 1; i <= n; i++){
trie.insert(a[i].str,i);
}
for(int i = 1; i <= m; i++){
int id=trie.getid(s[i]);
if(i>1) cout<<endl;
for(int j = 0; j < trie.vec[id].size(); j++){
cout<<a[trie.vec[id][j]].str<<endl;
}
}
}
return 0;
}

待修改 URAL 1542的更多相关文章

  1. URAL 1542. Autocompletion 字典树

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

  2. URAL 1934 Black Spot --- 最短的简单修改

    右侧是1.维护的同时保持最短路p值至少,我有直接存款(1-p).该概率不满足,为了使这个值极大. #include <iostream> #include <cstdlib> ...

  3. ural 2062 Ambitious Experiment

    2062. Ambitious Experiment Time limit: 3.0 secondMemory limit: 128 MB During several decades, scient ...

  4. ural 1251. Cemetery Manager

    1251. Cemetery Manager Time limit: 1.0 secondMemory limit: 64 MB There is a tradition at the USU cha ...

  5. HDU 1542 Atlantis(线段树扫描线+离散化求面积的并)

    Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  6. ●线段树的三个题(poj 3225,hdu 1542,hdu 1828)

    ●poj 3225 Help with Intervals(线段树区间问题) ○赘述题目 给出以下集合操作: 然后有初始的一个空集S,和以下题目给出的操作指令,并输入指令: 要求进行指令操作后,按格式 ...

  7. 【BZOJ1814】Ural 1519 Formula 1 (插头dp)

    [BZOJ1814]Ural 1519 Formula 1 (插头dp) 题面 BZOJ Vjudge 题解 戳这里 上面那个链接里面写的非常好啦. 然后说几个点吧. 首先是关于为什么只需要考虑三进制 ...

  8. HDU 1542 - Atlantis - [线段树+扫描线]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1542 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...

  9. HDU 1754 I Hate It<区间最值 单点修改>

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

随机推荐

  1. 数据从HDFS-->HIVE-->HBASE 执行过程

    1.数据已经load进去hdfs 2.hive.hbase已经安装成功(我用的是hadoop 2.4 hbase 0.98.12 hive 1.2.1) 3.开始! 4.在hive建立表同时生成对应的 ...

  2. 【259】ucpole.dat update

    2017年2月21日 57871 +0.020896 0.007232 +0.414732 0.009212 +0.418044 0.007533 p 57872 +0.022055 0.007284 ...

  3. jmeter CSV Data数据中带有逗号解决方法

    今天用jmeter做性能测试,由于参数的数据中含有逗号,一直失败,尝试了几次终于成功,先写下经验 首先看设置 E:\apache-jmeter-2.12\bin\litaojunzb.csv文件格式如 ...

  4. C语言-郝斌笔记-007是否为素数

    是否为素数 # include <stdio.h> bool IsPrime(int val) { int i; ; i<val; ++i) { ) break; } if (i = ...

  5. java中字符串处理、串联和转换的几个常用方法,以及如果需要自己编程实现的具体实施步骤。

    What? 如何分类? 如何使用? //String 类提供处理 Unicode 代码点(即字符)(TT观点:指的莫非就是对象的意思.)和 Unicode 代码单元(即 char 值)的方法.(TT观 ...

  6. Inheritance with EF Code First: Part 2 – Table per Type (TPT)

    In the previous blog post you saw that there are three different approaches to representing an inher ...

  7. SDUT 3374 数据结构实验之查找二:平衡二叉树

    数据结构实验之查找二:平衡二叉树 Time Limit: 400MS Memory Limit: 65536KB Submit Statistic Problem Description 根据给定的输 ...

  8. ios MVC笔记

    MVC:Model 模型 View 视图 Controller  控制器 关系图:

  9. python:格式化输出整数

    import math #default print "PI = %f" % math.pi #width = 10,precise = 3,align = left print ...

  10. 十天入门java教程 Day01

    这几年一直在想学一门语言,java,Python,php也都看过,但是没有一门是精的.语言学习并不是写出hello world就行了.个人感觉需要静心去学习. java语言是什么? java是一种计算 ...