fork download
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <iterator>
  4. #include <limits>
  5. #include <string>
  6. #include <type_traits>
  7.  
  8. template <typename InputIterator, typename OutputIterator>
  9. OutputIterator transformToT9(InputIterator first, InputIterator last,
  10. OutputIterator result) {
  11. using CharIndex = typename std::decay<decltype(*first)>::type;
  12.  
  13. CharIndex previous_t9_key = 0;
  14. auto toT9Key = [](CharIndex char_idx) {
  15. return '2' + std::min(7, (char_idx - (char_idx >> 4)) / 3);;
  16. };
  17.  
  18. while (first != last) {
  19. if (*first >= 'a' && *first <= 'z') {
  20. auto char_index = *first - 'a'; // Non-const - decr lookups
  21. const auto current_t9_key = toT9Key(char_index);
  22.  
  23. if (current_t9_key == previous_t9_key) {
  24. *result++ = ' ';
  25. }
  26. previous_t9_key = current_t9_key;
  27.  
  28. do {
  29. *result++ = current_t9_key;
  30. --char_index;
  31. }
  32. while ((char_index >= 0) &&
  33. (toT9Key(char_index) == current_t9_key));
  34. }
  35. else {
  36. const auto current_t9_key = '0';
  37. if (current_t9_key == previous_t9_key) {
  38. *result++ = ' ';
  39. }
  40. previous_t9_key = current_t9_key;
  41. *result++ = current_t9_key;
  42. }
  43.  
  44. ++first;
  45. }
  46.  
  47. return result;
  48. }
  49.  
  50. int main()
  51. try {
  52. std::istream& in_stream = std::cin;
  53. std::ostream& out_stream = std::cout;
  54.  
  55. int total_test_cases;
  56. in_stream >> total_test_cases;
  57.  
  58. if (total_test_cases < 0) {
  59. throw std::out_of_range("Input has invalid number of test cases.");
  60. }
  61.  
  62. // operator>>() leaves the carriage return, ignore it.
  63. in_stream.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  64.  
  65. std::string line_from_input_stream;
  66. for (int test_case = 1; test_case <= total_test_cases; ++test_case) {
  67. std::getline(in_stream, line_from_input_stream);
  68.  
  69. out_stream << "Case #" << test_case << ": ";
  70. transformToT9(std::begin(line_from_input_stream),
  71. std::end(line_from_input_stream),
  72. std::ostream_iterator<char>(out_stream));
  73. out_stream << "\n";
  74. }
  75. }
  76. catch (std::exception& e) {
  77. std::cout << e.what();
  78. }
Success #stdin #stdout 0s 3480KB
stdin
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
stdout
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