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

(1n10000

) — 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

(1q50000

) — 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.

 题意:有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=0;i<str.size();i++)
21     {
22         for(int j=1;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=0;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<<"0"<<" "<<"-"<<endl;
60         }
61     }
62 }

 

posted @ 2018-10-22 10:59  sdibt布谷鸟  阅读(242)  评论(0编辑  收藏  举报