我刚刚发现了 SPOJ 网站并提交了我的第一个问题解决方案:Alphacode http://www.spoj.com/problems/ACODE/
在线法官用下面的代码回应了一个NZEC错误,我不明白为什么。我选择了 Python 3.2.3(functools.lru_cache 出现在 Python3.2 中),我也尝试删除 lru_cache 并用 memoize 装饰器替换它,但同样的问题。
from functools import lru_cache
@lru_cache(maxsize=10000)
def acode(s, i=0):
if i == len(s):
return 1
if s[i] == "0":
return 0
res = acode(s, i+1)
if i + 1 < len(s) and (10 * int(s[i]) + int(s[i+1]) <= 26):
res += acode(s, i+2)
return res
def main():
i = input().strip()
while i != "0":
print(acode(i))
i = input().strip()
if __name__ == "__main__":
import sys
try:
main()
except:
sys.exit(0)
您可以使用以下命令对其进行测试:
$ echo "123\n1111\n21\n0" | python3 acode.py
注意:我也提交了没有“try:except:”但我在 SPOJ 网站上找不到输出日志。