Solution of 11530 - SMS Typing

Problem Description
source:https://uva.onlinejudge.org/external/115/11530.html


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.


In the above grid each cell represents one key. Here 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.

Input 

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.  

Output 

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 


welcome to ulab 
good luck and have fun 

Sample Output 

Case #1: 29 
Case #2: 41

Solution:
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#include<stdio.h>
#include<stdlib.h>

using namespace std;
int main()
{
    char ch;
    string input;
    int n,i,j,value;
    while(scanf("%d%c",&n,&ch)==2)
    {
        for(i=1;i<=n;i++)
        {
            value=0;
           getline(cin,input);
            for(j=0;input[j]!='\0';j++)
            {
                if(input[j]=='a'||input[j]=='d'||input[j]=='g'||input[j]=='j'||input[j]=='m'||input[j]=='p'||input[j]=='t'||input[j]=='w'||input[j]==' ')
                    value+=1;
                else if(input[j]=='b'||input[j]=='e'||input[j]=='h'||input[j]=='k'||input[j]=='n'||input[j]=='q'||input[j]=='u'||input[j]=='x')
                    value+=2;
                else if(input[j]=='c'||input[j]=='f'||input[j]=='i'||input[j]=='l'||input[j]=='o'||input[j]=='r'||input[j]=='v'||input[j]=='y')
                    value+=3;
                else if(input[j]=='s'||input[j]=='z')
                    value+=4;

            }
            printf("Case #%d: %d\n",i,value);
        }
    }
    return 0;
}

No comments:

Post a Comment

Write your comment - Share Knowledge and Experience