首页 > 其他分享 >C. Did We Get Everything Covered?

C. Did We Get Everything Covered?

时间:2024-02-03 17:22:06浏览次数:25  
标签:le string vis Did NO Everything length Covered first

C. Did We Get Everything Covered?

You are given two integers $n$ and $k$ along with a string $s$.

Your task is to check whether all possible strings of length $n$ that can be formed using the first $k$ lowercase English alphabets occur as a subsequence of $s$. If the answer is NO, you also need to print a string of length $n$ that can be formed using the first $k$ lowercase English alphabets which does not occur as a subsequence of $s$.

If there are multiple answers, you may print any of them.

Note: A string $a$ is called a subsequence of another string $b$ if $a$ can be obtained by deleting some (possibly zero) characters from $b$ without changing the order of the remaining characters.

Input

The first line of input contains a single integer $t \, (1 \le t \le 10^5)$, the number of test cases.

The first line of each test case contains $3$ integers $n \, (1 \le n \le 26), \: k \, (1 \le k \le 26), \: m \, (1 \le m \le 1000)$, where $n$ and $k$ are the same as described in the input and $m$ is the length of the string $s$.

The second line of each test case contains a single string $s$ of length $m$, comprising only of the first $k$ lowercase English alphabets.

It is guaranteed that the sum of $m$ and the sum of $n$ over all test cases does not exceed $10^6$.

Output

For each test case, print YES if all possible strings of length $n$ that can be formed using the first $k$ lowercase English alphabets occur as a subsequence of $s$, else print NO.

If your answer is NO, print a string of length $n$ that can be formed using the first $k$ lowercase English alphabets which does not occur as a subsequence of $s$ in the next line.

You may print each letter of YES or NO in any case (for example, YES, yES, YeS will all be recognized as a positive answer).

Example

input

3
2 2 4
abba
2 2 3
abb
3 3 10
aabbccabab

output

YES
NO
aa
NO
ccc

Note

For the first test case, all possible strings (aa, ab, ba, bb) of length $2$ that can be formed using the first $2$ English alphabets occur as a subsequence of abba.

For the second test case, the string aa is not a subsequence of abb.

 

解题思路

  受 A 题 We Got Everything Covered! 的启发,如果将前 $k$ 个字符构成一组,并把至少 $n$ 组拼接成字符串,那么字符串所有的子序列一定包含 $k^n$ 种由前 $k$ 个字符构成的长度为 $n$ 的串。不过这只说明了充分性,并不能说明必要性,即如果某字符串所有的子序列包含由前 $k$ 个字符构成的长度为 $n$ 的串,则该字符串一定至少包含 $n$ 个由前 $k$ 个字符构成的组。比赛的时候没想到也没去猜所以没做出来。

  事实上这个必要性是成立的。我们依次找到 $s$ 中由前 $k$ 个字符构成的组,即依次遍历 $s$ 的每个字符,用一个哈希表记录某个字符是否出现过,以及一个变量 $c$ 统计当成组中有多少种字符。当遍历到 $s_i$,哈希表的 $s_i$ 处记为 $\text{true}$,如果在变化之前为 $\text{false}$ 则 $c$ 加 $1$。如果此时的 $c=k$,说明已经找到一组,清空哈希表和 $c$ 继续找下一组。

  如果我们每次都选择每组中最后一个字符来尝试构成一个反例,显然如果字符串中小于 $n$ 组,那么就一定可以构造出一个反例,只需将最后一组中没出现过的字符填到反例后面直到长度变成 $n$。这种构造方案也证明了必要性。

  AC 代码如下,时间复杂度为 $O(m)$:

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

const int N = 1010;

char s[N];
bool vis[26];

void solve() {
    int n, m, k;
    scanf("%d %d %d %s", &n, &k, &m, s);
    string ans;
    memset(vis, 0, sizeof(vis));
    for (int i = 0, c = 0; i < m; i++) {
        if (!vis[s[i] - 'a']) c++;
        vis[s[i] - 'a'] = true;
        if (c == k) {
            ans.push_back(s[i]);
            memset(vis, 0, sizeof(vis));
            c = 0;
        }
    }
    if (ans.size() >= n) {
        printf("YES\n");
    }
    else {
        printf("NO\n");
        for (int i = 0; i < 26; i++) {
            if (!vis[i]) {
                while (ans.size() < n) {
                    ans.push_back('a' + i);
                }
                printf("%s\n", ans.c_str());
                return;
            }
        }
    }
}

int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        solve();
    }
    
    return 0;
}

 

参考资料

  Codeforces Round 921 (Div. 1, Div. 2) Editorial:https://codeforces.com/contest/1925/problem/C

标签:le,string,vis,Did,NO,Everything,length,Covered,first
From: https://www.cnblogs.com/onlyblues/p/18004970

相关文章

  • 字符串构建问题——cf_921_C. Did We Get Everything Covered?
    目录问题概述思路想法参考代码include<bits/stdc++.h>defineFAST_IOios::sync_with_stdio(false),cin.tie(0),cout.tie(0)defineendl'\n'definepllpair<longlong,longlong>definepiipair<int,int>definevivectordefinevlvectordefinelllo......
  • C. Did We Get Everything Covered
    原题链接前情提要限于自身知识水平的储备不足,无法对这道题的贪心算法做出一个证明,待来日学识渐长把这个证明写下题解我们可以把字符串s分成若干区间,每一区间对应一位数字的储备已知长度为n,那我们就一位一位地遍历,一旦所有元素遍历齐就开始下一位的遍历,因为再往后遍历也不起作......
  • [ARC096E] Everything on It
    这种题在想题的时候可以先随便找到一个做法再去优化,不要管复杂度,想多了容易混。首先容易发现,这道题直接计数比较困难。所以我们想到了用容斥来解决这个问题。但是容斥的方式可能比较多,可以多尝试然后选择最简便的方法。钦定\(f_i\)表示,我们强制\(n\)个数中的\(i\)个出现次......
  • k8s 报错: node(s) didn't match Pod's node affinity.
    前言k8s集群中,有pod出现了Affinity,使用kubectldescribepod命令,发现了报错2node(s)didn'tmatchPod'snodeaffinity.这是因为节点被打上了污点,导致了pod没有节点可以起来解决kubectlgetnodes-ojson|jq'.items[].spec'orkubectlgetnodes-oyaml找到......
  • Docker启动Nacos报错:Nacos Server did not start because dumpservice bean construct
    一、表象重启服务器之后Docker运行Nacos容器,启动成功,但是外网无法访问。查看了一下Nacos启动日志(dockerlogsnacos容器名)二、分析很明显是数据库配``置问题。。如果是数据库配置的问题,可以着重检查以下信息尤其是MySQL内网Host,查询方式见Docker安装Nacos三、解决我已......
  • 用RWEverything刷内存spd
    最近参与的一个项目,由于主板只支撑ddr3L的内存,需要把ddr3标压内存刷为ddr3L。通过百度找到如下:不无折腾篇五:DDR3标压内存改DDR3L低压条保姆级傻瓜教程于是动手实践了一下,特此记录本过程:1、找台支持ddr3标压的机子作为刷机平台,并下载好软件RWEverything1.72、寻找能刷spd的内......
  • Xcode 15 Release Candidate (15A240d) 发布 - Apple 平台 IDE
    Xcode15ReleaseCandidate(15A240d)发布-Apple平台IDEIDEforiOS/iPadOS/macOS/watchOS/tvOS/visonOS作者主页:sysin.orgvisonOS支持已更新。Xcode15使您能够为所有Apple平台开发、测试和分发应用程序。通过增强的代码完成、交互式预览和实时动画,更快地编写和设计您的......
  • 基于DID实现第三方应用的分布式身份登录
    在我们掌握了DID的基础知识(还没有掌握DID基础知识?请先阅读我之前的关于DID的文章),构建DID平台的时候,DID的常见应用就是基于DID实现第三方平台的登录。接下来,我们假设已经构建了一个基础的DID平台,用长安链实现了DID文档的链上管理,并提供了DID钱包托管用户的公私钥和VC证书,建设全新的......
  • Github Actions - Error: The connection to the server localhost:8080 was refused
     Runkubectlapply-feks/aws-auth.yamlkubectlapply-feks/aws-auth.yamlkubectlapply-feks/deployment.yamlkubectlapply-feks/service.yamlshell:/usr/bin/bash-e{0}env:AWS_DEFAULT_REGION:ap-southeast-1AWS_REGION:ap-southe......
  • Docker启动Nacos报错:Nacos Server did not start because dumpservice bean construct
    一、表象重启服务器之后Docker运行Nacos容器,启动成功,但是外网无法访问。查看了一下Nacos启动日志(dockerlogsnacos容器名)二、分析很明显是数据库配``置问题。。如果是数据库配置的问题,可以着重检查以下信息尤其是MySQL内网Host,查询方式见Docker安装Nacos三、解决我已......