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 ...
随机推荐
- [译] kubernetes:kube-scheduler 调度器代码结构概述
本文翻译自 https://github.com/kubernetes/community/blob/master/contributors/devel/sig-scheduling/scheduli ...
- bitcask论文翻译/笔记
翻译 论文来源:bitcask-intro.pdf (riak.com) 背景介绍 Bitcask的起源与Riak分布式数据库的历史紧密相连.在Riak的K/V集群中,每个节点都使用了可插拔的本地存储 ...
- 基于java+springboot的酒店预定网站、酒店客房管理系统
该系统是基于Java的酒店客房预订系统设计与实现.是给师弟开发的毕业设计.现将源代码开放出来,感兴趣的同学可以下载. 演示地址 前台地址: http://hotel.gitapp.cn 后台地址: h ...
- [转帖]Java 获取 Kafka 指定 topic 的消息总量
发表于 2020-11-29 分类于 Java , Apache , JavaClass , Kafka Valine: 0 Kafka Consumer API Kafka 提供了两套 API ...
- [转帖] 在Linux上查看活跃线程数与连接数
https://www.cnblogs.com/codelogs/p/17178675.html 原创:扣钉日记(微信公众号ID:codelogs),欢迎分享,非公众号转载保留此声明. 简介# 现 ...
- [转帖]xfs_repair命令详解
https://bbs.qunyingkeji.com/2052/ 1.现状 目前网上出现大量的主机输入输出错误,原因是由于主机文件系统损坏.一线人员大部分采用的是umont 和 mount的方式恢复 ...
- 【转帖】sqlserver 在高并发的select,update,insert的时候出现死锁的解决办法
最近在使用过程中使用SqlServer的时候发现在高并发情况下,频繁更新和频繁查询引发死锁.通常我们知道如果两个事务同时对一个表进行插入或修改数据,会发生在请求对表的X锁时,已经被对方持有了.由于得不 ...
- [转帖]Redis Scan 原理解析与踩坑
https://www.cnblogs.com/jelly12345/p/16424080.html 1. 概述由于 Redis 是单线程在处理用户的命令,而 Keys 命令会一次性遍历所有 Key, ...
- 最小化安装的CentOS7 上面安装Oracle12C的简单过程
首先声明自己对静默安装不熟,也害怕初问题,所以不使用静默安装的方式. 因为是最小化安装,所以必须安装GUI界面才可以,以下是过程(早上回忆的,全文字,无截图) 1. 安装GUI界面 yum group ...
- Oracle 以及 达梦数据库简单查询所有表行数的存储过程
1. 今天有一个场景需要查询一个数据库实例下面所有的表的行数. 本来想查询 user_tables 视图 但是发现 这个视图里面 达梦数据库 里面存的是null的.. 百度之后发现一个解决方案是 使用 ...