Virtual_Universe

chimestry

May 22nd, 2016
65
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. inf = open("chemistry.in","r")
  2. ouf = open("chemistry.out","w")
  3.  
  4. def p1(v,q): #функция возвращает o1 и b для строки[:k]
  5.     for i in range(len(q)):
  6.         if q[i] == v[0] and q[i:]+q[:i] == v:return [len(v),int(len(v)-i)]
  7.     return [len(v),None]
  8. def p2(v,q):#функция возвращает o2 и c для строки[k:]
  9.     for i in range(len(q)):
  10.         if q[i] == v[0] and q[i:]+q[:i] == v:return [len(v),int(i)]
  11.     return [len(v),None]
  12.  
  13. #######################################
  14. def main():
  15.     global inf,ouf
  16.     (n,k) = map(int,inf.readline().split())
  17.     word1 = inf.readline().strip()
  18.     word2 = inf.readline().strip()
  19.     if word1 == word2: #изначально исключаем ситуацию равных слов. Чтобы не тратить ресурсы.
  20.         print(0,file = ouf)
  21.         return 0
  22.    
  23.     (o1,b) = p1(word1[:k],word2[:k])
  24.     (o2,c) = p2(word1[k:],word2[k:])
  25.     if b == None or c == None:
  26.         print(-1,file = ouf)
  27.         return 0
  28.     o = o1/o2
  29.     u1 = (b-c)/o2
  30.     u2 = (b-c-1)/o2
  31.     i = 0
  32.     while (o*i+u1)%1 != 0 and (o*i+u2)%1 != 0: i+=1
  33.     if (o*i+u1)%1 == 0 and (o*i+u2)%1 == 0:
  34.         print(int(min(o1*i+b+o2*(o*i+u1)+c , o1*i+b+o2*(o*i+u2)+c)),file = ouf)
  35.     elif (o*i+u1)%1 == 0: print(int(o1*i+b+o2*(o*i+u1)+c),file = ouf)
  36.     else: print(int(o1*i+b+o2*(o*i+u2)+c),file = ouf)    
  37. main()
  38. inf.close()
  39. ouf.close()
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×