1、问题描述:
思路:
代码:自己的代码(运行了80%后,时间超限 ( Time Limit Exceeded (TLE) ))
import itertools
if __name__=='__main__':
n=int(raw_input())
s="abcdefghijkl"
ret =itertools.permutations(s)
count=0
res=[]
j=0
for i in range(n):
a=raw_input()
res.append(a)
for i in ret:
count+=1
if i in res and j<n:
res[res.index(i)]=count
j=j-1
count=0
if j==n:
break
for i in res:
print i
2、知识点补充:
库链接:https://docs.python.org/2.7/library/itertools.html
itertools模块:itertools — Functions creating iterators for efficient looping
The following module functions all construct and return iterators. Some provide streams of infinite length, so they should only be accessed by functions or loops that truncate the stream.
itertools.
permutations
(
iterable
[,
r
]
)
Return successive r length permutations of elements in the iterable.
If r is not specified or is None
, then r defaults to the length of the iterable and all possible full-length permutations are generated.
Permutations are emitted in lexicographic sort order. So, if the input iterable is sorted, the permutation tuples will be produced in sorted order.
Elements are treated as unique based on their position, not on their value. So if the input elements are unique, there will be no repeat values in each permutation.
Roughly equivalent to:
def permutations(iterable, r=None):
# permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC
# permutations(range(3)) --> 012 021 102 120 201 210
pool = tuple(iterable)
n = len(pool)
r = n if r is None else r
if r > n:
return
indices = range(n)
cycles = range(n, n-r, -1)
yield tuple(pool[i] for i in indices[:r])
while n:
for i in reversed(range(r)):
cycles[i] -= 1
if cycles[i] == 0:
indices[i:] = indices[i+1:] + indices[i:i+1]
cycles[i] = n - i
else:
j = cycles[i]
indices[i], indices[-j] = indices[-j], indices[i]
yield tuple(pool[i] for i in indices[:r])
break
else:
return
The code for permutations()
can be also expressed as a subsequence of product()
, filtered to exclude entries with repeated elements (those from the same position in the input pool):
def permutations(iterable, r=None):
pool = tuple(iterable)
n = len(pool)
r = n if r is None else r
for indices in product(range(n), repeat=r):
if len(set(indices)) == r:
yield tuple(pool[i] for i in indices)
The number of items returned is n! / (n-r)!
when 0 <= r <= n
or zero when r > n
.
New in version 2.6.
3、别人家的代码:
3.1、
n=int(raw_input())
a=''
b=[]
def jiecheng(n):
res=1
for i in range(1,n+1):
res=res*i
return res
for i in range(n):
a=raw_input()
c=[]
b=[]
for k in range(12):
b.append(a[k])
c.append(a[k])
#b='abcdefghijkl'
res=1
l=0
q=[]
for j in range(12):
c=sorted(c)
#print(c)
l=0
for p in q:
if ord(p)<ord(b[j]) and ord(p)>ord(c[0]):
l+=1
res+=(ord(b[j])-ord(c[0])-l)*jiecheng(11-j)
#print(res)
c.pop(c.index(b[j]))
q.append(b[j])
print res
3.2、
import math
numbers = int(raw_input())
for m in range(numbers):
s = (raw_input())
rlt = 1
sdi = 0
for i in range(12):
t = s[i]
s0 = s[i:]
n = 0
for j in s0:
if(t>j):
n += 1
rlt += math.factorial(11-i)*n
print rlt
3.3、
基础知识备注:
1、用ASCII转换函数ord(),当然也可以用最简单的字典,自己指定大小,eg:key_list = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11}
2、math包中的求阶乘函数:
import math
math.factorial(11 - j)