时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:2543

解决:1401

<dl> <dt> 题目描述: </dt> <dd>
按照手机键盘输入字母的方式,计算所花费的时间
如:a,b,c都在“1”键上,输入a只需要按一次,输入c需要连续按三次。
如果连续两个字符不在同一个按键上,则可直接按,如:ad需要按两下,kz需要按6下
如果连续两字符在同一个按键上,则两个按键之间需要等一段时间,如ac,在按了a之后,需要等一会儿才能按c。
现在假设每按一次需要花费一个时间段,等待时间需要花费两个时间段。
现在给出一串字符,需要计算出它所需要花费的时间。
</dd> </dl> <dl> <dt> 输入: </dt> <dd>

一个长度不大于100的字符串,其中只有手机按键上有的小写字母

</dd> </dl> <dl> <dt> 输出: </dt> <dd>
输入可能包括多组数据,对于每组数据,输出按出Input所给字符串所需要的时间
</dd> </dl> <dl> <dt> 样例输入: </dt> <dd>
bob
www
</dd> </dl> <dl> <dt> 样例输出: </dt> <dd>
7
7
</dd> </dl>

模拟题,把手机键盘一打开,然后按着题目意思直接模拟就好。

//Asimple
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cctype>
#include <cstdlib>
#include <stack>
#include <cmath>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <limits.h>
#define INF 0x7fffffff
using namespace std;
const int maxn = 105;
typedef long long ll;
char str[maxn];
int num[] = {2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,7,8,8,8,9,9,9,9};
int cnt[] = {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};

bool isOne(char a, char b) {
    int t1 = num[a-'a'];
    int t2 = num[b-'a'];
    if(t1 == t2) {
        return true;
    }
    return false;
}

int main(){
    while(scanf("%s",str) != EOF) {
        int len = strlen(str);
        int wait = cnt[(str[0] - 'a')];

        for(int i = 1; i < len; i++) {
            if(isOne(str[i-1], str[i])) {
                wait = wait + 2;
            }
            wait = wait + cnt[(str[i] - 'a')];
        }
        printf("%d\n",wait);
    }    
    return 0;
}