2018-2019 ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules) H(暴力map)

BerOS File Suggestion

题解:这道题说实话有点想多了,还考虑用 t r i e trie trie树去做(虽然有神犇这样做的),可惜蒻根本不会 t r i e trie trie树。实际上用 m a p map map统计子串出现次数并记录子串的原串就好了。

代码

#include<bits/stdc++.h>

using namespace std;
map<string,int> vis, cnt;
map<string,string> str;
int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.in","r",stdin);
#endif
	int n;
	ios::sync_with_stdio(false);
	cin>>n;
	string s;
	for(int i = 0; i < n; ++i){
		cin>>s;
		vis.clear();
		string ret;
		for(int i = 0; i < s.length(); ++i){
			ret = "";
			for(int j = i; j < s.length(); ++j){
				ret += s[j];
				if(vis[ret] == 0) {
					vis[ret] = 1;
					cnt[ret]++;
					str[ret] = s;
			// cout<<ret<<endl;
				}
			}
		}
	}
	int q;
	cin>>q;
	string ans;
	while(q--){
		cin>>ans;
		if(cnt[ans] == 0) {
			cout<<"0 -"<<endl;
		}else{
			cout<<cnt[ans]<<' '<<str[ans]<<endl;;
		}
	}
    return 0;
}

    原文作者:算法
    原文地址: https://www.twblogs.net/a/5bd39ac62b717778ac2087bd
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞