H - BerOS File Suggestion

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 f i — 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 s 1 , s 2 , … , s q , one per line. Each s j has length between 1 and 8 characters, inclusive.It contains only lowercase Latin letters, digits and dot characters (’.’).

4
test
contests
test.
.test
6
ts
.
st.
.test
contes.
st

寻找子串,不能用KMP,因为已知串的长度都不超过8,所以就可以直接找已知串的所有子串存到map里
定义三个map,一个记录这个子串出现的次数,一个用来标记这个子串是否找过,另一个存储这个子串的母串(任意一个)
代码:

#include<iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <cstdlib>
#include <algorithm>
#define ll long long
#define INF 0x3f3f3f
const int MAX=1e5+10;
using namespace std;
map <string,string> ans;
map <string,int> cnt;
map <string,bool> vis;
char a[20];
string str;
int main()
{
    int n;
    cin>>n;
    while(n--)
    {
        scanf("%s",a);
         vis.clear();       
        int len=strlen(a);
        for(int i=0;i<len;i++)
        {
            str="";       //清空str
            for(int j=i;j<len;j++)
            {
                str+=a[j];          //从串的第一位开始往后找所有子串,再从第二位开始
                if(!vis[str])       //这个子串之前未出现过
                {
                    cnt[str]++;         //子串出现次数加1
                    ans[str]=a;         //记录母串
                    vis[str]=true;     //表示这个子串已经存在
                }
            }
        }
    }
    int q;
    cin>>q;
    while(q--)
    {
        scanf("%s",a);
        if(cnt[a]==0)
            cout<<"0 -"<<endl;
        else
            cout<<cnt[a]<<" "<<ans[a]<<endl;
    }
  
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值