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. Python2入门(1)

    一.基础语法1 - 输出语句 print "hello world",print默认输出换行,如果需要实现不换行需在变量末尾加上逗号,; 2 - python合法标识符 3 - 字 ...

  2. 使用jsoup轻松爬数据

    刚刚学习爬虫,感觉使用jsoup爬虫挺容易的.记录一下自己爬取数据的过程. Jsoup介绍: Jsoup 是一个 Java 的开源HTML解析器,可直接解析某个URL地址.HTML文本内容.使用Jso ...

  3. 【转】FMX 动态创建及销毁(释放free)对象

    http://www.2pascal.com/thread-3037-1-1.html这是原文地址. (* ********************************************** ...

  4. CSS旋转缩放

    <style type="text/css"> figure{ float: left;}.test1{ border-radius: 0px; height: 200 ...

  5. 微信连wifi,中文ssid报Invalid sign tosign错误

    Invalid sign tosign错误如上: 是微信官方接受和回传的问题,改固定字符解决: 比如将ssid固定修改为字符串‘ssid’即可:

  6. pthon入门之strip()和split()函数简单区分

    小白,分享记录学习新感悟 路飞的第一次作业写一个登录的程序,作业的升级需求中有个锁文件的需求,大致上如果用户数错了密码三次将用户写到黑名单上,下次登录锁定: ok基本的要求写完,我们上代码 usern ...

  7. C# 读写西门子PLC数据,包含S7协议和Fetch/Write协议,s7支持200smart,300PLC,1200PLC,1500PLC

    本文将使用一个gitHub开源的组件技术来读写西门子plc数据,使用的是基于以太网的TCP/IP实现,不需要额外的组件,读取操作只要放到后台线程就不会卡死线程,本组件支持超级方便的高性能读写操作 官方 ...

  8. Python全栈之路----常用模块----random模块

    程序中有很多地方需要用到随机字符,比如登陆网站的随机验证码,通过random模块可以很容易生成随机字符串. >>> import random >>> random ...

  9. EasyUI datagrid 序列 化后时间 处理 九

    @{ ViewBag.Title = "Home Page"; Layout = null; } <!DOCTYPE html> <html> <he ...

  10. PythonStudy——数字类型 Number type

    # 了了解:py2中小整数用int存放,大整数用long# 1.整型 num = -1000000000000000000000000000000000000000000000000 print(nu ...