# 打牌博弈 dfs深度优先遍历搜索 排课表 拓扑排序 升序字符串 动态规划 剑指offer编程题整理 leetcode每日算法题

### 题目描述

https://blog.csdn.net/m0_38065572/article/details/105019514


def dfs(l):
if tuple(l) in memo:
return memo[tuple(state)]
if sum(l)==0:
return 0
else:
res = float('inf')

for i in range(10):
if i<=5 and l[i]>=1 and l[i+1]>=1 and l[i+2]>=1 and l[i+3]>=1 and l[i+4]>=1:
l[i:i+5]=[l[i+k]-1 for k in range(5)]
res = min(res, dfs(l)+1)
l[i:i+5]=[l[i+k]+1 for k in range(5)]
memo[tuple(state)] = res
if i<=7 and l[i]>=2 and l[i+1]>=2 and l[i+2]>=2:
l[i:i+3]=[l[i+k]-1 for k in range(3)]
res = min(res, dfs(l)+1)
l[i:i+3]=[l[i+k]+1 for k in range(3)]
memo[tuple(state)] = res
if l[i]>=2:
l[i]=l[i]-2
res = min(res, dfs(l)+1)
l[i]=l[i]+2
memo[tuple(state)] = res
if l[i]>=1:
l[i]=l[i]-1
res = min(res, dfs(l)+1)
l[i]=l[i]+1
memo[tuple(state)] = res
return res

def input():
l= '1 1 2 2 2 1 1 0 0 0'
return l

memo={}
state= list(map(int,input().split()))
print(state)
print(dfs(state))

### 题目描述

https://blog.csdn.net/m0_38065572/article/details/105019514


def longestAscString(strs:List[str])-> int:
strs=sorted(strs,key=lambda x:(x[-1],x[0])) #尾字符升序,第二关键字为首字符升
dp=[0]*26 #记录以字符j结尾的最长长度
res=0# 记录全局最优

last=0
for string in strs:
begin=ord(string[0])-ord('a')
end=ord(string[-1])-ord('a')
for i in range(last,end):
# 没有这些字符结尾的最长长度，使用前面的最长长度更新
dp[i+1]=dp[last]
dp[end]=max(dp[end],dp[begin]+len(string))#dp递推公式，从记录的上一步全局最有值，推到下一步全局最优
res=max(res,dp[end])
last=end

return res

a=[
"bcdefhijk",
"bcd",
"aaa",
"eeeefghhh",
"zzzz",
]
b=['abc',
'hpq',
'qrt',
'jklmnopqr',
'abcjklmnopqr',]

c=['abcd',
'deft',
'efghmnt',
'defghjkl',
'abcddefghjkl',]
print(longestAscString(c))


©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客