当前位置: 代码迷 >> 综合 >> BerOS File Suggestion (Codeforces 1070H)
  详细解决方案

BerOS File Suggestion (Codeforces 1070H)

热度:4   发布时间:2024-01-19 07:52:45.0

题目链接:  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;
}

 

  相关解决方案