2 条题解

  • 0
    @ 2025-3-30 10:56:11
    n = int(input())
    s = 'K' + input() + 'V'
    #对字符串s进行切片操作
    l = s.split('VK')
    #获取原字符串中含有“VK”的个数
    c = len(l) - 1
    #判断是否能通过更改一个字母,增加一个“VK”
    if 'VVV' in s or 'KKK' in s:
        c += 1
    print(c)
    

    这个方法,简洁明了,比玉米杰的好/^_^

  • 0
    @ 2025-3-29 14:45:13
    def count_vk_occurrences(s):
        count = 0
        for i in range(len(s) - 1):
            if s[i:i+2] == "VK":
                count += 1
        return count
    
    def maximize_vk_occurrences(n, s):
        max_count = count_vk_occurrences(s)
        
        for i in range(n):
            original_char = s[i]
            
            # 尝试将当前字符替换为'V'
            if original_char != 'V':
                s_replaced_with_v = s[:i] + 'V' + s[i+1:]
                max_count = max(max_count, count_vk_occurrences(s_replaced_with_v))
            
            # 尝试将当前字符替换为'K'
            if original_char != 'K':
                s_replaced_with_k = s[:i] + 'K' + s[i+1:]
                max_count = max(max_count, count_vk_occurrences(s_replaced_with_k))
            
            # 恢复原始字符,以便进行下一次替换尝试
            s = s[:i] + original_char + s[i+1:]
        
        return max_count
    
    # 读取输入
    n = int(input())
    s = input()
    
    # 输出结果
    print(maximize_vk_occurrences(n, s))
    

    不喜欢写注释

    • 1

    信息

    ID
    703
    时间
    1000ms
    内存
    256MiB
    难度
    9
    标签
    递交数
    13
    已通过
    5
    上传者