CF1823D Unique Palindromes
题解
知识点:构造。
首先反证法容易证明一个结论:每次增加一个字符,本质不同的回文子串至多增加一个。
那么无解的条件就是,\(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的更多相关文章
- UVa 353 - Pesky Palindromes
称号:字符串统计回文子的数量. 分析:dp,暴力.因为数据是小,直接暴力可以解决. 说明:(UVa最终评出800该). #include <iostream> #include <c ...
- URAL1960 Palindromes and Super Abilities
After solving seven problems on Timus Online Judge with a word “palindrome” in the problem name, Mis ...
- Ural 1960 Palindromes and Super Abilities
Palindromes and Super Abilities Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged ...
- [LeetCode] Unique Substrings in Wraparound String 封装字符串中的独特子字符串
Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...
- [LeetCode] Minimum Unique Word Abbreviation 最短的独一无二的单词缩写
A string such as "word" contains the following abbreviations: ["word", "1or ...
- [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- [LeetCode] Unique Word Abbreviation 独特的单词缩写
An abbreviation of a word follows the form <first letter><number><last letter>. Be ...
- [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- [LeetCode] Unique Paths II 不同的路径之二
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
随机推荐
- java基础-java面向对象-02-day09
目录 1. 封装 2. 继承 2.1 什么是方法的重写 2.2 super 2.3 object详解 2.4 equals方法 3. 多态 4. final修饰符 5.抽象类 6. 接口 7. 内部类 ...
- java基础-数组-day06
1. 引入数组 录入10个学生的成绩 求和 求平均 import java.util.Scanner; public class TestArray01{ public static void mai ...
- linux 查看服务器cpu 与内存配置
转载请注明出处: 1. 通过 lscpu 命令查看 服务器的cpu 配置 显示格式: Architecture: #架构 CPU(s): #逻辑cpu颗数 Thread(s) per core: #每 ...
- 如何取消VSCODE文件夹折叠
1.问题 如图所示,文件夹折叠在一起,导致我无法在父文件夹中新建一个文件夹,而是只能在子文件夹中新建文件夹 2.解决 原因:文件夹以紧凑方式呈现,取消即可 1. 打开设置,在里面搜索Explorer: ...
- 【ThreadX-NetX】Azure RTOS NetX概述
Azure RTOS NetX是工业级TCP / IP IPv4嵌入式网络堆栈,专门针对深度嵌入式,实时和IoT应用程序而设计.Azure RTOS NetX是Microsoft最初的IPv4网络堆栈 ...
- 选择DOM中除一个元素以外的所有元素[Jquery]
$(function(){ $('body > *').not('#myDiv') .on('mouseover', function(){ //... }) .on('click', func ...
- 【canvas】 绘制七巧板
效果图: 代码 : <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- CSS - 怎么样在不同分辨率的情况下计算根元素需要的font-size的值
一般我们做页面,肯定都会有设计图,移动端页面,一般情况下,UI出图都会定宽为640px,这也是移动端的标准尺寸:但是,我们也不能排除可能有其他特殊的情况可能需要做其他大小的设计图.所以,我们可以先定一 ...
- SpringBoot3集成Zookeeper
标签:Zookeeper3.8 ,Curator5.5: 一.简介 ZooKeeper是一个集中的服务,用于维护配置信息.命名.提供分布式同步.提供组服务.分布式应用程序以某种形式使用所有这些类型的服 ...
- Go-数组-实现队列
package main import ( "errors" "fmt" ) // 队列 // 特征: // 1. 按照元素的添加顺序排序,并且容量固定 // ...