题解 | #查找兄弟单词#

查找兄弟单词

https://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68

// HJ27 查找兄弟单词.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
#include<iostream>
#include<bits/stdc++.h>

using namespace std;

class Solution {
public:
	void Brother(vector<string>& str, string& s, int& k);
};

void Solution::Brother(vector<string>& str, string& s, int& k)
{
	int count = 0;
	int len = s.size();
	string tmp = s;
	sort(tmp.begin(), tmp.end());
	vector<string>vec;
	for (int i = 0; i < str.size(); i++)
	{
		if (str[i].compare(s) && str[i].size() == len)
		{
			string bmp=str[i];
			sort(bmp.begin(), bmp.end());
			if (bmp.compare(tmp) == 0)
			{
				count++;
				vec.push_back(str[i]);
			}
		}
	}
	sort(vec.begin(), vec.end());
	cout << count << endl;
	if (vec.size() < k)
		return;
	else
	{
		cout << vec[k - 1] << endl;
	}
}

int main()
{
	Solution a;
	int n;
	cin >> n;
	string tmp;
	vector<string>str;
	while (n--)
	{
		cin >> tmp;
		str.push_back(tmp);
	}
	string s;
	cin >> s;
	int k;
	cin >> k;
	a.Brother(str, s, k);
	return 0;
}

全部评论

相关推荐

09-01 09:00
已编辑
四川旅游学院 运营
牛客55195891...:主要是专业不好,别的没毛病
牛客解忧铺
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务