codeforces 1070 H. BerOS File Suggestion map

 题目:
传送门

思路:

将每一个字符串的每一个子串都映射出来,最后询问的时候输出结果即可。

代码如下:

#include <bits/stdc++.h>
using namespace std;
int n,q;
map<string,string>m1;
map<string,int>m2;
int main()
{
	string s;
	scanf("%d",&n);
	while (n--)
	{
		cin>>s;
		int len=s.size();
		map<string,int>rep;
		for (int i=1;i<=len;i++)
		{
			for (int j=0;j+i<=len;j++)
			{
				string t=s.substr(j,i);
				if(rep[t]==0)
				{
					rep[t]=1;
					m1[t]=s;
					m2[t]++;
				}
			}
		}
	}
	scanf("%d",&q);
	while (q--)
	{
		string s;
		cin>>s;
		if(m2[s]==0)
			cout<<0<<" "<<"-"<<endl;
		else
			cout<<m2[s]<<" "<<m1[s]<<endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41410799/article/details/84845778
今日推荐