BerOS File Suggestion(字符串匹配map)
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.
MAP是个好东西
题意:
给你n个字符串,再给你q次查询,问你每一次查询的字符串是n个字符串中多少个字符串的子串,并随机输出其中一个原串
分析:将给出的n个字符串的所有子串全部存起来,长度为1的子串,长度为2....长度为size的子串,再将查询的字符串与所有的子串比对。再开一个map,用来记录该子串所在的原串。

1 #include<cstdio>
2 #include<algorithm>
3 #include<cstring>
4 #include<map>
5 #include<iostream>
6 using namespace std;
7 map<string,int> strr;
8 map<string,string> sstr;
9 int main()
10 {
11 int n;
12 while(~scanf("%d",&n)) {
13 while(n--) {
14 string a;
15 cin>>a;
16 map<string,int> outrepeat;//去重,防止一个字符串中,同样的子串出现不止一次
17 for(int i=0;i<a.size();i++) {
18 for(int j=1;j<=a.size();j++) {
19 string b;
20 b=a.substr(i,j);
21 if(outrepeat[b]==0) {//去重
22 strr[b]++;//该子串在n个原串中出现的次数
23 sstr[b]=a;//记录该子串所属的原串
24 outrepeat[b]=1;
25 }
26 }
27 }
28 }
29 int m;
30 scanf("%d",&m);
31 while(m--) {
32 string c;
33 cin>>c;
34 if(strr[c]>0)
35 cout<<strr[c]<<" "<<sstr[c]<<endl;
36 else
37 cout<<"0 -"<<endl;
38 }
39 }
40 return 0;
41 }

BerOS File Suggestion(字符串匹配map)的更多相关文章
- BerOS File Suggestion(stl-map应用)
Polycarp is working on a new operating system called BerOS. He asks you to help with implementation ...
- [51nod1532]带可选字符的多字符串匹配
有一个文本串,它的长度为m (1 <= m <= 2000000),现在想找出其中所有的符合特定模式的子串位置. 符合特定模式是指,该子串的长度为n (1 <= n <= 50 ...
- 字符串匹配常见算法(BF,RK,KMP,BM,Sunday)
今日了解了一下字符串匹配的各种方法. 并对sundaysearch算法实现并且单元. 字符串匹配算法,是在实际工程中经常遇到的问题,也是各大公司笔试面试的常考题目.此算法通常输入为原字符串(strin ...
- HDU 1686 Oulipo / POJ 3461 Oulipo / SCU 2652 Oulipo (字符串匹配,KMP)
HDU 1686 Oulipo / POJ 3461 Oulipo / SCU 2652 Oulipo (字符串匹配,KMP) Description The French author George ...
- 浅谈Hash在多个字符串匹配类型问题中的应用
在生活中们有时会遇到一些有关字符串匹配的问题. 这时打暴力往往显得很愚蠢,效率低下. 所以就需要一些算法和数据结构来提高效率. Hash Hash,一般翻译做“散列”,也有直接音译为“哈希”的,就是把 ...
- 通用高效字符串匹配--Sunday算法
字符串匹配(查找)算法是一类重要的字符串算法(String Algorithm).有两个字符串, 长度为m的haystack(查找串)和长度为n的needle(模式串), 它们构造自同一个有限的字母表 ...
- KMP算法 字符串匹配(看猫片)
前言 此篇笔记根据自己的理解和练习心得来解释算法,只代表个人观点,如有不足请指出(我刚学QWQ) 浅谈字符串匹配 设想一个场景,假设你是一个净化网络语言环境的管理员,每天需要翻阅大量的文章和帖子来查找 ...
- Mybatis——动态sql+字符串匹配导致的判断问题
在mybatis的学习中,狂神建议字符串匹配直接将模糊匹配的符号放在字符串中,如:匹配'keWord',那么实际所使用的参数应该为'%keyWord%' map.put("keyWord&q ...
- 字符串匹配的KMP算法
~~~摘录 来源:阮一峰~~~ 字符串匹配是计算机的基本任务之一. 举例来说,有一个字符串”BBC ABCDAB ABCDABCDABDE”,我想知道,里面是否包含另一个字符串”ABCDABD”? 许 ...
随机推荐
- laravel 解决保存Emoji 表情问题
ALTER DATABASE database_name CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci; ALTER TABLE table ...
- 【Linux常用命令】
1. ps aux | grep nginx 使用场景:是想查看nginx的打印日志,又不知道nginx的日志路径. ps :是查看进程的命令.使用该命令 可以确定有哪些进程正在运行和运行的状态.进程 ...
- matlab学习(3) 保存和导入工作区
1.保存和导入工作区变量mat文件 假如创建了两个矩阵A=[1,2;3,4],B=[0,1;1,0] 则工作区就是这样的: 当函数有一个数据量非常大的返回值时,每次调用函数都要执行一遍函数,每次都要等 ...
- 网络ping不通原因总结
1.检查主机是否在线,硬件是否连接好 2.查看ip地址是否输入正确 3.查看是否防火墙拦截,如果有拦截关闭防火墙 4.两台电脑是否接入同一局域网(WiFi也是)
- 使用 vue-cli-service inspect 来查看一个 Vue CLI 3 项目的 webpack 配置信息(包括:development、production)
使用 vue-cli-service inspect 来查看一个 Vue CLI 3 项目的 webpack 配置信息(包括:development.production) --mode 指定环境模式 ...
- Flume调优
这是一个关于池子的故事.有一个池子,它一头进水,另一头出水,进水口可以配置各种管子,出水口也可以配置各种管子,可以有多个进水口.多个出水口.水术语称为Event,进水口术语称为Source.出水口术语 ...
- centos7 设置时区和时间
1.设置时区(同步时间前先设置) timedatectl set-timezone Asia/Shanghai 2.安装组件 yum -y install ntp systemctl enable n ...
- centos7升级Python2.x到3.x
CentOS 7 中默认安装了 Python,版本比较低(2.7.5),为了使用新版 3.x,需要对旧版本进行升级.由于很多基本的命令.软件包都依赖旧版本,比如:yum.所以,在更新 Python 时 ...
- 2017-2018-2 20165312 课下选做 MySort
2017-2018-2 20165312 课下选做 MySort 题目描述 模拟实现Linux下Sort -t : -k 2的功能,参考 Sort的实现. import java.util.*; pu ...
- minicom 抓取log
使用minicom也有很长时间了,只用minicom抓过uart log,但是从来没有去保存过这个log,也不知道有这个功能.后来在超级终端中发现有这个功能(传送->捕获文字),想想minico ...