Skip to content

Instantly share code, notes, and snippets.

@JanhaviDadhania
Last active January 11, 2021 18:13
Show Gist options
  • Save JanhaviDadhania/6686f2e61d02675108fc7206cacc013d to your computer and use it in GitHub Desktop.
Save JanhaviDadhania/6686f2e61d02675108fc7206cacc013d to your computer and use it in GitHub Desktop.
Chef gets scared in the exam if he sees a question containing description of more than 10 lines and leaves the exam
hall and due to this he is not able to perform well in his exams. Cheffina is chef's best friend and
she decides to give him a question which she believes would help chef to overcome his fear of reading lengthy questions.
Chef invites you for pair-problem-solving and askes you to help him in solving this question because he is again scared
to read it. Question is as below.
Raj has 10,000 sets of strings named 1, 2, 3, 4 and so on till 10,000, where string is sequence of alphabates e.g NAN.
A function f from set of alphabets to set {1,0} is defined as below:
f(x) = 0, x E {A,B,C,D,E,F,G,H,I,J,K,L,M}
f(x) = 1, x E {N,O,P,Q,R,S,T,U,V,W,X,Y,Z}
A many-to-one conversion function from set y to set of binary integers where y E Raj's 10,000 sets, is defined as below :
|y| = |conversion(y)| where |s| gives total elements in set.
binary representation of ith element of conversion(y) is given by :
i = I1I2I3...In where n = length(y[i]), for all j, Ij = f(y[i][j]) 1<=j<=n
He randomly picks up two sets from his 10,000 sets, let's call them set a and b.
Set A and B contains m and n elements respectively.
We define set A, B, C and P as below :
A = conversion(a)
B = conversion(b)
P = {x|x=i XOR j, for all i E A, j E B }.
C = A U B
Your task is to find total pair of elements x,y where ( x E P, y E C ) and x XOR y E C.
Note : for simplicity consider that duplicate elements are allowed in single set.
INPUT :
First line of input contains single integer T denoting the number of testcases.
First line for each testcase containes two integers a and b representing sets choosen by Raj.
Next line contains two integers n and m denoting total elements in choosen sets respectively.
Next two lines contains space separeted m and n strings denoting elements of sets.
OUTPUT :
For each testcase print one line containing number indicating total pair of elements x and y.
CONSTRAINTS :
1<= T <= 10,000
1 <= a,b <=10,000
1 <= n,m <= 1000
Consider i is the element from jth set and A1A2A3...AN is binary representation of j.
for each i, length of i = N and for each x, f(i[x]) = Ax , 1<=x<=N.
example input:
1
5 6
3 6
NAN PBQ ZIX
NOA PQB RSC TUD VWE XYF
output for above TC:
162
Explaination:
after performing conversion operation on sets of strings
{101, 101, 101}
{110, 110, 110, 110, 110, 110}
P = {011, 011, all 011.... total 18 elements}
C = union of both sets A and B
trick:
a XOR b = c
c XOR a = b
all elements from P and C when XORed gives an element which is present in set C.
(not gonna add this explaination for participants tho XD)
**********************************************SOLUTION****************************************************
#include<bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
while(t--) {
vector<string> setOne;
vector<string> setTwo;
int a,b,n,m;
string s;
cin>>a>>b>>n>>m;
for(int i=0;i<n;i++) {
cin>>s;
setOne.push_back(s);
}
for(int i=0;i<m;i++) {
cin>>s;
setTwo.push_back(s);
}
int elementsC = n + m;
int elementsP = n*m;
cout<<elementsC * elementsP<<endl;
}
return 0;
}
***************************************************testcase generator**************************
#include<bits/stdc++.h>
using namespace std;
int random_number(int upper_limit) {
return rand() % upper_limit;
}
vector<int> convert_binary(int a) {
vector<int> result;
if(a == 0) {
result.push_back(0);
return result;
}
while(a!=0) {
result.push_back(a%2);
a /= 2;
}
reverse(result.begin(), result.end());
return result;
}
int main() {
srand((unsigned) time(0));
int t;
cin>>t;
//first line of each testcase is T
cout<<t<<endl;
vector<char> firstSet;
vector<char> secondSet;
for(int i=65;i<78;i++) {
firstSet.push_back(i);
}
for(int i=78;i<91;i++) {
secondSet.push_back(i);
}
while(t--) {
int a = random_number(10000);
int b = random_number(10000);
int n = random_number(10000);
int m = random_number(10000);
cout<<a<<" "<<b<<endl<<n<<" "<<m<<endl;
vector<int> A = convert_binary(a);
vector<int> B = convert_binary(b);
vector<string> setA;
vector<string> setB;
for(int i=0;i<n;i++) {
string element;
for(int i=0;i<A.size();i++) {
char prepare;
int rand = random_number(13);
if(A[i] == 0) {
prepare = firstSet[rand];
}
else {
prepare = secondSet[rand];
}
element.push_back(prepare);
}
setA.push_back(element);
}
for(int i=0;i<m;i++) {
string element;
for(int i=0;i<B.size();i++) {
char prepare;
int rand = random_number(13);
if(B[i] == 0) {
prepare = firstSet[rand];
}
else {
prepare = secondSet[rand];
}
element.push_back(prepare);
}
setB.push_back(element);
}
for(int i=0;i<setA.size();i++) {
cout<<setA[i]<<" ";
}
cout<<endl;
for(int i=0;i<setB.size();i++) {
cout<<setB[i]<<" ";
}
cout<<endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment