题解 | #小红的数字删除#
小红的数字删除
https://www.nowcoder.com/practice/46a73f7cb2ab4a56bdb372b282b23c1e
import sys def solve(): s = sys.stdin.readline().strip() cnt = 0 ans = 0 for i in s: cnt += int(i) while True: vi, vx = -1, "99" # 初始化本次删的数位 if len(s) == 1: break while len(s) > 0 and s[0] == "0": s = s[1:] # 删除前导零 for i, v in enumerate(s): if (cnt - int(v)) % 3 == 0 and cnt != int(v): if v <= vx: # 考虑删除更靠后的数位 vx, vi = v, i if vx == "0": break if vi == -1: # 没有合适的数位可删 break else: ans += 1 cnt -= int(vx) # 更新数位和 s = s[:vi] + s[vi + 1 :] print(ans) T = int(input()) for _ in range(T): solve()
用python纯模拟,能过pypy3,python3过不去