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".

Input

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 ('.').

Output

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
Output

Copy
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)的更多相关文章

  1. BerOS File Suggestion(stl-map应用)

    Polycarp is working on a new operating system called BerOS. He asks you to help with implementation ...

  2. [51nod1532]带可选字符的多字符串匹配

    有一个文本串,它的长度为m (1 <= m <= 2000000),现在想找出其中所有的符合特定模式的子串位置. 符合特定模式是指,该子串的长度为n (1 <= n <= 50 ...

  3. 字符串匹配常见算法(BF,RK,KMP,BM,Sunday)

    今日了解了一下字符串匹配的各种方法. 并对sundaysearch算法实现并且单元. 字符串匹配算法,是在实际工程中经常遇到的问题,也是各大公司笔试面试的常考题目.此算法通常输入为原字符串(strin ...

  4. 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 ...

  5. 浅谈Hash在多个字符串匹配类型问题中的应用

    在生活中们有时会遇到一些有关字符串匹配的问题. 这时打暴力往往显得很愚蠢,效率低下. 所以就需要一些算法和数据结构来提高效率. Hash Hash,一般翻译做“散列”,也有直接音译为“哈希”的,就是把 ...

  6. 通用高效字符串匹配--Sunday算法

    字符串匹配(查找)算法是一类重要的字符串算法(String Algorithm).有两个字符串, 长度为m的haystack(查找串)和长度为n的needle(模式串), 它们构造自同一个有限的字母表 ...

  7. KMP算法 字符串匹配(看猫片)

    前言 此篇笔记根据自己的理解和练习心得来解释算法,只代表个人观点,如有不足请指出(我刚学QWQ) 浅谈字符串匹配 设想一个场景,假设你是一个净化网络语言环境的管理员,每天需要翻阅大量的文章和帖子来查找 ...

  8. Mybatis——动态sql+字符串匹配导致的判断问题

    在mybatis的学习中,狂神建议字符串匹配直接将模糊匹配的符号放在字符串中,如:匹配'keWord',那么实际所使用的参数应该为'%keyWord%' map.put("keyWord&q ...

  9. 字符串匹配的KMP算法

    ~~~摘录 来源:阮一峰~~~ 字符串匹配是计算机的基本任务之一. 举例来说,有一个字符串”BBC ABCDAB ABCDABCDABDE”,我想知道,里面是否包含另一个字符串”ABCDABD”? 许 ...

随机推荐

  1. ubuntu16.04下python2、python3环境选择与python升级(pip版本切换)

    参考链接:https://www.jianshu.com/p/63c1f22e1fed Ubuntu中python版本问题: 添加PPA: sudo add-apt-repository ppa:jo ...

  2. 问题 Duplicate entry '0' for key 'PRIMARY'

    今天使用了触发器,在一个表中执行增删改操作,然后在另一个表中执行相应的记录时,出现了这个问题 其实这个问题应该算是细节问题,有两种情况: 1.就是在插入数据的时候将id设置为not nul但是在插入数 ...

  3. C++取反交换两个数的值

    int a = 1; int b = 2; cout << "a: "<< a << endl; cout << "b: ...

  4. 自动化测试-18.selenium之bugFree代码注释

    #encoding=utf-8 import xlrd,time,os from xlutils.copy import copy from selenium import webdriver def ...

  5. Git 简介及简单操作

    一.GIT是什么 Git 是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目. Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本 ...

  6. 【paper】MTCNN

    参考 1. MTCNN笔记; 完

  7. PHP 框架

    LARAVEL/LUMEN,  CI  ,THINKPHP, YII ,SYMFONY YAF, PHALCON ,ICE FRAMEWORK

  8. 《Linux内核原理与分析》第八周作业

    课本:第七章 可执行程序工作原理 ELF目标文件格式 目标文件:编译器生成的文件. 目标文件的格式:out格式.COFF格式.PE(windows)格式.ELF(Linux)格式. ELF(Execu ...

  9. tgp助手开启逆战游戏无反应

    tgp助手开启逆战游戏无反应(一直显示正在运行游戏)就是没有游戏的登录界面 解决的一些方法(不一定有效): 检查显卡的驱动 检查游戏文件是否损坏 检查是否开启的防护软件程序

  10. hasura graphql-engine 集成zombodb

    zombodb 是一个很不错的pg 扩展,可以方便的把es 与pg 集成起来,使用方便 ,目前尽管有一些docker 镜像 但是版本都比较老,所以基于centos7 做了一个新的docker 镜像,同 ...