小米笔试题

题一:

 

题解:

#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <cstring>
#include <deque>
#include <queue>
#include <map> 

using namespace std;


bool jiaocha(const vector<int> &vec1, const vector<int> &vec2)
{
	for(auto it = vec2.begin(); it != vec2.end(); ++it) {
		if(find(vec1.begin(), vec1.end(), *it) != vec1.end()) {
			return true;
		}
	}
	return false;
}

void merge(vector<int> &vec1, vector<int> &vec2)
{
	for(auto it = vec2.begin(); it != vec2.end(); ++it) {
		if(find(vec1.begin(), vec1.end(), *it) == vec1.end()) {
			vec1.push_back(*it);
		}
	}
}

int main()
{
    int n, num;
	string s;
	cin >> n;
	getline(cin, s);
	vector<vector<int>> vec(n);
	for(auto row = vec.begin(); row != vec.end(); ++row) {
		getline(cin, s);		
		istringstream ss(s);
		while(ss >> num) {
			row->push_back(num);
		}
	} 
	
	for(auto row = vec.begin(); row != vec.end(); ++row) {
		for(auto row2 = vec.begin(); row2 != vec.end(); ++row2) {
			if(row == row2)	continue;
			bool bf = jiaocha(*row, *row2);
			if(bf) {
				merge(*row, *row2);
				row2 = vec.erase(row2);
				--row2;
			}
		}
	}
	
	int maxlen = 0;
	for(auto row = vec.begin(); row != vec.end(); ++row) {
		if(row->size() > maxlen) {
			maxlen = row->size();
		}
	}
	cout << vec.size() << endl << maxlen << endl;
    return 0;
}

  

posted @ 2018-09-27 15:27  GGBeng  阅读(576)  评论(1编辑  收藏  举报