题解 | #手机键盘#
手机键盘
https://www.nowcoder.com/practice/20082c12f1ec43b29cd27c805cd476cd
#include <iostream>
using namespace std;
int main() {
int keyTab[26] = {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};
string str;
while (cin >> str) {
int time = 0;
for (int i = 0; i < str.size(); i++) { //此处为str.size()
time += keyTab[str[i] - 'a'];
if (i != 0 &&
str[i] - str[i - 1] == keyTab[str[i] - 'a'] - keyTab[str[i - 1] - 'a']) {
time += 2;
}
}
cout << time << endl;
}
}

