AcWing 7-3. T9键盘
原题链接
简单
作者:
rookie.
,
2024-04-04 20:21:37
,
所有人可见
,
阅读 9
算法1
时间复杂度O(n^2)
C++ 代码
#include <bits/stdc++.h>
using namespace std;
string s;
int cnt;
int n;
int find(char c)
{
if(c=='A'||c=='B'||c=='C') return 2;
if(c=='D'||c=='E'||c=='F') return 3;
if(c=='G'||c=='H'||c=='I') return 4;
if(c=='J'||c=='K'||c=='L') return 5;
if(c=='M'||c=='N'||c=='O') return 6;
if(c=='P'||c=='Q'||c=='R'||c=='S') return 7;
if(c=='T'||c=='U'||c=='V') return 8;
if(c=='W'||c=='X'||c=='Y'||c=='Z') return 9;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin>>n;
for(int i=1;i<=n;i++)
{
cnt=0;
cin>>s;
for(int j=1;j<s.size();j++)
{
if(find(s[j])!=find(s[j-1])) cnt++;
}
cout<<"Case #"<<i<<": "<<cnt<<endl;
}
return 0;
}