Dangling work on O and R solution

This commit is contained in:
Claudio Maggioni 2019-05-16 16:18:43 +02:00
parent 578f914819
commit dd8f44e727
5 changed files with 122 additions and 0 deletions

38
ayb.py Executable file
View file

@ -0,0 +1,38 @@
#!/usr/bin/env python3
def ayb(s):
A = [None] * 128
Q = []
c = 1
for i in range(len(s)):
code = ord(s[i])
if A[code] is None:
A[code] = c
if c == 1:
c = 0
elif c == 0:
c = 2
else:
c += 1
Q.append((A[code],i))
if c == 1:
c = 0
if c == 0:
c = 2
num = 0
for tup in Q:
value, position = tup
num += value * (c ** (len(s) - position - 1))
return str(num)
def main():
t = int(input())
for i in range(1, t + 1):
print("Case #" + str(i) + ": " + ayb(input()))
if __name__ == "__main__":
main()

4
ayb.txt Normal file
View file

@ -0,0 +1,4 @@
3
11001001
cats
zig

5
tree.c Normal file
View file

@ -0,0 +1,5 @@
// vim: set ts=4 sw=4 et tw=80 :
#include <stdio.h>
int main() {

60
tree.py Executable file
View file

@ -0,0 +1,60 @@
#!/usr/bin/env python3
def min_remove(D):
m = len(D)
for k in range(1, len(D)):
BT = []
V = [None] * len(D)
r = len(D) - 1 - max_edges(D, BT, V, k)
if m is None or r < m:
m = r
return m
def max_edges(D, BT, V, k):
if V[k] is None:
BT.append(k)
if len(D[k]) <= 1: # Either one edge (parent) or two (parent and one child)
BT.remove(k)
V[k] = 1
else:
m1 = None
m2 = None
for e in D[k]:
if e in BT:
continue
d = max_edges(D, BT, V, e)
if m1 is None or d > m1:
m2 = m1
m1 = d
elif m2 is None or d > m2:
m2 = d
BT.remove(k)
if m2 is None:
V[k] = 1
else:
V[k] = 1 + m1 + m2
return V[k]
if __name__ == "__main__":
t = int(input())
for i in range(1, t+1):
n = int(input())
D = [None] * (n+1)
for j in range(n-1):
string = input().split(' ')
a, b = int(string[0]), int(string[1])
if D[a] is None:
D[a] = [b]
else:
D[a].append(b)
if D[b] is None:
D[b] = [a]
else:
D[b].append(a)
print("Case #" + str(i) + ": " + str(min_remove(D)))

15
tree.txt Normal file
View file

@ -0,0 +1,15 @@
3
3
2 1
1 3
7
4 5
4 2
1 2
3 1
6 4
3 7
4
1 2
2 3
3 4