BerOS File Suggestion(stl-map应用)
Polycarp is working on a new operating system called BerOS. He asks you to help with implementation of a file suggestion feature.
There are n
files on hard drive and their names are f1,f2,…,fn. Any file name contains between 1 and 8
characters, inclusive. All file names are unique.
The file suggestion feature handles queries, each represented by a string s
. For each query s it should count number of files containing s as a substring (i.e. some continuous segment of characters in a file name equals s
) and suggest any such file name.
For example, if file names are "read.me", "hosts", "ops", and "beros.18", and the query is "os", the number of matched files is 2
(two file names contain "os" as a substring) and suggested file name can be either "hosts" or "beros.18".
The first line of the input contains integer n
(1≤n≤10000
) — the total number of files.
The following n
lines contain file names, one per line. The i-th line contains fi — the name of the i-th file. Each file name contains between 1 and 8
characters, inclusive. File names contain only lowercase Latin letters, digits and dot characters ('.'). Any sequence of valid characters can be a file name (for example, in BerOS ".", ".." and "..." are valid file names). All file names are unique.
The following line contains integer q
(1≤q≤50000
) — the total number of queries.
The following q
lines contain queries s1,s2,…,sq, one per line. Each sj has length between 1 and 8
characters, inclusive. It contains only lowercase Latin letters, digits and dot characters ('.').
Print q
lines, one per query. The j-th line should contain the response on the j-th query — two values cj and tj
, where
- cjis the number of matched files for the j-th query, tj is the name of any file matched by the j-th query. If there is no such file, print a single character '-' instead. If there are multiple matched files, print any.
4
test
contests
test.
.test
6
ts
.
st.
.test
contes.
st
1 contests
2 .test
1 test.
1 .test
0 -
4 test.
题意:有n个字符串,q次查询,输出与之匹配字符串的个数,并输出任意一个字符串;
题解:由于每个字符串的长度<=8,所以我们可以把该字符串的字串全部存进map,并记录该字符串的位置;
直接输出就可以啦;
#include<cstdio>
#include<cstring>
#include<stack>
#include<queue>
#include<algorithm>
#include<iostream>
#include<map>
#include<vector>
#define PI acos(-1.0)
using namespace std;
typedef long long ll;
int m,n;
map<string,int>::iterator it;
map<string,int>mp;//存储所有字符串的字串
map<string,int>pos;//存储字符串的位置
void Substr(string str,int num)
{
string arr;
map<string,int>mp1;
for(int i=;i<str.size();i++)
{
for(int j=;j<=str.size();j++)
{
arr=str.substr(i,j);
if(!mp1.count(arr))
{
mp1[arr]++;
pos[arr]=num;
}
}
}
for(it=mp1.begin();it!=mp1.end();it++)
{
mp[it->first]++;
//cout<<it->first<<endl;
}
}
int main()
{
string s,str;
cin>>m;
map<int,string>kp;//记录原串
for(int i=;i<m;i++)
{
cin>>str;
kp[i]=str;
Substr(str,i);//求子串
}
cin>>n;
while(n--)
{
cin>>s;
if(mp.count(s))
{
cout<<mp[s]<<" "<<kp[pos[s]]<<endl;
}
else
{
cout<<""<<" "<<"-"<<endl;
}
}
}
BerOS File Suggestion(stl-map应用)的更多相关文章
- BerOS File Suggestion(字符串匹配map)
BerOS File Suggestion(stl-map应用) Polycarp is working on a new operating system called BerOS. He asks ...
- stl::map之const函数访问
如何在const成员数中访问stl::map呢?例如如下代码: string ConfigFileManager::MapQueryItem(const string& name) const ...
- hdu4941 Magical Forest (stl map)
2014多校7最水的题 Magical Forest Magical Forest Time Limit: 24000/12000 MS (Java/Others) Memory Limit ...
- [CareerCup] 13.2 Compare Hash Table and STL Map 比较哈希表和Map
13.2 Compare and contrast a hash table and an STL map. How is a hash table implemented? If the numbe ...
- STL MAP及字典树在关键字统计中的性能分析
转载请注明出处:http://blog.csdn.net/mxway/article/details/21321541 在搜索引擎在通常会对关键字出现的次数进行统计,这篇文章分析下使用C++ STL中 ...
- POJ 3096 Surprising Strings(STL map string set vector)
题目:http://poj.org/problem?id=3096 题意:给定一个字符串S,从中找出所有有两个字符组成的子串,每当组成子串的字符之间隔着n字符时,如果没有相同的子串出现,则输出 &qu ...
- STL MAP 反序迭代
ITS_NOTICE_MAP::reverse_iterator it = noticeMap.rbegin(); for ( ; it != noticeMap.rend(); ++it ) { I ...
- 泛型Binary Search Tree实现,And和STL map比较的经营业绩
问题叙述性说明: 1.binary search tree它是一种二进制树的.对于key值.比当前节点左孩子少大于右子. 2.binary search tree不是自平衡树.所以,当插入数据不是非常 ...
- Dictionary,hashtable, stl:map有什么异同?
相同点:字典和map都是泛型,而hashtable不是泛型. 不同点:三者算法都不相同 Hashtable,看名字能想到,它是采用传统的哈希算法:探测散列算法,而字典则采用的是散列拉链算法,效率较高, ...
随机推荐
- iOS NSUserDefaults
一.介绍 NSUserDefaults适合存储请练级的本地数据,对于一些简单的数据(NSString类型)来说是首选,但是如果我们自定义了一个对象,对象保存的是一些信息,这是就不能直接存储到NSUse ...
- MapReduce高级编程2
MapReduce Top N .二次排序,MapJoin: TOP N 对于一组输入List(key,value),我们要创建一个Top N 列表,这是一种过滤模式,查看输入数据特定子集,观察用户的 ...
- 小朋友学C语言(2):安装Dev C++编译器
(一)编译器 编译器是将“一种语言(通常为高级语言)”翻译为“另一种语言(通常为低级语言)”的程序.一个现代编译器的主要工作流程:源代码 (source code) -->预处理器 (prepr ...
- Solr游标查询提高翻页效率
长期以来,我们一直有一个深分页问题.如果直接跳到很靠后的页数,查询速度会比较慢.这是因为Solr的需要为查询从开始遍历所有数据.直到Solr的4.7这个问题一直没有一个很好的解决方案.与最近发布的So ...
- Linux安装vsftpd组件
1 安装vsftpd组件 安装完后,有/etc/vsftpd/vsftpd.conf 文件,是vsftp的配置文件. [root@hadoop1 ~]# yum -y install vsftpd 2 ...
- centos7 安装 nvm
cd 到 /usr/local下创建nvm文件夹,并进入nvm目录, 执行命令: wget -qO- https://raw.githubusercontent.com/creationix/nvm/ ...
- WPF DataGrid 数据绑定、样式、分页、增删改查,连接Access数据库
先上效果图: XAML: <UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&q ...
- tree-data
[{ label: '一级 1', children: [{ label: '二级 1-1', children: [{ label: '三级 1-1-1' }] }] }, { label: '一级 ...
- python-GIL
全局解释器锁GIL:在同一时刻仅有一个线程可被调度执行.对于单核环境,该实现简单高效.对于多线程的并发应用,一般通过多进程加协程充分发挥多核计算能力. 对于I/O密集型任务,线程发生阻塞时,会自动释放 ...
- 【Python爬虫实战】 使用代理服务器
代理服务器:是一个处于我们与互联网中间的服务器,如果使用代理服务器,我们浏览信息的时候,先向代理服务器发出请求,然后又代理服务向互联网获取信息,再返回给我们使用代理服务器进行信息爬取,可以很好的解决I ...