Cod sursa(job #2219673)

Utilizator eduardandrei20Nechifor Eduard Andrei eduardandrei20 Data 9 iulie 2018 15:16:15
Problema Ordine Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.11 kb
#include <bits/stdc++.h>

using namespace std;
ifstream in("ordine.in");
ofstream out("ordine.out");
std::map<char,int>ap;
int main()
{
    char x;
    int nr=0;
    while(in>>x)
    {nr++;
        ap[x]++;
    }
    char last = 'A';
         while(nr)
         { bool ok =false;
             for (std::map<char,int>::iterator it=ap.begin(); it!=ap.end(); ++it)
                if(it->first !=last and it->second>nr/2 and it->second)
             {
                 out<<it->first;
                 it->second--;
                 last=it->first;
                 ok=true;
                 break;
             }
             if(!ok)
             {
                  for (std::map<char,int>::iterator it=ap.begin(); it!=ap.end(); ++it)
                  {
                      if(it->first!=last and it->second)
                      {
                          out<<it->first;
                          it->second--;
                          last=it->first;
                          break;
                      }
                  }
             }

--nr;

         }


    return 0;
}