1. 程式人生 > >codeforces 1070 H. BerOS File Suggestion map

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