checkio (Determine the order)

来源:互联网 发布:视频去广告软件 编辑:程序博客网 时间:2024/06/15 09:50

The Robots have found an encrypted message. We cannot decrypt it at the moment, but we can take the first steps toward doing so. You have a set of "words" all in lower case, each word contains symbols in "alphabetical order" (it's not your typical alphabetical order, but a new and different order). We need to determine the order of the symbols from each "word" and create a single "word" with all of these symbols, placing them in the new alphabetial order. In some cases, if we cannot determine the order for several symbols, you should use the traditional latin alphabetical order. For example: Given words "acb", "bd", "zwa". As we can see "z" and "w" must be before "a" and "d" after "b". So the result is "zwacbd".

Precondition: In each test, there can be the only one solution.

Input: A list of strings.

Output: A string.

Example:

?
1
2
3
4
5
checkio(["acb","bd","zwa"])=="zwacbd"
checkio(["klm","kadl","lsm"])=="kadlsm"
checkio(["a","b","c"])=="abc"
checkio(["aazzss"])=="azs"
checkio(["dfg","frt","tyg"])=="dfrtyg"

把限制条件转换成树的结构,重新定义比较函数,排序得到答案。

def unique(data):    ret = ''    for c in data:        if c not in ret:            ret += c    return retfather = {}def mycmp(self, other):    #print self,other    if not find(self, other):        if not find(other, self):            #print '='            return cmp(self, other)        else:            #print '>'            return -1    #print '<'    return 1def find(x, target):    while x in father:        if father[x]==target:            return True        x = father[x]    return False    def checkio(data):    father.clear()    word = []    for condition in data:        condition = unique(condition)        for i in range(1,len(condition)):            father[condition[i]] = condition[i-1]    for condition in data:        for c in condition:            word.append(c)    word = list(unique(word))    word.sort(cmp=mycmp)    #print word    return ''.join(word)  #These "asserts" using only for self-checking and not necessary for auto-testingif __name__ == '__main__':        assert checkio(["acb", "bd", "zwa"]) == "zwacbd", \        "Just concatenate it"    assert checkio(["klm", "kadl", "lsm"]) == "kadlsm", \        "Paste in"    assert checkio(["a", "b", "c"]) == "abc", \        "Cant determine the order - use english alphabet"        assert checkio(["aazzss"]) == "azs", \        "Each symbol only once"    assert checkio(["dfg", "frt", "tyg"]) == "dfrtyg", \        "Concatenate and paste in"

A了后看了下高手的代码:

def checkio(data):    distinct_letters = sorted(set("".join(data)))    print distinct_letters    #We just get couples of letters (a, b) such that a must be before b    constraints = [(word[i], word[i + 1]) for word in data for i in range(len(word) - 1)]        solution_found = False    while not solution_found:        solution_found = True        for (a, b) in constraints:            ia = distinct_letters.index(a)            ib = distinct_letters.index(b)            if ia > ib:                #We swap the letters                distinct_letters[ia] = b                distinct_letters[ib] = a                solution_found = False    return "".join(distinct_letters)



0 0
原创粉丝点击