Skip to content

Instantly share code, notes, and snippets.

@gsathya
Last active August 29, 2015 14:10
Show Gist options
  • Save gsathya/a45b84211d22de5dad9d to your computer and use it in GitHub Desktop.
Save gsathya/a45b84211d22de5dad9d to your computer and use it in GitHub Desktop.
from collections import defaultdict
n_test_words = int(raw_input())
string_set = defaultdict(set)
substring_set = defaultdict(set)
for x in range(n_test_words):
line = raw_input()
for start in range(len(line)):
for end in range(start+1, len(line)+1):
substr = line[start:end]
# we've already seen this substring, it's not unique
if substr in substring_set:
for string in substring_set[substr]:
# remove all string -> substr mappings
string_set[string].remove(substr)
# remove all mappings of substr -> string, but leave
# the key to check future duplicates
substring_set[substr] = set()
# unique substr found
else:
string_set[line].add(substr)
substring_set[substr].add(line)
n_lookup_words = int(raw_input())
def min(values):
min_val = None
for val in values:
if min_val == None:
min_val = val
if len(val) < len(min_val):
min_val = val
elif len(val) == len(min_val):
for i in range(len(val)):
if ord(val[i]) < ord(min_val[i]):
min_val = val
break
return min_val
for x in range(n_lookup_words):
key = raw_input().strip()
if key not in string_set:
print key, "ERROR"
else:
val = min(string_set[key])
print key, val
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment