数学渣渣,数学解法不会,语文解法来一波
- Sub test()
- Dim N&, I&, K&, J&, P&, Arr, Arrt$(1), S$, T$, Y$, A&, B&, C&, D&, E&, mTimer#
- mTimer = Timer
- With ThisWorkbook.ActiveSheet
- Arr = .Range("a1:c" & .Cells(.Rows.Count, 1).End(3).Row).Value
- For N = LBound(Arr) To UBound(Arr)
- S = Arr(N, 1): A = Arr(N, 2): B = Arr(N, 3): P = 0
- If A > B Then Arrt(0) = "1": Arrt(1) = "0" Else Arrt(0) = "0": Arrt(1) = "1"
- J = countSS(Replace(S, "!", Arrt(0)), A, B)
- K = countSS(Replace(S, "!", Arrt(1)), A, B)
- If J > K Then
- T = Replace(S, "!", Arrt(1)): Y = Arrt(0): C = 1: D = Len(S): E = 1
- Else
- T = Replace(S, "!", Arrt(0)): K = J: Y = Arrt(1): C = Len(S): D = 1: E = -1
- End If
- For I = C To D Step E
- If Mid(S, I, 1) = "!" Then
- Mid(T, I, 1) = Y: J = countSS(T, A, B)
- If J <= K Then
- K = J
- 'ElseIf J > K Then '这个跳出条件有些情况下不对,如果能实现跳出会大幅提速
- ' Exit For
- End If
- End If
- Next I
- Arr(N, 1) = K
- Next N
- .[d1].Resize(UBound(Arr)).Value = Arr
- End With
- Debug.Print Timer - mTimer
- End Sub
- Function countSS(T$, A&, B&)
- Dim C&, I&, D&, E&, F&, G&
- For I = 1 To Len(T)
- If Mid(T, I, 1) = "1" Then
- E = E + C: G = G + 1
- Else
- C = C + 1: F = F + G
- End If
- Next I
- countSS = A * E + B * F
- End Function
复制代码 |