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

BerOS File Suggestion

題解:這道題說實話有點想多了,還考慮用trietrie樹去做(雖然有神犇這樣做的),可惜蒻根本不會trietrie樹。實際上用mapmap統計子串出現次數並記錄子串的原串就好了。

代碼

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章