AcWing 3381. 手机键盘
原题链接
简单
#include <bits/stdc++.h>
using namespace std;
int t, n, m, k, l, r, op, x, y;
string str;
int ha[200], pa[10];
void solve() {
for (int i = 'a', t = 0; i <= 'z'; i++) {
if (i == 's' || i == 'z')t++;
ha[i] = 1 + (i - 'a' - t) / 3;
if (!pa[ha[i]])pa[ha[i]] = i;
}
while (cin >> str) {
int siz = str.size();
int ls = str[0];
int cnt = ls-pa[ha[ls]]+1;
for (int i = 1; i < siz; i++) {
int ch = str[i];
if (ha[ls] == ha[ch]) {
cnt += 2;
}
cnt += ch-pa[ha[ch]]+1;
ls = ch;
}
cout << cnt << "\n";
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
solve();
return 0;
}