Advertisement
Guest User

cc

a guest
Dec 26th, 2015
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.04 KB | None | 0 0
  1. # div2 easy
  2. class PalindromePrime():
  3.     def count(self,L,R):
  4.         return sum(str(x) == str(x)[::-1] and not any (x % i == 0 for i in range(2,x)) for x in range(L,R+1))
  5.  
  6. # div2 med
  7. from itertools import permutations
  8. class FourStrings():
  9.     def shortestLength(self,a,b,c,d):
  10.         def h(x,y):
  11.             if y in x: return x
  12.             for w in range(min(len(x),len(y)),0,-1):
  13.                 if x[-w:] == y[:w]:
  14.                     return x + y[w:]
  15.             return x+y
  16.         return min(len(reduce(h, p)) for p in permutations([a,b,c,d]))
  17.  
  18. # div2 hard
  19. from collections import deque
  20. class PalindromePath():
  21.     def shortestLength(self,n,a,b,c):
  22.         sa = a+b
  23.         sb = b+a
  24.         conn = {(x,y):z for x,y,z in zip(sa,sb,c+c)}
  25.  
  26.         q = deque()
  27.         dist = {}
  28.         def push(s,d):
  29.             q.append(s)
  30.             dist[s] = d
  31.  
  32.         for i in range(n): push((i,i),0)
  33.         for x,y in zip(sa,sb): push((x,y),1)
  34.  
  35.         while len(q) > 0:
  36.             s = q.popleft()
  37.             for i in range(n):
  38.                 for j in range(n):
  39.                     if (i,j) not in dist and conn.get((s[0],i),"abc") == conn.get((j,s[1]),"def"):
  40.                         push((i,j),dist[s]+2)
  41.  
  42.         return dist.get((0,1),-1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement