首页 > 试题广场 >

丑陋的字符串

[编程题]丑陋的字符串
  • 热度指数:2185 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
牛牛喜欢字符串,但是他讨厌丑陋的字符串。对于牛牛来说,一个字符串的丑陋值是字符串中相同连续字符对的个数。比如字符串“ABABAABBB”的丑陋值是3,因为有一对"AA"和两对重叠的"BB"。现在给出一个字符串,字符串中包含字符'A'、'B'和'?'。牛牛现在可以把字符串中的问号改为'A'或者'B'。牛牛现在想让字符串的丑陋值最小,希望你能帮帮他。

输入描述:
输入包括一个字符串s,字符串长度length(1 ≤ length ≤ 50),字符串只包含'A','B','?'三种字符。


输出描述:
输出一个整数,表示最小的丑陋值
示例1

输入

A?A

输出

0
# 注意有个测试用例是“??”
import sys
strValue = list(sys.stdin.readline().strip())
leftPoint, strLen = 0,len(strValue)
count = 0
while leftPoint < strLen and strValue[leftPoint]=="?":
    leftPoint += 1
leftPoint += 1
while leftPoint<strLen:
    if strValue[leftPoint] == "?":
        strValue[leftPoint] = "B" if strValue[leftPoint-1] == "A" else "A"
    else:
        if strValue[leftPoint] == strValue[leftPoint-1]:
            count += 1
    leftPoint +=1
print(count)
发表于 2019-07-18 16:16:41 回复(0)

热门推荐

通过挑战的用户