当前位置: 首页> 房产> 市场 > 贪心+构造,1924A - Did We Get Everything Covered?

贪心+构造,1924A - Did We Get Everything Covered?

时间:2025/7/12 21:47:59来源:https://blog.csdn.net/EQUINOX1/article/details/142095517 浏览次数:0次

目录

一、题目

1、题目描述

2、输入输出

2.1输入

2.2输出

3、原题链接

二、解题报告

1、思路分析

2、复杂度

3、代码详解


一、题目

1、题目描述

2、输入输出

2.1输入

2.2输出

3、原题链接

1924A - Did We Get Everything Covered?

二、解题报告

1、思路分析

我们从头遍历s,用集合st代表出现的前k个小写字母的字符集

如果 st 在某个位置变成全集,说明长度为1的序列是ok的

我们将st清空,接着遍历

st 又在某个位置变成全集,说明长度为2的序列是ok的

……

那么如何输出非法方案?

我们依次将每个全集时刻加入st的字符保存,然后再加上一个非法时不在st中的任意字符即可

2、复杂度

时间复杂度: O(N)空间复杂度:O(N)

3、代码详解

 ​
import sys
import math
import heapq
from collections import defaultdict, Counter
from string import ascii_lowercaseinput = lambda: sys.stdin.readline().strip()
output = lambda x: sys.stdout.write(str(x) + '\n')
MII = lambda: map(int, input().split())
LMI = lambda: list(map(int, input().split()))
LI = lambda: list(input())
II = lambda: int(input())
I = lambda: input()
fmax = lambda x, y: x if x > y else y
fmin = lambda x, y: x if x < y else y# sys.stdin = open('in.txt', 'r')def solve():n, k, m = LMI()s = I()st = 0full = (1 << k) - 1ans = ''for x in s:if ord(x) - ord('a') < k:st |= 1 << (ord(x) - ord('a'))if st == full:st = 0ans += xif len(ans) < n:print('NO')for i in range(k):if ((st >> i) & 1) == 0:ans += str(chr(ord('a') + i)) * (n - len(ans))breakprint(ans)else:print('YES')    passif __name__ == "__main__":T = 1T = II()for _ in range(T):solve()

关键字:贪心+构造,1924A - Did We Get Everything Covered?

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: