def parse(inFile):
words = inFile.getWords()
C = int(words[0])
combinations = words[1:(C+1)]
D = int(words[C+1])
oppositions = words[C+2:(C+D+2)]
return (combinations, oppositions, words[C+D+3])
def solve((combinations, oppositions, word)):
cs = {}
for combo in combinations:
cs[combo[:2]] = combo[2]
cs[combo[1]+combo[0]] = combo[2]
os = set([])
for oppo in oppositions:
os.add(oppo)
os.add(oppo[::-1])
data = []
for letter in word:
if len(data) == 0:
data = [letter]
else:
if (data[-1] + letter) in cs:
data += [letter]
while (len(data) > 1 and data[-2]+data[-1] in cs):
data = data[:-2] + [cs[data[-2]+data[-1]]]
elif len([1 for datum in data if (datum + letter) in os]):
data = []
else:
data.append(letter)
return str(data).replace("'","")
if __name__ == "__main__":
from GCJ import GCJ
GCJ(parse, solve, "/Users/lpebody/gcj/2011_q/", "b").run()
ZGVmIHBhcnNlKGluRmlsZSk6CiAgICB3b3JkcyA9IGluRmlsZS5nZXRXb3JkcygpCiAgICBDID0gaW50KHdvcmRzWzBdKQogICAgY29tYmluYXRpb25zID0gd29yZHNbMTooQysxKV0KICAgIEQgPSBpbnQod29yZHNbQysxXSkKICAgIG9wcG9zaXRpb25zID0gd29yZHNbQysyOihDK0QrMildCiAgICByZXR1cm4gKGNvbWJpbmF0aW9ucywgb3Bwb3NpdGlvbnMsIHdvcmRzW0MrRCszXSkKCmRlZiBzb2x2ZSgoY29tYmluYXRpb25zLCBvcHBvc2l0aW9ucywgd29yZCkpOgogICAgY3MgPSB7fQogICAgZm9yIGNvbWJvIGluIGNvbWJpbmF0aW9uczoKICAgICAgICBjc1tjb21ib1s6Ml1dID0gY29tYm9bMl0KICAgICAgICBjc1tjb21ib1sxXStjb21ib1swXV0gPSBjb21ib1syXQogICAgb3MgPSBzZXQoW10pCiAgICBmb3Igb3BwbyBpbiBvcHBvc2l0aW9uczoKICAgICAgICBvcy5hZGQob3BwbykKICAgICAgICBvcy5hZGQob3Bwb1s6Oi0xXSkKICAgIGRhdGEgPSBbXQogICAgZm9yIGxldHRlciBpbiB3b3JkOgogICAgICAgIGlmIGxlbihkYXRhKSA9PSAwOgogICAgICAgICAgICBkYXRhID0gW2xldHRlcl0KICAgICAgICBlbHNlOgogICAgICAgICAgICBpZiAoZGF0YVstMV0gKyBsZXR0ZXIpIGluIGNzOgogICAgICAgICAgICAgICAgZGF0YSArPSBbbGV0dGVyXQogICAgICAgICAgICAgICAgd2hpbGUgKGxlbihkYXRhKSA+IDEgYW5kIGRhdGFbLTJdK2RhdGFbLTFdIGluIGNzKToKICAgICAgICAgICAgICAgICAgICBkYXRhID0gZGF0YVs6LTJdICsgW2NzW2RhdGFbLTJdK2RhdGFbLTFdXV0KICAgICAgICAgICAgZWxpZiBsZW4oWzEgZm9yIGRhdHVtIGluIGRhdGEgaWYgKGRhdHVtICsgbGV0dGVyKSBpbiBvc10pOgogICAgICAgICAgICAgICAgZGF0YSA9IFtdCiAgICAgICAgICAgIGVsc2U6CiAgICAgICAgICAgICAgICBkYXRhLmFwcGVuZChsZXR0ZXIpCiAgICByZXR1cm4gc3RyKGRhdGEpLnJlcGxhY2UoIiciLCIiKQoKaWYgX19uYW1lX18gPT0gIl9fbWFpbl9fIjoKICAgIGZyb20gR0NKIGltcG9ydCBHQ0oKICAgIEdDSihwYXJzZSwgc29sdmUsICIvVXNlcnMvbHBlYm9keS9nY2ovMjAxMV9xLyIsICJiIikucnVuKCkKCiAgICAgICAgICAgIAo=