From dd8f44e727909c160f5bd02a703b69ea8deddffe Mon Sep 17 00:00:00 2001 From: Claudio Maggioni Date: Thu, 16 May 2019 16:18:43 +0200 Subject: [PATCH] Dangling work on O and R solution --- ayb.py | 38 +++++++++++++++++++++++++++++++++++ ayb.txt | 4 ++++ tree.c | 5 +++++ tree.py | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ tree.txt | 15 ++++++++++++++ 5 files changed, 122 insertions(+) create mode 100755 ayb.py create mode 100644 ayb.txt create mode 100644 tree.c create mode 100755 tree.py create mode 100644 tree.txt diff --git a/ayb.py b/ayb.py new file mode 100755 index 0000000..abdbb94 --- /dev/null +++ b/ayb.py @@ -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() diff --git a/ayb.txt b/ayb.txt new file mode 100644 index 0000000..ea49633 --- /dev/null +++ b/ayb.txt @@ -0,0 +1,4 @@ +3 +11001001 +cats +zig diff --git a/tree.c b/tree.c new file mode 100644 index 0000000..2ed96c5 --- /dev/null +++ b/tree.c @@ -0,0 +1,5 @@ +// vim: set ts=4 sw=4 et tw=80 : +#include + +int main() { + diff --git a/tree.py b/tree.py new file mode 100755 index 0000000..24e8259 --- /dev/null +++ b/tree.py @@ -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))) + diff --git a/tree.txt b/tree.txt new file mode 100644 index 0000000..990e806 --- /dev/null +++ b/tree.txt @@ -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