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.
  1. 4
    test
    contests
    test.
    .test
    6
    ts
    .
    st.
    .test
    contes.
    st
Output

Copy
  1. 1 contests
    2 .test
    1 test.
    1 .test
    0 -
    4 test.

题意:有n个字符串,q次查询,输出与之匹配字符串的个数,并输出任意一个字符串;

题解:由于每个字符串的长度<=8,所以我们可以把该字符串的字串全部存进map,并记录该字符串的位置;

直接输出就可以啦;

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<stack>
  4. #include<queue>
  5. #include<algorithm>
  6. #include<iostream>
  7. #include<map>
  8. #include<vector>
  9. #define PI acos(-1.0)
  10. using namespace std;
  11. typedef long long ll;
  12. int m,n;
  13. map<string,int>::iterator it;
  14. map<string,int>mp;//存储所有字符串的字串
  15. map<string,int>pos;//存储字符串的位置
  16. void Substr(string str,int num)
  17. {
  18. string arr;
  19. map<string,int>mp1;
  20. for(int i=;i<str.size();i++)
  21. {
  22. for(int j=;j<=str.size();j++)
  23. {
  24. arr=str.substr(i,j);
  25. if(!mp1.count(arr))
  26. {
  27. mp1[arr]++;
  28. pos[arr]=num;
  29. }
  30. }
  31. }
  32. for(it=mp1.begin();it!=mp1.end();it++)
  33. {
  34. mp[it->first]++;
  35. //cout<<it->first<<endl;
  36. }
  37. }
  38. int main()
  39. {
  40. string s,str;
  41. cin>>m;
  42. map<int,string>kp;//记录原串
  43. for(int i=;i<m;i++)
  44. {
  45. cin>>str;
  46. kp[i]=str;
  47. Substr(str,i);//求子串
  48. }
  49. cin>>n;
  50. while(n--)
  51. {
  52. cin>>s;
  53. if(mp.count(s))
  54. {
  55. cout<<mp[s]<<" "<<kp[pos[s]]<<endl;
  56. }
  57. else
  58. {
  59. cout<<""<<" "<<"-"<<endl;
  60. }
  61. }
  62. }

BerOS File Suggestion(stl-map应用)的更多相关文章

  1. BerOS File Suggestion(字符串匹配map)

    BerOS File Suggestion(stl-map应用) Polycarp is working on a new operating system called BerOS. He asks ...

  2. stl::map之const函数访问

    如何在const成员数中访问stl::map呢?例如如下代码: string ConfigFileManager::MapQueryItem(const string& name) const ...

  3. hdu4941 Magical Forest (stl map)

    2014多校7最水的题   Magical Forest Magical Forest Time Limit: 24000/12000 MS (Java/Others)    Memory Limit ...

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

  5. STL MAP及字典树在关键字统计中的性能分析

    转载请注明出处:http://blog.csdn.net/mxway/article/details/21321541 在搜索引擎在通常会对关键字出现的次数进行统计,这篇文章分析下使用C++ STL中 ...

  6. POJ 3096 Surprising Strings(STL map string set vector)

    题目:http://poj.org/problem?id=3096 题意:给定一个字符串S,从中找出所有有两个字符组成的子串,每当组成子串的字符之间隔着n字符时,如果没有相同的子串出现,则输出 &qu ...

  7. STL MAP 反序迭代

    ITS_NOTICE_MAP::reverse_iterator it = noticeMap.rbegin(); for ( ; it != noticeMap.rend(); ++it ) { I ...

  8. 泛型Binary Search Tree实现,And和STL map比较的经营业绩

    问题叙述性说明: 1.binary search tree它是一种二进制树的.对于key值.比当前节点左孩子少大于右子. 2.binary search tree不是自平衡树.所以,当插入数据不是非常 ...

  9. Dictionary,hashtable, stl:map有什么异同?

    相同点:字典和map都是泛型,而hashtable不是泛型. 不同点:三者算法都不相同 Hashtable,看名字能想到,它是采用传统的哈希算法:探测散列算法,而字典则采用的是散列拉链算法,效率较高, ...

随机推荐

  1. Retrofit 使用方法

    public class MainActivity extends Activity { private ListView mListView; //private ImageListAdapter ...

  2. 一行代码避免OkHttp的网络库应用被抓包

    在建立socket连接之前,OkHttp会获取系统的代理信息,如果设置代理,那么通过DNS解析其IP然后使用代理IP来建立socket连接.如果没有设置代理,那么会使用请求中的url的IP地址,来建立 ...

  3. Type Cannot change version of project facet Dynamic Web Module to 2.5 报错

    项目下的.setings文件 夹中的 version 改为2.5

  4. UE4如何检测目标在锥形视野内

    转自:http://blog.csdn.net/l346242498/article/details/70237083 做UE4游戏AI方面经常会遇到一个问题,就是何如判定目标在AI单位的视野范围内, ...

  5. CentOS安装redis.tar.gz

    1. # cd /usr/local/src 2. # tar -zxvf redis-3.0.6.tar.gz 3. # cd redis-3.0.6 4.# make 5.#  make PREF ...

  6. C# DataGridView导出Excel

    using Microsoft.Office.Interop.Excel;                using Excel=Microsoft.Office.Interop.Excel; //这 ...

  7. 用tmux工具给linux命令行分栏

    介绍 有时候我们需要在命令行里同时开多个程序,但是每个程序都会占用命令行,这时候可以用tmux,给命令行界面分栏. 基本概念 tmux是分多层的 会话>窗口(我下文称作:页)>面板(或者叫 ...

  8. Java连接postgreSQL数据库,找不到表。

    postgreSQL数据库遵守SQL标准,表名库名不区分大小写. 数据库中是存在 gongan_address_ALL的表的,但是执行下列代码就会出错. stmt = c.createStatemen ...

  9. Hive快捷查询:不启用Mapreduce job启用Fetch task

    启用MapReduce Job是会消耗系统开销的.对于这个问题,从Hive0.10.0版本开始,对于简单的不需要聚合的类似SELECT <col> from <table> L ...

  10. Pyhton-Web框架之【Django】

    一.什么是web框架 框架,即framework,特指为解决一个开放性问题而设计的具有一定约束性的支撑结构,使用框架可以帮你快速开发特定的系统,简单地说,就是你用别人搭建好的舞台来做表演. 对于所有的 ...