CF1098B/CF1099E Nice table

题目地址:洛谷CF1098B

显然,a nice table需要满足如下条件:

要么,每行都由两个字符交替组成,相邻两行的字符均不相同

要么,每列都由两个字符交替组成,相邻两列的字符均不相同

然后枚举就完事啦

代码:

#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const string str = "ATGC";
vector<string> s, tmp, ans;

int main() {
    ios::sync_with_stdio(0);
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        string st;
        cin >> st;
        s.push_back(st);
    }
    int ansmin = INF, now;
    for (int c1 = 0; c1 < 4; c1++)
        for (int c2 = c1 + 1; c2 < 4; c2++) {
            string t;
            set<int> st;
            for (int i = 0; i < 4; i++) st.insert(i);
            st.erase(c1);
            st.erase(c2);
            set<int>::iterator it = st.begin();
            t.push_back(str[c1]);
            t.push_back(str[c2]);
            t.push_back(str[*(it++)]);
            t.push_back(str[*it]);
            now = 0;
            tmp.clear();
            for (int i = 0; i < n; i++) {
                tmp.push_back("");
                int b = (i & 1) << 1;
                int cnt1 = 0, cnt2 = 0;
                for (int j = 0; j < m; j++) {
                    if (s[i][j] != t[b+(j&1)]) ++cnt1;
                    if (s[i][j] != t[b+((j&1)^1)]) ++cnt2;
                }
                if (cnt1 < cnt2)
                    for (int j = 0; j < m; j++)
                        tmp[i].push_back(t[b+(j&1)]);
                else
                    for (int j = 0; j < m; j++)
                        tmp[i].push_back(t[b+((j&1)^1)]);
                now += min(cnt1, cnt2);
            }
            if (now < ansmin) {
                ans = tmp;
                ansmin = now;
            }
            now = 0;
            tmp.clear();
            for (int i = 0; i < n; i++) tmp.push_back("");
            for (int j = 0; j < m; j++) {
                int b = (j & 1) << 1;
                int cnt1 = 0, cnt2 = 0;
                for (int i = 0; i < n; i++) {
                    if (s[i][j] != t[b+(i&1)]) ++cnt1;
                    if (s[i][j] != t[b+((i&1)^1)]) ++cnt2;
                }
                if (cnt1 < cnt2)
                    for (int i = 0; i < n; i++)
                        tmp[i].push_back(t[b+(i&1)]);
                else
                    for (int i = 0; i < n; i++)
                        tmp[i].push_back(t[b+((i&1)^1)]);
                now += min(cnt1, cnt2);
            }
            if (now < ansmin) {
                ans = tmp;
                ansmin = now;
            }
        }
    for (int i = 0; i < n; i++) cout << ans[i] << endl;
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/xht37/p/10256962.html
今日推荐