Skip to content

Instantly share code, notes, and snippets.

@salvatorecapolupo
Last active November 27, 2025 16:18
Show Gist options
  • Select an option

  • Save salvatorecapolupo/6ec54bfe6d0da07b629be54d67e6a208 to your computer and use it in GitHub Desktop.

Select an option

Save salvatorecapolupo/6ec54bfe6d0da07b629be54d67e6a208 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
using namespace std;
#define SIZE_OF_ALPHABET 5
struct Report {
long long totalPossible = 0;
long long attemptsToMatch = -1;
};
bool brute_force(string target, int maxLen, Report& r) {
string alphabet = "abcde";
string attempt;
long long attemptCounter = 0;
cout << "Cerco target: "<< target << endl;
cout << "Uso alfabeto: "<< alphabet << endl;
// Calcola PRIMA tutti i tentativi possibili
for (int len = 1; len <= maxLen; len++) {
long long t = 1;
for (int i = 0; i < len; i++) t *= SIZE_OF_ALPHABET;
r.totalPossible += t;
}
// brute force
for (int len = 1; len <= maxLen; len++) {
long long totalLen = 1; //intero a 64 bit
for (int i = 0; i < len; i++) totalLen *= SIZE_OF_ALPHABET;
for (long long n = 0; n < totalLen; n++) {
attemptCounter++;
attempt = "";
long long x = n;
for (int i = 0; i < len; i++) {
attempt += alphabet[x % SIZE_OF_ALPHABET];
x /= SIZE_OF_ALPHABET;
}
if (attempt == target) {
r.attemptsToMatch = attemptCounter;
return true;
}
}
}
return false;
}
void printReport(const Report& r) {
cout << "\n--- REPORT FINALE ---\n";
cout << "Tentativi totali POSSIBILI: " << r.totalPossible << "\n";
if (r.attemptsToMatch >= 0) {
cout << "Tentativi necessari per trovare la parola: "
<< r.attemptsToMatch << "\n";
double perc = (double)r.attemptsToMatch / r.totalPossible * 100.0;
cout << "Percentuale percorso prima del successo: "
<< perc << "%\n";
} else {
cout << "Parola NON trovata.\n";
}
}
int main() {
//string target = "ab";
string target;
cout << "Inserisci la parola da trovare: ";
cin >> target;
int maxLen = target.size();
Report rep;
brute_force(target, maxLen, rep);
printReport(rep);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment