[codeforces1070H]BerOS File Suggestion
time limit per test : 3 seconds
memory limit per test : 256 megabytes
Polycarp is working on a new operating system called BerOS. He asks you to help with implementation of a file suggestion feature.
There are files on hard drive and their names are . Any file name contains between and 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
,
,
, and
, and the query is
, the number of matched files is
(two file names contain
as a substring) and suggested file name can be either
or
.
Input
The first line of the input contains integer — the total number of files.The following lines contain file names, one per line. The line contains — the name of the file. Each file name contains between and 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 — the total number of queries.
The following q lines contain queries , one per line. Each has length between and characters, inclusive. It contains only lowercase Latin letters, digits and dot characters .
Output
Print lines, one per query. The line should contain the response on the query — two values and , where is the number of matched files for the query, is the name of any file matched by the query. If there is no such file, print a single character instead. If there are multiple matched files, print any.
Input
4
test
contests
test.
.test
6
ts
.
st.
.test
contes.
st
Output
1 contests
2 .test
1 test.
1 .test
0 -
4 test.
題意:
給定n個字串
到
(長度為1~8),有
個詢問,每次詢問一個字串
,問有多少
滿足
是
的子串,並且輸出任意一個
,如果不存在則輸出
題解:
將所有
的所有子串雜湊,然後存入set,查詢的時候只要直接詢問就行了…不過cf機子特別快,你想用map<string,string>
也可以
#include<bits/stdc++.h>
#define LiangJiaJun main
#define MOD 19991227
#define ULL unsigned long long
using namespace std;
int n;
map<ULL,int>pos,Hash;
set<ULL>prota;
set<ULL>::iterator it;
char s[10004][14];
int w33ha(){
pos.clear();
Hash.clear();
for(int i=1;i<=n;i++){
scanf("%s",s[i]);
int l=strlen(s[i]);
prota.clear();
for(int j=0;j<l;j++){
ULL sum=0;
for(int L=1;j+L-1<l;L++){
sum=sum*256LL+(ULL)s[i][j+L-1];
prota.insert(sum);
}
}
for(it=prota.begin();it!=prota.end();it++){
if(!Hash[(*it)])pos[(*it)]=i;
Hash[(*it)]++;
}
}
int q;
scanf("%d",&q);
while(q--){
int l;
ULL sum=0;
char pt[14];
scanf("%s",pt);
l=strlen(pt);
for(int i=0;i<l;i++)sum=sum*256LL+(ULL)pt[i];
if(!Hash[sum]){
puts("0 -");
}
else{
printf("%d %s\n",Hash[sum],s[pos[sum]]);
}
}
return 0;
}
int LiangJiaJun(){
while(scanf("%d",&n)!=EOF)w33ha();
return 0;
}