Tuesday, August 18, 2009

SMS Typing

Of late I have been solving a lot of problems. Here s a problem which you guys can solve.

Cell phones have become an essential part of modern life. In addition to making voice calls, cell phones can be used to send text messages, which are known as SMS for short. Unlike computer keyboards, most cell phones have limited number of keys. To accommodate all alphabets, letters are compacted into single key. Therefore, to type certain characters, a key must be repeatedly pressed until that character is shown on the display panel.

In this problem we are interested in finding out the number of times keys on a cell phone must be pressed to type a particular message.

In this problem we will assume that the key pad of our cell phone is arranged as follows.


| | abc | def |


| ghi | jkl | mno |


| pqrs | tuv | wxyz |


| |sp | |


In the above grid each cell represents one key. Here SP means a space. In order to type the letter ‘a’, we must press that key once, however to type ‘b’ the same key must be repeatedly pressed twice and for ‘c’ three times. In the same manner, one key press for ‘d’, two for ‘e’ and three for ‘f’. This is also applicable for the remaining keys and letters. Note that it takes a single press to type a space.

The first line of input will be a positive integer T where T denotes the number of test cases. T lines will then follow each containing only spaces and lower case letters. Each line will contain at least 1 and at most 100 characters.

For every case of input there will be one line of output. It will first contain the case number followed by the number of key presses required to type the message of that case. Look at the sample output for exact formatting.

Sample Input : Sample Output

welcome to ulab : 29

good luck and have fun : 41

Problemsetter: Shamim Hafiz

Special Thanks: Sohel Hafiz

You can download the solution done by me and my friend amruth at here.
The code was compiled and executed on a gcc compiler