#!/usr/bin/env python #Copyright Brian Brazil 2006 import sys import random firsts = {} links = {} lasts = {} def processLines(): line = sys.stdin.readline() while '' != line: if line[-1] == '\n': line = line[:-1] words = [ x.lower() for x in line.split()] if 0 == len(words): line = sys.stdin.readline() continue addFirst(words[0]) for i in range(len(words) -1): addLink(words[i], words[i+1]) addLast(words[-1]) line = sys.stdin.readline() def addFirst(word): global firsts if firsts.has_key(word): firsts[word] += 1 else: firsts[word] = 1 def addLink(first,second): global firsts if links.has_key(first): links[first].append(second) else: links[first] = [second] def addLast(word): global Lasts if lasts.has_key(word): lasts[word] += 1 else: lasts[word] = 1 def makeString(): string = [] if 0 == len(firsts): return '' current = random.sample(firsts.keys(),1)[0] while True: string.append(current) if lasts.has_key(current): if not links.has_key(current): return ' '.join(string) if 0 < random.randint(-lasts[current],len(links[current])): return ' '.join(string) current = random.sample(links[current],1)[0] if '__main__' == __name__: processLines() for i in range(1000): print makeString()