If given a choice

If given a choice,I would go to the past;

If given a choice,I would live another life;

If given a choice,I would make less mistakens;

If given a choice,I would open the door of my heart

              to that girl;

And if given a choice,I must be happier!

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
This is an interesting problem! To solve it, you can use dynamic programming. Let dp[i][j] be the minimum cost to sort the first i characters of the pattern such that the i-th character is j (0 or 1). The base case is dp[0][0]=dp[0][1]=0, since an empty string is already sorted. Then, to compute dp[i][j], you can consider the last character of the pattern, which can be either 0, 1, or ?. If it's 0 or 1, you can simply match it with the i-th character of the binary string, and update dp[i][j] accordingly. If it's ?, you need to try both possibilities (0 or 1) and take the minimum cost. The final answer is the minimum of dp[n][0] and dp[n][1], where n is the length of the pattern. Here's the Python code: ``` s = input().strip() n = len(s) dp = [[float('inf')]*2 for _ in range(n+1)] dp[0][0] = dp[0][1] = 0 for i in range(1, n+1): if s[i-1] == '0': dp[i][0] = min(dp[i-1][0], dp[i-1][1]+1) elif s[i-1] == '1': dp[i][1] = min(dp[i-1][1], dp[i-1][0]+1) else: dp[i][0] = min(dp[i-1][0], dp[i-1][1]+1) dp[i][1] = min(dp[i-1][1], dp[i-1][0]+1) ans = [''] * n if dp[n][0] <= dp[n][1]: i, j = n, 0 while i > 0: if s[i-1] == '0': ans[i-1] = '0' i, j = i-1, 0 elif s[i-1] == '1': ans[i-1] = '1' i, j = i-1, 1 else: if dp[i][j] == dp[i-1][0]: ans[i-1] = '0' i, j = i-1, 0 else: ans[i-1] = '1' i, j = i-1, 1 else: i, j = n, 1 while i > 0: if s[i-1] == '0': ans[i-1] = '0' i, j = i-1, 0 elif s[i-1] == '1': ans[i-1] = '1' i, j = i-1, 1 else: if dp[i][j] == dp[i-1][1]: ans[i-1] = '1' i, j = i-1, 1 else: ans[i-1] = '0' i, j = i-1, 0 print(''.join(ans)) ``` Hope this helps!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值