PAT(Advanced Level) Practice(with python)——1093 Count PAT‘s

PAT(Advanced Level) Practice(with python)——1093 Count PAT‘s_第1张图片

Code

string = input()
length = len(string)
res = 0

# 暴力止步
# T_index = string.find('T')
# T_rindex = string.rfind('T')
# A_index = string.find('A')
# A_rindex = string.rfind('A')
# P_index = string.find('P')
# P_rindex = string.rfind('P')


# for i in range(P_index,min(T_rindex+1,P_rindex+1)):
#     if string[i]=='P':
#         for j in range(max(i+1,A_index),min(T_rindex+1,A_rindex+1)):
#             if string[j]=='A':
#                 for k in range(max(j+1,T_index),T_rindex+1):
#                     if string[k]=='T':
#                         res +=1

# res %= 1000000007
# print(res)

# 动态规划
dp = [[0,0,0] for i in range(length)]
if string[0]=='P':
    dp[0][0] = 1
for s in range(1,length):
    if string[s]=='P':
        dp[s][0] = dp[s-1][0] + 1
        dp[s][1] = dp[s-1][1]
        dp[s][2] = dp[s-1][2]
    elif string[s]=='A':
        dp[s][0] = dp[s-1][0]
        dp[s][1] = dp[s-1][1] + dp[s-1][0]
        dp[s][2] = dp[s-1][2]
    elif string[s] == 'T':
        dp[s][0] = dp[s-1][0]
        dp[s][1] = dp[s-1][1]
        dp[s][2] = dp[s-1][2] + dp[s-1][1]
        
dp[length-1][2]%=1000000007
print(dp[length-1][2])

PAT(Advanced Level) Practice(with python)——1093 Count PAT‘s_第2张图片

你可能感兴趣的:(PAT,python,开发语言,算法,动态规划)