Header Ad

HackerEarth The chocolate rooms problem solution

In this HackerEarth The chocolate rooms problem solution your task is to take some chocolates from N different rooms. Each room contains the Pi number of chocolates of the same or different brands. You are given an integer K.

Write a program to determine whether you can visit each room and find K different brands of chocolates.


HackerEarth The chocolate rooms problem solution


HackerEarth The chocolate rooms problem solution.

#include <iostream>
#include <map>
#include <string.h>

using namespace std;


int main()
{
int t;
scanf("%d", &t);
while(t--) {
int n, k;
string line = "";
scanf("%d%d", &n, &k);
map<string, bool> m;
string s;
int ans = 0;
for (int i = 0; i < n; i++) {
int p;
scanf("%d", &p);
while (p--) {
cin >> s;
if (m.find(s) == m.end()) {
ans++;
m[s]= 1;
}
}

}
if (ans >= k) printf("Yes\n");
else printf("No\n");
}
return 0;
}


Second solution

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD 1000000007
#define si(a) scanf("%d", &a)
#define sl(a) scanf("%lld", &a)
#define pi(a) printf("%d", a)
#define pl(a) printf("%lld", a)
#define pn printf("\n")
ll pow_mod(ll a, ll b) {
ll res = 1;
while(b) {
if(b & 1)
res = (res * a) % MOD;
a = (a * a) % MOD;
b >>= 1;
}
return res;
}
int main() {
int t;
si(t);
assert(t >= 1 && t <= 5);
while(t--) {
int n, k, p;
si(n); si(k);
assert(n >= 1 && n <= 100);
assert(k >= 1 && k <= 1000);
string str;
set<string> s;
bool poss = false;
for(int i = 0; i < n; ++i) {
si(p);
assert(p >= 1 && p <= 100);
for(int j = 0; j < p; ++j) {
cin >> str;
assert(str.length() >= 1 && str.length() <= 10);
s.insert(str);
}
}
if(s.size() >= k) {
poss = true;
}
if(poss) {
printf("Yes\n");
} else {
printf("No\n");
}
}
return 0;
}


Post a Comment

0 Comments