给出一个只包含 0 和 1 的 01 串 s ,下标从 1 开始,设第 i 位的价值为 vali ,则价值定义如下:
1. i=1时:val1 = 1
2. i>1时:
2.1 若 si ≠ si-1 , vali = 1
2.2 若 si = si-1 , vali = vali-1 + 1
字符串的价值等于 val1 + val2 + val3 + ... + valn
第一行一个正整数 n ,代表串长度。接下来一行一个 01 串 s 。1 ≤ n ≤ 5,000
输出一个整数代表答案
6 010101
7
删除后的串为0001或0111时有最大价值
20 11111000111011101100
94
4 1100
6
n=int(input())
s=input()
vals=[1]*n
cnt1=0
v0=1
for i in range(1,n):
if s[i]=='1':
cnt1+=1
if s[i]==s[i-1]:
vals[i]=vals[i-1]+1
v0+=vals[i]
if s[0]=='1':
cnt1+=1
cnt0=n-cnt1
left_char=s[0]
left_c=1
right_char=s[-1]
right_c=1
for i in range(1,n):
if s[i]!=s[i-1]:
break
left_c+=1
for i in range(n-2,-1,-1):
if s[i]!=s[i+1]:
break
right_c+=1
mp={0:cnt0,1:cnt1}
v1=left_c*(left_c+1)//2+mp[1-int(left_char)]*(mp[1-int(left_char)]+1)//2
v2=right_c*(right_c+1)//2+mp[1-int(right_char)]*(mp[1-int(right_char)]+1)//2
v3=1
v4=1
if left_char==right_char:
v3=left_c*(left_c+1)//2+right_c*(right_c+1)//2+mp[1-int(left_char)]*(mp[1-int(left_char)]+1)//2
v4=mp[int(left_char)]*(mp[int(left_char)]+1)//2
if max(cnt0,cnt1)==n:
print(v0)
else:
print(max(v0,v1,v2,v3,v4))
import sys
def solution():
def f(num):
return (1+num)*num // 2
n = int(input())
s = input()
count = 1
res = 0
nums = []
for i in range(len(s)):
if i > 0:
if s[i] == s[i-1]:
count += 1
else:
nums.append(count)
count = 1
nums.append(count)
# print(nums)
m = len(nums)
dp = [[0,0] for _ in range(m)]
dp[0][0] = nums[0]
dp[0][1] = f(dp[0][0])
if m <= 1:
return dp[-1][-1]
dp[1][0] = nums[1]
dp[1][1] = f(dp[0][0]) + f(dp[1][0])
for i in range(2,m):
dp[i][0] = dp[i-2][0] + nums[i]
if i % 2 == 1:
dp[i][1] = max(dp[i][1],f(dp[i][0])+dp[0][1])
else:
dp[i][1] = max(dp[i][1], f(dp[i][0]))
# dp[i][1] = max(dp[i][1],f(dp[i][0]))
for j in range(i-2,-1,-2):
# print(f'(i,j):{i,j} val:{f(dp[i][0] - dp[j][0]) + dp[j+1][1]}')
dp[i][1] = max(f(dp[i][0] - dp[j][0]) + dp[j+1][1],dp[i][1])
# print()
# print(dp)
return dp[-1][-1]
if __name__ == "__main__":
print(solution()) # 45ms 4700KB 所有测试样例全A!
from collections import defaultdict
n = int(input())
s = input()
zero_len = s.count("0")
one_len = s.count("1")
temp_dict = defaultdict(int)
if zero_len>one_len:
temp_dict["left"] = len(s[0:s.index("0")])
temp_dict["right"] = len(s[n-s[::-1].index("0"):])
temp_dict["max"] = zero_len
elif one_len>zero_len:
temp_dict["left"] = len(s[0:s.index("1")])
temp_dict["right"] = len(s[n-s[::-1].index("1"):])
temp_dict["max"] = one_len
elif one_len==zero_len:
temp_dict["max"] = one_len
if len(s[0:s.index("0")])+len(s[n-s[::-1].index("0"):])>=len(s[0:s.index("1")])+len(s[n-s[::-1].index("1"):]):
temp_dict["left"] = len(s[0:s.index("0")])
temp_dict["right"] = len(s[n-s[::-1].index("0"):])
else:
temp_dict["left"] = len(s[0:s.index("1")])
temp_dict["right"] = len(s[n-s[::-1].index("1"):])
sum_ = 0
for i in temp_dict:
cur_val = 1
for j in range(temp_dict[i]):
sum_+=cur_val
cur_val+=1
print(sum_) leng = int(input().strip()) string = list(map(int, input().strip())) vals = [0] * leng vals[0] = 1 for i in range(1, leng): vals[i] = vals[i - 1] + 1 cc = 1 j = i - 1 while j >= 0: if string[i] == string[j]: cc +=1 else: temp = vals[j] + (cc + 1) * cc / 2 if temp > vals[i]: vals[i] = temp j -= 1 temp = (cc + 1) * cc / 2 if temp > vals[i]: vals[i] = temp print(vals[leng - 1])