CF18E.Flag 2

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:128MB

AC君温馨提醒

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

题目描述

According to a new ISO standard, a flag of every country should have, strangely enough, a chequered field n × m, each square should be wholly painted one of 26 colours. The following restrictions are set:

  • In each row at most two different colours can be used.
  • No two adjacent squares can be painted the same colour.

Pay attention, please, that in one column more than two different colours can be used.

Berland's government took a decision to introduce changes into their country's flag in accordance with the new standard, at the same time they want these changes to be minimal. By the given description of Berland's flag you should find out the minimum amount of squares that need to be painted different colour to make the flag meet the new ISO standard. You are as well to build one of the possible variants of the new Berland's flag.

根据一项新的 ISO 标准,每个国家的国旗应具有一个奇特的棋盘状矩形区域,尺寸为 n×mn \times m,其中每个方格必须被完整地涂上 26 种颜色之一。该标准规定以下限制条件:

  • 每一行中至多使用两种不同的颜色;
  • 任意两个相邻的方格不能涂成相同颜色。

请注意:在一列中可以使用超过两种不同的颜色。

贝兰德(Berland)政府决定依据该新标准对其国旗进行修改,同时希望这些修改尽可能少。给定当前贝兰德国旗的描述,请计算出为使国旗满足新 ISO 标准所需重新涂色的方格的最小数量,并构造出一种满足要求的新贝兰德国旗方案。

输入格式

The first input line contains 2 integers n and m (1 ≤ n, m ≤ 500) — amount of rows and columns in Berland's flag respectively. Then there follows the flag's description: each of the following n lines contains m characters. Each character is a letter from a to z, and it stands for the colour of the corresponding square.

第一行输入包含两个整数 nn 和 mm(1 ≤ n, m ≤ 5001 \leq n, m \leq 500),分别表示贝尔兰国旗的行数和列数。随后是国旗的描述:接下来的 nn 行,每行包含 mm 个字符。每个字符均为小写字母 a 到 z 中的一个,代表对应方格的颜色。

输出格式

In the first line output the minimum amount of squares that need to be repainted to make the flag meet the new ISO standard. The following n lines should contain one of the possible variants of the new flag. Don't forget that the variant of the flag, proposed by you, should be derived from the old flag with the minimum amount of repainted squares. If the answer isn't unique, output any.

第一行输出为使旗帜符合新的 ISO 标准所需重涂的最小方格数。接下来的 nn 行应输出一种可能的新旗帜方案。请勿忘记:您所提出的旗帜方案必须由原旗帜通过最少次数的方格重涂得到。若答案不唯一,输出任意一种即可。

输入输出样例

  • 输入#1

    3 4
    aaaa
    bbbb
    cccc

    输出#1

    6
    abab
    baba
    acac
  • 输入#2

    3 3
    aba
    aba
    zzz

    输出#2

    4
    aba
    bab
    zbz

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

首页