Problem
Assume that an alphabet has a predetermined order; that is, we write the alphabet as a permutation , where . For instance, the English alphabet is organized as .
Given two strings and having the same length , we say that precedes in the lexicographic order (and write ) if the first symbol that doesn't match satisfies in .
Given: A collection of at most 10 symbols defining an ordered alphabet, and a positive integer ().
Return: All strings of length that can be formed from the alphabet, ordered lexicographically (use the standard order of symbols in the English alphabet).
假设字母 具有预定顺序;也就是说,我们将字母写成一个置换,其中。例如,英语字母组织为。
给定两个字符串,并具有相同的长度,我们说先于在字典顺序(和写),如果第一个符号不匹配满足的。
给定:最多包含10个符号的集合,定义一个有序字母和一个正整数()。
返回值:可以由字母组成的所有长度的字符串,按字典顺序排序(使用英语字母中符号的标准顺序)。
Sample Dataset
A C G T 2
Sample Output
AA AC AG AT CA CC CG CT GA GC GG GT TA TC TG TT
python解决方案
import itertools
# with open("../data/rosalind_lexf.txt", 'r') as f:
# string = f.readline().split()
# n = int(f.readline().strip())
string = 'ACGT'
n = 2
result = list(itertools.product(string, repeat = n))
print("\n".join(["".join(x) for x in result]))