uva 10507 Waking up brain Solution

Waking up brain Solution

uva id : erfan05Accepted Time : 0.000

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<set>
#include<map>

using namespace std;

typedef set<char> set_char;
typedef map<char, set_char> map_char_set_char;
typedef map_char_set_char::iterator map_char_set_char_iterator;
typedef set_char::iterator set_char_iterator;

int main(){

map_char_set_char mymap;
set_char_iterator sit,it;
map_char_set_char_iterator mit;
set_char life,life_progress;
string str;
char ch1, ch2;
int i, n, year, row;
while (cin >> n >> row){
cin >> str;
life.clear();
life_progress.clear();
mymap.clear();
life.insert(str[0]);
life.insert(str[1]);
life.insert(str[2]);

for (i = 0; i < row; i++){
cin >> str;
ch1 = str[0];
ch2 = str[1];
sit = life.find(ch1);
if (sit == life.end()){
mit = mymap.find(ch1);
if (mit == mymap.end()){
set_char temp;
temp.insert(ch2);
mymap[ch1] = temp;
}
else{
mit->second.insert(ch2);
}
}

sit = life.find(ch2);
if (sit == life.end()){
mit = mymap.find(ch2);
if (mit == mymap.end()){
set_char temp;
temp.insert(ch1);
mymap[ch2] = temp;
}
else{
mit->second.insert(ch1);
}
}
}
year = 0;
while (true){
for (mit = mymap.begin(); mit != mymap.end(); mit++){
set_char temp = mit->second;
i = 0;
for (sit = life.begin(); sit != life.end(); sit++){
it = temp.find(*sit);
if (it != temp.end()){
i++;
if (i == 3){
life_progress.insert(mit->first);
break;
}
}
}
}

if (life_progress.empty())
break;
else{
year++;
for (sit = life_progress.begin(); sit != life_progress.end(); sit++){
mymap.erase(mymap.find(*sit));
life.insert(*sit);
}
life_progress.clear();
}
}

if (life.size() == n){
cout << "WAKE UP IN, "<<year<<", YEARS" << endl;
}
else{
cout << "THIS BRAIN NEVER WAKES UP"<<endl;
}
}
return 0;
}

Comments

Popular posts from this blog

uva 679 - Dropping Balls Solution

uva 481 - What Goes Up Solution

uva-10077 Solution --- The Stern-Brocot Number System