判断一个给定字符序列在可以由该序列字符组成的所有序列里按Lexical

题目:

Given a permutation which may contain repeated numbers, find its index in all the permutations of these numbers, which are ordered in lexicographical order. The index begins at 1.

Example

Given the permutation [1, 4, 2, 2], return 3.

注意index可能会超过int的表示范围。

def get_ranks(words):
    cc = {}
    def inv(a, n):
        t, nt = 0, 1
        r, nr = n, a
        while nr != 0:
            q = r / nr
            t, nt = nt, t - q * nt
            r, nr = nr, r - q * nr
        if t < 0: t += n
        return t

    def fact(n, M):
        if n == 1: return 1
        if n not in cc:
            cc[n] = (fact(n - 1, M) * n) % M
        return cc[n]

    def perm(w):
        M = int(1e9+7)
        n = len(w)
        w = sorted(w)
        x = fact(n, M)
        y = 1
        xs = [0] + filter(lambda i:w[i] != w[i - 1], range(1, n)) + [n]
        for i in xrange(1, len(xs)):
            y = (y * fact(xs[i] - xs[i - 1], M)) % M
        return (x * inv(y, M)) % M

    def cnt(w):
        t = 0
        x, rest = w[0], w[1:]
        v = set()
        for i in xrange(len(rest)):
            c = rest[i]
            if c < x and not c in v:
                v.add(c)
                t = (t + perm(rest[:i] + x + rest[i+1:])) % int(1e9+7)
        return t

    ret = []
    for word in words:
        c = 0
        n = len(word)
        for i in xrange(n - 1):
            c = (c + cnt(word[i:])) % int(1e9+7)
        ret.append(c)

    return ret

def get_rank(words):
    ret = []
    for i in range(len(words)):
        cur = words[i]
        hashmap = {}
        index, fact, divident = 0, 1, 1
        for j in range(len(cur) - 1, -1, -1):
            if cur[j] in hashmap:
                hashmap[cur[j]] += 1
                divident = (divident * hashmap[cur[j]])
            else:
                hashmap[cur[j]] = 1
            rank = 0
            for k in range(j + 1, len(cur)):
                if ord(cur[j]) > ord(cur[k]):
                    rank += 1
            #index += (rank * fact % int(1e9+7) / divident)
            index += (rank * fact / divident)
            fact *= len(cur) - j
        ret.append(index % int(1e9+7))
    return ret

def main():
    #f = open(os.environ['OUTPUT_PATH'], 'w')

    _words_cnt = int(raw_input())
    _words_i = 0
    _words = []

    while _words_i < _words_cnt:
        _words_item = raw_input()
        _words.append(_words_item)
        _words_i += 1

    res = get_ranks(_words)
    print res

    res2 = get_rank(_words)
    print res2


你可能感兴趣的:(Algorithm,面试题)