建表去加上字母的對應次數即可得解。
C++(0.016)
/*******************************************************/
/* UVa 11530 SMS Typing */
/* Author: Maplewing [at] knightzone.studio */
/* Version: 2014/12/26 */
/*******************************************************/
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
int main(){
const int press[] = { 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 T;
while( scanf("%d", &T) != EOF ){
string text;
getline(cin, text); // get \n
for( int i = 1 ; i <= T; ++i ){
getline(cin, text);
int pressCount = 0;
for( int j = 0 ; j < text.length() ; ++j ){
if( text[j] == ' ' ){
++pressCount;
continue;
}
pressCount += press[(int)(text[j]-'a')];
}
printf("Case #%d: %d\n", i, pressCount);
}
}
return 0;
}