寻找一段长为m的序列当中不存在的子序列
Problem - C - Codeforces
void solve() {int n, k, m;std::cin >> n >> k >> m;std::string s;std::cin >> s;std::string t;int j = 0;bool ok = true;//长为nfor (int i = 0; i < n; i++) {//计数int cnt = 0;//k个std::vector<int> occ(k);//计k个,在m个字符中找while (cnt < k && j < m) {//s[j]是当前字符,说明找到了,如果它为0,就说明前面没找过,cnt++;cnt += !occ[s[j] - 'a'];occ[s[j] - 'a'] = 1;j += 1;}if (cnt < k) {ok = false;for (int x = 0; x < k; x++) {//每次放入没出现过的字符,且只放入一次,因为下面有个breakif (!occ[x]) {t += 'a' + x;break;}}}else {t += s[j - 1];}}if (ok) {std::cout << "YES\n";}else {std::cout << "NO\n";std::cout << t << "\n";}
}
主体部分
//n组k种字符在总共m个字符当中寻找int n, k, m;std::cin >> n >> k >> m;std::string s;std::cin >> s;std::string t;int j = 0;//辅助遍历bool ok = true;//n组for (int i = 0; i < n; i++) {int cnt = 0;//计数k个std::vector<int>occ(k);//记录是否找过//找到k个了或者字符遍历完了就退出while (cnt < k && j < m) {cnt += !occ[s[j] - 'a'];//记录新字符occ[s[j] - 'a'] = 1;//标记找过j += 1;//字符往后挪一位}//要么找全,要么遍历完,出现没有找全肯定是遍历完了//因为for循环设的n次不可能找到超出n次if (cnt < k) {ok = false;for (int x = 0; x < k; x++) {if (!occ[x]) {t += 'a' + x;break;}}}else {t += s[j - 1];}}if (ok) {std::cout << "YES\n";}else {std::cout << "NO\n";std::cout << t << '\n';}