QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#794986 | #7963. 多折较差验证 | thelongXiao# | TL | 0ms | 0kb | Python3 | 1.6kb | 2024-11-30 17:19:29 | 2024-11-30 17:19:31 |
answer
def min_folds_and_asymmetry(s):
n = len(s)
# 初始化dp数组,dp[i][j]表示前i个折痕折叠成j条纸带的最小折叠次数和最小不对称程度
dp = [[(float('inf'), float('inf')) for _ in range(n + 1)] for _ in range(n + 1)]
dp[0][0] = (0, 0) # 没有折痕时不需要折叠,且不对称程度为0
for i in range(1, n + 1):
for j in range(1, i + 1):
for k in range(j, i + 1):
# 检查是否可以折叠
if is_foldable(s, j - 1, k - 1):
# 更新dp值
folds, asymmetry = dp[i - k][j - 1]
new_folds = folds + 1
new_asymmetry = asymmetry + abs(i - 2 * (k - j + 1))
if new_folds < dp[i][j][0] or (new_folds == dp[i][j][0] and new_asymmetry < dp[i][j][1]):
dp[i][j] = (new_folds, new_asymmetry)
# 找到最小折叠次数和对应的最小不对称程度
min_folds = float('inf')
min_asymmetry = float('inf')
for j in range(n + 1):
folds, asymmetry = dp[n][j]
if folds < min_folds or (folds == min_folds and asymmetry < min_asymmetry):
min_folds = folds
min_asymmetry = asymmetry
return min_folds, min_asymmetry
def is_foldable(s, start, end):
# 检查从start到end的折痕是否可以折叠
for i in range((end - start + 1) // 2):
if s[start + i] != s[end - i]:
return False
return True
n = int(input())
s = input()
result = min_folds_and_asymmetry(s)
print(result[0], result[1])
詳細信息
Test #1:
score: 0
Time Limit Exceeded
input:
5000 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^...