Problem:


problem

-->

Extraordinary Girl (II)

    She loves e-mail so much! She sends e-mails by her cellular phone to
her friends when she has breakfast, she talks with other friends, and even
when she works in the library! Her cellular phone has somewhat simple
layout (Figure 1). Pushing button 1 once displays a character (’), pushing

it twice in series displays a character (,), and so on, and pushing it 6 times
displays (’) again. Button 2 corresponds to charaters (abcABC), and, for
example, pushing it four times displays (A). Button 3-9 have is similar to
button 1. Button 0 is a special button: pushing it once make her possible
to input characters in the same button in series. For example, she has to
push “20202” to display “aaa” and “660666” to display “no”. In addition,
pushing button 0 n times in series (n > 1) displays n − 1 spaces. She never
pushes button 0 at the very beginning of her input. Here are some examples
of her input and output:

              666660666 --> No
44444416003334446633111 --> I’m fine.
20202202000333003330333 --> aaba f ff

    One day, the chief librarian of the library got very angry with her and
hacked her cellular phone when she went to the second floor of the library
to return books in shelves. Now her cellular phone can only display button
numbers she pushes. Your task is to write a program to convert the sequence
of button numbers into correct characters and help her continue her e-mails!

Input

Input consists of several lines. Each line contains the sequence of button
numbers without any spaces. You may assume one line contains no more
than 10000 numbers. Input terminates with EOF.

Output

For each line of input, output the corresponding sequence of characters in
one line.

Sample Input

666660666
44444416003334446633111
20202202000333003330333

Output for the Sample Input

No
I'm fine.
aaba  f ff



Sample input:
666660666
44444416003334446633111
20202202000333003330333


Sample output:
No
I'm fine.
aaba  f ff

Submission status: Wrong Answer

Solution code:
alpha = ["","'.!?","abcABC","defDEF","ghiGHI","jklJKL","mnoMNO","pqrsPQRS","tuvTUV","wxyzWXYZ"]
while 1:
    try:
        ans = ""
        sers = ""
        c = 0
        for key in raw_input():
            if key == "0":
                if len(sers) > 0:
                    n,l = int(sers[0]),len(sers)
                    l = (l-1)%len(alpha[n])
                    ans += alpha[n][l]
                    sers = ""
                else:
                    ans += " "
            elif len(sers) == 0 or sers[-1] == key:
                sers += key
            else:
                n,l = int(sers[0]),len(sers)
                l = (l-1)%len(alpha[n])
                ans += alpha[n][l]
                sers = key
        if len(sers) > 0:
            if sers[0] == 0:
                ans += " "*len(sers)
            else:
                n,l = int(sers[0]),len(sers)
                l = (l-1)%len(alpha[n])
                ans += alpha[n][l]
        print ans
    except:
        break

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
import re
import math
import collections
import itertools
import functools

DEBUG = True
DEBUG = False

def dbg(*args):
    if DEBUG:
        print("DBG: ", file=sys.stderr, end="")
        print(*args, file=sys.stderr)

BUTTON_CHARS = (
    "',.!?",
    "abcABC",
    "defDEF",
    "ghiGHI",
    "jklJKL",
    "mnoMNO",
    "pqrsPRQS",
    "tuvTUV",
    "wxyzWXYZ",
)

def pieces(s):
    char  = None
    count = 0;

    for c in s:
        if c != char:
            if char is not None:
                yield (char, count)
            char  = c
            count = 1
        else:
            count += 1

    yield (char, count)

def solve(s):
    for char, count in pieces(s):
        if char in "123456789":
            idx = int(char) - 1
            bchars = BUTTON_CHARS[idx]
            out = bchars[(count-1) % len(bchars)]
            print(out, end="")
        elif char == "0":
            print(" " * (count-1), end="")
        else:
            assert False

    print()

def main():
    for s in sys.stdin:
        s = s.strip()
        solve(s)

if __name__ == "__main__": main()

Submission status: Wrong Answer

Solution code:
#-*- coding:utf-8 -*-
import sys

#入力されたデータに関して携帯のキーを返す
def decode_keitai(str):

    #携帯キーを表す配列を作成
    keitai_keys = [" ","',.!?","abcABC","defDEF","ghiGHI","jklJKL","mnoMNO","pqrsPQRS","tuvTUV","wxyzWXYZ"]

    #結果文字列
    results = []

    #入力文字列をリストに変換
    inputs = str.split("\n")

    #各文字列について
    for input in inputs:

        input = input.replace("\n","")
        result = "" #デコード結果
        #前の文字
        s_before = ""

        #文字列の最後まで
        while len(input) > 0:

            print "s_before:" + s_before

            #文頭の1文字を取得
            s = input[0]
            input = input[1:]
            print s

            #最初の1文字ならcontinue
            if s_before == "":
                s_before = s
                continue

            #最初の1文字が前の文字と違うなら携帯コードにデコード
            if not s in s_before:
                if s_before[0] == "0":
                    result += s_before[:-1].replace("0"," ")
                else:
                    id = int(s_before[0])
                    keylen = len(keitai_keys[id])
                    result += keitai_keys[id][(len(s_before)-1)%keylen]
                s_before = s
            else:
                s_before += s
            

        #最後に残った文字列を変換
        if len(s_before) > 0:
            if s_before[0] == "0":
                result += s_before[:-1].replace("0"," ")
            else:
                id = int(s_before[0])
                keylen = len(keitai_keys[id])
                result += keitai_keys[id][(len(s_before)-1)%keylen]
            s_before = ""

        results.append(result)

    return "\n".join(results)

if __name__=="__main__":
    lines = []
    for line in sys.stdin:
        lines.append(line.replace("\n",""))
    print decode_keitai("\n".join(lines))

Submission status: Time Limit Exceeded

Solution code:
a=[['\'',',','.','!','?'],
   ['a','b','c','A','B','C'],
   ['d','e','f','D','E','F'],
   ['g','h','i','G','H','I'],
   ['j','k','l','J','K','L'],
   ['m','n','o','M','N','O'],
   ['p','q','r','s','P','Q','R','S'],
   ['t','u','v','T','U','V'],
   ['w','x','y','z','W','X','Y','Z']]
while 1:
    try:b=input()+'@'
    except:pass
    c=0
    ans=''
    for i in range(len(b)-1):
        if b[i]!=b[i+1]:
            if b[i]!='0':
                d=int(b[i])-1
                ans+=a[d][c%len(a[d])]
            c=0
        else: c+=1
        if b[i]==b[i+1]=='0':ans+=' '
    print(ans)

Submission status: Accepted

Solution code:
alpha = ["","',.!?","abcABC","defDEF","ghiGHI","jklJKL","mnoMNO","pqrsPQRS","tuvTUV","wxyzWXYZ"]
while 1:
    try:
        ans = ""
        sers = ""
        c = 0
        for key in raw_input():
            if key == "0":
                if len(sers) > 0:
                    n,l = int(sers[0]),len(sers)
                    l = (l-1)%len(alpha[n])
                    ans += alpha[n][l]
                    sers = ""
                else:
                    ans += " "
            elif len(sers) == 0 or sers[-1] == key:
                sers += key
            else:
                n,l = int(sers[0]),len(sers)
                l = (l-1)%len(alpha[n])
                ans += alpha[n][l]
                sers = key
        if len(sers) > 0:
            if sers[0] == 0:
                ans += " "*len(sers)
            else:
                n,l = int(sers[0]),len(sers)
                l = (l-1)%len(alpha[n])
                ans += alpha[n][l]
        print ans
    except:
        break

Submission status: Accepted

Solution code:
ref = ["",
       "',.!?",
       "abcABC",
       "defDEF",
       "ghiGHI",
       "jklJKL",
       "mnoMNO",
       "pqrsPQRS",
       "tuvTUV",
       "wxyzWXYZ"]

def inp(s):
    n = int(s[0])
    p = (len(s)-1)%len(ref[n])
    return ref[n][p]

while 1:
    try:
        pushes = raw_input()
    except:
        break
    ans = ""
    s = ""
    for push in pushes:
        l = len(s)
        if push == "0":
            if l > 0: ans += inp(s)
            else: ans += " "
            s = ""
        elif l == 0 or s[-1] == push:
            s += push
        else:
            ans += inp(s)
            s = push
    if l > 0:
        ans += " "*l if s[0] == "0" else inp(s)
    print ans

Submission status: Accepted

Solution code:
def getchar(button,num):
    if button=='0':
        return ' '*(num-1)
    elif button=='1':
        num=(num-1)%5
        return "',.!?"[num]
    elif button=='2':
        num=(num-1)%6
        return "abcABC"[num]
    elif button=='3':
        num=(num-1)%6
        return "defDEF"[num]
    elif button=='4':
        num=(num-1)%6
        return "ghiGHI"[num]
    elif button=='5':
        num=(num-1)%6
        return "jklJKL"[num]
    elif button=='6':
        num=(num-1)%6
        return "mnoMNO"[num]
    elif button=='7':
        num=(num-1)%8
        return "pqrsPQRS"[num]
    elif button=='8':
        num=(num-1)%6
        return "tuvTUV"[num]
    elif button=='9':
        num=(num-1)%8
        return "wxyzWXYZ"[num]



while(1):
    try:
        keyin=raw_input()
    except:
        break
    oldb='0'
    num=1
    ans=''
    for k in keyin:
        if oldb!=k:
            ans=ans+getchar(oldb,num)
            num=1
        else:
            num=num+1
        oldb=k
    ans=ans+getchar(oldb,num)
    print ans

Submission status: Accepted

Solution code:
a=[['\'',',','.','!','?'],
   ['a','b','c','A','B','C'],
   ['d','e','f','D','E','F'],
   ['g','h','i','G','H','I'],
   ['j','k','l','J','K','L'],
   ['m','n','o','M','N','O'],
   ['p','q','r','s','P','Q','R','S'],
   ['t','u','v','T','U','V'],
   ['w','x','y','z','W','X','Y','Z']]
while 1:
    try:b=input()+'@'
    except:break
    c=0
    ans=''
    for i in range(len(b)-1):
        if b[i]!=b[i+1]:
            if b[i]!='0':
                d=int(b[i])-1
                ans+=a[d][c%len(a[d])]
            c=0
        else: c+=1
        if b[i]==b[i+1]=='0':ans+=' '
    print(ans)

Submission status: Accepted

Solution code:
import fileinput

chr_master = [
    ' ', '\',.!?', 'abcABC', 'defDEF', 'ghiGHI',
    'jklJKL', 'mnoMNO', 'pqrsPQRS', 'tuvTUV', 'wxyzWXYZ'
]

for s in (line.strip() for line in fileinput.input()):
    s += '!'
    prev_c = None
    cnt = 0
    chr_list = []
    for c in s:
        if c == prev_c:
            cnt += 1
            continue
        if prev_c == '0':
            if cnt > 1:
                chr_list.extend([(0, 0)] * (cnt - 1))
        else:
            if prev_c is not None:
                chr_list.append((int(prev_c), cnt - 1))

        prev_c = c
        cnt = 1
    print(''.join(chr_master[i][j] for i, j in chr_list))

Submission status: Accepted

Solution code:
import fileinput

chr_master = [
    ' ', '\',.!?', 'abcABC', 'defDEF', 'ghiGHI',
    'jklJKL', 'mnoMNO', 'pqrsPQRS', 'tuvTUV', 'wxyzWXYZ'
]

for s in (line.strip() for line in fileinput.input()):
    s += '!'
    prev_c = None
    cnt = 0
    ans = ''
    for c in s:
        if c == prev_c:
            cnt += 1
            continue
        if prev_c == '0':
            if cnt > 1:
                ans += ' ' * (cnt - 1)
        else:
            if prev_c is not None:
                ans += chr_master[int(prev_c)][cnt - 1]

        prev_c = c
        cnt = 1
    print(ans)

Submission status: Accepted

Solution code:
import fileinput

chr_master = [' ', '\',.!?', 'abcABC', 'defDEF', 'ghiGHI', 'jklJKL', 'mnoMNO', 'pqrsPQRS', 'tuvTUV', 'wxyzWXYZ']

for s in (line.strip() for line in fileinput.input()):
    s += '!'
    prev_c, cnt = 0, 1
    ans = ''
    for c in s:
        if c == prev_c:
            cnt += 1
            continue
        if prev_c == '0':
            if cnt > 1:
                ans += ' ' * (cnt - 1)
        else:
            ans += chr_master[int(prev_c)][cnt - 1]
        prev_c, cnt = c, 1
    print(ans[1:])

Submission status: Accepted

Solution code:
# AOJ 1003: Extraordinary Girl II
# Python3 2018.7.4 bal4u

tbl = ["", "',.!?", "abcABC", "defDEF", "ghiGHI", "jklJKL", \
       "mnoMNO", "pqrsPQRS", "tuvTUV", "wxyzWXYZ"]

while True:
	try: s = input().strip()
	except: break
	ans, i = '', 0
	while i < len(s):
		c = s[i]
		w, d, i = 0, int(c), i+1
		while i < len(s) and s[i] == c: i, w = i+1, w+1
		if d == 0: ans += ' '*w
		else: ans += tbl[d][w%len(tbl[d])]
	print(ans)

Submission status: Accepted

Solution code:
Button={
"1":"',.!?",
"2":"abcABC",
"3":"defDEF",
"4":"ghiGHI",
"5":"jklJKL",
"6":"mnoMNO",
"7":"pqrsPQRS",
"8":"tuvTUV",
"9":"wxyz",
"0":" "}

while True:
    try:
        sent=raw_input()
    except EOFError:
        break
    init=sent[0]
    cnt=1
    ans=[]
    for c in sent[1:]:
        if c==init:
            cnt+=1
        elif init=="0" and c!="0":
            init=c
            ans.append(" "*(cnt-1))
            cnt=1
        else:
            ans.append(Button[init][cnt-1])
            init=c
            cnt=1
    else:
        ans.append(Button[init][cnt-1])
    print "".join(ans)

Submission status: Accepted

Solution code:
#-*- coding:utf-8 -*-
import sys

#入力されたデータに関して携帯のキーを返す
def decode_keitai(str):

    #携帯キーを表す配列を作成
    keitai_keys = [" ","',.!?","abcABC","defDEF","ghiGHI","jklJKL","mnoMNO","pqrsPQRS","tuvTUV","wxyzWXYZ"]

    #結果文字列
    results = []

    #入力文字列をリストに変換
    inputs = str.split("\n")

    #各文字列について
    for input in inputs:

        input = input.replace("\n","")
        result = "" #デコード結果
        #前の文字
        s_before = ""

        #文字列の最後まで
        while len(input) > 0:

            #文頭の1文字を取得
            s = input[0]
            input = input[1:]

            #最初の1文字ならcontinue
            if s_before == "":
                s_before = s
                continue

            #最初の1文字が前の文字と違うなら携帯コードにデコード
            if not s in s_before:
                if s_before[0] == "0":
                    result += s_before[:-1].replace("0"," ")
                else:
                    id = int(s_before[0])
                    keylen = len(keitai_keys[id])
                    result += keitai_keys[id][(len(s_before)-1)%keylen]
                s_before = s
            else:
                s_before += s
            

        #最後に残った文字列を変換
        if len(s_before) > 0:
            if s_before[0] == "0":
                result += s_before[:-1].replace("0"," ")
            else:
                id = int(s_before[0])
                keylen = len(keitai_keys[id])
                result += keitai_keys[id][(len(s_before)-1)%keylen]
            s_before = ""

        results.append(result)

    return "\n".join(results)

if __name__=="__main__":
    lines = []
    for line in sys.stdin:
        lines.append(line.replace("\n",""))
    print decode_keitai("\n".join(lines))

Submission status: Accepted

Solution code:
T = [
    None,
    "',.!?",
    "abcABC",
    "defDEF",
    "ghiGHI",
    "jklJKL",
    "mnoMNO",
    "pqrsPQRS",
    "tuvTUV",
    "wxyzWXYZ",
]
try:
    while 1:
        ans = []
        *S, = map(int, input())
        S.append(0)
        prv = 0
        cur = -1
        for c in S:
            if c == prv:
                if c == 0:
                    ans.append(" ")
                    cur = 0
                else:
                    cur += 1
            else:
                if prv != 0:
                    t = T[int(prv)]
                    ans.append(t[cur%len(t)])
                cur = 0
            prv = c
        print(*ans, sep='')
except EOFError:
    ...

Submission status: Accepted

Solution code:
#!/usr/bin/env python

from collections import deque
import itertools as it
import sys
import math

sys.setrecursionlimit(10000000)

S = ["", "',.!?", "abcABC", "defDEF", "ghiGHI", 
"jklJKL", "mnoMNO", "pqrsPQRS", "tuvTUV", "wxyzWXYZ"]

for line in sys.stdin:
    ans = ''
    k = 0
    cnt = 0
    line = list(line)
    line[-1] = "-1"
    for c in line:
        if int(c) == k:
            cnt += 1
        else:
            if k == 0:
                ans += ' ' * cnt
            else:
                ans += S[k][cnt % len(S[k])]
            cnt = 0
            k = int(c)
    print ans

Submission status: Runtime Error

Solution code:
ref = ["","',.!?","abcABC","defDEF","ghiGHI","jklJKL","mnoMNO","pqrsPQRS","tuvTUV","wxyzWXYZ"]
def inp(s[0],l):
    n = int(s[0])
    p = (l-1)%len(ref[n])
    return ref[n][p]

while 1:
    try:
        ans = ""
        s = ""
        for push in raw_input():
            l = len(s)
            if push == "0":
                if l > 0:
                    ans += inp(s[0],l)
                    s = ""
                else:
                    ans += " "
            elif l == 0 or s[-1] == push:
                s += push
            else:
                ans += inp(s[0],l)
                s = push
        if l > 0:
            ans += " "*l if s[0] == 0 else ans += inp(s[0],l):
        print ans
    except:
        break

Submission status: Runtime Error

Solution code:
#-*- coding:utf-8 -*-
import sys

#入力されたデータに関して携帯のキーを返す
def decode_keitai(str):

    #携帯キーを表す配列を作成
    keitai_keys = [" ","',.!?","abcABC","defDEF","ghiGHI","jklJKL","mnoMNO","pqrsPQRS","tuvTUV","wxyzWXYZ"]

    #結果文字列
    results = []

    #入力文字列をリストに変換
    inputs = str.split("\n")

    #各文字列について
    for input in inputs:

        result = "" #デコード結果
        #前の文字
        s_before = ""

        #文字列の最後まで
        while len(str) > 0:

            #文頭の1文字を取得
            s = str[0]
            str = str[1:]
            print s

            #最初の1文字ならcontinue
            if s_before == "":
                s_before = s
                continue

            #最初の1文字が前の文字と違うなら携帯コードにデコード
            if not s in s_before:
                if s_before[0] == "0":
                    result += s_before[:-1].replace("0"," ")
                else:
                    id = int(s_before[0])
                    keylen = len(keitai_keys[id])
                    result += keitai_keys[id][(len(s_before)-1)%keylen]
                s_before = s
            else:
                s_before += s
            

        #最後に残った文字列を変換
        if len(s_before) > 0:
            if s_before[0] == "0":
                result += s_before[:-1].replace("0"," ")
            else:
                id = int(s_before[0])
                keylen = len(keitai_keys[id])
                result += keitai_keys[id][(len(s_before)-1)%keylen]

        results.append(result)

    return "\n".join(results)

if __name__=="__main__":
    print decode_keitai(sys.argv[1])

Submission status: Runtime Error

Solution code:
#-*- coding:utf-8 -*-
import sys

#入力されたデータに関して携帯のキーを返す
def decode_keitai(str):

    #携帯キーを表す配列を作成
    keitai_keys = [" ","',.!?","abcABC","defDEF","ghiGHI","jklJKL","mnoMNO","pqrsPQRS","tuvTUV","wxyzWXYZ"]

    #結果文字列
    results = []

    #入力文字列をリストに変換
    inputs = str.split("\n")

    #各文字列について
    for input in inputs:

        result = "" #デコード結果
        #前の文字
        s_before = ""

        #文字列の最後まで
        while len(str) > 0:

            #文頭の1文字を取得
            s = str[0]
            str = str[1:]
            print s

            #最初の1文字ならcontinue
            if s_before == "":
                s_before = s
                continue

            #最初の1文字が前の文字と違うなら携帯コードにデコード
            if not s in s_before:
                if s_before[0] == "0":
                    result += s_before[:-1].replace("0"," ")
                else:
                    id = int(s_before[0])
                    keylen = len(keitai_keys[id])
                    result += keitai_keys[id][(len(s_before)-1)%keylen]
                s_before = s
            else:
                s_before += s
            

        #最後に残った文字列を変換
        if len(s_before) > 0:
            if s_before[0] == "0":
                result += s_before[:-1].replace("0"," ")
            else:
                id = int(s_before[0])
                keylen = len(keitai_keys[id])
                result += keitai_keys[id][(len(s_before)-1)%keylen]

        results.append(result)

    return "\n".join(results)

if __name__=="__main__":
    lines = []
    for line in sys.stdin:
        lines.append(line)
    print decode_keitai("\n".join(lines))

Submission status: Runtime Error

Solution code:
def main():

    count = -1
    data = input()
    before_number = data[0]
    output = []
    for n in data:
        if before_number == n:
            count += 1

        else:
#             print('before_number: ' + before_number + ', count:  ' + str(count))
#             print()
            if before_number == '0':
                for zero in range(count):
                    output.append(' ')
            else:
                output.append(keys[int(before_number)][count])
#                 print(keys[int(before_number)][count], end='')

            before_number = n
            count = 0

    else:
#         print('before_number: ' + before_number + ', count:  ' + str(count))
#         print()
        if before_number == '0':
            for zero in range(count):
                output.append(' ')
        else:
            output.append(keys[int(before_number)][count])
#             print(keys[int(before_number)][count], end='')


#         output.append(keys[int(before_number)][count])
#         print(keys[int(n)][count])

    print(''.join(output))


if __name__ == '__main__':
    while True:
        try:
            main()
        except EOFError:
            break

Submission status: Runtime Error

Solution code:
keys = [[' '],
       ['\'', ',','.', '!', '?'],
       ['a', 'b', 'c', 'A', 'B', 'C'],
       ['d', 'e', 'f', 'D', 'E', 'F'],
       ['g', 'h', 'i', 'G', 'H', 'I'],
       ['j', 'k', 'l', 'J', 'K', 'L'],
       ['m', 'n', 'o', 'M', 'N', 'O'],
       ['p', 'q', 'r', 'P', 'Q', 'R'],
       ['t', 'u', 'v', 'T', 'U', 'V'],
       ['x', 'y', 'z', 'X', 'Y', 'Z']]


def main():

    count = -1
    data = input()
    before_number = data[0]
    output = []
    for n in data:
        if before_number == n:
            count += 1

        else:
            if before_number == '0':
                for zero in range(count):
                    output.append(' ')
            else:
                output.append(keys[int(before_number)][count])
#                 print(keys[int(before_number)][count], end='')

            before_number = n
            count = 0

    else:
#         print('before_number: ' + before_number + ', count:  ' + str(count))
#         print()
        if before_number == '0':
            for zero in range(count):
                output.append(' ')
        else:
            output.append(keys[int(before_number)][count])
#             print(keys[int(before_number)][count], end='')


#         output.append(keys[int(before_number)][count])
#         print(keys[int(n)][count])

    print(''.join(output))


if __name__ == '__main__':
    while True:
        try:
            main()
        except EOFError:
            break

Submission status: Runtime Error

Solution code:
keys = [[' '],
       ['\'', ',','.', '!', '?'],
       ['a', 'b', 'c', 'A', 'B', 'C'],
       ['d', 'e', 'f', 'D', 'E', 'F'],
       ['g', 'h', 'i', 'G', 'H', 'I'],
       ['j', 'k', 'l', 'J', 'K', 'L'],
       ['m', 'n', 'o', 'M', 'N', 'O'],
       ['p', 'q', 'r', 'P', 'Q', 'R'],
       ['t', 'u', 'v', 'T', 'U', 'V'],
       ['x', 'y', 'z', 'X', 'Y', 'Z']]


def main():

    count = -1
    data = input()
    before_number = data[0]
    output = []
    for n in data:
        if before_number == n:
            count += 1

        else:
            if before_number == '0':
                for zero in range(count):
                    output.append(' ')
            else:
                output.append(keys[int(before_number)][count])

            before_number = n
            count = 0

    else:
        if before_number == '0':
            for zero in range(count):
                output.append(' ')
        else:
            output.append(keys[int(before_number)][count])

    print(''.join(output))


if __name__ == '__main__':
    while True:
        try:
            main()
        except EOFError:
            break