Untitled

 avatar
unknown
c_cpp
2 years ago
1.6 kB
8
Indexable
/*
Nom     : Remi Jones
NI      : A00191005
Cadre   : INFO4302 - Algorithmes avancees
Objet   : Examen intra - Exercice 1
Fichier : anagrammes.cpp
Date    : 2023-02-16
*/

#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <set>
#include <algorithm>

using namespace std;

int main() {
    // lecture
    int N;
    cin >> N;
    cin.ignore();
    for (int i = 0; i<N; i++) {
        // lecture
        set<string> mots;
        string s;
        getline(cin, s);
        stringstream ss(s);
        string token;
        while (ss >> token) {
            // set afin d'assurer qu'on a des mots uniques
            mots.insert(token);
        }

        // vector afin de pouvoir adresser par index
        vector<string> motsUniques;
        for (auto el : mots)
            motsUniques.push_back(el);
        
        int ana = 0; // nombre d'anagrammes
        for (int i = 0; i<motsUniques.size(); i++) {
            // iterer a partir de i + 1 pour eviter les doublons
            for (int j = i+1; j<motsUniques.size(); j++) {
                string motI = motsUniques[i];
                string motJ = motsUniques[j];
                // trier les mots en ordre alphabetique
                sort(motI.begin(), motI.end());
                sort(motJ.begin(), motJ.end());
                if (motI == motJ) {
                    ana++;
                }
            }
        }
        cout << ana << endl;
        
    }
}

// ressource consultée : https://stackoverflow.com/questions/9107516/sorting-characters-of-a-c-string
Editor is loading...