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,看名字能想到,它是采用传统的哈希算法:探测散列算法,而字典则采用的是散列拉链算法,效率较高, ...
随机推荐
- go语言学习--channel的关闭
在使用Go channel的时候,一个适用的原则是不要从接收端关闭channel,也不要在多个并发发送端中关闭channel.换句话说,如果sender(发送者)只是唯一的sender或者是chann ...
- SCCM2012 R2实战系列之六:安装客户端代理软件
在安装客户端代理软件之前,请大家确保已经对本系列的第四和第五部分有了基本了解,而且对SCCM环境做了初始化配置和发现方法.我们目前讨论的是加域的计算机,对于工作组的计算机还需要进行额外的配置.在上篇文 ...
- 报错:Column count doesn't match value count at row 1
mysql错误:Column count doesn't match value count at row 1 意思是存储的数据与数据库表的字段类型定义不相匹配. 解决办法:检查段类型是否正确, 是否 ...
- Ext.NET Combobox实现数据源静态配置
背景 项目中使用增值税税率, 需求原本是统一的指定税率, 譬如3% 5% 等固定的. 随着业务增加, 发现不同业务有不同的税率, 一会加一个, 一下要改很多个界面. 就想着放到统一的地方. 技术背景 ...
- Unity中进程间通信——使用异步Socket
开发Unity项目过程中,即时通信功能来完成服务器与客户端自定义的数据结构封装. 如果要序列化和数据封装参考:Unity3D之C#用Socket传数据包 蓝鸥3G封装的类 客户端脚本ClientScr ...
- MAC 无脑编译OpenCV
1:准备好OpenCV 源码包 下载地址:https://opencv.org/releases.html 编译教程:https://blog.csdn.net/computerme/article/ ...
- Mysql-Client编码问题
Mysql编码问题! 首先,安装完mysql之后,登录进去(从控制台), 先要查看mysql编码:SHOW VARIABLES LIKE 'char%': (我这里是修改好的) 如果是没修改的,因为当 ...
- RxJava学习;数据转换、线程切换;
Observable(被观察者,发射器)发送数据: just:发送单个的数据: Observable.just("cui","chen","bo&qu ...
- 有关cookies与session的详细信息
COOKIES与SESSION 详细信息参考:https://www.cnblogs.com/linguoguo/p/5106618.html 会话(Session)跟踪是Web程序中常用的技术,用来 ...
- three.js学习:三维空间下的直线
index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...