BerOS File Suggestion (Codeforces 1070H)

题目链接:  http://codeforces.com/problemset/problem/1070/H

思路:直接找的话,会时间超限,所以可以用map把上面输入的串的所有字串都记下来,用两个map,一个记录有几个串有匹配的,一个记录匹配的串是哪个。

#include<cstdio>
#include<iostream>
#include <string>
#include<cstring>
#include <algorithm>
#include <map>
using namespace std;
typedef long long LL;
const int MAXN = 1e6;
int main()
{
    map<string,string> m1;  //记录与之匹配的是哪个串
    map<string,int> m2;     //记录匹配的串的个数
    int n;cin>>n;
    while(n--)
    {
        map<string,int> quchong;  //去重用,因为在一个串中匹配多次的话,应该只记一次
        string a;cin>>a;
        int len=a.size();
        int i,j,k;
        for(i=0;i<len;i++)
        {
            for(j=1;j<=len;j++)
            {
                string t=a.substr(i,j);   //截取所有的字串
                if(quchong[t]==0)
                {
                    m1[t]=a;
                    m2[t]++;
                    quchong[t]=1;
                }

            }
        }
    }
    int q;cin>>q;
    while(q--)
    {
        string t;cin>>t;
        if(m2[t]==0)
        {
            cout<<"0 -"<<endl;
        }
        else
        {
            cout<<m2[t]<<" "<<m1[t]<<endl;
        }
    }
    return 0;
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值