/* Topcoder SRM 708, D2 500-pointer * BuildingStrings * James S. Plank * Mon Feb 20 15:59:09 EST 2017 */ #include #include #include #include #include using namespace std; typedef vector SVec; class BuildingStrings { public: vector findAny(int K); }; vector BuildingStrings::findAny(int K) { int i, score; vector rv; string s; /* Create the string s */ for (i = 0; i < 50; i++) s.push_back('a' + i%26); /* Do steps 2 and 3 from the writeup -- while s's score is less than or equal to K, add it to the return vector and decrement the score from K. While s's score is greater than K, remove the last letter from s. Do this until K <= 50. */ while (K > 50) { score = (s.size() > 26) ? (26 * s.size()) : (s.size() * s.size()); while (score <= K) { rv.push_back(s); K -= score; } s.pop_back(); } /* Step 4: K is <= 50, so you can create a string of length K with a single character. Its score will be K. */ if (K > 0) { s.clear(); s.resize(K, 'a'); rv.push_back(s); } return rv; }