Cod sursa(job #2219638)

Utilizator eduardandrei20Nechifor Eduard Andrei eduardandrei20 Data 9 iulie 2018 14:11:55
Problema Ordine Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.25 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]++;
    }
    bool prim=false;
    char y;
         while(nr--)
         {   if(!prim)
         {
             prim=true;
              for (std::map<char,int>::iterator it=ap.begin(); it!=ap.end(); ++it)
               {y=it->first;it->second--; break;
               }
               out<<y;
         }
         else
         {
             bool ok =false;
              for (std::map<char,int>::iterator it=ap.begin(); it!=ap.end(); ++it)
                if(it->second>nr/2 and it->first!=y and it->second)
              {
                  out<<it->first; --it->second;
                  ok=true;
                  y=it->first;
                  break;

              }
              if(!ok)
              for (std::map<char,int>::iterator it=ap.begin(); it!=ap.end(); ++it)
                {  //if(!it->second)ap.erase(it);
                    if(it->second && it->first!=y)
                {
    out << it->first; it->second--; y=it->first; break;
                }
                }
         }
         }


    return 0;
}