这是一道OpenJudge和洛谷里都有的题目,我的解题思路是先考虑 '-' 两端的字符是否同为小写字母或同为数字,并且右端的ASCII码大于左端。然后考虑p1是否为3,最后再考虑p1和p3的具体取值。我的代码如下:
def fillstr(string, p1, p2, p3):
s = [i for i in string]
for i in range(len(s)):
if s[i] == '-' and ord(s[i - 1]) < ord(s[i + 1]):
if (s[i - 1].isdigit() and s[i + 1].isdigit()) or (s[i - 1].isalpha() and s[i + 1].isalpha()):
if p1 != 3:
s[i] = [p2 * chr(x) for x in range(ord(s[i - 1]) + 1, ord(s[i + 1]))]
elif p1 == 3:
s[i] = [p2 * (ord(s[i + 1]) - ord(s[i - 1]) - 1) * '*']
finalstr = [] #在执行下面的函数之前,s里应该被替换的 ‘-’ 都以子列表的形式存储,所以下面的条件语句都只考虑子列表
for i in s:
if isinstance(i, list) and p1 == 2:
i = [j.upper() for j in i]
if isinstance(i, list) and p3 == 1:
finalstr.extend(i)
elif isinstance(i, list) and p3 == 2:
finalstr.extend(i[::-1])
else:
finalstr.append(i)
return ''.join(finalstr)
para_list = [int(i) for i in input().split()]
String = input()
print(fillstr(String, para_list[0], para_list[1], para_list[2]))
这串代码在洛谷的评分中是满分,但OpenJudge的评分结果是Runtime Error,请问是因为还应该再进一步简化吗?