Cod sursa(job #2011219)

Utilizator stefdascalescuStefan Dascalescu stefdascalescu Data 15 august 2017 17:13:36
Problema Ordine Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.56 kb
#include<bits/stdc++.h>
using namespace std;
ifstream f("ordine.in");
ofstream g("ordine.out");
int poz,l,v[30],q;
char c1[1000002];
char c2[1000002];
int main()
{
    f>>c1;
    l=strlen(c1);
    for(int i=0;i<l;++i)
        v[c1[i]-'a']++;
    for(int i=0;;++i)
        if(v[i])
        {
            char c='a'+i;
            c2[0]=c;
            --v[i];
            poz=i;
            --l;
            break;
        }
    while(l>0)
    {
        bool ok=0;
        for(int j=0;j<poz;++j)
            if(v[j]>0)
            {
                char c='a'+j;
                c2[++q]=c;
                --v[j];
                poz=j;
                --l;
                ok=1;
                break;
            }
        if(ok==0)
            for(int j=poz+1;j<26;++j)
                if(v[j]>0)
                {
                    char c='a'+j;
                    c2[++q]=c;
                    --v[j];
                    poz=j;
                    --l;
                    ok=1;
                    break;
                }
        if(ok==0)
            break;
    }
    poz=0;
    for(int j=0;j<26;++j)
        if(v[j])
        {
            poz=j;
            break;
        }
    char x='a'+poz;
    for(int j=0;v[poz] && j<=q;++j)
        if(c2[j]!=x)
        {
            if(j==0)
                if(c2[j+1]!=x)
                    c2[++q]=c2[j],c2[j]=x,--v[poz];
            else
                if(c2[j+1]!=x && c2[j-1]!=x)
                    c2[++q]=c2[j],c2[j]=x,--v[poz];
        }
    g<<c2;
    return 0;
}