Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # div2 easy
- class PalindromePrime():
- def count(self,L,R):
- 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))
- # div2 med
- from itertools import permutations
- class FourStrings():
- def shortestLength(self,a,b,c,d):
- def h(x,y):
- if y in x: return x
- for w in range(min(len(x),len(y)),0,-1):
- if x[-w:] == y[:w]:
- return x + y[w:]
- return x+y
- return min(len(reduce(h, p)) for p in permutations([a,b,c,d]))
- # div2 hard
- from collections import deque
- class PalindromePath():
- def shortestLength(self,n,a,b,c):
- sa = a+b
- sb = b+a
- conn = {(x,y):z for x,y,z in zip(sa,sb,c+c)}
- q = deque()
- dist = {}
- def push(s,d):
- q.append(s)
- dist[s] = d
- for i in range(n): push((i,i),0)
- for x,y in zip(sa,sb): push((x,y),1)
- while len(q) > 0:
- s = q.popleft()
- for i in range(n):
- for j in range(n):
- if (i,j) not in dist and conn.get((s[0],i),"abc") == conn.get((j,s[1]),"def"):
- push((i,j),dist[s]+2)
- return dist.get((0,1),-1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement