There are a number of people who will be attending ACM-ICPC World Finals .Each of them may be well versed in a number of topics. Given a list of topics known by each attendee, presented as binary strings, determine the maximum number of topics a 2-person team can know. Each subject has a column in the binary string, and a '1' means the subject is known while '0' means it is not. Also determine the number of teams that know the maximum number of topics. Return an integer array with two elements. The first is the maximum number of topics known, and the second is the number of teams that know that number of topics.
Example:
Input: n=4,m=5,str[]={"10101","11100","11010","00101"
Output: 5
2
Approach
C++
#include <bits/stdc++.h>using namespace std;vector<int> acmICPC(string str[], int n, int m){vector<int> v;int x = n * (n - 1) / 2;int res[x];for (int i = 0; i < x; i++)res[i] = 0;int l = 0;for (int i = 0; i < n - 1; i++){int j = i + 1;while (j < n){int cnt = 0;for (int k = 0; k < m; k++){if (str[i][k] == '1' || str[j][k] == '1')cnt++;}res[l] = cnt;l++;j++;}}sort(res, res + x);int max1 = res[x - 1];int p = x - 1;int cnt = 0;while (p >= 0 && res[p] == max1){cnt++;p--;}v.push_back(max1);v.push_back(cnt);return v;}int main(){int n = 4, m = 5;string str[n] = {"10101", "11100", "11010", "00101"};vector<int> ans = acmICPC(str, n, m);cout << ans[0] << "\n"<< ans[1] << "\n";return 0;}
No comments:
Post a Comment