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;
}

猜你喜欢

转载自blog.csdn.net/Septembre_/article/details/83241802
今日推荐