-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCloseMatch.py
132 lines (115 loc) · 3.84 KB
/
CloseMatch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
def finish(a, b):
larger = 0
a = a.copy()
b = b.copy()
for j in range(len(a)):
if larger == 0:
if a[j] == b[j]:
continue
else:
if a[j] == '?':
a[j] = b[j]
elif b[j] == '?':
b[j] = a[j]
if a[j] > b[j]:
larger = 1
else:
larger = -1
elif larger == 1:
if a[j] == b[j]:
if a[j] == '?':
a[j] = '0'
b[j] = '9'
else:
pass
else:
if a[j] == '?':
a[j] = '0'
elif b[j] == '?':
b[j] = '9'
else:
pass
else:
if a[j] == b[j]:
if a[j] == '?':
a[j] = '9'
b[j] = '0'
else:
pass
else:
if a[j] == '?':
a[j] = '9'
elif b[j] == '?':
b[j] = '0'
else:
pass
return a, b
def decide(tempC,tempJ, bestC, bestJ, bestDif):
tempDif = abs(int("".join(x for x in tempC)) - int("".join(x for x in tempJ)))
if tempDif < bestDif:
bestC = tempC
bestJ = tempJ
bestDif = tempDif
elif tempDif == bestDif:
if tempC < bestC:
bestC = tempC
bestJ = tempJ
bestDif = tempDif
elif tempC == bestC:
if tempJ < bestJ:
bestC = tempC
bestJ = tempJ
bestDif = tempDif
return bestC, bestJ, bestDif
T = int(input())
for i in range(1, T+1):
C, J = [list(s) for s in input().split()]
larger = 0
bestC = []
bestJ = []
bestDif = 2**80
for j in range(len(C)):
if C[j] == J[j] and C[j] != '?':
continue
if C[j] != J[j] and C[j] != '?' and J[j] != '?':
tempC, tempJ = finish(C, J)
bestC, bestJ, bestDif = decide(tempC, tempJ, bestC, bestJ, bestDif)
C = bestC
J = bestJ
break
if C[j] != J[j] and (C[j] == '?' or J[j] == '?'):
if C[j] == '?':
if int(J[j]) != 0:
C[j] = str(int(J[j]) - 1)
tempC, tempJ = finish(C, J)
bestC, bestJ, bestDif = decide(tempC, tempJ, bestC, bestJ, bestDif)
if int(J[j]) != 9:
C[j] = str(int(J[j]) + 1)
tempC, tempJ = finish(C, J)
bestC, bestJ, bestDif = decide(tempC, tempJ, bestC, bestJ, bestDif)
C[j] = J[j]
elif J[j] == '?':
if int(C[j]) != 0:
J[j] = str(int(C[j]) - 1)
tempC, tempJ = finish(C, J)
bestC, bestJ, bestDif = decide(tempC, tempJ, bestC, bestJ, bestDif)
if int(C[j]) != 9:
J[j] = str(int(C[j]) + 1)
tempC, tempJ = finish(C, J)
bestC, bestJ, bestDif = decide(tempC, tempJ, bestC, bestJ, bestDif)
J[j] = C[j]
continue
if C[j] == '?' and J[j] == '?':
C[j] = '0'
J[j] = '1'
tempC, tempJ = finish(C, J)
bestC, bestJ, bestDif = decide(tempC, tempJ, bestC, bestJ, bestDif)
C[j] = '1'
J[j] = '0'
tempC, tempJ = finish(C, J)
bestC, bestJ, bestDif = decide(tempC, tempJ, bestC, bestJ, bestDif)
C[j] = '0'
J[j] = '0'
continue
bestC, bestJ, bestDif = decide(C, J, bestC, bestJ, bestDif)
print("Case #{}: {} {}".format(i, "".join(str(x) for x in bestC), "".join(str(x) for x in bestJ)))