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:
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)
- checkio (Determine the order)
- 【checkio】the Moore neighbourhood
- checkio-the flat dictionary
- CheckIO The Longest Palindromic
- checkio-the most wanted letter
- checkio the most frequent weekdays
- CheckIO The Most Wanted Letter
- checkio--
- UVA11800 Determine the Shape
- UVA11800 - Determine the Shape
- the Determine in June
- Python checkio "The Most Frequent Weekdays"解决方案
- DBPITR:Determine the target time
- UVa 11800 - Determine the Shape
- uva 11800 - Determine the Shape
- UVA 11800 Determine the Shape
- UVa 11800 Determine the Shape
- UVA 11800 - Determine the Shape
- android fragment getActivity() null
- git常用命令(一)
- [delphi]小程序一例,自动生成随机数。
- 有序的结构体数组
- easyui panel 使用汇总
- checkio (Determine the order)
- 智能手机 华为 索尼
- (CF)A. Inna and Choose Options
- Ubuntu中查看各种设备和资源的命名汇总
- 完成端口(CompletionPort)详解 - 手把手教你玩转网络编程系列之三
- 第六周:图书馆管理系统
- SDUTOJ 2119 数据结构实验之链表四:有序链表的归并
- POST方式跨域上传文件
- String to Integer (atoi)