CF1913F.Palindromic Problem

省选/NOI-

通过率:0%

时间限制:5.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a string ss of length nn, consisting of lowercase Latin letters.

You are allowed to replace at most one character in the string with an arbitrary lowercase Latin letter.

Print the lexicographically minimal string that can be obtained from the original string and contains the maximum number of palindromes as substrings. Note that if a palindrome appears more than once as a substring, it is counted the same number of times it appears.

The string aa is lexicographically smaller than the string bb if and only if one of the following holds:

  • aa is a prefix of bb, but a≠ba \ne b;
  • in the first position where aa and bb are different, the string aa contains a letter that appears earlier in the alphabet than the corresponding letter in bb.

给你一个长度为 nn 的字符串 ss,它仅由小写拉丁字母组成。

你最多可以将字符串中的一个字符替换为任意一个小写拉丁字母。

请输出一个字典序最小的字符串,该字符串由原字符串经过至多一次字符替换得到,且其作为子串所包含的回文串数量达到最大。注意:如果某个回文串在字符串中多次出现,则每次出现均单独计数。

当且仅当满足以下条件之一时,字符串 aa 的字典序小于字符串 bb:

  • aa 是 bb 的前缀,但 a≠ba \ne b;
  • 在 aa 与 bb 首次出现不同字符的位置上,aa 中的字符在字母表中位于 bb 中对应字符之前。

输入格式

The first line contains one integer nn (1≤n≤3⋅1051 \leq n \leq 3 \cdot 10^5) — the number of characters in the string.

The second line contains the string ss itself, consisting of exactly nn lowercase Latin letters.

第一行包含一个整数 nn(1≤n≤3⋅1051 \leq n \leq 3 \cdot 10^5)—— 字符串的字符个数。

第二行包含字符串 ss 本身,由恰好 nn 个小写拉丁字母组成。

输出格式

In the first line, print one integer — the maximum number of palindromic substrings that can be obtained using the operation described in the statement at most once.

In the second line, print the string that can be obtained from ss and has the maximum possible number of palindromic substrings. If there are multiple answers, print the lexicographically smallest one.

第一行,输出一个整数——即最多执行一次题目所述操作后,所能得到的回文子串的最大数量。

第二行,输出由字符串 ss 经过该操作后所得、且具有最多可能回文子串的字符串。若存在多个满足条件的答案,请输出字典序最小的一个。

输入输出样例

  • 输入#1

    5
    aabaa

    输出#1

    15
    aaaaa
  • 输入#2

    5
    aaaaa

    输出#2

    15
    aaaaa
  • 输入#3

    4
    awoo

    输出#3

    7
    aooo

输入解题思路,AI测评打分。不知道怎么写?

首页