Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <map>
- #include <unordered_map>
- #include <string>
- #include <set>
- using namespace std;
- void ordena_parella(pair<string,string>& p){
- if(p.first > p.second) swap(p.first,p.second);
- }
- void desordena_parella(pair<string,string>& p){
- if(p.first < p.second) swap(p.first,p.second);
- }
- int main () {
- /* inicialitzacions*/
- pair<string,string> entrada; // Casos: 1-> Només 1 dels dos està ja a parelles
- map <string,string> parelles; // 2-> Els 2 es torben a parelles (però a parelles diferents només)
- unordered_map <string,string> parellesalreves;
- set <string> solters;
- string comanda;
- while(cin >> comanda) {
- if(comanda == "affair") {
- cin >> entrada.first >> entrada.second;
- ordena_parella(entrada);
- unordered_map<string,string>::iterator it2;
- /* PRIMER QUE ENTRA */
- map<string,string>::iterator it = parelles.find(entrada.first);
- if(it != parelles.end()) { // Si hem trobat el nom de la parella a 1a pos
- solters.insert(it->second); // Afegim com a solter a l'altre
- it2 = parellesalreves.find(it->second);
- parellesalreves.erase(it2);
- parelles.erase(it);
- }
- else { // Busco a 2a posicio
- it2 = parellesalreves.find(entrada.first);
- if(it2 != parellesalreves.end()) {
- solters.insert(it2->second); // Afegim com a solter a l'altre
- it = parelles.find(it2->second);
- parellesalreves.erase(it2);
- parelles.erase(it);
- }
- }
- /*--------------------------*/
- /* SEGON QUE ENS ENTREN */
- it = parelles.find(entrada.second);
- if(it != parelles.end()) { // Si hem trobat el nom de la parella a 1a pos
- solters.insert(it->second); // Afegim com a solter a l'altre
- it2 = parellesalreves.find(it->second);
- parellesalreves.erase(it2);
- parelles.erase(it);
- }
- else {
- it2 = parellesalreves.find(entrada.second); // On estarà el 2n (si està)
- if(it2 != parellesalreves.end()) { // Comprovem
- it = parelles.find(it2->second);
- parelles.erase(it);
- solters.insert(it2->second); // L'afegim com a solter al 2n
- parellesalreves.erase(it2);
- }
- }
- /* -----------------------*/
- parelles.insert(entrada);
- desordena_parella(entrada);
- parellesalreves.insert(entrada);
- set<string>::iterator itsolt = solters.find(entrada.first);
- if(itsolt != solters.end()) {
- solters.erase(itsolt);
- }
- set<string>::iterator itsolt2 = solters.find(entrada.second);
- if(itsolt2 != solters.end()) {
- solters.erase(itsolt2);
- }
- }
- else if(comanda == "info") {
- cout << "COUPLES:" << endl;
- for(map<string,string>::iterator escriu = parelles.begin(); escriu != parelles.end(); ++escriu) {
- cout << escriu->first << " "<< escriu->second << endl;
- }
- cout << "ALONE:" << endl;
- for(set<string>::iterator escribemelo = solters.begin(); escribemelo != solters.end(); ++escribemelo) {
- cout << *escribemelo<< endl;
- }
- cout << "----------" << endl;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement