Cod sursă (job #800084)

Utilizator avatar AnSeDra Andrei Sebastian Dragulescu AnSeDra IP ascuns
Problemă Cristela (clasele 9-12) Compilator cpp-32 | 1,99 kb
Rundă Arhiva de probleme Status evaluat
Dată 16 dec. 2024 12:33:20 Scor 15
#include <fstream>
#include <algorithm>
#include <unordered_map>
#include <string>

using namespace std;

ifstream fin("cristela.in");
ofstream fout("cristela.out");

const int Nmax = 500005;
const char LiteraMax = 't';

int n, sol;
string nume[Nmax];
unordered_map<string, int> secvente, folosit;

void citire(){
    fin >> n;
    for(int i = 1; i <= n; i++){
        fin >> nume[i];
    }
}

void prelucrare_nume(){
    string aux;

    for(int i = 1; i <= n; i++){
        sort(nume[i].begin(), nume[i].end());

        aux.clear();
        aux.push_back(nume[i][0]);
        for(unsigned j = 1; j < nume[i].size(); j++){
            if(nume[i][j] != nume[i][j - 1]){
                aux.push_back(nume[i][j]);
            }
        }
        swap(nume[i], aux);
    }
}

void numarare_secvente(){
    string aux;

    for(int i = 1; i <= n; i++){
        folosit.clear();
        for(int mask = 0; mask < (1 << nume[i].size()); mask++){
            aux.clear();
            for(unsigned bit = 0; bit < nume[i].size(); bit++){
                if(mask & (1 << bit)){
                    aux.push_back(nume[i][bit]);
                }
            }

            if(!folosit[aux]){
                secvente[aux]++;
                folosit[aux] = 1;
            }
        }
    }
}

int comb_2(int nr){
    return nr * (nr - 1) / 2;
}

void pinex(string curent, int sgn){
    char c;
    string aux;

    if(curent.empty()){
        c = 'a';
    }
    else{
        c = curent[curent.size() - 1];
    }

    for(; c <= LiteraMax; c++){
        aux = curent + c;

        if(secvente[aux]){
            sol += sgn * comb_2(secvente[aux]);
            pinex(aux, -sgn);
        }
    }
}

void calculare_solutie(){
    sol = 0;
    pinex("", 1);
}

void afisare_solutie(){
    fout << sol;
}

int main(){
    citire();

    prelucrare_nume();
    numarare_secvente();
    calculare_solutie();

    afisare_solutie();

    return 0;
}