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”? 许 ...
随机推荐
- shell脚本监测DNS链接状态给传给zabbix值
#!/bin/sh time_out=0 querygt3s=0 i=1 while [[ $i -le 15 ]] do i=`expr $i + 1` sleep 2 while read lin ...
- ItelliJ idea tomcat 配置
用ItelliJ idea 开发javaWeb. 1. Idea 安装Tomcat 打开Idea,选择设置,并在设置中左边框中选择 Application Servers 点击中间空白框上面的 ’+‘ ...
- 【leetcode】443. String Compression
problem 443. String Compression Input ["a","a","b","b"," ...
- MP和OMP算法
转载:有点无耻哈,全部复制别人的.写的不错 作者:scucj 文章链接:MP算法和OMP算法及其思想 主要介绍MP(Matching Pursuits)算法和OMP(Orthogonal Matchi ...
- 位运算 - a^b
求 a 的 b 次方对 p 取模的值. 输入格式 三个整数 a,b,p ,在同一行用空格隔开. 输出格式 输出一个整数,表示a^b mod p的值. 数据范围 1≤a,b,p≤109 输入样例: 3 ...
- java——慎用可变参数列表
说起可变参数,我们先看下面代码段,对它有个直观的认识,下方的红字明确地解释了可变参数的意思: public class VarargsDemo{ static int sum(int... args) ...
- C#的发展历程 -- 系列介绍
C#的发展历程第五 - C# 7开始进入快速迭代道路 C#与C++的发展历程第四 - C#6的新时代 C#与C++的发展历程第三 - C#5.0异步编程巅峰 C#与C++的发展历程第二 - C#4.0 ...
- Pytho, struct处理二进制(pack和unpack)
[转]Python使用struct处理二进制(pack和unpack用法) Leave a reply 转载自:http://www.cnblogs.com/gala/archive/2011/09/ ...
- python中的is和==
is和== Python中的对象包含三要素:id.type.value id方法的返回值就是对象的内存地址其中id用来唯一标识一个对象,type标识对象的类型,value是对象的值 is 判断的是a对 ...
- HAproxy增加日志记录功能和自定义日志输出内容、格式
http://blog.51cto.com/eric1/1854574 一.增加haproxy日志记录功能 1.1 由于数据分析的需要,我们必须打开haproxy日志,记录相关信息. 在配置前,我 ...