fork download
  1. Trie={}
  2. def insert(Trie,s,n):
  3. my_try=Trie
  4.  
  5. for i in range(n):
  6. if(s[i] in my_try):
  7. my_try=my_try[s[i]][0]
  8. else:
  9. my_try[s[i]]=[{},False]
  10. if(i==n-1):
  11. my_try[s[n-1]][1]=True
  12. my_try=my_try[s[i]][0]
  13. return(Trie)
  14.  
  15. def search(Trie,s,n):
  16. my_try=Trie
  17. for i in range(n):
  18. if(s[i] not in my_try):
  19. return(i)
  20. else:
  21. if(i==n-1):
  22. if(my_try[s[n-1]][1]==True or my_try[s[n-1]][1]==False ):
  23. return(-1)
  24. my_try=my_try[s[i]][0]
  25.  
  26. t=int(input())
  27. pos=[]
  28. neg=[]
  29. count=0
  30. for i in range(t):
  31. k=(input())
  32. if(k[0]=="-"):
  33. count=count+1
  34. neg.append(k[1:])
  35. else:
  36. pos.append(k[1:])
  37. if(count==0):
  38. print(0)
  39. elif(count==t):
  40. print(1)
  41. print('')
  42. else:
  43. output=[]
  44. ans=0
  45. for i in pos:
  46. Trie=insert(Trie,i,len(i))
  47. for i in neg:
  48. val=search(Trie,i,len(i))
  49. if(val==-1):
  50. print(-1)
  51. ans=-1
  52. break
  53. else:
  54. output.append(i[:val+1])
  55. if(ans>=0):
  56. we=list(set(output))
  57. we.sort()
  58. print(len(we))
  59. for q in we:
  60. print(q)
  61.  
  62.  
Success #stdin #stdout 0.02s 9176KB
stdin
4
-code
+cc
-cccc
-ccctt
stdout
2
ccc
co