欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

[codeforces1070H]BerOS File Suggestion

程序员文章站 2024-02-17 12:48:16
...

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 nn files on hard drive and their names are f1,f2,,fnf_1,f_2,…,f_n. Any file name contains between 11 and 88 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""read.me", "hosts""hosts", "ops""ops", and "beros.18""beros.18", and the query is "os""os", the number of matched files is 22 (two file names contain "os""os" as a substring) and suggested file name can be either "hosts""hosts" or "beros.18""beros.18".

Input

The first line of the input contains integer n(1n10000)n(1≤n≤10000) — the total number of files.The following nn lines contain file names, one per line. The ithi-th line contains fif_i — the name of the ithi-th file. Each file name contains between 11 and 88 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)q(1≤q≤50000) — the total number of queries.

The following q lines contain queries s1,s2,,sqs_1,s_2,…,s_q, one per line. Each sjs_j has length between 11 and 88 characters, inclusive. It contains only lowercase Latin letters, digits and dot characters (.)('.').

Output

Print qq lines, one per query. The jthj-th line should contain the response on the jthj-th query — two values cjc_j and tjt_j, where cjc_j is the number of matched files for the jthj - th query, tjt_j is the name of any file matched by the jthj -th 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个字符串f1f_1fnf_n(长度为1~8),有qq个询问,每次询问一个字符串ss,问有多少fif_i满足ssfif_i的子串,并且输出任意一个fif_i,如果不存在则输出‘-’

题解:
将所有fif_i的所有子串哈希,然后存入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;
}