华为OJ中级题-查找兄弟单词

时间:2021-06-23 18:52:21
bool findBrother(string x,string y){
	int len = x.length();
	if (len != y.length()){ return false; }
	if (x == y){ return false; }

	int book[256] = { 0 }, book2[256] = {0};
	for (int i = 0; i < len; ++i){
		++book[x[i]];
	}

	int c = 0;
	for (int i = 0; i < len; ++i){
		for (int j = 0; j < len; ++j){
			if (x[i] == y[j]&&book2[y[j]]<book[x[j]]){ ++c; ++book2[y[j]]; }
		}
	}

	if (c == len)return true;
	else return false;
}
void HWoj(){
	int N,n;
	vector<string> words,outStr;
	string tmp;
	cin >> N;
	while (N--){
		cin >> tmp;
		words.push_back(tmp);
	}
	cin >> tmp;
	cin >> n;
	int lens = words.size();
	int count=0;
	int i = 0;
	for (i; i < lens; ++i){
		if (findBrother(tmp, words.at(i))){ ++count; outStr.push_back(words.at(i)); }
	}
	lens = outStr.size();
	cout << count << endl;
	sort(outStr.begin(), outStr.end());
	cout << outStr.at(n-1) << endl;
	
}