simple BFS using a queue.
#include <iostream> #include <sstream> #include <cstring> #include <cstdio> #include <cstdlib> #include <algorithm> #include <ctime> #include <cctype> #include <cmath> #include <vector> #include <map> #include <set> #include <queue> #include<bitset> #define foreach(it, l) for (typeof(l.begin()) it = l.begin(); it != l.end(); it++) #define db(a) \ cout << #a << " = " << a << endl #define db2(a, b) \ cout << #a << " = " << a << " " << #b << " = " << b << endl #define inf (1<<30) using namespace std; int main() { #ifdef dennisbot freopen("in.in", "r", stdin); #endif string s; map<string, int> mapa; queue<string> q; q.push(""); int cont = 0; while (!q.empty()) { string val = q.front(); q.pop(); mapa[val] = cont++; if (val.size() == 5) continue; for (char i = (val == "") ? 'a' : val[val.size() - 1] + 1; i < 'z' + 1; i++) { q.push(val + i); } } char linea[6]; while (gets(linea)) { printf("%d\n", mapa[linea]); } return 0; }