题目链接:https://codeforces.com/problemset/problem/1099/E

You are given an $n×m$ table, consisting of characters «A», «G», «C», «T». Let's call a table nice, if every $2×2$ square contains all four distinct characters. Your task is to find a nice table (also consisting of «A», «G», «C», «T»), that differs from the given table in the minimum number of characters.

Input
First line contains two positive integers $n$ and $m$ — number of rows and columns in the table you are given $(2≤n,m,n×m≤300000)$. Then, $n$ lines describing the table follow. Each line contains exactly $m$ characters «A», «G», «C», «T».

Output
Output $n$ lines, $m$ characters each. This table must be nice and differ from the input table in the minimum number of characters.

Examples
Input
2 2
AG
CT
Output
AG
CT
Input
3 5
AGCAG
AGCAG
AGCAG
Output
TGCAT
CATGC
TGCAT
Note
In the first sample, the table is already nice. In the second sample, you can change $9$ elements to make the table nice.

题解:

一个 nice table 必然属于以下两种情况:

  1、每行都由两个字符交替组成,相邻两行的字符集合交集为空;

  2、每列都由两个字符交替组成,相邻两列的字符集合交集为空。

对于上述两种情况中的一种,不妨先对于每行,枚举其可能的字符集,而对于一个字符集,又有正序和逆序两种情况。

AC代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=3e5+;
const char choice[][]={{'A','C'},{'A','G'},{'A','T'},{'C','G'},{'C','T'},{'G','T'}}; int n,m;
string str[maxn]; int ord[][maxn][],cnt[][maxn]; string out[maxn];
void print(int rc,int k)
{
for(int i=;i<n;i++) out[i]="";
if(rc==) //R
{
for(int i=;i<n;i++)
for(int j=;j<m;j++)
out[i]+=choice[(i&)?-k:k][(j&)^ord[][i][k]];
}
else //C
{
for(int j=;j<m;j++)
for(int i=;i<n;i++)
out[i]+=choice[(j&)?-k:k][(i&)^ord[][j][k]];
}
for(int i=;i<n;i++) cout<<out[i]<<endl;
}
int main()
{
cin>>n>>m;
for(int i=;i<n;i++) cin>>str[i]; memset(cnt,,sizeof(cnt));
for(int i=;i<n;i++)
{
for(int k=;k<;k++)
{
int now1=, now2=;
for(int j=;j<m;j++)
{
now1+=(str[i][j]!=choice[(i&)?-k:k][j&]);
now2+=(str[i][j]!=choice[(i&)?-k:k][(j&)^]);
}
ord[][i][k]=now1<now2?:; //0正序,1逆序
cnt[][k]+=min(now1,now2);
}
}
for(int j=;j<m;j++)
{
for(int k=;k<;k++)
{
int now1=, now2=;
for(int i=;i<n;i++)
{
now1+=(str[i][j]!=choice[(j&)?-k:k][i&]);
now2+=(str[i][j]!=choice[(j&)?-k:k][(i&)^]);
}
ord[][j][k]=now1<now2?:; //0正序,1逆序
cnt[][k]+=min(now1,now2);
}
} int ans=0x3f3f3f3f,RC,K;
for(int rc=;rc<=;rc++)
{
for(int k=;k<;k++)
{
if(cnt[rc][k]<ans) ans=cnt[rc][k], RC=rc, K=k;
}
}
//cout<<ans<<endl;
print(RC,K);
}

(注:借鉴了https://www.luogu.org/blog/xht37/cf1098bcf1099e-nice-table的代码,到了这个点心态真的要放平……心态不稳代码基本不可能AC……)

CodeForces 1099E - Nice table - [好题]的更多相关文章

  1. Codeforces 417E Square Table(随机算法)

    题目链接:Codeforces 417E Square Table 题目大意:给出n和m.要求给出一个矩阵,要求每一列每一行的元素的平方总和是一个平方数. 解题思路:构造.依照 a a a b a a ...

  2. Codeforces#441 Div.2 四小题

    Codeforces#441 Div.2 四小题 链接 A. Trip For Meal 小熊维尼喜欢吃蜂蜜.他每天要在朋友家享用N次蜂蜜 , 朋友A到B家的距离是 a ,A到C家的距离是b ,B到C ...

  3. You Are Given a Decimal String... CodeForces - 1202B [简单dp][补题]

    补一下codeforces前天教育场的题.当时只A了一道题. 大致题意: 定义一个x - y - counter :是一个加法计数器.初始值为0,之后可以任意选择+x或者+y而我们由每次累加结果的最后 ...

  4. codeforces 1165F1/F2 二分好题

    Codeforces 1165F1/F2 二分好题 传送门:https://codeforces.com/contest/1165/problem/F2 题意: 有n种物品,你对于第i个物品,你需要买 ...

  5. Codeforces Codeforces Round #319 (Div. 2) A. Multiplication Table 水题

    A. Multiplication Table Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/57 ...

  6. Codeforces 675C Money Transfers 思维题

    原题:http://codeforces.com/contest/675/problem/C 让我们用数组a保存每个银行的余额,因为所有余额的和加起来一定为0,所以我们能把整个数组a划分为几个区间,每 ...

  7. Codeforces Gym 100531G Grave 水题

    Problem G. Grave 题目连接: http://codeforces.com/gym/100531/attachments Description Gerard develops a Ha ...

  8. Codeforces #662C Binary Table

    听说这是一道$ Tourist$现场没出的题 Codeforces #662C 题意: 给定$n*m的 01$矩阵,可以任意反转一行/列($0$变$1$,$1$变$0$),求最少$ 1$的数量 $ n ...

  9. Codeforces 1137D - Cooperative Game - [交互题+思维题]

    题目链接:https://codeforces.com/contest/1137/problem/D 题意: 交互题. 给定如下一个有向图: 现在十个人各有一枚棋子(编号 $0 \sim 9$),在不 ...

随机推荐

  1. 【Windows】Dos中的日期的和时间

    在Dos中,我们可以通过动态变量DATE来取得当前日期,通过动态变量TIME来取得当前日期.实例1 C:\Users\hubin5>echo %date% 2013/03/11 周一 C:\Us ...

  2. goland激活码

    http://idea.youbbs.org      

  3. 【iCore1S 双核心板_FPGA】例程三:计数器实验——计数器的使用

    实验现象: 程序下载成功后,程序中的计数器开始计数,每次计满后,计数器清零,三色LED中红色LED的状态反转.可以看到,红色LED以一定的时间间隔闪烁. 核心源代码: //-------------- ...

  4. Angularjs的那些事 – 视图的生命周期

    Angularjs的最主要的一个应用场景就是单页面应用(SinglePageApplication),但是SPA中会有一个明显的问题,在视图切换的时候,它只会用新视图去替换视图容器内的HTML,但如果 ...

  5. Java知多少(30)多态和动态绑定

    在Java中,父类的变量可以引用父类的实例,也可以引用子类的实例. 请大家先看一段代码: public class Demo { public static void main(String[] ar ...

  6. CentOS 添加 Oracle YUM 源

    最新文章:Virson's Blog 文章来自:Oracle 官方 YUM 源 Introduction The Oracle public yum server offers a free and ...

  7. Git -- 自定义git样式

    在安装Git一节中,我们已经配置了user.name和user.email,实际上,Git还有很多可配置项. 比如,让Git显示颜色,会让命令输出看起来更醒目: $ git config --glob ...

  8. linux 修改文件类型

    :Unix及类Unix系统里,每行结尾只有换行“\n”,Windows系统里面,每行结尾是换行+回车“\n\r”. 利用Linux下的vim编辑器,可以方便的在dos文件.unix文件之间进行切换,且 ...

  9. Nginx-nginx和反向代理

    使用版本:nginx-1.10.2(windows环境使用稳定版本) 下载地址:http://nginx.org 什么是nginx? Nginx (engine x) 是一款轻量级的Web 服务器 . ...

  10. ubuntu安装cocos2dx

    操作系统:ubuntu16.04 LTSIDE:Code::blocks 16.01Cocos2dx版本:cocos2d-x 3.11.1这篇随笔将会简要地演示如何在ubuntu下安装cocos2dx ...