Cell phones have become an essential part of modern life. In addition tomaking voice calls, cell phones can be used to send text messages, whichare known as SMS for short. Unlike computer keyboards, most cell phoneshave limited number of keys. To accommodate all alphabets, letters arecompacted into single key. Therefore, to type certain characters, a keymust be repeatedly pressed until that character is shown on the displaypanel.
In this problem we are interested in finding out the number of times keys on a cell phone must bepressed 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 twiceand for ‘c’ three times. In the same manner, one key press for ‘d’, two for ‘e’ and three for ‘f’. This isalso 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 lineswill then follow each containing only spaces and lower case letters. Each line will contain at least 1 andat most 100 characters.
Output
For every case of input there will be one line of output. It will first contain the case number followedby the number of key presses required to type the message of that case. Look at the sample output forexact formatting.
Sample Input
2
welcome to ulab
good luck and have fun
Sample Output
Case #1: 29
Case #2: 41
问题链接:UVA11530 SMS Typing
问题简述:(略)
问题分析:(略)程序说明:
这个问题直接模拟实现。
功能手机键盘的各个字母按键次数是固定,使用map来存储各个字符的按键次数是一种比较简单的做法。
然而用数组来存储各个字符的按键次数,效率更高。
题记:离散表通常用map来实现,连续的表则用数组来实现,效率很重要。
参考链接:(略)
AC的C语言程序如下:
/* UVA11530 SMS Typing */
#include <stdio.h>
#include <ctype.h>
#define N 100
char s[N + 1];
int m[] = {1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 1, 2, 3, 4};
int main(void)
{
int t, caseno=0, sum;
scanf("%d", &t);
getchar();
while(t--) {
gets(s);
sum = 0;
for(int i=0; s[i]; i++)
if(islower(s[i]))
sum += m[s[i] - 'a'];
else if(s[i] == ' ')
sum += 1;
printf("Case #%d: %d\n", ++caseno, sum);
}
return 0;
}
AC的C++语言程序如下:
/* UVA11530 SMS Typing */
#include <iostream>
#include <map>
#include <stdio.h>
using namespace std;
const int N = 100;
char s[N + 1];
map <char, int> m;
int main()
{
int t, caseno=0, sum;
m['a'] = m['d'] = m['g'] = m['j'] = m['m'] = m['p'] = m['t'] = m['w'] = m[' '] = 1;
m['b'] = m['e'] = m['h'] = m['k'] = m['n'] = m['q'] = m['u'] = m['x'] = 2;
m['c'] = m['f'] = m['i'] = m['l'] = m['o'] = m['r'] = m['v'] = m['y'] = 3;
m['s'] = m['z'] = 4;
scanf("%d", &t);
getchar();
while(t--) {
gets(s);
sum = 0;
for(int i=0; s[i]; i++)
sum += m[s[i]];
printf("Case #%d: %d\n", ++caseno, sum);
}
return 0;
}