网站 开发流程,怎么创建微信小程序店铺,网站建设管理人员推荐表,自己电脑可以做网站服务器吗【题目来源】https://www.acwing.com/problem/content/3384/【题目描述】 请你计算按照手机键盘#xff08;9键输入法#xff09;输入字母的方式#xff0c;键入给定字符串#xff08;由小写字母构成#xff09;所花费的时间。 具体键入规则和花费时间如下描述#xff1a…【题目来源】https://www.acwing.com/problem/content/3384/【题目描述】 请你计算按照手机键盘9键输入法输入字母的方式键入给定字符串由小写字母构成所花费的时间。 具体键入规则和花费时间如下描述 对于同一键上的字符例如 a,b,c 都在 “1” 键上输入 a 只需要按一次输入 c 需要连续按三次。 如果连续两个字符不在同一个按键上则可直接按如ad 需要按两下kz 需要按 6 下。 如果连续两字符在同一个按键上则两个按键之间需要等一段时间如 ac在按了 a 之后需要等一会儿才能按 c。 现在假设每按一次需要花费一个时间段等待时间需要花费两个时间段。【输入格式】 输入包含多组测试数据。 每组数据占一行包含一个由小写字母构成的字符串。【输出格式】 对于每组输入输出一行结果表示键入给定字符串所需要花费的时间。【数据范围】 每个输入最多包含 100 组测试数据。 所有字符串的长度都不超过 100。【输入样例】 bob www【输出样例】 7 7【算法分析】 ● 注意循环条件写为 while(cinstr)不要写为 while(1) 之后且在其循环体中写 cinstr因为实践证明会 TLE。【算法代码一】
#include bits/stdc.h
using namespace std;//The number of times to press the letter a~z
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};
//The key where the letter a~z is located
int at[] {1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,6,7,7,7,8,8,8,8};string str;
int main() {while(cinstr) {int ans0;for(int i0; istr.size(); i) {if(i0 at[str[i]-a]at[str[i-1]-a]) ans2;anscnt[str[i]-a];}coutansendl;}return 0;
}/*
in:
bob
wwwout:
7
7
*/【算法代码二TLE】 下面的写法超时了TLE 。和上一个算法代码的差别在于将 cinstr 写到 while 循环里了同时循环条件写为 while(1)。
#include bits/stdc.h
using namespace std;//The number of times to press the letter a~z
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};
//The key where the letter a~z is located
int at[] {1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,6,7,7,7,8,8,8,8};string str;
int main() {while(1) {int ans0;cinstr;for(int i0; istr.size(); i) {if(i0 at[str[i]-a]at[str[i-1]-a]) ans2;anscnt[str[i]-a];}coutansendl;}return 0;
}/*
in:
bob
wwwout:
7
7
*/
【参考文献】https://www.acwing.com/solution/content/102126/https://www.acwing.com/solution/content/124363/