Last active
April 9, 2016 19:31
-
-
Save sandes/047adcb55bddf9515eca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <list> | |
#include <map> | |
using namespace std; | |
/*NOTA: es necesario compilar con -std=c++11 (standard 2011)*/ | |
/* | |
-Si una clave key esta contenida en A o B pero no en ambos, entonces C no debe contener dicha clave. | |
-Si una clave key esta contenida en A y B a la vez, entonces C debe contener dicha clave y su valor | |
asociado debe ser una lista que contenga todos los elementos comunes y sin repeticion de las listas | |
asociadas a key en A y B. | |
*/ | |
bool contains(list<int>&L, int x){ | |
for (int i:L) if (i==x) return true; | |
return false; | |
} | |
typedef map<string,list<int>> map_p; | |
void intersec_map(const map_p &A, const map_p &B, map_p &C){ | |
auto it = A.begin(); | |
while(it!=A.end()){ | |
if(B.find(it->first)!=B.end()){ | |
list<int> tmpA,tmpB,lC; | |
auto q = B.find(it->first); | |
tmpA = it->second; | |
tmpB = q->second; | |
list<int>::iterator p = tmpA.begin(); | |
while(++p!=tmpA.end()) | |
if(contains(tmpB,*p)&&(!contains(lC,*p))) lC.push_back(*p); | |
C[it->first] = lC; | |
lC.clear(); | |
} | |
it++; | |
} | |
} | |
void display(map<string,list<int>>&LM){ | |
auto it = LM.begin(); | |
while(it!=LM.end()){ | |
list<int> tmp =it->second; | |
cout << it->first << ": "; | |
for(auto i:tmp) cout << i << " "; | |
it++; | |
cout << endl; | |
} | |
} | |
int main(int argc, char *argv[]) { | |
list<int> L1 = {3,3,1,2,2,7}; | |
list<int> L2 = {7,1,5,5,4,1}; | |
list<int> L3 = {3,3,4,5,8,1}; | |
list<int> L4 = {1,1,9}; | |
map<string,list<int>>A,B,C; | |
A["XX"] = L1; B["YY"] = L3; | |
A["YY"] = L2; B["ZZ"] = L4; | |
intersec_map(A,B,C); | |
display(C); /* SI TODO SALE BIEN EL DISPLAY MOSTRARA YY: 1,4,5 */ | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment