|
发表于 2017-8-6 17:27:47
|
显示全部楼层
本帖最后由 chunchun2017 于 2017-8-6 17:38 编辑
本人是新手,热爱编程,热爱Python,以下是解答过程,请高手指点:
如果仅仅是要计算出有多少种情况可以得到奖励,而不要求将所有的奖励字符串打印出来,那么下面的方法可以得到答案:
运算结果为:1918080160
基本思路:如果用set(m+n)表示所有的(m+n)位的奖励字符串,那么可以考虑set(m+n)是由setm和setn排列组合,然后排除一些不符合条件的字符串,就会得到set(m+n),当然set(m+n)也可以通过其他的方式,比如set(2)与set(m+n-2)...等很多方式排列组合得到
==============================================================================
'''
本函数通过setm和setn构造出set(m+n)
将setm细分为6个子集:以OA或LA结尾且含有L的元素组成setm_1,以OA或LA结尾且不含L的元素组成setm_2,以AA结尾且不含L的元素组成setm_3,以AA结尾且含有L的元素组成setm_4,
以AO,LO,OO,LA,LO结尾且不包含L的元素组成setm_6,剩下的元素组成setm_6;同理,将setn分成类似的6个子集(setn_1...setn_6)
setm与setn排列组合,实际上是setm_1...setm_6与setn_1...setn_6排列组成,
以下几种组合得到的元素显然不能放入set(m+n)中:
1.以A结尾的元素与AA开头的元素组合
2.以AA结尾的元素与A开头的元素组合
3.包含L的元素与包含L的元素组合
(1,2会生成AAA,3会生成多L)
剩下的各种情况排列组合得到的元素都应该放入set(m+n)中
'''
def combm_n(setm,setn):
setm_1=set()
setm_2=set()
setm_3=set()
setm_4=set()
setm_5=set()
setm_6=set()
setm_n=set()
for x in setm:
if((x.endswith('OA') or (x.endswith('LA'))) and (x.count('L')==1)):
setm_1.add(x)
elif (x.endswith('OA') and x.find('L')==-1):
setm_2.add(x)
elif (x.endswith('AA') and x.count('L')==1):
setm_3.add(x)
elif (x.endswith('AA') and x.find('L')==-1):
setm_4.add(x)
elif (x.endswith('AO') or x.endswith('OO')) and (x.find('L')==-1):
setm_6.add(x)
else:
setm_5.add(x)
setn_1=set()
setn_2=set()
setn_3=set()
setn_4=set()
setn_5=set()
setn_6=set()
for x in setn:
if((x.startswith('AL') or (x.startswith('AO'))) and (x.count('L')==1)):
setn_1.add(x)
elif (x.startswith('AO') and x.find('L')==-1):
setn_2.add(x)
elif (x.startswith('AA') and x.find('L')==-1):
setn_3.add(x)
elif (x.startswith('AA') and x.count('L')==1):
setn_4.add(x)
elif ((x.startswith('OO') or (x.startswith('OA')))and x.find('L')==-1):
setn_5.add(x)
else:
setn_6.add(x)
for each1 in setm_1:
for each2 in setn_2:
str0=each1+each2
setm_n.add(str0)
for each1 in setm_1:
for each2 in setn_5:
str0=each1+each2
setm_n.add(str0)
for each1 in setm_2:
for each2 in setn_1:
str0=each1+each2
setm_n.add(str0)
for each1 in setm_2:
for each2 in setn_2:
str0=each1+each2
setm_n.add(str0)
for each1 in setm_2:
for each2 in setn_5:
str0=each1+each2
setm_n.add(str0)
for each1 in setm_2:
for each2 in setn_6:
str0=each1+each2
setm_n.add(str0)
for each1 in setm_3:
for each2 in setn_5:
str0=each1+each2
setm_n.add(str0)
for each1 in setm_4:
for each2 in setn_5:
str0=each1+each2
setm_n.add(str0)
for each1 in setm_4:
for each2 in setn_6:
str0=each1+each2
setm_n.add(str0)
for each1 in setm_5:
for each2 in setn_2:
str0=each1+each2
setm_n.add(str0)
for each1 in setm_5:
for each2 in setn_3:
str0=each1+each2
setm_n.add(str0)
for each1 in setm_5:
for each2 in setn_5:
str0=each1+each2
setm_n.add(str0)
for each1 in setm_6:
for each2 in setn_1:
str0=each1+each2
setm_n.add(str0)
for each1 in setm_6:
for each2 in setn_2:
str0=each1+each2
setm_n.add(str0)
for each1 in setm_6:
for each2 in setn_3:
str0=each1+each2
setm_n.add(str0)
for each1 in setm_6:
for each2 in setn_4:
str0=each1+each2
setm_n.add(str0)
for each1 in setm_6:
for each2 in setn_5:
str0=each1+each2
setm_n.add(str0)
for each1 in setm_6:
for each2 in setn_6:
str0=each1+each2
setm_n.add(str0)
return setm_n
def countm_n(setm,setn): #与combm_n(setm,setn)函数类似,只不过不需要生成set(m+n)的每个元素,只计算set(m+n)集合中元素的个数
setm_1=set()
setm_2=set()
setm_3=set()
setm_4=set()
setm_5=set()
setm_6=set()
setm_n=set()
sum=0
for x in setm:
if((x.endswith('OA') or (x.endswith('LA'))) and (x.count('L')==1)):
setm_1.add(x)
elif (x.endswith('OA') and x.find('L')==-1):
setm_2.add(x)
elif (x.endswith('AA') and x.count('L')==1):
setm_3.add(x)
elif (x.endswith('AA') and x.find('L')==-1):
setm_4.add(x)
elif (x.endswith('AO') or x.endswith('OO')) and (x.find('L')==-1):
setm_6.add(x)
else:
setm_5.add(x)
setn_1=set()
setn_2=set()
setn_3=set()
setn_4=set()
setn_5=set()
setn_6=set()
for x in setn:
if((x.startswith('AL') or (x.startswith('AO'))) and (x.count('L')==1)):
setn_1.add(x)
elif (x.startswith('AO') and x.find('L')==-1):
setn_2.add(x)
elif (x.startswith('AA') and x.find('L')==-1):
setn_3.add(x)
elif (x.startswith('AA') and x.count('L')==1):
setn_4.add(x)
elif ((x.startswith('OO') or (x.startswith('OA')))and x.find('L')==-1):
setn_5.add(x)
else:
setn_6.add(x)
sum=sum+len(setm_1)*len(setn_2)+len(setm_1)*len(setn_5)
sum+=len(setm_2)*len(setn_1)+len(setm_2)*len(setn_2)+len(setm_2)*len(setn_5)+len(setm_2)*len(setn_6)
sum+=len(setm_3)*len(setn_5)+len(setm_4)*len(setn_5)+len(setm_4)*len(setn_6)+len(setm_5)*len(setn_2)
sum+=len(setm_5)*len(setn_3)+len(setm_5)*len(setn_5)
sum+=len(setm_6)*len(setn)
return sum
def combm_1(setm):#用于通过setm构造出set(m+1)
setm_n = set()
for x in setm:
if(x.find('L')==-1):
x+='L'
setm_n.add(x)
for x in setm:
x+='O'
setm_n.add(x)
for x in setm:
if (x.endswith('OO')or x.endswith('AO') or x.endswith('LA') or x.endswith('OA') or x.endswith('LL') or x.endswith('LO') or x.endswith('OL') or x.endswith('AL')):
x+='A'
setm_n.add(x)
return setm_n
import time
tt=time.time()
set2 = {"AA","AL","AO","LA","LO","OA","OL","OO"}
set4 = {"OOOO","OOOA","OOOL","OOAO","OOAA","OOAL",
"OOLO","OOLA","OAOO","OAOA","OAOL","OAAO","OAAL",
"OALO","OALA","OLOO","OLOA","OLAO","OLAA","AOOO",
"AOOA","AOOL","AOAO","AOAA","AOAL","AOLO","AOLA",
"AAOO","AAOA","AAOL","AALO","AALA","ALOO",
"ALOA","ALAO","ALAA","LOOO","LOOA","LOAO",
"LOAA","LAOO","LAOA","LAAO"}
set3=combm_1(set2)
set7=combm_n(set4,set3)
set8=combm_1(set7)
set15=combm_n(set8,set7)
print(countm_n(set15,set15))
print('Time used: {} sec'.format(time.time()-tt))
如果要求不但要计算出所有可能的奖励字符串的数量,还要打印出所有的奖励字符串,那上面的程序只能计算到25位,耗时:78.96465563774109 sec(去掉countm_n()函数),到计算26位奖励字符串时,内存不够用(电脑配置为16G内存),后面想尽了各种办法,也没办法计算出所有26位奖励字符串,当然更不用说30位了,很想知道能用什么高效的办法把所有的30位奖励字符串都打印出来。. |
评分
-
查看全部评分
|