Cod sursa(job #2515237)

Utilizator memecoinMeme Coin memecoin Data 28 decembrie 2019 09:12:46
Problema Litere Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.89 kb
#include <fstream>
#include <string>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
#include <set>
#include <map>
#include <string.h>
#include <queue>

using namespace std;

#ifdef DEBUG
string name = "data";
#else
string name = "litere";
#endif

ifstream fin(name + ".in");
ofstream fout(name + ".out");

string s;
int n;

int main() {
    
    fin >> n;
    fin >> s;
    
    int change = 0;
    
    int start = 0;
    
    for (char c = 'a'; c <= 'z'; ++c) {
        for (int i = start; i < n; ++i) {
            if (s[i] == c) {
                change += i - start;
                
                for (int j = i ; j > start; j--) {
                    auto temp = s[j];
                    s[j] = s[j - 1];
                    s[j - 1] = temp;
                }
                start++;
            
            }
        }
    }
    
    fout << change;
    
    return 0;
}