题目链接

题解

知识点:构造。

首先反证法容易证明一个结论:每次增加一个字符,本质不同的回文子串至多增加一个。

那么无解的条件就是,\(c_i - c_{i-1} > x_i -x_{i-1}\) ,即距离不够数量的增加。

其他情况均有解,可以考虑利用 abc 作尾部填充,因为其只在一开始提供 \(3\) 个本质不同的回文子串,之后不仅不提供还会隔断其他字符成为回文串。其他位置,用一个字母填。我们按段考虑,填字母即可。

例如对于样例6,可以构造 dabc|ea|fffb

时间复杂度 \(O(n)\)

空间复杂度 \(O(n)\)

代码

#include <bits/stdc++.h>
using namespace std;
using ll = long long; int x[30], c[30];
bool solve() {
int n, k;
cin >> n >> k;
for (int i = 1;i <= k;i++) cin >> x[i];
for (int i = 1;i <= k;i++) cin >> c[i]; if (c[1] > x[1]) return false; int f = 0;
string s = string(c[1] - 3, 'd');
for (int i = c[1] - 2;i <= x[1];i++, (++f) %= 3) s += 'a' + f;
for (int i = 2;i <= k;i++) {
if (c[i] - c[i - 1] > x[i] - x[i - 1]) return false;
for (int j = 1;j <= c[i] - c[i - 1];j++) s += 'c' + i;
for (int j = c[i] - c[i - 1] + 1;j <= x[i] - x[i - 1];j++, (++f) %= 3) s += 'a' + f;
}
cout << "YES" << '\n';
cout << s << '\n';
return true;
} int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
cin >> t;
while (t--) {
if (!solve()) cout << "NO" << '\n';
}
return 0;
}

CF1823D Unique Palindromes的更多相关文章

  1. UVa 353 - Pesky Palindromes

    称号:字符串统计回文子的数量. 分析:dp,暴力.因为数据是小,直接暴力可以解决. 说明:(UVa最终评出800该). #include <iostream> #include <c ...

  2. URAL1960 Palindromes and Super Abilities

    After solving seven problems on Timus Online Judge with a word “palindrome” in the problem name, Mis ...

  3. Ural 1960 Palindromes and Super Abilities

    Palindromes and Super Abilities Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged ...

  4. [LeetCode] Unique Substrings in Wraparound String 封装字符串中的独特子字符串

    Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...

  5. [LeetCode] Minimum Unique Word Abbreviation 最短的独一无二的单词缩写

    A string such as "word" contains the following abbreviations: ["word", "1or ...

  6. [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数

    Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...

  7. [LeetCode] Unique Word Abbreviation 独特的单词缩写

    An abbreviation of a word follows the form <first letter><number><last letter>. Be ...

  8. [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  9. [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  10. [LeetCode] Unique Paths II 不同的路径之二

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

随机推荐

  1. Oracle数据库如何解决创建用户名开头必须要C##问题?

    1.问题 我们在创建用户,概要文件等时,由于使用的是容器数据库,其文件名必须以C##开头. 我们在学习过程中暂时不需要对齐进行区分,所以如何修改这个设定呢? 2.解决 参考链接如何解决创建用户名开头必 ...

  2. JDK21更新特性详解

    有的时候博客内容会有变动,首发博客是最新的,其他博客地址可能会未同步,认准https://blog.zysicyj.top 首发博客地址 文章更新计划 文章更新计划 | 430: | String T ...

  3. [转帖]SQL Server 2008~2022版本序列号/密钥/激活码 汇总

    https://www.cnblogs.com/cqpanda/p/16148822.html SQL Server 2022# Enterprise: J4V48-P8MM4-9N3J9-HD97X ...

  4. [转帖]TIDB - 使用BR工具进行数据热备份与恢复

    一.BR工具 BR 全称为 Backup & Restore,是 TiDB 分布式备份恢复的命令行工具,用于对 TiDB 集群进行数据备份和恢复.BR 只支持在 TiDB v3.1 及以上版本 ...

  5. openssh 修改版本号显示

    #背景介绍:G端项目经常收到相关漏洞但有时升级最新版本(8.8p)还是会有相关漏洞(CVE-2020-15778),只能禁用相关命令或修改版本号 #漏洞名称OpenSSH 命令注入漏洞(CVE-202 ...

  6. C# AsyncLocal 是如何实现 Thread 间传值

    一:背景 1. 讲故事 这个问题的由来是在.NET高级调试训练营第十期分享ThreadStatic底层玩法的时候,有朋友提出了AsyncLocal是如何实现的,虽然做了口头上的表述,但总还是会不具体, ...

  7. linux中如何统计千万个文件总和

    很简单.很简单.很简单.重要事情说三遍 命令:ls | grep '匹配信息' | wc -l ls查看该目录下的所有文件,果然隐藏文件也要匹配上的话,需要:ls -a grep匹配,如查看文件中有. ...

  8. Ant Design Vue中TreeSelect详解

    <template> <a-tree-select v-model:value="value" style="width: 320px" :t ...

  9. vue动画进入-完整的动画/有进入离开

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. 【JS 逆向百例】某网站加速乐 Cookie 混淆逆向详解

    声明 本文章中所有内容仅供学习交流,抓包内容.敏感网址.数据接口均已做脱敏处理,严禁用于商业用途和非法用途,否则由此产生的一切后果均与作者无关,若有侵权,请联系我立即删除! 逆向目标 目标:加速乐加密 ...