赛码网 打字/最优打字策略 Python3
题目:
时间限制: 3000MS
内存限制: 589824KB题目描述:小明很喜欢打字,今天小红给了小明一个字符串。
这个字符串只包含大写和小写字母。
我们知道,按下CapsLock键,可以切换大小写模式。
我们在小写模式时候,同时按下shift+字母键,就能写出大写字母。
在大写模式的时候,按下shift+字母键,就能写出小写字母。
现在问题来了,给你一个字符串,问你最少使用多少个按键,就可以写出这个字符串呢?
注意,按shift和字母键,算两次按键。开始时均为小写状态。
输入描述:
第一行一个T,表示有T组输入。
接下来T组数据:
每组数据一个字符串s,s的长度小于等于100。仅包含大小写字母。
输出描述:
对于每组数据,输出最少按键次数。
示例:
输入:
3 A AA AAAAAA输出
2 3 7跟京东2020年春招测开笔试题基本一致,只是输入输出的问题,但是不知道为什么就是只能AC 9%。同样的函数在赛码的京东笔试题上就可以AC 100%。烦躁。代码贴在这里了,有时间再来看看怎么回事吧。
def pressnum(l): flag, res, n = 0, 0, len(l) for i in range(n): if (l[i].islower() and flag == 0) or (l[i].isupper() and flag == 1): res += 1 elif (l[i].islower() and flag == 1) or (l[i].isupper() and flag == 0): if i + 1 <= n - 1: if (l[i].islower() and l[i+1].islower()) or (l[i].isupper() and l[i+1].isupper()): res += 2 flag = 1 - flag else: res += 2 else: res += 2 return res T = int(input()) l = [] for i in range(T): l.append(input()) for i in range(T): cur = pressnum(l[i]) print(cur)另外附上AC 100%的京东测开题目原题和源代码:
时间限制: 3000MS
内存限制: 589824KB题目描述:在英文的输入中,我们经常会遇到大小写切换的问题,频繁切换大小写会增加我们的按键次数,也会降低我们的打字效率。
众所周知,切换大小写有两种方式,一种是按下“caps locks”,也就是大写锁定键,这样一来,之后的输入模式都会被切换。另一种是同时按下shift和需要打印的字母,可以临时切换大小写(算作按下两个键)。
已知初始状态下,打字模式是小写,现在给出需要打印的字符串(区分大小写),请你计算出最少需按键多少次才能打印出来。
输入描述输入第一行仅包含一个正整数n,表示字符串的长度(1<=n<=1000000)。
输入第二行包含一个长度为n的字符串,仅包含大小写字母。
输出描述输出仅包含一个正整数,即最少的按键次数。
def pressnum(l): flag, res, n = 0, 0, len(l) for i in range(n): if (l[i].islower() and flag == 0) or (l[i].isupper() and flag == 1): res += 1 elif l[i].islower() and flag == 1: if i + 1 <= n - 1: if l[i+1].islower(): res += 2 flag = 0 else: res += 2 else: res += 2 elif l[i].isupper() and flag == 0: if i + 1 <= n - 1: if l[i+1].isupper(): res += 2 flag = 1 else: res += 2 else: res += 2 return res n = int(input()) l = input() cur = pressnum(l) print(cur)啊,到底是为什么啊~