#include #include #include #include using namespace std; string inventory(string n) { map counts; for (size_t i=0; i::iterator i = counts.begin(); i != counts.end(); ++i) { if (i->second > 0) out << i->second << i->first; } return out.str(); } void doit(string n) { string x = n; //cout << n << endl; map encountered; encountered[n] = 0; for (int i=0; i<15; ++i) { string inv = inventory(x); //cout << " " << inv << endl; if (inv == x) { if (i == 0) cout << n << " is self-inventorying" << endl; else cout << n << " becomes self-inventorying " << "after " << i << " steps" << endl; return; } map::iterator it = encountered.find(inv); if (it != encountered.end()) { cout << n << " enters an inventory loop of length " << i - it->second + 1 << endl; return; } encountered[inv] = i+1; x = inv; } cout << n << " cannot be classified after 15 iterations" << endl; } int main() { string n; while (true) { cin >> n; if (n == "-1") break; doit(n); } return 0; }