#include <algorithm> #include <iostream> #include <iterator> #include <limits> #include <string> #include <type_traits> template <typename InputIterator, typename OutputIterator> OutputIterator transformToT9(InputIterator first, InputIterator last, OutputIterator result) { using CharIndex = typename std::decay<decltype(*first)>::type; CharIndex previous_t9_key = 0; auto toT9Key = [](CharIndex char_idx) { return '2' + std::min(7, (char_idx - (char_idx >> 4)) / 3);; }; while (first != last) { if (*first >= 'a' && *first <= 'z') { auto char_index = *first - 'a'; // Non-const - decr lookups const auto current_t9_key = toT9Key(char_index); if (current_t9_key == previous_t9_key) { *result++ = ' '; } previous_t9_key = current_t9_key; do { *result++ = current_t9_key; --char_index; } while ((char_index >= 0) && (toT9Key(char_index) == current_t9_key)); } else { const auto current_t9_key = '0'; if (current_t9_key == previous_t9_key) { *result++ = ' '; } previous_t9_key = current_t9_key; *result++ = current_t9_key; } ++first; } return result; } int main() try { std::istream& in_stream = std::cin; std::ostream& out_stream = std::cout; int total_test_cases; in_stream >> total_test_cases; if (total_test_cases < 0) { throw std::out_of_range("Input has invalid number of test cases."); } // operator>>() leaves the carriage return, ignore it. in_stream.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); std::string line_from_input_stream; for (int test_case = 1; test_case <= total_test_cases; ++test_case) { std::getline(in_stream, line_from_input_stream); out_stream << "Case #" << test_case << ": "; transformToT9(std::begin(line_from_input_stream), std::end(line_from_input_stream), std::ostream_iterator<char>(out_stream)); out_stream << "\n"; } } catch (std::exception& e) { std::cout << e.what(); }
100 yes no foo bar hello world hddq kkyhoyre uzfcnb acx rbgddvtcw hclntbql uu wdcugsavyc tlw pmtrkiiay c xezwcgdnbeeqe xbdkseuxgqlkx cjedbfsw hsyfsxei kehnuvvyffd iednzsvei cgvudnyjnp klqrk kiaoqhfirmu ym fw pzdbw jdz borbesw ckjsgrrr pvi elns owpnkykbovps hqkwe yozri ijg yb nqdoazstpgkysf msszwreoc ndfxj teg ufaviwl wxhscjasjne k pbaamphpelja opkrduk kjihlh cxbdnkmchcyt rpuluy oepgumcc wgaduzdluecjv rdzo kzr du f njh ppynzc ddl kkeuuh gzo ep lrfwpvrwswnbro uf wijamwj nvqiepe rt acwaxeh cqmgd uhcldds uxryfve uonjrx hfjrplomjw zbcvzb qsjacnxv ecvskxckxt yyxmdsnvvpjrp qmwjid ovzjiizibnz zyyrvraxdc ga vqsufbrbt pyfxxzz r lultcbltjp fmqzvvt x wkvpbjqnu ptzhkkxoyrjs eix olih eblhxhang gwlc bzqhsfaal ecoanr uvkqtluuekf knc crknhjyxplzh suqotqfdnb vcm idosqppcl zdayayeijgsk jhnbrk eocyoktsgruu wpk ajnxkbxqoj tr msan xnkxog haoznacoh kw ys jc utumijxbn rmcfbojv ibuiqcawfvhm
Case #1: 0 Case #2: 999337777 Case #3: 66 666 Case #4: 333666 666022 2777 Case #5: 4433555 555666096667775553 Case #6: 443 377 Case #7: 55 559994466699977733 Case #8: 8899993332226622 Case #9: 2 22299 Case #10: 7772243 3888 822290 Case #11: 442225556682277555088 88 Case #12: 9322288477772888999222 Case #13: 85559 Case #14: 76877755444 4442999 Case #15: 0222 Case #16: 99339999 922243662233 337733 Case #17: 99223557777338899477555 5599 Case #18: 222533 32233377779 Case #19: 44777799933377779933444 Case #20: 5533446688 888 888999333 333 3 Case #21: 44433 3669999777788833444 Case #22: 2224888 883669995667 Case #23: 55 55577 77755 Case #24: 5544426667744333444777688 Case #25: 9996033390799993229 Case #26: 539999 Case #27: 22666777223377779 Case #28: 22255 577774777 777 7770 Case #29: 7888444033555667777 Case #30: 66697665599955226668887 7777 Case #31: 44775593309996669999777444 Case #32: 44454099922 Case #33: 66773666299997777874559997777333 Case #34: 67777 77779999 977733666222 Case #35: 663 33399508334 Case #36: 8833328884449555 Case #37: 9 9944777722252777756633055 Case #38: 722 2 26744733555 52 Case #39: 066675577738855 Case #40: 55 5444 4455544 Case #41: 2229922366556222442229998 Case #42: 777 78855588999 Case #43: 6663374886222 222 Case #44: 9423889999355588332225888 Case #45: 77739999666 Case #46: 559999777 Case #47: 388 Case #48: 33306654407 7999669999222 Case #49: 3 3555 Case #50: 55 553388 88440 Case #51: 49999666 Case #52: 337 Case #53: 555777333978887779777796622777666 Case #54: 88333 Case #55: 944452695 Case #56: 668887744433733 Case #57: 7778 Case #58: 2 22292993344 Case #59: 22277643088442225553 37777 Case #60: 889977799933388833 Case #61: 88666 66577799 Case #62: 443335777 7555666 659 Case #63: 999922 222888999922 Case #64: 77 777752 2226699888 Case #65: 332228887777559922255998 Case #66: 999 999 9963777766888 88875777 7 Case #67: 776954443 Case #68: 66688899995444 444999944422669999 Case #69: 9999 999 9997778887772993222042 Case #70: 88877 77778833322777228 Case #71: 799933399 99 9999 9999 Case #72: 7770555885558222 22555857 Case #73: 3336779999888 888 8099 Case #74: 9558887225776688 Case #75: 7899994455 559966699977757777 Case #76: 3344499 Case #77: 666555444 44 Case #78: 33225554499442664 Case #79: 49555222 Case #80: 229999774477773332 25550 Case #81: 33222666266777 Case #82: 88 8885577855588 883355333 Case #83: 5566222 Case #84: 2227775566445999 997555999944 Case #85: 77778877666877333 36622 Case #86: 8882226 Case #87: 44436667777 77 7 7222555 Case #88: 99993299929993344454777755 Case #89: 544662277755 Case #90: 336662229996665587777477788 88 Case #91: 9755 Case #92: 25669955229977666508777 Case #93: 67777266 Case #94: 996655996664 Case #95: 4426669999662 22266644 Case #96: 559 Case #97: 9997777 Case #98: 5222088 8 8864445992266 Case #99: 7776222333226665888 Case #100: 444228844477222 29333888446