题目链接

题解

知识点:构造。

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

那么无解的条件就是,\(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. Go 汇编学习笔记

    0.前言 学习 Go 离不开看源码,源码又包含大量汇编代码,离开汇编是学不好 Go 的.同样,离开汇编去学习计算机是不完整的,汇编是基石,是离操作系统和硬件最近的一层. 虽然之前学过一点 Go 汇编, ...

  2. React报错之The tag is unrecognized in this browser

    正文从这开始~ 总览 当我们使用一个在浏览器中不存在的标签或以小写字母开头的组件名称时,会产生"The tag is unrecognized in this browser"Re ...

  3. DFT Architecture

    Design For Test 在实际生产过程中产生的physical defect是导致芯片功能出错的根本原因 如何根据结构产生测试向量呢?主要考虑physical defect physical ...

  4. 274. H 指数

    1.题目介绍 给你一个整数数组 citations ,其中 citations[i] 表示研究者的第 i 篇论文被引用的次数.计算并返回该研究者的 h 指数. 根据维基百科上 h 指数的定义:h 代表 ...

  5. [转帖]dd命令中dsync和fsync区别

    在Linux系统中经常会使用dd命令来测试硬盘的写入速度,命令会涉及几个常用参数:sync.dsync.fsync与fdatasync # dd if=/dev/zero of=/tmp/1G bs= ...

  6. Nginx与Tomcat作为前端服务器的性能比较

    Nginx与Tomcat作为前端服务器的性能比较 摘要 最近总遇到使用tomcat还是使用nginx进行前端文件访问的争论 想着出差周末在酒店, 可以自己进行一下简单的测试. 希望能够对未来的工作进行 ...

  7. rpm包方式安装oracle21c

    下载相关依赖包 https://yum.oracle.com/repo/OracleLinux/OL8/appstream/x86_64/index.htmlhttps://www.oracle.co ...

  8. node+express+ multer 实现文件上传入门

    文件上传 文件上传需要借助一个中间件 multer 因此我们需要安装 cnpm install multer --save 前端界面 在express创建的项目下的 public/upload目录下创 ...

  9. 【笔记】vm-storage的go profile调用图表(没什么实际意义,就是为了做笔记)

    作者:张富春(ahfuzhang),转载时请注明作者和引用链接,谢谢! cnblogs博客 zhihu Github 公众号:一本正经的瞎扯 1.启动 force merge curl -G &quo ...

  10. MySQL知识点总结(完整版)

    MySQL学习笔记 登录和退出MySQL服务器 # 登录MySQL $ mysql -u root -p12345612 # 退出MySQL数据库服务器 exit; 基本语法 -- 显示所有数据库 s ...