题目链接

题解

知识点:构造。

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

那么无解的条件就是,\(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. java基础-Junit 注解 枚举-day12

    目录 1. Junit 2. 注解 annotation 3. 枚举 1. Junit 白盒测试 黑盒测试 自行baidu了解 java单元测试 package com.msb01; import o ...

  2. zookeeper 使用api 进行节点增删改查及实现简易的配置中心

    本文为博主原创,未经允许不得转载: 目录: 1. 对 zookeeper 节点进行增删改查既配置acl 权限等 2.使用 zookeeper  实现一个简易的配置中心 1. 对 zookeeper 节 ...

  3. -- spi flash 擦除接口调用HAL库不同函数的区别

    [描述] 在使用STM32F429操作W25Q128时,为验证flash工作正常,做简单的读写数据校验,在擦除接口中使用 HAL_SPI_Transmit 方法一直工作异常,使用 HAL_SPI_Tr ...

  4. [转帖]Run Grafana behind a reverse proxy

    On this page Introduction Configure NGINX Configure HAProxy Configure IIS Configure Traefik Summary ...

  5. 【转帖】26.Java本地方法的理解(native方法)

    目录 1.什么是本地方法? 2. 为什么要使用Native method? 1.什么是本地方法? 本地方法就是java代码里面写的native方法,它没有方法体.是为了调用C/C++代码而写的.在JN ...

  6. [转帖]总结:SpringBoot启动参数配置

    一.背景 由于项目中加了bootstrap.properties文件,且文件中有变量,如spring.cloud.config.profile=${spring.profiles.active},而b ...

  7. jconsole的简单学习

    摘要 jconsole 是JDK自带的一款图形化监测工具 他可以监测本地程序,也可以检测远程的机器 在没有其他监控手段可以使用的情况下可以快速进行必要的监测 使用方法也比较简单. 本地监控 jcons ...

  8. 海量数据 vastbase G100 V2.2安装简单总结

    海量数据vastbase G100 V2.2 安装总结 背景说明 最近进行信创四期的数据库兼容性验证, 获取了海量数据的一个信创名录内的安装介质. 一直忙于出差, 今天晚上趁着冬至回家比较早在家里进行 ...

  9. docker的架构及工作原理(详解)

    目录 一.docker架构图 二.Client 客户端 三.Host 主机(docker引擎) 四.Image 镜像 五.Container 容器 六.镜像分层 可写的容器层 七.Volume 数据卷 ...

  10. 从零开始配置 vim(13)——标签页插件

    原始的vim中标签页已经足够好用了.你完全可以使用原始 vim 提供的功能,但是使用插件可以让它更好看.这里我比较喜欢使用 bufferline 这个插件 安装 首先我们来安装它, 在使用 packe ...