text
stringlengths 49
983k
|
|---|
#include <bits/stdc++.h>
using namespace std;
const long double pai = acos(-1.0L);
const long double eps = 1e-10;
const long long mod = 1e9 + 7;
const int MXN = 1e6 + 5;
vector<char> a[MXN], ans[MXN], t[MXN];
char f[4] = {'A', 'G', 'C', 'T'};
int g[MXN], mi = INT_MAX, u, ss1, ss2;
int n, m;
void work(int s1, int s2, int uu) {
int ss[2][2] = {s1, s2, -1, -1};
for (int i = 0; i < 4; i++) {
if (ss[0][0] != i && ss[0][1] != i && ss[1][0] == -1)
ss[1][0] = i;
else {
if (ss[0][0] != i && ss[0][1] != i && ss[1][1] == -1) ss[1][1] = i;
}
}
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++) ss[i][j] = f[ss[i][j]];
int cnt = 0;
for (int i = 1; i <= n; i++) {
int c = 0, cc = 0;
for (int j = 1; j <= m; j++) c = c + (ss[i % 2][j % 2] != a[i][j]);
swap(ss[0][0], ss[0][1]);
swap(ss[1][1], ss[1][0]);
for (int j = 1; j <= m; j++) cc = cc + (ss[i % 2][j % 2] != a[i][j]);
swap(ss[0][0], ss[0][1]);
swap(ss[1][1], ss[1][0]);
cnt += min(c, cc);
}
if (cnt < mi) {
mi = cnt;
u = uu;
ss1 = s1, ss2 = s2;
}
}
int main() {
g['A'] = 0;
g['G'] = 1;
g['C'] = 2;
g['T'] = 3;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
a[i].push_back('*');
for (int j = 1; j <= m; j++) {
char x;
cin >> x;
a[i].push_back(x);
}
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++)
if (i != j) work(i, j, 0);
}
for (int i = 1; i <= m; i++) {
t[i].push_back('*');
for (int j = 1; j <= n; j++) {
t[i].push_back(a[j][i]);
}
}
for (int i = 1; i <= n; i++)
while ((int)a[i].size()) a[i].pop_back();
for (int i = 1; i <= m; i++) {
a[i].push_back('*');
for (int j = 1; j <= n; j++) {
a[i].push_back(t[i][j]);
}
}
swap(n, m);
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++)
if (i != j) work(i, j, 1);
}
swap(n, m);
int ss[2][2] = {ss1, ss2, -1, -1};
for (int i = 0; i < 4; i++) {
if (ss[0][0] != i && ss[0][1] != i && ss[1][0] == -1)
ss[1][0] = i;
else {
if (ss[0][0] != i && ss[0][1] != i && ss[1][1] == -1) ss[1][1] = i;
}
}
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++) ss[i][j] = f[ss[i][j]];
if (u == 0) {
for (int i = 1; i <= n; i++) {
int c = 0, cc = 0;
for (int j = 1; j <= m; j++) c = c + (ss[i % 2][j % 2] != a[j][i]);
swap(ss[0][0], ss[0][1]);
swap(ss[1][1], ss[1][0]);
for (int j = 1; j <= m; j++) cc = cc + (ss[i % 2][j % 2] != a[j][i]);
swap(ss[0][0], ss[0][1]);
swap(ss[1][1], ss[1][0]);
if (c < cc) {
for (int j = 1; j <= m; j++) cout << (char)ss[i % 2][j % 2];
} else {
swap(ss[0][0], ss[0][1]);
swap(ss[1][1], ss[1][0]);
for (int j = 1; j <= m; j++) cout << (char)ss[i % 2][j % 2];
swap(ss[0][0], ss[0][1]);
swap(ss[1][1], ss[1][0]);
}
puts("");
}
} else {
for (int i = 1; i <= m; i++) {
int c = 0, cc = 0;
for (int j = 1; j <= n; j++) c = c + (ss[i % 2][j % 2] != a[i][j]);
swap(ss[0][0], ss[0][1]);
swap(ss[1][1], ss[1][0]);
for (int j = 1; j <= n; j++) cc = cc + (ss[i % 2][j % 2] != a[i][j]);
swap(ss[0][0], ss[0][1]);
swap(ss[1][1], ss[1][0]);
if (c < cc) {
for (int j = 1; j <= n; j++) ans[j].push_back(ss[i % 2][j % 2]);
} else {
swap(ss[0][0], ss[0][1]);
swap(ss[1][1], ss[1][0]);
for (int j = 1; j <= n; j++) ans[j].push_back(ss[i % 2][j % 2]);
swap(ss[0][0], ss[0][1]);
swap(ss[1][1], ss[1][0]);
}
}
for (int i = 1; i <= n; i++) {
for (auto j : ans[i]) cout << j;
puts("");
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void base() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
vector<string> s, ans;
int n, m;
int counth(string ss, int prin) {
int total = 0;
for (int i = 0; i < n; ++i) {
int c1 = 0, c2 = 0;
for (int j = 0; j < m; ++j) {
if (~i & 1) {
if (~j & 1) {
c1 += (s[i][j] != ss[0]);
c2 += (s[i][j] != ss[1]);
} else {
c1 += (s[i][j] != ss[1]);
c2 += (s[i][j] != ss[0]);
}
} else {
if (~j & 1) {
c1 += (s[i][j] != ss[2]);
c2 += (s[i][j] != ss[3]);
} else {
c1 += (s[i][j] != ss[3]);
c2 += (s[i][j] != ss[2]);
}
}
}
total += min(c1, c2);
if (prin == 0) continue;
if (c1 < c2) {
if (~i & 1) {
for (int j = 0; j < m; ++j) {
if (~j & 1)
ans[i] += ss[0];
else
ans[i] += ss[1];
}
} else {
for (int j = 0; j < m; ++j) {
if (~j & 1)
ans[i] += ss[2];
else
ans[i] += ss[3];
}
}
} else {
if (~i & 1) {
for (int j = 0; j < m; ++j) {
if (~j & 1)
ans[i] += ss[1];
else
ans[i] += ss[0];
}
} else {
for (int j = 0; j < m; ++j) {
if (~j & 1)
ans[i] += ss[3];
else
ans[i] += ss[2];
}
}
}
}
return total;
}
int countv(string ss, int prin) {
int total = 0;
for (int j = 0; j < m; ++j) {
int c1 = 0, c2 = 0;
for (int i = 0; i < n; ++i) {
if (~j & 1) {
if (~i & 1) {
c1 += (s[i][j] != ss[0]);
c2 += (s[i][j] != ss[2]);
} else {
c1 += (s[i][j] != ss[2]);
c2 += (s[i][j] != ss[0]);
}
} else {
if (~i & 1) {
c1 += (s[i][j] != ss[1]);
c2 += (s[i][j] != ss[3]);
} else {
c1 += (s[i][j] != ss[3]);
c2 += (s[i][j] != ss[1]);
}
}
}
total += min(c1, c2);
if (prin == 0) continue;
if (c1 < c2) {
if (~j & 1) {
for (int i = 0; i < n; ++i) {
if (~i & 1)
ans[i] += ss[0];
else
ans[i] += ss[2];
}
} else {
for (int i = 0; i < n; ++i) {
if (~i & 1)
ans[i] += ss[1];
else
ans[i] += ss[3];
}
}
} else {
if (~j & 1) {
for (int i = 0; i < n; ++i) {
if (~i & 1)
ans[i] += ss[2];
else
ans[i] += ss[0];
}
} else {
for (int i = 0; i < n; ++i) {
if (~i & 1)
ans[i] += ss[3];
else
ans[i] += ss[1];
}
}
}
}
return total;
}
void solve() {
cin >> n >> m;
for (int i = 0; i < n; ++i) {
string ts;
cin >> ts;
s.push_back(ts);
ans.push_back("");
}
string ss = "AGCT";
int mins = 1e9;
string sm = ss;
int hor = 1;
for (int i = 0; i < ss.size(); ++i)
for (int j = 0; j < ss.size(); ++j)
for (int k = 0; k < ss.size(); ++k)
for (int l = 0; l < ss.size(); ++l) {
if (i == j || i == k || i == l || j == k || j == l || k == l)
continue;
string sc = "";
sc += ss[i];
sc += ss[j];
sc += ss[k];
sc += ss[l];
int x = counth(sc, 0);
int y = countv(sc, 0);
if (mins > x) {
mins = x;
sm = sc;
hor = 1;
}
if (mins > y) {
mins = y;
sm = sc;
hor = 0;
}
}
if (hor) {
counth(sm, 1);
} else {
countv(sm, 1);
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
cout << ans[i][j];
}
cout << "\n";
}
}
int main() {
base();
int t = 1;
while (t--) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<vector<char>> a, res;
int main() {
ios::sync_with_stdio(false);
cin >> n >> m;
a.assign(n + 10, vector<char>(m + 10));
res = a;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) cin >> a[i][j];
int ans = n * m;
for (int t1 = 1; t1 <= 4; t1++)
for (int t2 = 1; t2 <= 4; t2++)
for (int t3 = 1; t3 <= 4; t3++)
for (int t4 = 1; t4 <= 4; t4++) {
set<int> s;
s.insert({t1, t2, t3, t4});
if (s.size() < 4) continue;
map<char, int> id;
map<int, char> di;
id['A'] = t1, di[t1] = 'A';
id['G'] = t2, di[t2] = 'G';
id['C'] = t3, di[t3] = 'C';
id['T'] = t4, di[t4] = 'T';
int ans1 = 0, ans2 = 0;
vector<vector<char>> x(n + 10, vector<char>(m + 10)),
y(n + 10, vector<char>(m + 10));
for (int i = 0, a1 = 1, a2 = 2, a3 = 3, a4 = 4; i < n;
i++, swap(a1, a3), swap(a2, a4)) {
int now1 = 0, now2 = 0;
for (int j = 0, c1 = a1, c2 = a2; j < m; j++, swap(c1, c2))
now1 += id[a[i][j]] != c1, now2 += id[a[i][j]] != c2;
ans1 += min(now1, now2);
for (int j = 0, c1 = a1, c2 = a2; j < m; j++, swap(c1, c2))
x[i][j] = now1 < now2 ? di[c1] : di[c2];
}
for (int j = 0, a1 = 1, a2 = 2, a3 = 3, a4 = 4; j < m;
j++, swap(a1, a3), swap(a2, a4)) {
int now1 = 0, now2 = 0;
for (int i = 0, c1 = a1, c2 = a2; i < n; i++, swap(c1, c2))
now1 += id[a[i][j]] != c1, now2 += id[a[i][j]] != c2;
ans2 += min(now1, now2);
for (int i = 0, c1 = a1, c2 = a2; i < n; i++, swap(c1, c2))
y[i][j] = now1 < now2 ? di[c1] : di[c2];
}
if (min(ans1, ans2) < ans) {
if (ans1 < ans2)
ans = ans1, res = x;
else
ans = ans2, res = y;
}
}
for (int i = 0; i < n; i++, cout << '\n')
for (int j = 0; j < m; j++) cout << res[i][j];
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
char Order[] = {'A', 'C', 'G', 'T'};
pair<long long, vector<string> > ans;
long long n, m;
vector<string> s;
pair<long long, vector<string> > check1() {
pair<long long, vector<string> > ans;
string S[2][2];
for (long long i = 0; i < m; i++) {
S[0][0].push_back(Order[i % 2]);
S[0][1].push_back(Order[(i + 1) % 2]);
S[1][0].push_back(Order[2 + (i % 2)]);
S[1][1].push_back(Order[2 + ((i + 1) % 2)]);
}
for (long long i = 0; i < n; i++) {
long long cnt1 = 0, cnt2 = 0;
for (long long j = 0; j < m; j++) {
if (s[i][j] != S[i % 2][0][j]) cnt1++;
if (s[i][j] != S[i % 2][1][j]) cnt2++;
}
if (cnt1 <= cnt2) {
ans.second.push_back(S[i % 2][0]);
ans.first += cnt1;
} else {
ans.second.push_back(S[i % 2][1]);
ans.first += cnt2;
}
}
return ans;
}
pair<long long, vector<string> > check2() {
pair<long long, vector<string> > ans;
ans.second.resize(n);
string S[2][2];
for (long long i = 0; i < n; i++) {
S[0][0].push_back(Order[i % 2]);
S[0][1].push_back(Order[(i + 1) % 2]);
S[1][0].push_back(Order[2 + (i % 2)]);
S[1][1].push_back(Order[2 + ((i + 1) % 2)]);
}
for (long long i = 0; i < m; i++) {
long long cnt1 = 0, cnt2 = 0;
for (long long j = 0; j < n; j++) {
if (s[j][i] != S[i % 2][0][j]) cnt1++;
if (s[j][i] != S[i % 2][1][j]) cnt2++;
}
if (cnt1 <= cnt2) {
for (long long j = 0; j < n; j++) ans.second[j].push_back(S[i % 2][0][j]);
ans.first += cnt1;
} else {
for (long long j = 0; j < n; j++) ans.second[j].push_back(S[i % 2][1][j]);
ans.first += cnt2;
}
}
return ans;
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
s.resize(n);
for (long long i = 0; i < n; i++) {
cin >> s[i];
}
ans.first = n * m * 3;
pair<long long, vector<string> > tmp;
do {
if ((tmp = check1()) < ans) ans = tmp;
if ((tmp = check2()) < ans) ans = tmp;
} while (next_permutation(Order, Order + 4));
for (long long i = 0; i < n; i++) cout << ans.second[i] << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m;
string s[300005];
string q[300005];
string make(char a, char b) {
string res;
res += a;
res += b;
return res;
}
int sol(bool f, int mx) {
int ans = 1000000007;
string w = "ACGT";
do {
string a[2][2] = {{make(w[0], w[1]), make(w[1], w[0])},
{make(w[2], w[3]), make(w[3], w[2])}};
int cur = 0;
for (int i = 0; i < n; i++) {
int cnt[2] = {0, 0};
for (int j = 0; j < m; j++) {
for (int k = 0; k < 2; k++) {
cnt[k] += s[i][j] != a[i % 2][k][j % 2];
}
}
cur += min(cnt[0], cnt[1]);
}
ans = min(ans, cur);
if (f and mx == ans) {
for (int i = 0; i < n; i++) {
int cnt[2] = {0, 0};
for (int j = 0; j < m; j++) {
for (int k = 0; k < 2; k++) {
cnt[k] += s[i][j] != a[i % 2][k][j % 2];
}
}
if (cnt[0] < cnt[1]) {
for (int j = 0; j < m; j++) {
s[i][j] = a[i % 2][0][j % 2];
}
} else {
for (int j = 0; j < m; j++) {
s[i][j] = a[i % 2][1][j % 2];
}
}
}
return 1;
}
cur = 0;
for (int i = 0; i < m; i++) {
int cnt[2] = {0, 0};
for (int j = 0; j < n; j++) {
for (int k = 0; k < 2; k++) {
cnt[k] += s[j][i] != a[i % 2][k][j % 2];
}
}
cur += min(cnt[0], cnt[1]);
}
ans = min(ans, cur);
if (f and mx == ans) {
for (int i = 0; i < m; i++) {
int cnt[2] = {0, 0};
for (int j = 0; j < n; j++) {
for (int k = 0; k < 2; k++) {
cnt[k] += s[j][i] != a[i % 2][k][j % 2];
}
}
if (cnt[0] < cnt[1]) {
for (int j = 0; j < n; j++) {
s[j][i] = a[i % 2][0][j % 2];
}
} else {
for (int j = 0; j < n; j++) {
s[j][i] = a[i % 2][1][j % 2];
}
}
}
return 1;
}
} while (next_permutation((w).begin(), (w).end()));
return ans;
}
void solve() {
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> s[i], q[i] = s[i];
int ans = sol(0, 0);
sol(1, ans);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) ans -= q[i][j] != s[i][j];
assert(ans == 0);
for (int i = 0; i < n; i++) cout << s[i] << '\n';
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
while (t--) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
void splitstr(const string &s, vector<T> &out) {
istringstream in(s);
out.clear();
copy(istream_iterator<T>(in), istream_iterator<T>(), back_inserter(out));
}
template <class T>
T gcd(T a, T b) {
return b ? gcd(b, a % b) : a;
}
static void redirect(int argc, const char **argv) {
ios::sync_with_stdio(false);
cin.tie(NULL);
if (argc > 1) {
static filebuf f;
f.open(argv[1], ios::in);
cin.rdbuf(&f);
if (!cin) {
cerr << "Failed to open '" << argv[1] << "'" << endl;
exit(1);
}
}
if (argc > 2) {
static filebuf f;
f.open(argv[2], ios::out | ios::trunc);
cout.rdbuf(&f);
if (!cout) {
cerr << "Failed to open '" << argv[2] << "'" << endl;
}
}
}
static char chs[4] = {'A', 'C', 'G', 'T'};
static vector<string> transpose(const vector<string> &grid) {
int R = ((long long)(grid).size());
int C = ((long long)(grid[0]).size());
vector<string> out(C, string(R, '?'));
for (int i = 0; i < R; i++)
for (int j = 0; j < C; j++) out[j][i] = grid[i][j];
return out;
}
static int delta(const string &row1, const string &row2) {
int ans = 0;
for (int i = 0; i < ((long long)(row1).size()); i++)
ans += row1[i] != row2[i];
return ans;
}
static int delta(const vector<string> &grid1, const vector<string> &grid2) {
int ans = 0;
for (int i = 0; i < ((long long)(grid1).size()); i++)
ans += delta(grid1[i], grid2[i]);
return ans;
}
static pair<int, vector<string> > solve(const vector<string> &grid) {
int R = ((long long)(grid).size());
int C = ((long long)(grid[0]).size());
pair<int, vector<string> > ans;
ans.first = INT_MAX;
for (int i = 0; i < 4; i++)
for (int j = i + 1; j < 4; j++) {
char ch[4];
ch[0] = chs[i];
ch[1] = chs[j];
int k = 0;
while (k == i || k == j) k++;
ch[2] = chs[k++];
while (k == i || k == j) k++;
ch[3] = chs[k];
vector<string> cand(R);
for (int r = 0; r < R; r++) {
int f = (r & 1) ? 2 : 0;
string row[2];
for (int c = 0; c < C; c++) {
row[0] += ch[f + c % 2];
row[1] += ch[f + (c + 1) % 2];
}
if (delta(grid[r], row[1]) < delta(grid[r], row[0]))
cand[r] = move(row[1]);
else
cand[r] = move(row[0]);
}
int score = delta(grid, cand);
if (score < ans.first) {
ans.first = score;
ans.second = cand;
}
}
return ans;
}
int main(int argc, const char **argv) {
redirect(argc, argv);
int R, C;
cin >> R >> C;
vector<string> grid(R);
for (int i = 0; i < R; i++) cin >> grid[i];
pair<int, vector<string> > ans = solve(grid);
grid = transpose(grid);
pair<int, vector<string> > ans2 = solve(grid);
if (ans2.first < ans.first) {
ans.first = ans2.first;
ans.second = transpose(ans2.second);
}
for (int i = 0; i < R; i++) cout << ans.second[i] << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9 + 2;
const long long MOD = 998244353;
const long long MAXN = 1000006;
long long M, N;
vector<string> Board;
char Letras[] = "ACGT";
char Even[2];
char Odd[2];
vector<string> BoardTest;
vector<string> BoardAns;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> N >> M;
string s;
for (int i = 0; i < int(N); i++) {
cin >> s;
Board.push_back(s);
BoardTest.push_back(s);
BoardAns.push_back(s);
}
long long ans = INF;
for (int i = 0; i < int(4); i++)
for (int j = i + 1; j < 4; j++) {
Even[0] = Letras[i];
Even[1] = Letras[j];
int oddCtr = 0;
for (int k = 0; k < int(4); k++)
if (Letras[k] != Even[0] and Letras[k] != Even[1])
Odd[oddCtr++] = Letras[k];
long long actAns = 0;
for (int f = 0; f < int(N); f++) {
int bestOff = 0;
int bestDiff = INF;
if (f % 2) {
for (int offset = 0; offset < int(2); offset++) {
int diff = 0;
for (int pos = 0; pos < int(M); pos++)
if (Board[f][pos] != Even[(pos + offset) % 2]) diff++;
if (diff < bestDiff) {
bestOff = offset;
bestDiff = diff;
}
}
for (int pos = 0; pos < int(M); pos++)
BoardTest[f][pos] = Even[(pos + bestOff) % 2];
} else {
for (int offset = 0; offset < int(2); offset++) {
int diff = 0;
for (int pos = 0; pos < int(M); pos++)
if (Board[f][pos] != Odd[(pos + offset) % 2]) diff++;
if (diff < bestDiff) {
bestOff = offset;
bestDiff = diff;
}
}
for (int pos = 0; pos < int(M); pos++)
BoardTest[f][pos] = Odd[(pos + bestOff) % 2];
}
actAns += bestDiff;
}
if (actAns < ans) {
ans = actAns;
BoardAns = BoardTest;
}
ans = min(actAns, ans);
actAns = 0;
for (int c = 0; c < int(M); c++) {
int bestOff = 0;
int bestDiff = INF;
if (c % 2) {
for (int offset = 0; offset < int(2); offset++) {
int diff = 0;
for (int pos = 0; pos < int(N); pos++)
if (Board[pos][c] != Even[(pos + offset) % 2]) diff++;
if (diff < bestDiff) {
bestOff = offset;
bestDiff = diff;
}
}
for (int pos = 0; pos < int(N); pos++)
BoardTest[pos][c] = Even[(pos + bestOff) % 2];
} else {
for (int offset = 0; offset < int(2); offset++) {
int diff = 0;
for (int pos = 0; pos < int(N); pos++)
if (Board[pos][c] != Odd[(pos + offset) % 2]) diff++;
if (diff < bestDiff) {
bestOff = offset;
bestDiff = diff;
}
}
for (int pos = 0; pos < int(N); pos++)
BoardTest[pos][c] = Odd[(pos + bestOff) % 2];
}
actAns += bestDiff;
}
if (actAns < ans) {
ans = actAns;
BoardAns = BoardTest;
}
}
for (int i = 0; i < int(N); i++) cout << BoardAns[i] << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const signed inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double pi = acos(-1.0);
template <class T>
int chkmin(T &a, T b) {
return a > b ? a = b, 1 : 0;
}
template <class T>
int chkmax(T &a, T b) {
return a < b ? a = b, 1 : 0;
}
template <class T>
T sqr(T a) {
return a * a;
}
template <class T>
T mmin(T a, T b) {
return a < b ? a : b;
}
template <class T>
T mmax(T a, T b) {
return a > b ? a : b;
}
template <class T>
T aabs(T a) {
return a < 0 ? -a : a;
}
template <class T>
int dcmp(T a, T b) {
return a > b;
}
template <int *a>
int cmp_a(int first, int second) {
return a[first] < a[second];
}
namespace io {
const int SIZE = (1 << 21) + 1;
char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c,
qu[55];
int f, qr;
inline void flush() {
fwrite(obuf, 1, oS - obuf, stdout);
oS = obuf;
}
inline void putc(char first) {
*oS++ = first;
if (oS == oT) flush();
}
inline bool read(signed &first) {
for (f = 1, c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++);
c < '0' || c > '9';
c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++))
if (c == '-')
f = -1;
else if (c == EOF)
return 0;
for (first = 0; c <= '9' && c >= '0';
c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++))
first = first * 10 + (c & 15);
first *= f;
return 1;
}
inline bool read(long long &first) {
for (f = 1, c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++);
c < '0' || c > '9';
c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++))
if (c == '-')
f = -1;
else if (c == EOF)
return 0;
for (first = 0; c <= '9' && c >= '0';
c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++))
first = first * 10 + (c & 15);
first *= f;
return 1;
}
inline bool read(char &first) {
while ((first = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++)) == ' ' ||
first == '\n' || first == '\r')
;
return first != EOF;
}
inline bool read(char *first) {
while ((*first = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++)) == '\n' ||
*first == ' ' || *first == '\r')
if (*first == EOF) return 0;
while (!(*first == '\n' || *first == ' ' || *first == '\r'))
*(++first) = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++);
*first = 0;
return 1;
}
template <typename A, typename... B>
inline bool read(A &first, B &...second) {
return read(first) && read(second...);
}
inline bool write(signed first) {
if (!first) putc('0');
if (first < 0) putc('-'), first = -first;
while (first) qu[++qr] = first % 10 + '0', first /= 10;
while (qr) putc(qu[qr--]);
return 0;
}
inline bool write(long long first) {
if (!first) putc('0');
if (first < 0) putc('-'), first = -first;
while (first) qu[++qr] = first % 10 + '0', first /= 10;
while (qr) putc(qu[qr--]);
return 0;
}
inline bool write(char first) {
putc(first);
return 0;
}
inline bool write(const char *first) {
while (*first) {
putc(*first);
++first;
}
return 0;
}
inline bool write(char *first) {
while (*first) {
putc(*first);
++first;
}
return 0;
}
template <typename A, typename... B>
inline bool write(A first, B... second) {
return write(first) || write(second...);
}
struct Flusher_ {
~Flusher_() { flush(); }
} io_flusher_;
} // namespace io
using io ::putc;
using io ::read;
using io ::write;
char s[600005], t[600005], mx[600005];
int ans;
int n, m;
char a[10] = "ACGT";
void chk() {
int cnt = 0;
for (int i = 0; i < n; ++i) {
int c0 = 0, c1 = 0;
for (int j = 0; j < m; ++j) {
if (a[(i & 1) << 1 | (j & 1)] == s[i * m + j]) ++c0;
if (a[(i & 1) << 1 | (!(j & 1))] == s[i * m + j]) ++c1;
}
for (int j = 0; j < m; ++j)
t[i * m + j] = a[(i & 1) << 1 | ((j & 1) ^ (c0 < c1))];
}
for (int i = 0; i < n * m; ++i)
if (s[i] != t[i]) ++cnt;
if (cnt < ans) {
for (int i = 0; i < n * m; ++i) mx[i] = t[i];
ans = cnt;
}
cnt = 0;
for (int j = 0; j < m; ++j) {
int c0 = 0, c1 = 0;
for (int i = 0; i < n; ++i) {
if (a[(j & 1) << 1 | (i & 1)] == s[i * m + j]) ++c0;
if (a[(j & 1) << 1 | (!(i & 1))] == s[i * m + j]) ++c1;
}
for (int i = 0; i < n; ++i)
t[i * m + j] = a[(j & 1) << 1 | ((i & 1) ^ (c0 < c1))];
}
for (int i = 0; i < n * m; ++i)
if (s[i] != t[i]) ++cnt;
if (cnt < ans) {
for (int i = 0; i < n * m; ++i) mx[i] = t[i];
ans = cnt;
}
}
int main() {
cin >> n >> m;
ans = n * m;
for (int i = 0; i < n * m; ++i) read(s[i]);
for (int i = 1; i <= n; ++i) cin >> s[i];
do {
chk();
} while (next_permutation(a, a + 4));
for (int i = 0; i < n; ++i, write('\n'))
for (int j = 0; j < m; ++j) write(mx[i * m + j]);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int INFL = (int)1e9;
const long long int INFLL = (long long int)1e18;
const double INFD = numeric_limits<double>::infinity();
const double PI = 3.14159265358979323846;
bool nearlyeq(double x, double y) { return abs(x - y) < 1e-9; }
bool inrange(int x, int t) { return x >= 0 && x < t; }
long long int rndf(double x) {
return (long long int)(x + (x >= 0 ? 0.5 : -0.5));
}
long long int floorsqrt(double x) {
long long int m = (long long int)sqrt(x);
return m + (m * m <= (long long int)(x) ? 0 : -1);
}
long long int ceilsqrt(double x) {
long long int m = (long long int)sqrt(x);
return m + ((long long int)x <= m * m ? 0 : 1);
}
long long int rnddiv(long long int a, long long int b) {
return (a / b + (a % b * 2 >= b ? 1 : 0));
}
long long int ceildiv(long long int a, long long int b) {
return (a / b + (a % b == 0 ? 0 : 1));
}
long long int gcd(long long int m, long long int n) {
if (n == 0)
return m;
else
return gcd(n, m % n);
}
vector<vector<string>> patterns = {{"AG", "CT"}, {"AC", "GT"}, {"AT", "GC"},
{"GC", "AT"}, {"GT", "AC"}, {"CT", "AG"}};
int ans_cnt = INT_MAX;
vector<vector<char>> ans;
void solve(const vector<string> &board, int n, int m, bool t) {
for (int k = 0; k < (int)(patterns.size()); k++) {
int cnt_buf = 0;
vector<vector<char>> buf(n, vector<char>(m));
for (int j = 0; j < (int)(m); j++) {
vector<int> cnt(2);
for (int i = 0; i < (int)(n); i++) {
if (patterns[k][j % 2][i % 2] != board[i][j]) cnt[0]++;
if (patterns[k][j % 2][(i + 1) % 2] != board[i][j]) cnt[1]++;
}
if (cnt[0] < cnt[1]) {
for (int i = 0; i < (int)(n); i++)
buf[i][j] = patterns[k][j % 2][i % 2];
cnt_buf += cnt[0];
} else {
for (int i = 0; i < (int)(n); i++)
buf[i][j] = patterns[k][j % 2][(i + 1) % 2];
cnt_buf += cnt[1];
}
}
if (cnt_buf < ans_cnt) {
ans_cnt = cnt_buf;
if (t) {
for (int i = 0; i < (int)(m); i++) {
for (int j = 0; j < (int)(n); j++) {
ans[i][j] = buf[j][i];
}
}
} else
ans = buf;
}
}
}
int main() {
int n, m;
cin >> n >> m;
vector<string> board(n);
for (int i = 0; i < (int)(n); i++) cin >> board[i];
ans = vector<vector<char>>(n, vector<char>(m));
solve(board, n, m, false);
vector<string> tboard(m, "");
for (int i = 0; i < (int)(m); i++) {
for (int j = 0; j < (int)(n); j++) {
tboard[i] += board[j][i];
}
}
solve(tboard, m, n, true);
for (int i = 0; i < (int)(n); i++) {
for (int j = 0; j < (int)(m); j++) {
cout << ans[i][j];
}
cout << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
const long long MAX = 3e5 + 9;
const long long INF = 1e12;
string X[MAX];
char arr[] = {'A', 'G', 'C', 'T'};
int N, M, res, cnt[2][300009][30], cnt2[2][300009][30];
int check1(int f, int s, int ff, int ss) {
int res = 0;
for (int A = 0; A < M; A++) {
if (A % 2 == 0)
res += max(cnt[0][A][f - 'A'] + cnt[1][A][s - 'A'],
cnt[0][A][s - 'A'] + cnt[1][A][f - 'A']);
else
res += max(cnt[0][A][ff - 'A'] + cnt[1][A][ss - 'A'],
cnt[0][A][ss - 'A'] + cnt[1][A][ff - 'A']);
}
return N * M - res;
}
int check2(int f, int s, int ff, int ss) {
int res = 0;
for (int A = 0; A < N; A++) {
if (A % 2 == 0)
res += max(cnt2[0][A][f - 'A'] + cnt2[1][A][s - 'A'],
cnt2[0][A][s - 'A'] + cnt2[1][A][f - 'A']);
else
res += max(cnt2[0][A][ff - 'A'] + cnt2[1][A][ss - 'A'],
cnt2[0][A][ss - 'A'] + cnt2[1][A][ff - 'A']);
}
return N * M - res;
}
void print1(int f, int s, int ff, int ss) {
vector<char> res1, res2;
for (int A = 0; A < M; A++) {
if (A % 2 == 0) {
if (cnt[0][A][f - 'A'] + cnt[1][A][s - 'A'] >
cnt[0][A][s - 'A'] + cnt[1][A][f - 'A'])
res1.push_back(f), res2.push_back(s);
else
res1.push_back(s), res2.push_back(f);
} else {
if (cnt[0][A][ff - 'A'] + cnt[1][A][ss - 'A'] >
cnt[0][A][ss - 'A'] + cnt[1][A][ff - 'A'])
res1.push_back(ff), res2.push_back(ss);
else
res1.push_back(ss), res2.push_back(ff);
}
}
for (int A = 0; A < N; A++) {
if (A % 2 == 0)
for (auto A : res1) cout << A;
else
for (auto A : res2) cout << A;
cout << "\n";
}
exit(0);
return;
}
void print2(int f, int s, int ff, int ss) {
for (int A = 0; A < N; A++) {
if (A % 2 == 0) {
if (cnt2[0][A][f - 'A'] + cnt2[1][A][s - 'A'] <
cnt2[0][A][s - 'A'] + cnt2[1][A][f - 'A'])
swap(f, s);
for (int B = 0; B < M; B++) cout << (B % 2 == 0 ? (char)f : (char)s);
} else {
if (cnt2[0][A][ff - 'A'] + cnt2[1][A][ss - 'A'] <
cnt2[0][A][ss - 'A'] + cnt2[1][A][ff - 'A'])
swap(ff, ss);
for (int B = 0; B < M; B++) cout << (B % 2 == 0 ? (char)ff : (char)ss);
}
cout << "\n";
}
exit(0);
return;
}
signed main() {
ios_base::sync_with_stdio(false);
res = MAX;
cin >> N >> M;
for (int A = 0; A < N; A++) {
cin >> X[A];
for (int B = 0; B < M; B++)
cnt[A % 2][B][X[A][B] - 'A']++, cnt2[B % 2][A][X[A][B] - 'A']++;
}
for (int A = 0; A < 4; A++) {
for (int B = 0; B < 4; B++) {
if (B == A) continue;
for (int C = 0; C < 4; C++) {
if (C == B or C == A) continue;
for (int D = 0; D < 4; D++) {
if (D == C or D == B or D == A) continue;
res = min(res, check1(arr[A], arr[B], arr[C], arr[D]));
res = min(res, check2(arr[A], arr[B], arr[C], arr[D]));
}
}
}
}
for (int A = 0; A < 4; A++) {
for (int B = 0; B < 4; B++) {
if (B == A) continue;
for (int C = 0; C < 4; C++) {
if (C == B or C == A) continue;
for (int D = 0; D < 4; D++) {
if (D == C or D == B or D == A) continue;
if (check1(arr[A], arr[B], arr[C], arr[D]) == res)
print1(arr[A], arr[B], arr[C], arr[D]);
if (check2(arr[A], arr[B], arr[C], arr[D]) == res)
print2(arr[A], arr[B], arr[C], arr[D]);
}
}
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXn = 3e5 + 10;
string s = "ACGT";
vector<char> ar[MAXn];
int n, m, ans;
string anss;
bool flag;
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> m;
ans = n * m + 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
char ch;
cin >> ch;
ar[i].push_back(ch);
}
}
do {
long long ansq = 0;
int a = 0, b = 2;
for (int i = 0; i < n; i++) {
if (i % 2) {
int ansqq = 0, ansqq2 = 0;
b = 2;
for (int j = 0; j < m; j++) {
if (ar[i][j] != s[b]) {
ansqq++;
}
b++;
if (b == 4) b = 2;
}
b = 3;
for (int j = 0; j < m; j++) {
if (ar[i][j] != s[b]) {
ansqq2++;
}
b++;
if (b == 4) b = 2;
}
ansq += min(ansqq, ansqq2);
} else {
a = 0;
int ansqq = 0, ansqq2 = 0;
for (int j = 0; j < m; j++) {
if (ar[i][j] != s[a]) {
ansqq++;
}
a++;
if (a == 2) a = 0;
}
a = 1;
for (int j = 0; j < m; j++) {
if (ar[i][j] != s[a]) {
ansqq2++;
}
a++;
if (a == 2) a = 0;
}
ansq += min(ansqq, ansqq2);
}
}
if (ansq < ans) {
anss = s;
ans = ansq;
flag = false;
}
ansq = 0;
a = 0;
b = 2;
for (int i = 0; i < m; i++) {
if (i % 2) {
int ansqq = 0, ansqq2 = 0;
b = 2;
for (int j = 0; j < n; j++) {
if (ar[j][i] != s[b]) {
ansqq++;
}
b++;
if (b == 4) b = 2;
}
b = 3;
for (int j = 0; j < n; j++) {
if (ar[j][i] != s[b]) {
ansqq2++;
}
b++;
if (b == 4) b = 2;
}
ansq += min(ansqq, ansqq2);
} else {
a = 0;
int ansqq = 0, ansqq2 = 0;
for (int j = 0; j < n; j++) {
if (ar[j][i] != s[a]) {
ansqq++;
}
a++;
if (a == 2) a = 0;
}
a = 1;
for (int j = 0; j < n; j++) {
if (ar[j][i] != s[a]) {
ansqq2++;
}
a++;
if (a == 2) a = 0;
}
ansq += min(ansqq, ansqq2);
}
}
if (ansq < ans) {
anss = s;
ans = ansq;
flag = true;
}
} while (next_permutation(s.begin(), s.end()));
if (flag) {
s = anss;
int a = 0, b = 2;
for (int i = 0; i < m; i++) {
if (i % 2) {
int ansqq = 0, ansqq2 = 0;
b = 2;
for (int j = 0; j < n; j++) {
if (ar[j][i] != s[b]) {
ansqq++;
}
b++;
if (b == 4) b = 2;
}
b = 3;
for (int j = 0; j < n; j++) {
if (ar[j][i] != s[b]) {
ansqq2++;
}
b++;
if (b == 4) b = 2;
}
if (ansqq < ansqq2) {
b = 2;
for (int j = 0; j < n; j++) {
ar[j][i] = s[b];
b++;
if (b == 4) b = 2;
}
} else {
b = 3;
for (int j = 0; j < n; j++) {
ar[j][i] = s[b];
b++;
if (b == 4) b = 2;
}
}
} else {
a = 0;
int ansqq = 0, ansqq2 = 0;
for (int j = 0; j < n; j++) {
if (ar[j][i] != s[a]) {
ansqq++;
}
a++;
if (a == 2) a = 0;
}
a = 1;
for (int j = 0; j < n; j++) {
if (ar[j][i] != s[a]) {
ansqq2++;
}
a++;
if (a == 2) a = 0;
}
if (ansqq < ansqq2) {
a = 0;
for (int j = 0; j < n; j++) {
ar[j][i] = s[a];
a++;
if (a == 2) a = 0;
}
} else {
a = 1;
for (int j = 0; j < n; j++) {
ar[j][i] = s[a];
a++;
if (a == 2) a = 0;
}
}
}
}
} else {
s = anss;
int a = 0, b = 2;
for (int i = 0; i < n; i++) {
if (i % 2) {
int ansqq = 0, ansqq2 = 0;
b = 2;
for (int j = 0; j < m; j++) {
if (ar[i][j] != s[b]) {
ansqq++;
}
b++;
if (b == 4) b = 2;
}
b = 3;
for (int j = 0; j < m; j++) {
if (ar[i][j] != s[b]) {
ansqq2++;
}
b++;
if (b == 4) b = 2;
}
if (ansqq < ansqq2) {
b = 2;
for (int j = 0; j < m; j++) {
ar[i][j] = s[b];
b++;
if (b == 4) b = 2;
}
} else {
b = 3;
for (int j = 0; j < m; j++) {
ar[i][j] = s[b];
b++;
if (b == 4) b = 2;
}
}
} else {
a = 0;
int ansqq = 0, ansqq2 = 0;
for (int j = 0; j < m; j++) {
if (ar[i][j] != s[a]) {
ansqq++;
}
a++;
if (a == 2) a = 0;
}
a = 1;
for (int j = 0; j < m; j++) {
if (ar[i][j] != s[a]) {
ansqq2++;
}
a++;
if (a == 2) a = 0;
}
if (ansqq < ansqq2) {
a = 0;
for (int j = 0; j < m; j++) {
ar[i][j] = s[a];
a++;
if (a == 2) a = 0;
}
} else {
a = 1;
for (int j = 0; j < m; j++) {
ar[i][j] = s[a];
a++;
if (a == 2) a = 0;
}
}
}
}
}
for (int i = 0; i < n; i++, cout << endl)
for (int j = 0; j < m; j++) cout << ar[i][j];
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
int n, m;
char s[N];
vector<int> a[N], b[N], c[N], d[N];
int calc(int c1, int c2, int n, int m, vector<int> *a) {
int c3, c4, res = 0;
for (int i = (1); i <= (4); ++i)
if (i != c1 && i != c2) {
c3 = i;
break;
}
for (int i = (1); i <= (4); ++i)
if (i != c1 && i != c2 && i != c3) {
c4 = i;
break;
}
for (int i = (1); i <= (n); ++i) {
int t1, t2, mn = m + 1, tmp = 0;
if (i & 1)
t1 = c1, t2 = c2;
else
t1 = c3, t2 = c4;
for (int j = (1); j <= (m); ++j) {
if ((j & 1) && a[i][j] != t1) ++tmp;
if (((j & 1) ^ 1) && a[i][j] != t2) ++tmp;
}
mn = min(mn, tmp), swap(t1, t2), tmp = 0;
for (int j = (1); j <= (m); ++j) {
if ((j & 1) && a[i][j] != t1) ++tmp;
if (((j & 1) ^ 1) && a[i][j] != t2) ++tmp;
}
mn = min(mn, tmp), res += mn;
}
return res;
}
void otp(int c1, int c2, int n, int m, vector<int> *a, vector<int> *b) {
int c3, c4;
for (int i = (1); i <= (4); ++i)
if (i != c1 && i != c2) {
c3 = i;
break;
}
for (int i = (1); i <= (4); ++i)
if (i != c1 && i != c2 && i != c3) {
c4 = i;
break;
}
for (int i = (1); i <= (n); ++i) {
int t1, t2, mn = m + 1, tmp = 0;
if (i & 1)
t1 = c1, t2 = c2;
else
t1 = c3, t2 = c4;
for (int j = (1); j <= (m); ++j) {
if ((j & 1) && a[i][j] != t1) ++tmp;
if (((j & 1) ^ 1) && a[i][j] != t2) ++tmp;
}
b[i].push_back(0);
for (int j = (1); j <= (m); ++j) b[i].push_back((j & 1) ? t1 : t2);
mn = tmp, swap(t1, t2), tmp = 0;
for (int j = (1); j <= (m); ++j) {
if ((j & 1) && a[i][j] != t1) ++tmp;
if (((j & 1) ^ 1) && a[i][j] != t2) ++tmp;
}
if (tmp < mn)
for (int j = (1); j <= (m); ++j) b[i][j] = ((j & 1) ? t1 : t2);
}
}
int main() {
scanf("%d%d", &n, &m);
for (int i = (1); i <= (n); ++i) {
scanf("%s", s + 1);
a[i].push_back(0);
for (int j = (1); j <= (m); ++j) {
if (s[j] == 'A') a[i].push_back(1);
if (s[j] == 'C') a[i].push_back(2);
if (s[j] == 'G') a[i].push_back(3);
if (s[j] == 'T') a[i].push_back(4);
}
}
for (int i = (1); i <= (m); ++i) {
b[i].push_back(0);
for (int j = (1); j <= (n); ++j) b[i].push_back(a[j][i]);
}
int ans1 = n * m + 1, ans2 = n * m + 1, c1, c2, d1, d2;
for (int i = (1); i <= (4); ++i)
for (int j = (1); j <= (i - 1); ++j) {
int tmp = calc(i, j, n, m, a);
if (tmp < ans1) ans1 = tmp, c1 = i, c2 = j;
}
for (int i = (1); i <= (4); ++i)
for (int j = (1); j <= (i - 1); ++j) {
int tmp = calc(i, j, m, n, b);
if (tmp < ans2) ans2 = tmp, d1 = i, d2 = j;
}
if (ans1 < ans2)
otp(c1, c2, n, m, a, d);
else {
otp(d1, d2, m, n, b, c);
for (int i = (1); i <= (n); ++i) {
d[i].push_back(0);
for (int j = (1); j <= (m); ++j) d[i].push_back(c[j][i]);
}
}
for (int i = (1); i <= (n); ++i) {
for (int j = (1); j <= (m); ++j) {
if (d[i][j] == 1) putchar('A');
if (d[i][j] == 2) putchar('C');
if (d[i][j] == 3) putchar('G');
if (d[i][j] == 4) putchar('T');
}
puts("");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
pair<int, vector<vector<char> > > solve(vector<vector<char> > mat) {
int n = mat.size(), m = mat[0].size();
vector<char> g = {'A', 'C', 'G', 'T'};
vector<vector<char> > best = mat;
int bestans = 1000000;
do {
vector<vector<char> > cur = mat;
int ans = 0;
for (int i = 0; i < n; i++) {
int a = 0, b = 0;
for (int j = 0; j < m; j++) {
if (i & 1) {
if (mat[i][j] != g[j & 1]) a++;
if (mat[i][j] != g[(j & 1) ^ 1]) b++;
} else {
if (mat[i][j] != g[2 + (j & 1)]) a++;
if (mat[i][j] != g[2 + ((j & 1) ^ 1)]) b++;
}
}
if (a < b) {
ans += a;
for (int j = 0; j < m; j++) {
if (i & 1) {
cur[i][j] = g[j & 1];
} else {
cur[i][j] = g[2 + (j & 1)];
}
}
} else {
ans += b;
for (int j = 0; j < m; j++) {
if (i & 1) {
cur[i][j] = g[(j & 1) ^ 1];
} else {
cur[i][j] = g[2 + ((j & 1) ^ 1)];
}
}
}
}
if (ans < bestans) best.swap(cur), bestans = ans;
} while (next_permutation(g.begin(), g.end()));
return {bestans, best};
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
vector<vector<char> > mat(n, vector<char>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> mat[i][j];
}
}
pair<int, vector<vector<char> > > ans1 = solve(mat);
vector<vector<char> > tmat(m, vector<char>(n));
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
tmat[i][j] = mat[j][i];
}
}
pair<int, vector<vector<char> > > ans2 = solve(tmat);
if (ans1.first < ans2.first) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << ans1.second[i][j];
}
cout << "\n";
}
} else {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << ans2.second[j][i];
}
cout << "\n";
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
long long mult(long long a, long long b, long long p = 1000000007) {
long long ans = ((a % p) * (b % p)) % p;
return (ans + p) % p;
}
long long add(long long a, long long b, long long p = 1000000007) {
long long ans = (a % p + b % p) % p;
return (ans + p) % p;
}
long long fpow(long long n, long long k, long long p = 1000000007) {
long long r = 1;
for (; k; k >>= 1) {
if (k & 1) r = r * n % p;
n = n * n % p;
}
return r;
}
long long inv(long long a, long long p = 1000000007) {
return fpow(a, p - 2, p);
}
const int N = 3e5 + 10, inf = 1e9;
vector<string> inp(N);
vector<string> transposeinp(N);
vector<string> place1(N);
vector<string> place2(N);
void transpose(int n, int m) {
for (int i = 0; i < m; i++) transposeinp[i].resize(n);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) transposeinp[j][i] = inp[i][j];
}
int fillandcomp(string &ip, string &dup, char A, char G, int b) {
int ans = 0;
for (int i = 0; i < b; i++) {
dup[i] = ((i & 1) == 0) ? A : G;
ans += (ip[i] == dup[i]) ? 0 : 1;
}
return ans;
}
int solve(vector<string> &ip, vector<string> &dup, int a, int b,
string letters) {
char A = letters[0], G = letters[1];
char C = letters[2], T = letters[3];
int ans = 0;
for (int i = 0; i < a; i++) {
swap(A, C);
swap(G, T);
int w1 = fillandcomp(ip[i], dup[i], A, G, b);
int w2 = fillandcomp(ip[i], dup[i], G, A, b);
if (w1 < w2) fillandcomp(ip[i], dup[i], A, G, b);
ans += min(w1, w2);
}
return ans;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> inp[i];
transpose(n, m);
for (int i = 0; i < n; i++) place1[i].resize(m);
for (int i = 0; i < m; i++) place2[i].resize(n);
string letters = "AGCT";
string lseq;
int ans = INT_MAX, tr = -1;
do {
int tempans = solve(inp, place1, n, m, letters);
if (tempans < ans) {
lseq = letters;
tr = 0;
ans = tempans;
}
tempans = solve(transposeinp, place2, m, n, letters);
if (tempans < ans) {
lseq = letters;
tr = 1;
ans = tempans;
}
} while (next_permutation(letters.begin(), letters.end()));
if (!tr) {
solve(inp, place1, n, m, lseq);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) cout << place1[i][j];
cout << endl;
}
} else {
solve(transposeinp, place2, m, n, lseq);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) cout << place2[j][i];
cout << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline void setin(string s) { freopen(s.c_str(), "r", stdin); }
inline void setout(string s) { freopen(s.c_str(), "w", stdout); }
template <typename T>
inline T range(T l, T r) {
return uniform_int_distribution<T>(l, r)(rng);
}
template <typename T>
void Min(T &a, T b) {
a = min(a, b);
}
template <typename T>
void Max(T &a, T b) {
a = max(a, b);
}
const int inf = 2e9;
const int mod = 1e9 + 7;
const int N = 3e5 + 15;
int n, m;
inline char rev_gt(int x) {
if (x == 1) return 'A';
if (x == 2) return 'T';
if (x == 3) return 'G';
return 'C';
}
inline int gt(char c) {
if (c == 'A') return 1;
if (c == 'T') return 2;
if (c == 'G') return 3;
return 4;
}
vector<vector<int>> ans, a, b;
int val = inf;
bool flag;
vector<vector<int>> transpose(const vector<vector<int>> &a) {
vector<vector<int>> nw(a[0].size(), vector<int>(a.size(), 0));
for (int i = 0; i < a.size(); ++i)
for (int j = 0; j < a[i].size(); ++j) nw[j][i] = a[i][j];
return nw;
}
void solve(bool rev = false) {
vector<int> ord = {1, 2, 3, 4};
do {
b[1][1] = ord[0], b[1][2] = ord[1], b[2][1] = ord[2], b[2][2] = ord[3];
for (int i = 3; i <= n; ++i)
for (int j = 1; j <= 2; ++j) b[i][j] = b[i - 2][j];
for (int j = 3; j <= m; ++j) {
pair<int, int> val(0, 0);
vector<int> is = {};
if (j & 1)
is = {ord[0], ord[2]};
else
is = {ord[1], ord[3]};
for (int i = 1; i <= n; ++i) {
val.first += is[i & 1] != a[i][j];
val.second += is[(i & 1) ^ 1] != a[i][j];
}
for (int i = 1; i <= n; ++i)
b[i][j] = (val.first < val.second ? is[i & 1] : is[(i & 1) ^ 1]);
}
int cur = 0;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j) cur += a[i][j] != b[i][j];
if (cur < val) ans = b, val = cur, flag = rev;
} while (next_permutation(ord.begin(), ord.end()));
}
int main() {
scanf("%d %d\n", &n, &m);
a = vector<vector<int>>(n + 5, vector<int>(m + 5, 0));
b = a;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
char c = getchar();
a[i][j] = gt(c);
}
getchar();
}
solve();
a = transpose(a);
swap(n, m);
b = a;
solve(true);
assert(!ans.empty());
if (flag) ans = transpose(ans);
swap(n, m);
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) putchar(rev_gt(ans[i][j]));
puts("");
}
return 0;
}
|
#include <bits/stdc++.h>
template <typename T>
bool ckmax(T &x, T y) {
return x < y ? x = y, true : false;
}
template <typename T>
bool ckmin(T &x, T y) {
return x > y ? x = y, true : false;
}
using namespace std;
const long long inf = (1ll << 60);
const double Pi = acos(-1);
const long long mod = 998244353;
const double eps = 1e-6;
inline long long fpow(long long a, long long b = mod - 2, long long p = mod) {
a %= p;
long long res = 1;
while (b) {
if (b & 1) res = res * a % p;
a = a * a % p;
b >>= 1;
}
return res;
}
inline long long read() {
char c = getchar();
long long x = 0;
bool f = 0;
for (; !isdigit(c); c = getchar()) f ^= !(c ^ 45);
for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
if (f) x = -x;
return x;
}
inline void write(long long x) {
if (x < 0) {
putchar('-');
write(-x);
return;
}
if (x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
inline void writesp(long long x) { write(x), putchar(' '); }
inline void writeln(long long x) {
write(x);
putchar('\n');
}
long long n, m;
long long ans = inf;
string s;
vector<char> v = {'A', 'C', 'G', 'T'};
signed main() {
n = read(), m = read();
vector<vector<char>> a(n + 5, vector<char>(m + 5)),
b(n + 5, vector<char>(m + 5)), c(n + 5, vector<char>(m + 5));
for (long long i = (0), iE = (n - 1); i <= iE; i++) {
cin >> s;
for (long long j = (0), jE = (m - 1); j <= jE; j++) a[i][j] = s[j];
}
do {
long long cnt = 0;
for (long long i = (0), iE = (n - 1); i <= iE; i++) {
long long A = 0, B = 0, add = (i & 1) << 1;
for (long long j = (0), jE = (m - 1); j <= jE; j++)
A += (a[i][j] != v[add + (j & 1)]), B += (a[i][j] != v[add + !(j & 1)]);
cnt += min(A, B);
if (A < B)
for (long long j = (0), jE = (m - 1); j <= jE; j++)
b[i][j] = v[add + (j & 1)];
else
for (long long j = (0), jE = (m - 1); j <= jE; j++)
b[i][j] = v[add + !(j & 1)];
}
if (cnt < ans) {
ans = cnt;
for (long long i = (0), iE = (n - 1); i <= iE; i++)
for (long long j = (0), jE = (m - 1); j <= jE; j++) c[i][j] = b[i][j];
}
cnt = 0;
for (long long j = (0), jE = (m - 1); j <= jE; j++) {
long long A = 0, B = 0, add = (j & 1) << 1;
for (long long i = (0), iE = (n - 1); i <= iE; i++)
A += (a[i][j] != v[add + (i & 1)]), B += (a[i][j] != v[add + !(i & 1)]);
cnt += min(A, B);
if (A < B)
for (long long i = (0), iE = (n - 1); i <= iE; i++)
b[i][j] = v[add + (i & 1)];
else
for (long long i = (0), iE = (n - 1); i <= iE; i++)
b[i][j] = v[add + !(i & 1)];
}
if (cnt < ans) {
ans = cnt;
for (long long i = (0), iE = (n - 1); i <= iE; i++)
for (long long j = (0), jE = (m - 1); j <= jE; j++) c[i][j] = b[i][j];
}
} while (next_permutation(v.begin(), v.end()));
for (long long i = (0), iE = (n - 1); i <= iE; i++) {
for (long long j = (0), jE = (m - 1); j <= jE; j++) cout << c[i][j];
puts("");
}
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, mi, num;
vector<string> t, v, res;
int f(char a, char b, char c, char d, vector<string>& v) {
char x, y;
for (int i = 0; i < n; ++i) {
if (i % 2) {
x = c;
y = d;
} else {
x = a;
y = b;
}
int xy = 0;
int yx = 0;
for (int j = 0; j < m; ++j) {
char c = t[i][j];
if (j % 2 == 0) {
if (c != x) ++xy;
if (c != y) ++yx;
}
if (j % 2 == 1) {
if (c != y) ++xy;
if (c != x) ++yx;
}
}
if (yx < xy) swap(x, y);
for (int j = 0; j < m; ++j) v[i][j] = (j % 2 == 0 ? x : y);
}
num = 0;
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
if (v[i][j] != t[i][j]) ++num;
return num;
}
int g(char a, char b, char c, char d, vector<string>& v) {
char x, y;
for (int j = 0; j < m; ++j) {
if (j % 2) {
x = c;
y = d;
} else {
x = a;
y = b;
}
int xy = 0;
int yx = 0;
for (int i = 0; i < n; ++i) {
char c = t[i][j];
if (i % 2 == 0) {
if (c != x) ++xy;
if (c != y) ++yx;
}
if (i % 2 == 1) {
if (c != y) ++xy;
if (c != x) ++yx;
}
}
if (yx < xy) swap(x, y);
for (int i = 0; i < n; ++i) v[i][j] = (i % 2 == 0 ? x : y);
}
num = 0;
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
if (v[i][j] != t[i][j]) ++num;
return num;
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> m;
t.resize(n);
for (int i = 0; i < n; ++i) cin >> t[i];
v = t;
mi = n * m + 5;
if (mi > f('A', 'G', 'C', 'T', v)) {
mi = num;
res = v;
}
if (mi > f('A', 'C', 'G', 'T', v)) {
mi = num;
res = v;
}
if (mi > f('A', 'T', 'C', 'G', v)) {
mi = num;
res = v;
}
if (mi > f('C', 'G', 'A', 'T', v)) {
mi = num;
res = v;
}
if (mi > f('T', 'G', 'C', 'A', v)) {
mi = num;
res = v;
}
if (mi > f('C', 'T', 'A', 'G', v)) {
mi = num;
res = v;
}
if (mi > g('A', 'G', 'C', 'T', v)) {
mi = num;
res = v;
}
if (mi > g('A', 'C', 'G', 'T', v)) {
mi = num;
res = v;
}
if (mi > g('A', 'T', 'C', 'G', v)) {
mi = num;
res = v;
}
if (mi > g('C', 'G', 'A', 'T', v)) {
mi = num;
res = v;
}
if (mi > g('T', 'G', 'C', 'A', v)) {
mi = num;
res = v;
}
if (mi > g('C', 'T', 'A', 'G', v)) {
mi = num;
res = v;
}
for (int i = 0; i < n; ++i) cout << res[i] << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e8;
const int N = 300100;
int n, m, ans, nans;
string mat[N], ansmat[N], nmat[N];
void upd() {
if (nans >= ans) return;
ans = nans;
for (int i = 0; i < n; ++i) ansmat[i] = nmat[i];
}
int main() {
cin.tie(NULL);
ios_base::sync_with_stdio(0);
cin >> n >> m;
for (int i = 0; i < n; ++i) cin >> mat[i];
for (int i = 0; i < n; ++i) ansmat[i] = nmat[i] = mat[i];
vector<char> v = {'A', 'C', 'G', 'T'};
ans = INF;
do {
nans = 0;
for (int j = 0; j < m; ++j) {
int op1 = 0, op2 = 0;
for (int i = 0; i < n; ++i) {
op1 += v[2 * (j & 1) + (i & 1)] != mat[i][j];
op2 += v[2 * (j & 1) + !(i & 1)] != mat[i][j];
}
nans += min(op1, op2);
bool f = op1 > op2;
for (int i = 0; i < n; ++i) nmat[i][j] = v[2 * (j & 1) + (f ^ (i & 1))];
}
upd();
nans = 0;
for (int i = 0; i < n; ++i) {
int op1 = 0, op2 = 0;
for (int j = 0; j < m; ++j) {
op1 += v[2 * (i & 1) + (j & 1)] != mat[i][j];
op2 += v[2 * (i & 1) + !(j & 1)] != mat[i][j];
}
nans += min(op1, op2);
bool f = op1 > op2;
for (int j = 0; j < m; ++j) nmat[i][j] = v[2 * (i & 1) + (f ^ (j & 1))];
}
upd();
} while (next_permutation(v.begin(), v.end()));
for (int i = 0; i < n; ++i) cout << ansmat[i] << '\n';
return 0;
}
|
#include <bits/stdc++.h>
const int maxn = int(3e5) + 7;
char buf[maxn];
struct Matrix {
char data[maxn];
int n, m;
bool reversed;
int index(int x, int y) { return (x - 1) * m + y - 1; }
char &at(int x, int y) { return data[index(x, y)]; }
void reverse() {
memcpy(buf, data, sizeof(char) * n * m);
std::swap(n, m);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) at(i, j) = buf[(j - 1) * n + i - 1];
reversed = !reversed;
}
void print() {
if (reversed) reverse();
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) printf("%c%s", at(i, j), j == m ? "\n" : "");
}
Matrix &operator=(const Matrix &tmp) {
n = tmp.n, m = tmp.m, reversed = tmp.reversed;
memcpy(data, tmp.data, sizeof(char) * n * m);
return (*this);
}
int calc(const Matrix &mat, const char *table) {
reversed = mat.reversed;
n = mat.n, m = mat.m;
int ret = 0;
for (int i = 1; i <= n; i++) {
int cur = (i & 1) << 1, min = 0x3f3f3f3f, best = -1;
for (int k = 0; k < 2; k++) {
int cnt = 0;
for (int j = 1; j <= m; j++)
if (mat.data[(i - 1) * m + j - 1] != table[cur + (j & 1) ^ k]) cnt++;
if (cnt < min) min = cnt, best = k;
}
ret += min;
for (int j = 1; j <= m; j++) at(i, j) = table[cur + (j & 1) ^ best];
}
return ret;
}
void read() {
reversed = false;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) scanf(" %c", &at(i, j));
}
} ans, input;
int min = 0x3f3f3f3f;
void calc(const Matrix &mat) {
char table[] = "AGCT";
Matrix ret;
do {
int tmp = ret.calc(mat, table);
if (tmp < min) min = tmp, ans = ret;
} while (std::next_permutation(table, table + 4));
}
int main() {
input.read();
calc(input);
input.reverse();
calc(input);
ans.print();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, t1, t2, minres = 0x7ffffff;
char t[300009], tmp,
sa[7][2][2] = {{0},
{{'A', 'T'}, {'C', 'G'}},
{{'A', 'C'}, {'T', 'G'}},
{{'A', 'G'}, {'C', 'T'}},
{{'T', 'C'}, {'A', 'G'}},
{{'T', 'G'}, {'A', 'C'}},
{{'C', 'G'}, {'A', 'T'}}};
bool re[300009], tm[300009];
int main() {
int n, m, res1, res2, res3, total;
scanf("%d%d", &n, &m);
for (int i = (1), ilim = (n); i <= ilim; i++)
for (int j = (1), jlim = (m); j <= jlim; j++) {
tmp = 0;
while (!isalpha(tmp)) tmp = getchar();
t[(i - 1) * m + j] = tmp;
}
for (int i = (1), ilim = (6); i <= ilim; i++) {
total = 0;
for (int j = (1), jlim = (n); j <= jlim; j++) {
res1 = res2 = res3 = 0;
for (int k = (1), klim = (m); k <= klim; k++) {
if (t[(j - 1) * m + k] == sa[i][j & 1][k & 1])
res2++;
else if (t[(j - 1) * m + k] == sa[i][j & 1][1 ^ (k & 1)])
res1++;
else
res3++;
}
if (res1 < res2) {
tm[j] = 0;
} else {
res1 = res2;
tm[j] = 1;
}
total += res1 + res3;
}
if (total < minres) {
minres = total;
memcpy(re, tm, n + 1);
t1 = 0;
t2 = i;
}
}
for (int i = (1), ilim = (6); i <= ilim; i++) {
total = 0;
for (int j = (1), jlim = (m); j <= jlim; j++) {
res1 = res2 = res3 = 0;
for (int k = (1), klim = (n); k <= klim; k++) {
if (t[(k - 1) * m + j] == sa[i][j & 1][k & 1])
res2++;
else if (t[(k - 1) * m + j] == sa[i][j & 1][1 ^ (k & 1)])
res1++;
else
res3++;
}
if (res1 < res2) {
tm[j] = 0;
} else {
res1 = res2;
tm[j] = 1;
}
total += res1 + res3;
}
if (total < minres) {
minres = total;
memcpy(re, tm, m + 1);
t1 = 1;
t2 = i;
}
}
for (int i = (1), ilim = (n); i <= ilim; i++) {
for (int j = (1), jlim = (m); j <= jlim; j++) {
if (t1) {
t[(i - 1) * m + j] = sa[t2][j & 1][re[j] ^ (i & 1)];
} else {
t[(i - 1) * m + j] = sa[t2][i & 1][re[i] ^ (j & 1)];
}
printf("%c", t[(i - 1) * m + j]);
}
putchar('\n');
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int NM = 300005;
char p[12][2] = {{'0', '1'}, {'0', '2'}, {'0', '3'}, {'1', '2'},
{'1', '3'}, {'2', '3'}, {'1', '0'}, {'2', '0'},
{'3', '0'}, {'2', '1'}, {'3', '1'}, {'3', '2'}};
int ok[12][12];
void work_init() {
for (int k1 = 0; k1 < 12; k1++)
for (int k2 = 0; k2 < 12; k2++) {
ok[k1][k2] = 1;
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
if (p[k1][i] == p[k2][j]) ok[k1][k2] = 0;
}
}
int work(int n, int m, vector<char> *a, vector<char> *c) {
static int f[NM][12], pre[NM][12];
for (int k2 = 0; k2 < 12; k2++) {
int cnt = 0;
for (int j = 0; j < m; j++) cnt += a[0][j] != p[k2][j & 1];
f[0][k2] = cnt;
}
for (int i = 1; i < n; i++) {
memset(f[i], 0x3f, sizeof(f[i]));
for (int k1 = 0; k1 < 12; k1++)
for (int k2 = 0; k2 < 12; k2++)
if (ok[k1][k2]) {
int cnt = 0;
for (int j = 0; j < m; j++) cnt += a[i][j] != p[k2][j & 1];
if (f[i][k2] > f[i - 1][k1] + cnt)
f[i][k2] = f[i - 1][k1] + cnt, pre[i][k2] = k1;
}
}
int mk = 0;
for (int k2 = 0; k2 < 12; k2++)
if (f[n - 1][k2] < f[n - 1][mk]) mk = k2;
for (int i = n - 1, t = mk; i >= 0; t = pre[i][t], i--) {
for (int j = 0; j < m; j++) c[i].push_back(p[t][j & 1]);
}
return f[n - 1][mk];
}
char tra0[130], tra1[130];
void trans_init() {
tra0['A'] = '0', tra1['0'] = 'A';
tra0['C'] = '1', tra1['1'] = 'C';
tra0['G'] = '2', tra1['2'] = 'G';
tra0['T'] = '3', tra1['3'] = 'T';
}
char trans(char x, int f) {
if (!f)
return tra0[x];
else
return tra1[x];
}
vector<char> a[NM], b[NM], f[NM], g[NM];
int main() {
trans_init();
work_init();
int i, j, n, m;
cin >> n >> m;
for (i = 0; i < n; i++) {
static char s[NM];
scanf("%s", s);
for (j = 0; j < m; j++) a[i].push_back(trans(s[j], 0));
}
int ans1 = work(n, m, a, f);
for (i = 0; i < n; i++)
for (j = 0; j < m; j++) b[j].push_back(a[i][j]);
int ans2 = work(m, n, b, g);
if (ans1 > ans2) {
for (i = 0; i < n; i++) f[i].clear();
for (j = 0; j < m; j++)
for (i = 0; i < n; i++) f[i].push_back(g[j][i]);
}
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) printf("%c", trans(f[i][j], 1));
puts("");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f, mod = 1000000007;
const double pi = 3.1415926535897932, eps = 1e-6;
void chmax(int &x, int y) {
if (x < y) x = y;
}
void chmin(int &x, int y) {
if (x > y) x = y;
}
int n, m, ansc;
string s[300005], t[300005], ans[300005], pat[300005];
char mpc[5] = "ACGT";
bool flg;
void deal(bool mode) {
int tmp = 0;
for (int(i) = (1); (i) <= (n); (i)++) pat[i] = "";
for (int(i) = (1); (i) <= (n); (i)++) pat[i].resize(m + 1);
for (int(a) = (0); (a) <= (3); (a)++)
for (int(b) = (a + 1); (b) <= (3); (b)++) {
tmp = 0;
int c = 0, d = 0;
while (c == a || c == b) c++;
while (d == a || d == b || d == c) d++;
for (int(i) = (1); (i) <= (n); (i)++) {
int num1 = 0, num2 = 0;
if (i & 1) {
for (int(j) = (1); (j) <= (m); (j)++) {
if (j & 1)
num1 += (s[i][j] == mpc[a]);
else
num1 += (s[i][j] == mpc[b]);
}
for (int(j) = (1); (j) <= (m); (j)++) {
if (j & 1)
num2 += (s[i][j] == mpc[b]);
else
num2 += (s[i][j] == mpc[a]);
}
if (num1 > num2)
for (int(j) = (1); (j) <= (m); (j)++)
pat[i][j] = (j & 1) ? mpc[a] : mpc[b];
else
for (int(j) = (1); (j) <= (m); (j)++)
pat[i][j] = (j & 1) ? mpc[b] : mpc[a];
tmp += max(num1, num2);
} else {
for (int(j) = (1); (j) <= (m); (j)++) {
if (j & 1)
num1 += (s[i][j] == mpc[c]);
else
num1 += (s[i][j] == mpc[d]);
}
for (int(j) = (1); (j) <= (m); (j)++) {
if (j & 1)
num2 += (s[i][j] == mpc[d]);
else
num2 += (s[i][j] == mpc[c]);
}
if (num1 > num2)
for (int(j) = (1); (j) <= (m); (j)++)
pat[i][j] = (j & 1) ? mpc[c] : mpc[d];
else
for (int(j) = (1); (j) <= (m); (j)++)
pat[i][j] = (j & 1) ? mpc[d] : mpc[c];
tmp += max(num1, num2);
}
}
if (tmp > ansc) {
for (int(i) = (1); (i) <= (n); (i)++) ans[i] = pat[i];
if (mode) flg = 1;
ansc = tmp;
}
}
}
int main() {
ios::sync_with_stdio(false);
cin >> n >> m;
for (int(i) = (1); (i) <= (n); (i)++) {
cin >> s[i];
s[i] = ' ' + s[i];
}
deal(0);
for (int(i) = (1); (i) <= (m); (i)++) t[i].resize(n + 1);
for (int(i) = (1); (i) <= (n); (i)++)
for (int(j) = (1); (j) <= (m); (j)++) t[j][i] = s[i][j];
swap(n, m);
for (int(i) = (1); (i) <= (n); (i)++) s[i] = t[i];
deal(1);
if (!flg)
for (int(i) = (1); (i) <= (m); (i)++) cout << ans[i].substr(1) << '\n';
else {
for (int(i) = (1); (i) <= (m); (i)++) t[i] = "";
for (int(i) = (1); (i) <= (m); (i)++) t[i].resize(n + 1);
for (int(i) = (1); (i) <= (n); (i)++)
for (int(j) = (1); (j) <= (m); (j)++) t[j][i] = ans[i][j];
for (int(i) = (1); (i) <= (m); (i)++) cout << t[i].substr(1) << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename S, typename T>
ostream& operator<<(ostream& out, pair<S, T> const& p) {
out << '(' << p.first << ", " << p.second << ')';
return out;
}
template <typename T>
ostream& operator<<(ostream& out, vector<T> const& v) {
long long l = v.size();
for (long long i = 0; i < l - 1; i++) out << v[i] << ' ';
if (l > 0) out << v[l - 1];
return out;
}
template <typename T>
void trace(const char* name, T&& arg1) {
cout << name << " : " << arg1 << "\n";
}
template <typename T, typename... Args>
void trace(const char* names, T&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');
cout.write(names, comma - names) << " : " << arg1 << " | ";
trace(comma + 1, args...);
}
long long n, m;
vector<string> arr;
map<long long, char> let;
vector<string> res;
long long best = 1000000000000000001;
long long aa, bb, cc, dd;
bool col;
long long try_row(long long r, long long a, long long b) {
char f = let[a];
char s = let[b];
long long ans = 0;
for (long long i = 0; i < m; i++) {
ans += (arr[r][i] != f);
swap(f, s);
}
return ans;
}
long long try_rows(long long a, long long b, long long c, long long d) {
long long ans = 0;
for (long long i = 0; i < n; i++) {
ans += min(try_row(i, a, b), try_row(i, b, a));
swap(a, c), swap(b, d);
}
return ans;
}
long long try_col(long long r, long long a, long long b) {
char f = let[a];
char s = let[b];
long long ans = 0;
for (long long i = 0; i < n; i++) {
ans += (arr[i][r] != f);
swap(f, s);
}
return ans;
}
long long try_cols(long long a, long long b, long long c, long long d) {
long long ans = 0;
for (long long i = 0; i < m; i++) {
ans += min(try_col(i, a, b), try_col(i, b, a));
swap(a, c), swap(b, d);
}
return ans;
}
void fill_col(long long r, long long a, long long b) {
char f = let[a];
char s = let[b];
for (long long i = 0; i < n; i++) {
res[i][r] = f;
swap(f, s);
}
}
void fill_cols(long long a, long long b, long long c, long long d) {
string temp = "";
for (long long i = 0; i < m; i++) {
temp += "*";
}
for (long long i = 0; i < n; i++) {
res.push_back(temp);
}
for (long long i = 0; i < m; i++) {
if (try_col(i, a, b) < try_col(i, b, a)) {
fill_col(i, a, b);
} else {
fill_col(i, b, a);
}
swap(a, c), swap(b, d);
}
}
void fill_row(long long r, long long a, long long b) {
char f = let[a];
char s = let[b];
for (long long i = 0; i < m; i++) {
res[r][i] = f;
swap(f, s);
}
}
void fill_rows(long long a, long long b, long long c, long long d) {
string temp = "";
for (long long i = 0; i < m; i++) {
temp += "*";
}
for (long long i = 0; i < n; i++) {
res.push_back(temp);
}
for (long long i = 0; i < n; i++) {
if (try_row(i, a, b) < try_row(i, b, a)) {
fill_row(i, a, b);
} else {
fill_row(i, b, a);
}
swap(a, c), swap(b, d);
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m;
for (long long i = 0; i < n; i++) {
string temp;
cin >> temp;
arr.push_back(temp);
}
let[0] = 'T';
let[1] = 'A';
let[2] = 'G';
let[3] = 'C';
for (long long a = 0; a < 4; a++) {
for (long long b = 0; b < 4; b++) {
for (long long c = 0; c < 4; c++) {
for (long long d = 0; d < 4; d++) {
set<long long> s;
s.insert({a, b, c, d});
if ((long long)s.size() == 4) {
long long lol = try_rows(a, b, c, d);
if (lol < best) {
best = lol;
aa = a;
bb = b;
cc = c;
dd = d;
col = false;
}
lol = try_cols(a, b, c, d);
if (lol < best) {
best = lol;
aa = a;
bb = b;
cc = c;
dd = d;
col = true;
}
}
}
}
}
}
if (col == true) {
fill_cols(aa, bb, cc, dd);
for (long long i = 0; i < n; i++) {
cout << res[i] << "\n";
}
} else {
fill_rows(aa, bb, cc, dd);
for (long long i = 0; i < n; i++) {
cout << res[i] << "\n";
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
vector<string> ar;
unordered_map<int, char> tmp[150005];
unordered_map<int, char> ans[150005];
int N, M, small, flip, flip_ans;
void solve2(string a, string b) {
int cost = 0;
for (int j = 0; j < M; j++) {
if (j % 2 == 0) {
tmp[0][j] = a[0];
tmp[1][j] = b[0];
} else {
tmp[0][j] = a[1];
tmp[1][j] = b[1];
}
cost += (tmp[0][j] != ar[0][j]) + (tmp[1][j] != ar[1][j]);
}
for (int i = 2; i < N; i++) {
char F, S;
char ext;
int cost1 = 0, cost2 = 0;
if (i % 2 == 0) {
F = a[0];
S = a[1];
} else {
F = b[0];
S = b[1];
}
for (int j = 0; j < M; j++) {
if (j % 2 == 0)
ext = F;
else
ext = S;
cost1 += (ext != ar[i][j]);
}
ext = S;
S = F;
F = ext;
for (int j = 0; j < M; j++) {
if (j % 2 == 0)
ext = F;
else
ext = S;
cost2 += (ext != ar[i][j]);
}
if (cost1 < cost2) {
cost += cost1;
ext = S;
S = F;
F = ext;
for (int j = 0; j < M; j++) {
if (j % 2 == 0)
tmp[i][j] = F;
else
tmp[i][j] = S;
}
} else {
cost += cost2;
for (int j = 0; j < M; j++) {
if (j % 2 == 0)
tmp[i][j] = F;
else
tmp[i][j] = S;
}
}
}
if (small > cost) {
small = cost;
if (flip) flip_ans = 1;
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++) ans[i][j] = tmp[i][j];
}
}
void solve(string a, string b) {
solve2(a, b);
solve2(b, a);
}
int main() {
cin >> N >> M;
string str;
for (int i = 0; i < N; i++) {
cin >> str;
ar.push_back(str);
}
small = 150005 * 2;
flip = 0;
solve("AG", "CT");
solve("AG", "TC");
solve("GA", "CT");
solve("GA", "TC");
solve("AC", "GT");
solve("AC", "TG");
solve("CA", "GT");
solve("CA", "TG");
solve("AT", "GC");
solve("AT", "CG");
solve("TA", "GC");
solve("TA", "CG");
vector<string> tmp2;
for (int i = 0; i < M; i++) {
string nw = "";
for (int j = 0; j < N; j++) nw += ar[j][i];
tmp2.push_back(nw);
}
ar.clear();
int tmp3 = M;
M = N;
N = tmp3;
ar = tmp2;
flip = 1;
flip_ans = 0;
for (int i = 0; i < 150005; i++) tmp[i].clear();
solve("AG", "CT");
solve("AG", "TC");
solve("GA", "CT");
solve("GA", "TC");
solve("AC", "GT");
solve("AC", "TG");
solve("CA", "GT");
solve("CA", "TG");
solve("AT", "GC");
solve("AT", "CG");
solve("TA", "GC");
solve("TA", "CG");
if (flip_ans) {
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
cout << ans[j][i];
}
cout << endl;
}
return (0);
}
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
cout << ans[i][j];
}
cout << endl;
}
return (0);
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 300010;
int n, m, mn = INT_MAX, a[maxn], b[maxn], ans[maxn];
int cnt[3][maxn][4], f[maxn][4][4];
pair<int, int> pre[maxn][4][4];
char t[maxn];
map<char, int> mp;
bool flag;
inline int id(int x, int y) { return (x - 1) * m + y; }
int main() {
scanf("%d %d", &n, &m);
mp['A'] = 0, mp['G'] = 1, mp['C'] = 2, mp['T'] = 3;
for (int i = 1; i <= n; i++) {
scanf("%s", t + 1);
for (int j = 1; j <= m; j++) {
a[id(i, j)] = mp[t[j]];
if (i & 1)
cnt[1][j][mp[t[j]]]++;
else
cnt[2][j][mp[t[j]]]++;
}
}
auto solve = [&]() {
memset(f, -1, sizeof(f));
for (int i : {0, 1, 2, 3})
for (int j : {0, 1, 2, 3})
if (i ^ j) {
f[1][i][j] = cnt[1][1][i] + cnt[2][1][j];
}
for (int i = 1; i < m; i++)
for (int a : {0, 1, 2, 3}) {
for (int b : {0, 1, 2, 3})
if (~f[i][a][b]) {
for (int c : {0, 1, 2, 3})
if (a ^ c && b ^ c) {
for (int d : {0, 1, 2, 3})
if (a ^ d && b ^ d && c ^ d) {
int num = f[i][a][b] + cnt[1][i + 1][c] + cnt[2][i + 1][d];
if (num > f[i + 1][c][d])
f[i + 1][c][d] = num, pre[i + 1][c][d] = make_pair(a, b);
}
}
}
}
};
solve();
int mx1 = 0, ta, tb;
for (int a : {0, 1, 2, 3})
for (int b : {0, 1, 2, 3}) {
if (f[m][a][b] > mx1) mx1 = f[m][a][b], ta = a, tb = b;
}
function<void(int, int, int)> print = [&](int i, int a, int b) {
for (int j = 1; j <= n; j++) {
if (j & 1)
ans[id(j, i)] = a;
else
ans[id(j, i)] = b;
}
if (i > 1) print(i - 1, pre[i][a][b].first, pre[i][a][b].second);
};
print(m, ta, tb);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
b[(j - 1) * n + i] = a[id(i, j)];
}
}
swap(n, m), flag = 1;
memcpy(a, b, sizeof(a));
memset(cnt, 0, sizeof(cnt));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (i & 1)
cnt[1][j][a[id(i, j)]]++;
else
cnt[2][j][a[id(i, j)]]++;
}
}
solve();
int mx2 = 0;
for (int a : {0, 1, 2, 3})
for (int b : {0, 1, 2, 3}) {
if (f[m][a][b] > mx2) mx2 = f[m][a][b], ta = a, tb = b;
}
if (mx1 > mx2) {
swap(n, m), flag = 0;
} else {
print(m, ta, tb);
}
if (!flag) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
putchar("AGCT"[ans[id(i, j)]]);
}
putchar('\n');
}
} else {
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
putchar("AGCT"[ans[id(j, i)]]);
}
putchar('\n');
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
pair<int, vector<vector<int> > > diff(int a, int b, int c, int d, int n, int m,
vector<vector<int> > arr) {
vector<vector<int> > ans;
int finans = 0;
for (int i = 0; i < n; i++) {
vector<int> curvec;
if (i % 2 == 0) {
int ctr1 = 0;
for (int j = 0; j < m; j++) {
if (j % 2 == 0 && arr[i][j] != a)
ctr1++;
else if (j % 2 != 0 && arr[i][j] != b)
ctr1++;
}
int ctr2 = 0;
for (int j = 0; j < m; j++) {
if (j % 2 == 0 && arr[i][j] != b)
ctr2++;
else if (j % 2 != 0 && arr[i][j] != a)
ctr2++;
}
if (ctr1 < ctr2) {
for (int j = 0; j < m; j++) {
if (j % 2 == 0)
curvec.push_back(a);
else if (j % 2 != 0)
curvec.push_back(b);
}
} else {
for (int j = 0; j < m; j++) {
if (j % 2 == 0)
curvec.push_back(b);
else if (j % 2 != 0)
curvec.push_back(a);
}
}
finans += min(ctr1, ctr2);
ans.push_back(curvec);
} else {
int ctr1 = 0;
for (int j = 0; j < m; j++) {
if (j % 2 == 0 && arr[i][j] != c)
ctr1++;
else if (j % 2 != 0 && arr[i][j] != d)
ctr1++;
}
int ctr2 = 0;
for (int j = 0; j < m; j++) {
if (j % 2 == 0 && arr[i][j] != d)
ctr2++;
else if (j % 2 != 0 && arr[i][j] != c)
ctr2++;
}
if (ctr1 < ctr2) {
for (int j = 0; j < m; j++) {
if (j % 2 == 0)
curvec.push_back(c);
else if (j % 2 != 0)
curvec.push_back(d);
}
} else {
for (int j = 0; j < m; j++) {
if (j % 2 == 0)
curvec.push_back(d);
else if (j % 2 != 0)
curvec.push_back(c);
}
}
finans += min(ctr1, ctr2);
ans.push_back(curvec);
}
}
vector<vector<int> > ans1;
int finans1 = 0;
for (int i = 0; i < n; i++) {
vector<int> curvec;
if (i % 2 != 0) {
int ctr1 = 0;
for (int j = 0; j < m; j++) {
if (j % 2 == 0 && arr[i][j] != a)
ctr1++;
else if (j % 2 != 0 && arr[i][j] != b)
ctr1++;
}
int ctr2 = 0;
for (int j = 0; j < m; j++) {
if (j % 2 == 0 && arr[i][j] != b)
ctr2++;
else if (j % 2 != 0 && arr[i][j] != a)
ctr2++;
}
if (ctr1 < ctr2) {
for (int j = 0; j < m; j++) {
if (j % 2 == 0)
curvec.push_back(a);
else if (j % 2 != 0)
curvec.push_back(b);
}
} else {
for (int j = 0; j < m; j++) {
if (j % 2 == 0)
curvec.push_back(b);
else if (j % 2 != 0)
curvec.push_back(a);
}
}
finans1 += min(ctr1, ctr2);
ans1.push_back(curvec);
} else {
int ctr1 = 0;
for (int j = 0; j < m; j++) {
if (j % 2 == 0 && arr[i][j] != c)
ctr1++;
else if (j % 2 != 0 && arr[i][j] != d)
ctr1++;
}
int ctr2 = 0;
for (int j = 0; j < m; j++) {
if (j % 2 == 0 && arr[i][j] != d)
ctr2++;
else if (j % 2 != 0 && arr[i][j] != c)
ctr2++;
}
if (ctr1 < ctr2) {
for (int j = 0; j < m; j++) {
if (j % 2 == 0)
curvec.push_back(c);
else if (j % 2 != 0)
curvec.push_back(d);
}
} else {
for (int j = 0; j < m; j++) {
if (j % 2 == 0)
curvec.push_back(d);
else if (j % 2 != 0)
curvec.push_back(c);
}
}
finans1 += min(ctr1, ctr2);
ans1.push_back(curvec);
}
}
if (finans1 < finans) {
return make_pair(finans1, ans1);
} else {
return make_pair(finans, ans);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, m;
cin >> n >> m;
vector<vector<int> > arr, arrinv;
for (int i = 0; i < n; i++) {
string str;
cin >> str;
vector<int> curvec;
for (int j = 0; j < m; j++) {
if (str[j] == 'A') {
curvec.push_back(0);
} else if (str[j] == 'T') {
curvec.push_back(1);
} else if (str[j] == 'C') {
curvec.push_back(2);
} else {
curvec.push_back(3);
}
}
arr.push_back(curvec);
}
pair<int, vector<vector<int> > > ans1 = diff(0, 1, 2, 3, n, m, arr);
pair<int, vector<vector<int> > > ans2 = diff(0, 2, 1, 3, n, m, arr);
pair<int, vector<vector<int> > > ans3 = diff(0, 3, 1, 2, n, m, arr);
int ans = min(ans1.first, min(ans2.first, ans3.first));
for (int i = 0; i < m; i++) {
vector<int> curvec;
for (int j = 0; j < n; j++) {
curvec.push_back(arr[j][i]);
}
arrinv.push_back(curvec);
}
pair<int, vector<vector<int> > > ans4 = diff(0, 1, 2, 3, m, n, arrinv);
pair<int, vector<vector<int> > > ans5 = diff(0, 2, 1, 3, m, n, arrinv);
pair<int, vector<vector<int> > > ans6 = diff(0, 3, 1, 2, m, n, arrinv);
int ansinv = min(ans4.first, min(ans5.first, ans6.first));
if (ans < ansinv) {
vector<vector<int> > finarr;
if (ans == ans1.first) {
finarr = ans1.second;
} else if (ans == ans2.first) {
finarr = ans2.second;
} else {
finarr = ans3.second;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (finarr[i][j] == 0) {
cout << 'A';
} else if (finarr[i][j] == 1) {
cout << 'T';
} else if (finarr[i][j] == 2) {
cout << 'C';
} else {
cout << 'G';
}
}
cout << endl;
}
} else {
vector<vector<int> > finarr;
if (ansinv == ans4.first) {
finarr = ans4.second;
} else if (ansinv == ans5.first) {
finarr = ans5.second;
} else {
finarr = ans6.second;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (finarr[j][i] == 0) {
cout << 'A';
} else if (finarr[j][i] == 1) {
cout << 'T';
} else if (finarr[j][i] == 2) {
cout << 'C';
} else {
cout << 'G';
}
}
cout << endl;
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
int best, N, M;
char **sir, **ans;
int code(char c) {
if (c == 'A') return 1;
if (c == 'C') return 2;
if (c == 'G') return 3;
return 4;
}
char getCh(int c) {
if (c == 1) return 'A';
if (c == 2) return 'C';
if (c == 3) return 'G';
return 'T';
}
void tryLines() {
int bestSoFar = N * M + 2, a1 = -1, b1 = -1, a2 = -1, b2 = -1;
for (int i1 = 1; i1 <= 4; i1++)
for (int j1 = i1 + 1; j1 <= 4; j1++) {
int i2 = 1, j2;
while (i2 == i1 || i2 == j1) i2++;
j2 = i2 + 1;
while (j2 == i1 || j2 == j1) j2++;
int currCost = 0;
for (int i = 1; i <= N; i++) {
int x = i1, y = j1;
if (i % 2 == 0) x = i2, y = j2;
int cnt1 = 0, cnt2 = 0;
for (int j = 1; j <= M; j += 2)
cnt1 += (sir[i][j] != x), cnt2 += (sir[i][j] != y);
for (int j = 2; j <= M; j += 2)
cnt1 += (sir[i][j] != y), cnt2 += (sir[i][j] != x);
currCost += min(cnt1, cnt2);
}
if (currCost < bestSoFar)
bestSoFar = currCost, a1 = i1, b1 = j1, a2 = i2, b2 = j2;
}
if (bestSoFar < best) {
best = bestSoFar;
int i1 = a1, j1 = b1, i2 = a2, j2 = b2;
for (int i = 1; i <= N; i++) {
int x = i1, y = j1;
if (i % 2 == 0) x = i2, y = j2;
int cnt1 = 0, cnt2 = 0;
for (int j = 1; j <= M; j += 2)
cnt1 += (sir[i][j] != x), cnt2 += (sir[i][j] != y);
for (int j = 2; j <= M; j += 2)
cnt1 += (sir[i][j] != y), cnt2 += (sir[i][j] != x);
if (cnt1 > cnt2) swap(x, y);
for (int j = 1; j <= M; j += 2) ans[i][j] = x;
for (int j = 2; j <= M; j += 2) ans[i][j] = y;
}
}
}
void tryColumns() {
int bestSoFar = N * M + 2, a1 = -1, b1 = -1, a2 = -1, b2 = -1;
for (int i1 = 1; i1 <= 4; i1++)
for (int j1 = i1 + 1; j1 <= 4; j1++) {
int i2 = 1, j2;
while (i2 == i1 || i2 == j1) i2++;
j2 = i2 + 1;
while (j2 == i1 || j2 == j1) j2++;
int currCost = 0;
for (int j = 1; j <= M; j++) {
int x = i1, y = j1;
if (j % 2 == 0) x = i2, y = j2;
int cnt1 = 0, cnt2 = 0;
for (int i = 1; i <= N; i += 2)
cnt1 += (sir[i][j] != x), cnt2 += (sir[i][j] != y);
for (int i = 2; i <= N; i += 2)
cnt1 += (sir[i][j] != y), cnt2 += (sir[i][j] != x);
currCost += min(cnt1, cnt2);
}
if (currCost < bestSoFar)
bestSoFar = currCost, a1 = i1, b1 = j1, a2 = i2, b2 = j2;
}
if (bestSoFar < best) {
best = bestSoFar;
int i1 = a1, j1 = b1, i2 = a2, j2 = b2;
for (int j = 1; j <= M; j++) {
int x = i1, y = j1;
if (j % 2 == 0) x = i2, y = j2;
int cnt1 = 0, cnt2 = 0;
for (int i = 1; i <= N; i += 2)
cnt1 += (sir[i][j] != x), cnt2 += (sir[i][j] != y);
for (int i = 2; i <= N; i += 2)
cnt1 += (sir[i][j] != y), cnt2 += (sir[i][j] != x);
if (cnt1 > cnt2) swap(x, y);
for (int i = 1; i <= N; i += 2) ans[i][j] = x;
for (int i = 2; i <= N; i += 2) ans[i][j] = y;
}
}
}
int main() {
scanf("%d %d\n", &N, &M);
sir = new char *[N + 3], ans = new char *[N + 3];
for (int i = 1; i <= N; i++) {
sir[i] = new char[M + 3];
ans[i] = new char[M + 3];
scanf("%s", sir[i] + 1);
for (int j = 1; j <= M; j++) sir[i][j] = code(sir[i][j]);
}
best = N * M + 10;
tryLines();
tryColumns();
for (int i = 1; i <= N; i++, printf("\n"))
for (int j = 1; j <= M; j++) printf("%c", getCh(ans[i][j]));
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long int mod = 1e9 + 7;
double error = 1e-8;
inline long long int MOD(long long int x, long long int m = mod) {
long long int y = x % m;
return (y >= 0) ? y : y + m;
}
const int inf = INT_MAX;
const long long int infl = LLONG_MAX;
const int nmax = 3e5 + 10;
int mp[128];
char rev[4] = {'A', 'C', 'G', 'T'};
char mat[2][nmax];
char ans[nmax];
int cnt[2][nmax][2][4];
char best[2][nmax];
int bestperm[2];
char tmp[nmax];
int perm[6][2] = {{0, 1}, {0, 2}, {0, 3}, {1, 2}, {1, 3}, {2, 3}};
void precal(int n, int m, int x) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) cnt[x][i][j & 1][mp[mat[x][i * m + j]]]++;
}
}
int calc(int n, int m, int x) {
int a = (m + 1) / 2;
int b = m / 2;
int ret = inf;
for (int p = 0; p < 6; p++) {
int miss = 0;
for (int i = 0; i < n; i++) {
int q = (i & 1) ? p : 5 - p;
int tmp1 = a - cnt[x][i][0][perm[q][0]] + b - cnt[x][i][1][perm[q][1]];
int tmp2 = a - cnt[x][i][0][perm[q][1]] + b - cnt[x][i][1][perm[q][0]];
if (tmp1 < tmp2)
tmp[i] = rev[perm[q][0]], miss += tmp1;
else
tmp[i] = rev[perm[q][1]], miss += tmp2;
}
if (miss < ret) {
memcpy(best[x], tmp, sizeof(char) * n);
ret = miss;
bestperm[x] = p;
}
}
return ret;
}
void fillup(int n, int m, int x) {
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
int p = (i & 1) ? bestperm[x] : 5 - bestperm[x];
ans[i * m + j] = (j % 2 == 0) ? mp[best[x][i]]
: perm[p][0] + perm[p][1] - mp[best[x][i]];
}
}
int main() {
mp['A'] = 0;
mp['C'] = 1;
mp['G'] = 2;
mp['T'] = 3;
int n, m;
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++) {
getchar();
for (int j = 0; j < m; j++) {
mat[0][i * m + j] = getchar();
mat[1][j * n + i] = mat[0][i * m + j];
}
}
precal(n, m, 0);
precal(m, n, 1);
int ans1 = calc(n, m, 0);
int ans2 = calc(m, n, 1);
if (ans1 < ans2) {
fillup(n, m, 0);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) putchar(rev[ans[i * m + j]]);
puts("");
}
} else {
fillup(m, n, 1);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) putchar(rev[ans[j * n + i]]);
if (i != n - 1) puts("");
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
long long i, j, k, l, n, m, timer, res;
vector<vector<long long> > a, na;
long long nums[300009][5][2];
vector<pair<long long, long long> > c, ress;
vector<string> mres, nres;
string s;
long long count(long long num1, long long num2, long long num3,
long long num4) {
long long curr = nums[1][num1][0] + nums[1][num2][1];
c[1] = {num2, num1};
for (i = 2; i <= n; i++) {
if (i % 2 == 0) {
if (nums[i][num3][0] + nums[i][num4][1] >
nums[i][num3][1] + nums[i][num4][0])
c[i] = {num4, num3};
else
c[i] = {num3, num4};
curr += max(nums[i][num3][0] + nums[i][num4][1],
nums[i][num3][1] + nums[i][num4][0]);
}
if (i % 2 == 1) {
if (nums[i][num1][0] + nums[i][num2][1] >
nums[i][num1][1] + nums[i][num2][0])
c[i] = {num2, num1};
else
c[i] = {num1, num2};
curr += max(nums[i][num1][0] + nums[i][num2][1],
nums[i][num1][1] + nums[i][num2][0]);
}
}
return curr;
}
char get(long long num) {
if (num == 1) return 'A';
if (num == 2) return 'G';
if (num == 3) return 'C';
if (num == 4) return 'T';
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n >> m;
long long nn = n, mm = m;
a.resize(n + 1);
c.resize(max(m, n) + 1);
ress.resize(max(n, m) + 1);
for (i = 1; i <= n; i++) {
cin >> s;
a[i].resize(m + 1);
for (j = 1; j <= m; j++) {
if (s[j - 1] == 'A') a[i][j] = 1;
if (s[j - 1] == 'G') a[i][j] = 2;
if (s[j - 1] == 'C') a[i][j] = 3;
if (s[j - 1] == 'T') a[i][j] = 4;
nums[i][a[i][j]][j % 2]++;
}
}
vector<long long> aa = {1, 2, 3, 4};
res = ((long long)1e18 + 9ll);
do {
long long num = count(aa[0], aa[1], aa[2], aa[3]);
if (n * m - num < res) {
res = n * m - num;
ress = c;
}
} while (next_permutation(aa.begin(), aa.end()));
long long flag = 0;
mres.resize(n + 1);
for (i = 1; i <= n; i++) {
mres[i] += '0';
for (j = 1; j <= m; j++) {
if (j % 2 == 1)
mres[i] += get(ress[i].first);
else
mres[i] += get(ress[i].second);
}
}
na = a;
a.resize(m + 1);
for (i = 1; i <= m; i++) a[i].resize(n + 1);
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
a[j][i] = na[i][j];
}
}
memset(nums, 0, sizeof(nums));
swap(n, m);
for (i = 1; i <= n; i++) {
a[i].resize(m + 1);
for (j = 1; j <= m; j++) {
nums[i][a[i][j]][j % 2]++;
}
}
aa = {1, 2, 3, 4};
do {
long long num = count(aa[0], aa[1], aa[2], aa[3]);
if (n * m - num < res) {
res = n * m - num;
ress = c;
flag = 1;
}
} while (next_permutation(aa.begin(), aa.end()));
nres.resize(n + 1);
for (i = 1; i <= n; i++) {
nres[i] += '0';
for (j = 1; j <= m; j++) {
if (j % 2 == 1)
nres[i] += get(ress[i].first);
else
nres[i] += get(ress[i].second);
}
}
if (flag) {
for (i = 1; i <= m; i++) {
mres[i] = "";
for (j = 0; j <= n; j++) mres[i] += '0';
}
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
mres[j][i] = nres[i][j];
}
}
}
swap(n, m);
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) cout << mres[i][j];
cout << '\n';
}
exit(0);
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3e5 + 10;
int r, s, best = 1e9 + 10;
int pref_row[MAXN], suff_row[MAXN], pref_col[MAXN], suff_col[MAXN];
vector<string> grid, sol, nxt;
inline void clear() {
for (int i = 0; i < r; ++i) {
for (int j = 0; j < s; ++j) nxt[i][j] = '.';
}
}
void check_best() {
int dist = 0;
for (int i = 0; i < r; ++i)
for (int j = 0; j < s; ++j) dist += nxt[i][j] != grid[i][j];
if (dist < best) {
best = dist;
sol = nxt;
}
}
void fill_grid(const string &p, int rr, int ss) {
clear();
for (int i = 0; i < 4; ++i) nxt[i / 2][i % 2] = p[i];
if (rr == -1 && ss == -1) {
for (int i = 0; i < r; ++i)
for (int j = 0; j < s; ++j) nxt[i][j] = nxt[i % 2][j % 2];
check_best();
return;
}
if (ss != -1) {
for (int j = 2; j < ss; ++j) {
nxt[0][j] = nxt[0][j - 2];
nxt[1][j] = nxt[1][j - 2];
}
nxt[0][ss] = nxt[1][ss - 2];
nxt[1][ss] = nxt[0][ss - 2];
for (int i = 2; i < r; ++i)
for (int j = 0; j <= ss; ++j) nxt[i][j] = nxt[i - 2][j];
for (int j = ss + 1; j < s; ++j) {
int a = 0, b = 0;
for (int i = 0; i < r; ++i) {
a += grid[i][j] != nxt[i % 2][j % 2];
b += grid[i][j] != nxt[(i + 1) % 2][j % 2];
}
for (int i = 0; i < r; ++i)
if (a < b)
nxt[i][j] = nxt[i % 2][j % 2];
else
nxt[i][j] = nxt[(i + 1) % 2][j % 2];
}
} else {
for (int i = 2; i < rr; ++i) {
nxt[i][0] = nxt[i - 2][0];
nxt[i][1] = nxt[i - 2][1];
}
nxt[rr][0] = nxt[rr - 2][1];
nxt[rr][1] = nxt[rr - 2][0];
for (int j = 2; j < s; ++j)
for (int i = 0; i <= rr; ++i) nxt[i][j] = nxt[i][j - 2];
for (int i = rr + 1; i < r; ++i) {
int a = 0, b = 0;
for (int j = 0; j < s; ++j) {
a += grid[i][j] != nxt[i % 2][j % 2];
b += grid[i][j] != nxt[i % 2][(j + 1) % 2];
}
for (int j = 0; j < s; ++j) {
if (a < b)
nxt[i][j] = nxt[i % 2][j % 2];
else
nxt[i][j] = nxt[i % 2][(j + 1) % 2];
}
}
}
check_best();
}
void init(const string &p) {
char P[3][3];
for (int i = 0; i < 4; ++i) P[i / 2][i % 2] = p[i];
for (int i = 0; i < r; ++i) {
int dist = 0;
for (int j = 0; j < s; ++j) dist += grid[i][j] != P[i % 2][j % 2];
pref_row[i] = dist;
if (i > 0) pref_row[i] += pref_row[i - 1];
}
for (int i = r - 1; i >= 0; --i) {
int a = 0, b = 0;
for (int j = 0; j < s; ++j) {
a += grid[i][j] != P[i % 2][j % 2];
b += grid[i][j] != P[i % 2][(j + 1) % 2];
}
suff_row[i] = min(a, b);
suff_row[i] += suff_row[i + 1];
}
for (int j = 0; j < s; ++j) {
int dist = 0;
for (int i = 0; i < r; ++i) dist += grid[i][j] != P[i % 2][j % 2];
pref_col[j] = dist;
if (j > 0) pref_col[j] += pref_col[j - 1];
}
for (int j = s - 1; j >= 0; --j) {
int a = 0, b = 0;
for (int i = 0; i < r; ++i) {
a += grid[i][j] != P[i % 2][j % 2];
b += grid[i][j] != P[(i + 1) % 2][j % 2];
}
suff_col[j] = min(a, b);
suff_col[j] += suff_col[j + 1];
}
}
void solve_grid(const string &p) {
init(p);
int rr = -1, ss = -1;
int curr = pref_row[r];
char P[3][3];
for (int i = 0; i < 4; ++i) P[i / 2][i % 2] = p[i];
int _best = best;
for (int i = 2; i < r; ++i) {
int rev = 0;
for (int j = 0; j < s; ++j) rev += grid[i][j] != P[i % 2][(j + 1) % 2];
if (pref_row[i - 1] + rev + suff_row[i + 1] < _best) {
_best = pref_row[i - 1] + rev + suff_row[i + 1];
rr = i;
ss = -1;
}
}
for (int j = 2; j < s; ++j) {
int rev = 0;
for (int i = 0; i < r; ++i) rev += grid[i][j] != P[(i + 1) % 2][j % 2];
if (pref_col[j - 1] + rev + suff_col[j + 1] < _best) {
_best = pref_col[j - 1] + rev + suff_col[j + 1];
rr = -1;
ss = j;
}
}
fill_grid(p, rr, ss);
}
int main(void) {
ios::sync_with_stdio(false);
cin >> r >> s;
for (int i = 0; i < r; ++i) {
string str;
cin >> str;
grid.push_back(str);
}
string perm = "ACGT";
nxt = grid;
do {
fill_grid(perm, -1, -1);
solve_grid(perm);
} while (next_permutation(perm.begin(), perm.end()));
for (int i = 0; i < r; ++i) cout << sol[i] << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
string s;
cin >> n >> m;
char c1[n][m], c2[m][n];
vector<vector<char> > bcn(2, vector<char>(n));
vector<vector<char> > bcm(2, vector<char>(m));
vector<vector<char> > cn(2, vector<char>(n));
vector<vector<char> > cm(2, vector<char>(m));
bool usen = true;
int res = n * m;
for (int i = 0; i < n; ++i) {
cin >> s;
for (int j = 0; j < m; ++j) {
c1[i][j] = s[j];
c2[j][i] = s[j];
}
}
s = "ACGT";
do {
int cur = 0;
for (int i = 0; i < m; ++i) {
char cp[2];
cp[0] = s[(i % 2) * 2];
cp[1] = s[(i % 2) * 2 + 1];
int cc[2];
cc[0] = 0;
cc[1] = 0;
for (int j = 0; j < n; ++j) {
if (c2[i][j] != cp[0]) cc[j % 2]++;
if (c2[i][j] != cp[1]) cc[(j + 1) % 2]++;
}
if (cc[0] < cc[1]) {
cm[0][i] = cp[0];
cm[1][i] = cp[1];
} else {
cm[0][i] = cp[1];
cm[1][i] = cp[0];
}
cur += min(cc[0], cc[1]);
}
if (cur < res) {
res = cur;
bcm = cm;
usen = false;
}
cur = 0;
for (int i = 0; i < n; ++i) {
char cp[2];
cp[0] = s[i % 2];
cp[1] = s[2 + (i % 2)];
int cc[2];
cc[0] = 0;
cc[1] = 0;
for (int j = 0; j < m; ++j) {
if (c1[i][j] != cp[0]) cc[j % 2]++;
if (c1[i][j] != cp[1]) cc[(j + 1) % 2]++;
}
if (cc[0] < cc[1]) {
cn[0][i] = cp[0];
cn[1][i] = cp[1];
} else {
cn[0][i] = cp[1];
cn[1][i] = cp[0];
}
cur += min(cc[0], cc[1]);
}
if (cur < res) {
res = cur;
bcn = cn;
usen = true;
}
} while (next_permutation(s.begin(), s.end()));
if (usen) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
c1[i][j] = bcn[j % 2][i];
}
}
} else {
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
c1[j][i] = bcm[j % 2][i];
}
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) cout << c1[i][j];
cout << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
char c[6][2];
char s[4] = {'A', 'C', 'G', 'T'};
int k = 0;
for (int i = 0; i < 4; ++i) {
for (int j = i + 1; j < 4; ++j) {
c[k][0] = s[i];
c[k][1] = s[j];
k++;
}
}
int m, n;
cin >> n >> m;
vector<string> v(n);
for (int l = 0; l < n; ++l) {
cin >> v[l];
}
int l;
int min_ = 1 << 30;
for (int i = 0; i < 6; ++i) {
string s1 = "";
string s2 = "";
for (int j = 0; j < m; ++j) {
s1 += c[i][j % 2];
s2 += c[i][(j + 1) % 2];
}
string s3 = "";
string s4 = "";
for (int j = 0; j < m; ++j) {
s3 += c[5 - i][j % 2];
s4 += c[5 - i][(j + 1) % 2];
}
int dist = 0;
for (int i1 = 0; i1 < n; ++i1) {
int d1, d2;
d1 = d2 = 0;
for (int j = 0; j < m; ++j) {
if (i1 % 2 == 0) {
if (v[i1][j] != s1[j]) d1++;
if (v[i1][j] != s2[j]) d2++;
} else {
if (v[i1][j] != s3[j]) d1++;
if (v[i1][j] != s4[j]) d2++;
}
}
dist += min(d1, d2);
}
if (dist < min_) {
min_ = dist;
l = i;
}
}
int l1;
int min1 = 1 << 30;
for (int i = 0; i < 6; ++i) {
string s1 = "";
string s2 = "";
for (int j = 0; j < n; ++j) {
s1 += c[i][j % 2];
s2 += c[i][(j + 1) % 2];
}
string s3 = "";
string s4 = "";
for (int j = 0; j < n; ++j) {
s3 += c[5 - i][j % 2];
s4 += c[5 - i][(j + 1) % 2];
}
int dist = 0;
for (int i1 = 0; i1 < m; ++i1) {
int d1, d2;
d1 = d2 = 0;
for (int j = 0; j < n; ++j) {
if (i1 % 2 == 0) {
if (v[j][i1] != s1[j]) d1++;
if (v[j][i1] != s2[j]) d2++;
} else {
if (v[j][i1] != s3[j]) d1++;
if (v[j][i1] != s4[j]) d2++;
}
}
dist += min(d1, d2);
}
if (dist < min1) {
min1 = dist;
l1 = i;
}
}
if (min_ < min1) {
string s1 = "";
string s2 = "";
for (int j = 0; j < m; ++j) {
s1 += c[l][j % 2];
s2 += c[l][(j + 1) % 2];
}
string s3 = "";
string s4 = "";
for (int j = 0; j < m; ++j) {
s3 += c[5 - l][j % 2];
s4 += c[5 - l][(j + 1) % 2];
}
int dist = 0;
for (int i1 = 0; i1 < n; ++i1) {
int d1, d2;
d1 = d2 = 0;
for (int j = 0; j < m; ++j) {
if (i1 % 2 == 0) {
if (v[i1][j] != s1[j]) d1++;
if (v[i1][j] != s2[j]) d2++;
} else {
if (v[i1][j] != s3[j]) d1++;
if (v[i1][j] != s4[j]) d2++;
}
}
if (d1 < d2) {
for (int j = 0; j < m; ++j) {
if (i1 % 2 == 0)
v[i1][j] = s1[j];
else
v[i1][j] = s3[j];
}
} else {
for (int j = 0; j < m; ++j) {
if (i1 % 2 == 0)
v[i1][j] = s2[j];
else
v[i1][j] = s4[j];
}
}
}
} else {
string s1 = "";
string s2 = "";
for (int j = 0; j < n; ++j) {
s1 += c[l1][j % 2];
s2 += c[l1][(j + 1) % 2];
}
string s3 = "";
string s4 = "";
for (int j = 0; j < n; ++j) {
s3 += c[5 - l1][j % 2];
s4 += c[5 - l1][(j + 1) % 2];
}
int dist = 0;
for (int i1 = 0; i1 < m; ++i1) {
int d1, d2;
d1 = d2 = 0;
for (int j = 0; j < n; ++j) {
if (i1 % 2 == 0) {
if (v[j][i1] != s1[j]) d1++;
if (v[j][i1] != s2[j]) d2++;
} else {
if (v[j][i1] != s3[j]) d1++;
if (v[j][i1] != s4[j]) d2++;
}
}
if (d1 < d2) {
for (int j = 0; j < n; ++j) {
if (i1 % 2 == 0)
v[j][i1] = s1[j];
else
v[j][i1] = s3[j];
}
} else {
for (int j = 0; j < n; ++j) {
if (i1 % 2 == 0)
v[j][i1] = s2[j];
else
v[j][i1] = s4[j];
}
}
}
}
for (int j1 = 0; j1 < n; ++j1) {
cout << v[j1] << endl;
}
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
long long sqr(long long x) { return x * x; }
long double sqrd(long double x) { return x * x; }
inline long long gcd(long long a, long long b) {
while (b) {
a %= b, swap(a, b);
}
return a;
}
inline long long lcm(long long a, long long b) {
return a * b / max(1ll, gcd(a, b));
}
mt19937 Random((long long)time(0));
inline long long rnd(long long x) { return Random() % x; }
const long long INF = (long long)1e9 + 282;
const long long MOD = (long long)1e9 + 7;
const long double PI = acosl(-1.0);
const long long N = 3e5 + 10;
vector<vector<long long>> first, ff;
vector<vector<vector<long long>>> rs, cs;
pair<long long, long long> w[4][4];
char ccc[4] = {'A', 'T', 'G', 'C'};
void calcR(long long r, long long a, long long b, long long n, long long m) {
for (long long i = 0; i < m; i++) {
if (i % 2 == 0) {
rs[r][a][b] += first[r][i] != a;
} else {
rs[r][a][b] += first[r][i] != b;
}
}
}
void calcC(long long c, long long a, long long b, long long n, long long m) {
for (long long i = 0; i < n; i++) {
if (i % 2 == 0) {
cs[c][a][b] += first[i][c] != a;
} else {
cs[c][a][b] += first[i][c] != b;
}
}
}
pair<long long, pair<long long, long long>> goV(long long n, long long m) {
pair<long long, pair<long long, long long>> ans = {INF, {-1, -1}};
for (long long a = 0; a < 4; a++) {
for (long long b = a + 1; b < 4; b++) {
long long c = w[a][b].first, d = w[a][b].second;
long long cur = 0;
for (long long i = 0; i < m; i++) {
if (i % 2 == 0) {
cur += min(cs[i][a][b], cs[i][b][a]);
} else {
cur += min(cs[i][c][d], cs[i][d][c]);
}
}
if (cur < ans.first) {
ans = {cur, {a, b}};
}
}
}
return ans;
}
pair<long long, pair<long long, long long>> goH(long long n, long long m) {
pair<long long, pair<long long, long long>> ans = {INF, {-1, -1}};
for (long long a = 0; a < 4; a++) {
for (long long b = a + 1; b < 4; b++) {
long long c = w[a][b].first, d = w[a][b].second;
long long cur = 0;
for (long long i = 0; i < n; i++) {
if (i % 2 == 0) {
cur += min(rs[i][a][b], rs[i][b][a]);
} else {
cur += min(rs[i][c][d], rs[i][d][c]);
}
}
if (cur < ans.first) {
ans = {cur, {a, b}};
}
}
}
return ans;
}
void goAnsV(long long n, long long m, long long a, long long b) {
long long c = w[a][b].first, d = w[a][b].second;
for (long long i = 0; i < m; i++) {
if (i % 2 == 0) {
if (cs[i][a][b] < cs[i][b][a]) {
for (long long j = 0; j < n; j++) {
if (j % 2 == 0) {
ff[j][i] = a;
} else {
ff[j][i] = b;
}
}
} else {
for (long long j = 0; j < n; j++) {
if (j % 2 == 0) {
ff[j][i] = b;
} else {
ff[j][i] = a;
}
}
}
} else {
if (cs[i][c][d] < cs[i][d][c]) {
for (long long j = 0; j < n; j++) {
if (j % 2 == 0) {
ff[j][i] = c;
} else {
ff[j][i] = d;
}
}
} else {
for (long long j = 0; j < n; j++) {
if (j % 2 == 0) {
ff[j][i] = d;
} else {
ff[j][i] = c;
}
}
}
}
}
}
void goAnsH(long long n, long long m, long long a, long long b) {
long long c = w[a][b].first, d = w[a][b].second;
for (long long i = 0; i < n; i++) {
if (i % 2 == 0) {
if (rs[i][a][b] < rs[i][b][a]) {
for (long long j = 0; j < m; j++) {
if (j % 2 == 0) {
ff[i][j] = a;
} else {
ff[i][j] = b;
}
}
} else {
for (long long j = 0; j < m; j++) {
if (j % 2 == 0) {
ff[i][j] = b;
} else {
ff[i][j] = a;
}
}
}
} else {
if (rs[i][c][d] < rs[i][d][c]) {
for (long long j = 0; j < m; j++) {
if (j % 2 == 0) {
ff[i][j] = c;
} else {
ff[i][j] = d;
}
}
} else {
for (long long j = 0; j < m; j++) {
if (j % 2 == 0) {
ff[i][j] = d;
} else {
ff[i][j] = c;
}
}
}
}
}
}
void mane() {
long long n, m;
cin >> n >> m;
first.resize(n, vector<long long>(m, 0));
map<char, long long> mp;
mp['A'] = 0;
mp['T'] = 1;
mp['G'] = 2;
mp['C'] = 3;
for (long long i = 0; i < 4; i++) {
for (long long j = i + 1; j < 4; j++) {
set<long long> st;
for (long long k = 0; k < 4; k++) {
st.insert(k);
}
st.erase(i);
st.erase(j);
w[i][j].first = *st.begin();
st.erase(st.begin());
w[i][j].second = *st.begin();
}
}
for (long long i = 0; i < n; i++) {
for (long long j = 0; j < m; j++) {
char c;
cin >> c;
first[i][j] = mp[c];
}
}
rs.resize(n, vector<vector<long long>>(4, vector<long long>(4, 0)));
cs.resize(m, vector<vector<long long>>(4, vector<long long>(4, 0)));
for (long long i = 0; i < n; i++) {
for (long long a = 0; a < 4; a++) {
for (long long b = 0; b < 4; b++) {
if (a == b) {
continue;
}
calcR(i, a, b, n, m);
}
}
}
for (long long i = 0; i < m; i++) {
for (long long a = 0; a < 4; a++) {
for (long long b = 0; b < 4; b++) {
if (a == b) {
continue;
}
calcC(i, a, b, n, m);
}
}
}
auto v = goV(n, m), h = goH(n, m);
ff = first;
if (v.first < h.first) {
goAnsV(n, m, v.second.first, v.second.second);
} else {
goAnsH(n, m, h.second.first, h.second.second);
}
for (long long i = 0; i < n; i++) {
for (long long j = 0; j < m; j++) {
cout << ccc[ff[i][j]];
}
cout << "\n";
}
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout.setf(ios_base::fixed, ios_base::floatfield);
cout.precision(12);
mane();
}
|
#include <bits/stdc++.h>
using namespace std;
const int AXN = 1e6;
int n, k;
vector<int> a[AXN];
int cur[20], ans_col[20], ans_row[20], pair_ans;
pair<int, int> col[20], row[20];
int calc_col(int q, int x, int y) {
int ans = 0;
for (int i = 0; i < n; i++)
if (i % 2 == 0)
ans += (a[i][q] != x);
else
ans += (a[i][q] != y);
return ans;
}
int calc_row(int q, int x, int y) {
int ans = 0;
for (int i = 0; i < k; i++)
if (i % 2 == 0)
ans += (a[q][i] != x);
else
ans += (a[q][i] != y);
return ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
for (int i = 0; i < n; i++) {
for (int q = 0; q < k; q++) {
char c;
cin >> c;
if (c == 'A') a[i].push_back(0);
if (c == 'C') a[i].push_back(1);
if (c == 'G') a[i].push_back(2);
if (c == 'T') a[i].push_back(3);
}
}
int ans = 1e9;
vector<pair<int, int> > col[k];
for (int q = 0; q < k; q++) {
col[q].resize(16);
for (int i = 0; i < 4; i++)
for (int w = i + 1; w < 4; w++) {
cur[(1 << i) + (1 << w)] = calc_col(q, i, w);
if (q % 2 == 0)
col[q][(1 << i) + (1 << w)] = make_pair(i, w);
else
col[q][15 ^ ((1 << i) + (1 << w))] = make_pair(i, w);
}
for (int i = 0; i < 4; i++)
for (int w = 0; w < i; w++) {
int k = calc_col(q, i, w);
if (cur[(1 << i) + (1 << w)] > k) {
cur[(1 << i) + (1 << w)] = k;
if (q % 2 == 0)
col[q][(1 << i) + (1 << w)] = make_pair(i, w);
else
col[q][15 ^ ((1 << i) + (1 << w))] = make_pair(i, w);
}
}
for (int i = 0; i < 4; i++)
for (int w = 0; w < i; w++)
if (q % 2)
ans_col[15 ^ ((1 << i) + (1 << w))] += cur[(1 << i) + (1 << w)];
else
ans_col[(1 << i) + (1 << w)] += cur[(1 << i) + (1 << w)];
}
for (int i = 0; i < 4; i++)
for (int w = 0; w < i; w++)
if (ans > ans_col[(1 << i) + (1 << w)]) {
ans = ans_col[(1 << i) + (1 << w)];
pair_ans = (1 << i) + (1 << w);
}
vector<pair<int, int> > row[n];
for (int q = 0; q < n; q++) {
row[q].resize(16);
for (int i = 0; i < 4; i++)
for (int w = i + 1; w < 4; w++) {
cur[(1 << i) + (1 << w)] = calc_row(q, i, w);
if (q % 2 == 0)
row[q][(1 << i) + (1 << w)] = make_pair(i, w);
else
row[q][15 ^ ((1 << i) + (1 << w))] = make_pair(i, w);
}
for (int i = 0; i < 4; i++)
for (int w = 0; w < i; w++) {
int k = calc_row(q, i, w);
if (cur[(1 << i) + (1 << w)] > k) {
cur[(1 << i) + (1 << w)] = k;
if (q % 2 == 0)
row[q][(1 << i) + (1 << w)] = make_pair(i, w);
else
row[q][15 ^ ((1 << i) + (1 << w))] = make_pair(i, w);
}
}
for (int i = 0; i < 4; i++)
for (int w = 0; w < i; w++)
if (q % 2)
ans_row[15 ^ ((1 << i) + (1 << w))] += cur[(1 << i) + (1 << w)];
else
ans_row[(1 << i) + (1 << w)] += cur[(1 << i) + (1 << w)];
}
bool flag = false;
for (int i = 0; i < 4; i++)
for (int w = 0; w < i; w++)
if (ans > ans_row[(1 << i) + (1 << w)]) {
flag = true;
ans = ans_row[(1 << i) + (1 << w)];
pair_ans = (1 << i) + (1 << w);
}
if (!flag) {
for (int i = 0; i < n; i++)
for (int q = 0; q < k; q++)
if (i % 2 == 0)
a[i][q] = col[q][pair_ans].first;
else
a[i][q] = col[q][pair_ans].second;
} else {
for (int i = 0; i < n; i++)
for (int q = 0; q < k; q++)
if (q % 2 == 0)
a[i][q] = row[i][pair_ans].first;
else
a[i][q] = row[i][pair_ans].second;
}
for (int i = 0; i < n; i++) {
for (int q = 0; q < k; q++) {
if (a[i][q] == 0) cout << 'A';
if (a[i][q] == 1) cout << 'C';
if (a[i][q] == 2) cout << 'G';
if (a[i][q] == 3) cout << 'T';
}
cout << "\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
void out() {
cout.flush();
int n;
cin >> n;
exit(0);
}
void WA() {
cout << "No\n";
out();
}
vector<string> s;
vector<string> invert(const vector<string> &s) {
int n = s.size();
int m = s[0].length();
vector<string> a(m, "");
for (int j = 0; j < m; ++j)
for (int i = 0; i < n; ++i) a[j].push_back(s[i][j]);
return a;
}
int findDist(const string &s, const string &t) {
int ans = 0;
for (int i = 0; i < (int)s.size(); ++i) ans += (s[i] != t[i]);
return ans;
}
string gen(int n, char c1, char c2) {
string s = "";
for (int i = 0; i < n; ++i) {
s.push_back(c1);
swap(c1, c2);
}
return s;
}
int ans;
pair<int, vector<string> > findHor() {
char CS[4] = {'A', 'C', 'G', 'T'};
int n = s.size();
int m = s[0].size();
pair<int, vector<string> > ans(1000000, vector<string>());
do {
string AC = gen(m, CS[0], CS[1]);
string CA = gen(m, CS[1], CS[0]);
string GT = gen(m, CS[2], CS[3]);
string TG = gen(m, CS[3], CS[2]);
vector<string> ss;
int sum = 0;
for (int i = 0; i < n; ++i) {
int d1 = findDist(s[i], AC);
int d2 = findDist(s[i], CA);
if (d1 < d2) {
sum += d1;
ss.push_back(AC);
} else {
sum += d2;
ss.push_back(CA);
}
AC.swap(GT);
CA.swap(TG);
}
if (ans.first > sum) ans = make_pair(sum, ss);
} while (next_permutation(CS, CS + 4));
return ans;
}
pair<int, vector<string> > findVert() {
s = invert(s);
pair<int, vector<string> > ans = findHor();
s = invert(s);
ans.second = invert(ans.second);
return ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
s.resize(n);
for (int i = 0; i < n; ++i) cin >> s[i];
pair<int, vector<string> > ans = min(findHor(), findVert());
for (int i = 0; i < n; ++i) cout << ans.second[i] << "\n";
out();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long int n, m, fc = pow(10, 6), cc;
int ifr = 0;
string s1, s2, p = "AGCT";
vector<string> v, a, s;
void comp(int isr) {
long long int i, j, rc1, rc2, f1, f2, f;
string cs;
cc = 0;
if (!isr) {
for (i = 0; i < n; i++) {
f1 = 0;
f2 = 1;
rc1 = 0;
rc2 = 0;
cs = s1;
if (i % 2) cs = s2;
for (j = 0; j < m; j++) {
if (cs[f1] - v[i][j]) rc1++;
if (cs[f2] - v[i][j]) rc2++;
f1 ^= 1;
f2 ^= 1;
}
f1 = 0;
f = 1;
if (rc1 < rc2) f = f1;
for (j = 0; j < m; j++) {
s[i][j] = cs[f];
f ^= 1;
}
cc += min(rc1, rc2);
}
} else {
for (i = 0; i < m; i++) {
f1 = 0;
f2 = 1;
rc1 = 0;
rc2 = 0;
cs = s1;
if (i % 2) cs = s2;
for (j = 0; j < n; j++) {
if (cs[f1] - v[j][i]) rc1++;
if (cs[f2] - v[j][i]) rc2++;
f1 ^= 1;
f2 ^= 1;
}
f1 = 0;
f = 1;
if (rc1 < rc2) f = f1;
for (j = 0; j < n; j++) {
s[j][i] = cs[f];
f ^= 1;
}
cc += min(rc1, rc2);
}
}
if (cc < fc) {
fc = cc;
a = s;
ifr = isr;
}
}
void gen(int isr) {
long long int i, j, k;
for (i = 0; i < 4; i++) {
for (j = i + 1; j < 4; j++) {
s1.clear();
s2.clear();
s1 += p[i];
s1 += p[j];
for (k = 0; k < 4; k++) {
if (k != i && k != j) s2 += p[k];
}
comp(isr);
}
}
}
int main() {
ios_base::sync_with_stdio(false);
long long int i;
cin >> n >> m;
v.resize(n);
for (i = 0; i < n; i++) cin >> v[i];
a = v;
s = v;
gen(0);
gen(1);
for (i = 0; i < n; i++) cout << a[i] << endl;
}
|
#include <bits/stdc++.h>
const int N = 300010;
std::string s[N];
char tmp[N];
int minx[N][12], miny[N][12];
char zzh[] = "ATGC";
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 0; i < n; ++i) {
scanf("%s", tmp);
s[i] = tmp;
}
std::map<std::pair<int, int>, int> map;
for (int i = 0; i < n; ++i) {
int cnt = 0;
for (int u = 0; u < 4; ++u) {
for (int v = 0; v < 4; ++v) {
if (u == v) continue;
map[{u, v}] = cnt;
for (int j = 0; j < m; ++j) {
if ((!(j & 1) && s[i][j] != zzh[u]) ||
((j & 1) && s[i][j] != zzh[v])) {
++minx[i][cnt];
}
}
++cnt;
}
}
}
for (int i = 0; i < m; ++i) {
int cnt = 0;
for (int u = 0; u < 4; ++u) {
for (int v = 0; v < 4; ++v) {
if (u == v) continue;
for (int j = 0; j < n; ++j) {
if ((!(j & 1) && s[j][i] != zzh[u]) ||
((j & 1) && s[j][i] != zzh[v])) {
++miny[i][cnt];
}
}
++cnt;
}
}
}
std::vector<int> oppo[12];
std::vector<int> same[12];
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
if (i == j) continue;
for (int u = 0; u < 4; ++u) {
if (u == i || u == j) continue;
for (int v = 0; v < 4; ++v) {
if (v == i || v == j || v == u) continue;
oppo[map[{i, j}]].push_back(map[{u, v}]);
}
}
same[map[{i, j}]].push_back(map[{i, j}]);
same[map[{i, j}]].push_back(map[{j, i}]);
}
}
int min = INT_MAX, type1, type2;
for (int i = 0; i < 12; ++i) {
int sum = 0;
for (int j = 0; j < n; ++j) {
int mmin = INT_MAX;
auto &vec = j & 1 ? oppo[i] : same[i];
for (auto u : vec) {
mmin = std::min(mmin, minx[j][u]);
}
sum += mmin;
}
if (sum < min) {
min = sum;
type1 = 0;
type2 = i;
}
}
for (int i = 0; i < 12; ++i) {
int sum = 0;
for (int j = 0; j < m; ++j) {
int mmin = INT_MAX;
auto &vec = j & 1 ? oppo[i] : same[i];
for (auto u : vec) {
mmin = std::min(mmin, miny[j][u]);
}
sum += mmin;
}
if (sum < min) {
min = sum;
type1 = 1;
type2 = i;
}
}
if (type1 == 0) {
for (int i = 0; i < n; ++i) {
int mmin = INT_MAX;
int type;
auto &vec = i & 1 ? oppo[type2] : same[type2];
for (auto u : vec) {
if (mmin > minx[i][u]) {
mmin = minx[i][u];
type = u;
}
}
int u, v;
for (int j = 0; j < 4; ++j) {
for (int k = 0; k < 4; ++k) {
if (j == k) continue;
if (map[{j, k}] == type) {
u = j;
v = k;
}
}
}
for (int j = 0; j < m; ++j) {
s[i][j] = j & 1 ? zzh[v] : zzh[u];
}
}
} else {
for (int i = 0; i < m; ++i) {
int mmin = INT_MAX;
int type;
auto &vec = i & 1 ? oppo[type2] : same[type2];
for (auto u : vec) {
if (mmin > miny[i][u]) {
mmin = miny[i][u];
type = u;
}
}
int u, v;
for (int j = 0; j < 4; ++j) {
for (int k = 0; k < 4; ++k) {
if (j == k) continue;
if (map[{j, k}] == type) {
u = j;
v = k;
}
}
}
for (int j = 0; j < n; ++j) {
s[j][i] = j & 1 ? zzh[v] : zzh[u];
}
}
}
for (int i = 0; i < n; ++i) {
printf("%s\n", s[i].c_str());
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int Maxn = 300005;
char choose[][2] = {{0, 0}, {'A', 'G'}, {'A', 'C'}, {'A', 'T'},
{'C', 'T'}, {'G', 'T'}, {'C', 'G'}};
int pos[3][Maxn][7], n, now[3], m, way1, way2, tot[3][Maxn];
string str[Maxn], T[Maxn];
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) cin >> str[i];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= 6; j++) {
now[1] = now[2] = 0;
for (int k = 1; k <= m; k++) {
now[1] += choose[(j + (i & 1) * 3 - 1) % 6 + 1][k & 1] != str[i][k - 1];
now[2] +=
choose[(j + (i & 1) * 3 - 1) % 6 + 1][!(k & 1)] != str[i][k - 1];
}
pos[0][i][j] = now[1] < now[2] ? 1 : 2;
tot[0][j] += min(now[1], now[2]);
}
}
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= 6; j++) {
now[1] = now[2] = 0;
for (int k = 1; k <= n; k++) {
now[1] += choose[(j + (i & 1) * 3 - 1) % 6 + 1][k & 1] != str[k][i - 1];
now[2] +=
choose[(j + (i & 1) * 3 - 1) % 6 + 1][!(k & 1)] != str[k][i - 1];
}
pos[1][i][j] = now[1] < now[2] ? 1 : 2;
tot[1][j] += min(now[1], now[2]);
}
}
int ans = 0x3f3f3f3f;
for (int i = 0; i <= 1; i++)
for (int j = 1; j <= 6; j++)
if (tot[i][j] < ans) ans = tot[i][j], way1 = i, way2 = j;
if (!way1) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++)
printf("%c", choose[(way2 + (i & 1) * 3 - 1) % 6 + 1]
[(j & 1) != (pos[0][i][way2] - 1)]);
printf("\n");
}
} else {
for (int i = 1; i <= m; i++)
for (int j = 1; j <= n; j++)
T[j] += choose[(way2 + (i & 1) * 3 - 1) % 6 + 1]
[(j & 1) != (pos[1][i][way2] - 1)];
for (int i = 1; i <= n; i++) cout << T[i] << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
const int N = 3e5 + 1;
using namespace std;
vector<char> dime[N], ans1[N];
string s[] = {"ATGC", "AGCT", "CAGT", "GCAT", "CTAG", "GTCA"};
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
string t;
int n, m, i, j, l, ans = N, type = -1, now, sum1, sum0;
bool iscac = true;
cin >> n >> m;
char x;
for (i = 1; i <= n; i++) {
dime[i].push_back('?');
ans1[i].push_back('?');
for (j = 1; j <= m; j++) {
cin >> x;
dime[i].push_back(x);
}
}
for (i = n + 1; i <= m; i++) {
ans1[i].push_back('?');
}
for (l = 0; l < 6; l++) {
t = s[l];
now = 0;
for (i = 1; i <= n; i++) {
sum0 = 0;
sum1 = 0;
for (j = 1; j <= m; j++) {
if (dime[i][j] != t[(j + 1) % 2 + (i % 2) * 2]) {
sum0++;
}
if (dime[i][j] != t[j % 2 + (i % 2) * 2]) {
sum1++;
}
}
now += min(sum0, sum1);
}
if (now < ans) {
ans = now;
iscac = false;
type = l;
}
now = 0;
for (i = 1; i <= m; i++) {
sum0 = 0;
sum1 = 0;
for (j = 1; j <= n; j++) {
if (dime[j][i] != t[(j + 1) % 2 + (i % 2) * 2]) {
sum0++;
}
if (dime[j][i] != t[j % 2 + (i % 2) * 2]) {
sum1++;
}
}
now += min(sum0, sum1);
}
if (now < ans) {
ans = now;
iscac = true;
type = l;
}
}
t = s[type];
if (!iscac) {
for (i = 1; i <= n; i++) {
sum0 = 0;
sum1 = 0;
for (j = 1; j <= m; j++) {
if (dime[i][j] != t[(j + 1) % 2 + (i % 2) * 2]) {
sum0++;
}
if (dime[i][j] != t[j % 2 + (i % 2) * 2]) {
sum1++;
}
}
if (sum0 < sum1) {
for (j = 1; j <= m; j++) {
ans1[i].push_back(t[(j + 1) % 2 + (i % 2) * 2]);
}
} else {
for (j = 1; j <= m; j++) {
ans1[i].push_back(t[j % 2 + (i % 2) * 2]);
}
}
}
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
cout << ans1[i][j];
}
cout << '\n';
}
} else {
for (i = 1; i <= m; i++) {
sum0 = 0;
sum1 = 0;
for (j = 1; j <= n; j++) {
if (dime[j][i] != t[(j + 1) % 2 + (i % 2) * 2]) {
sum0++;
}
if (dime[j][i] != t[j % 2 + (i % 2) * 2]) {
sum1++;
}
}
if (sum0 < sum1) {
for (j = 1; j <= n; j++) {
ans1[i].push_back(t[(j + 1) % 2 + (i % 2) * 2]);
}
} else {
for (j = 1; j <= n; j++) {
ans1[i].push_back(t[j % 2 + (i % 2) * 2]);
}
}
}
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
cout << ans1[j][i];
}
cout << '\n';
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
mt19937 gen_rand;
template <typename T, typename U>
ostream &operator<<(ostream &os, pair<T, U> &a) {
os << "(";
os << a.first << ", ";
os << a.second;
os << ")";
return os;
}
template <typename T>
ostream &operator<<(ostream &os, vector<T> &a) {
os << "{";
bool was = false;
for (auto &x : a) {
if (was) {
os << ", ";
}
was = true;
os << x;
}
os << "}";
return os;
}
template <typename T>
inline void debug(const char *sdbg, T x) {
cerr << "The value of " << sdbg << " is " << x << "\n";
};
template <typename T, typename... U>
inline void debug(const char *sdbg, T h, U... t) {
cerr << "The value of ";
while (*sdbg != ',') {
cerr << *sdbg++;
}
cerr << " is " << h << "\n";
debug(sdbg + 1, t...);
cerr << "\n";
}
template <typename T>
T abs(T x) {
if (x < 0) {
return -x;
} else {
return x;
}
}
template <typename T>
T sqr(T x) {
return x * x;
};
const long long INF_FOR_SHORT_TIME = (long long)(1e9);
template <typename T>
T mul(T a, T b, T m) {
if (a <= INF_FOR_SHORT_TIME && b <= INF_FOR_SHORT_TIME) {
return (a * b) % m;
}
T q = (long long)((long double)a * (long double)b / (long double)m);
T r = a * b - q * m;
return (r + 5 * m) % m;
}
template <typename T, typename U>
T binpow(T a, U n, T MOD) {
T res = 1;
while (n) {
if (n & 1) {
res = mul(res, a, MOD);
}
a = mul(a, a, MOD);
n >>= 1;
}
return res;
};
template <typename T>
int sign(T x) {
if (x < 0) {
return -1;
} else if (x > 0) {
return 1;
} else {
return 0;
}
};
template <typename T>
T gcd(T a, T b) {
if (a > b) {
swap(a, b);
}
while (a) {
b %= a;
swap(a, b);
}
return b;
};
template <typename T>
bool uin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
};
template <typename T>
bool uax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
};
int rev(char c) {
if (c == 'A') {
return 0;
} else if (c == 'C') {
return 1;
} else if (c == 'G') {
return 2;
} else {
return 3;
}
}
char rev(int c) {
if (c == 0) {
return 'A';
} else if (c == 1) {
return 'C';
} else if (c == 2) {
return 'G';
} else {
return 'T';
}
}
struct mtr {
int n, m;
vector<vector<int>> a;
void clr() {
n = 0;
m = 0;
a.clear();
}
void read() {
cin >> n >> m;
a.resize(n, vector<int>(m));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
char c;
cin >> c;
a[i][j] = rev(c);
}
}
}
void cpy(mtr &x) {
n = x.n;
m = x.m;
a = x.a;
}
void rot() {
vector<vector<int>> b(m, vector<int>(n));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
b[m - j - 1][i] = a[i][j];
}
}
swap(n, m);
swap(a, b);
}
void print() {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
cout << rev(a[i][j]);
}
cout << "\n";
}
cout << "\n";
}
};
pair<int, mtr> solve(mtr m) {
mtr res, cur;
cur.cpy(m);
res.cpy(m);
int cnt = m.n * m.m + 1;
for (int vf = 1; vf < 4; ++vf) {
for (int vs = 0; vs < vf; ++vs) {
vector<vector<int>> to(2);
to[0] = {vf, vs};
for (int i = 0; i < 4; ++i) {
if (vf == i || vs == i) {
continue;
}
to[1].push_back(i);
}
int cur_cnt = 0;
for (int i = 0; i < m.n; ++i) {
int cur0 = 0;
int cur1 = 0;
for (int j = 0; j < m.m; ++j) {
if (m.a[i][j] == to[i & 1][j & 1]) {
++cur0;
}
if (m.a[i][j] == to[i & 1][(j & 1) ^ 1]) {
++cur1;
}
}
int buf = 0;
if (uax(cur0, cur1)) {
buf = 1;
}
cur_cnt += (m.m - cur0);
for (int j = 0; j < m.m; ++j) {
cur.a[i][j] = to[i & 1][(j & 1) ^ buf];
}
}
if (uin(cnt, cur_cnt)) {
res = cur;
}
}
}
return make_pair(cnt, res);
}
int main() {
gen_rand.seed(time(0));
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
mtr m;
m.read();
auto res = solve(m);
m.rot();
auto new_res = solve(m);
if (res.first < new_res.first) {
res.second.print();
} else {
new_res.second.rot();
new_res.second.rot();
new_res.second.rot();
new_res.second.print();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
char **a;
char **sol;
int sol1(int n, int m, char c1, char c2, char c3, char c4) {
int ans = 0;
for (int i = 1; i <= n; ++i) {
int a1 = 0, a2 = 0;
if (i % 2) {
for (int j = 1; j <= m; ++j)
if (j % 2) {
if (a[i][j] != c1) a1++;
if (a[i][j] != c2) a2++;
} else {
if (a[i][j] != c2) a1++;
if (a[i][j] != c1) a2++;
}
} else {
for (int j = 1; j <= m; ++j)
if (j % 2) {
if (a[i][j] != c3) a1++;
if (a[i][j] != c4) a2++;
} else {
if (a[i][j] != c4) a1++;
if (a[i][j] != c3) a2++;
}
}
ans += min(a1, a2);
}
return ans;
}
int sol2(int n, int m, char c1, char c2, char c3, char c4) {
int ans = 0;
for (int j = 1; j <= m; ++j) {
int a1 = 0, a2 = 0;
if (j % 2) {
for (int i = 1; i <= n; ++i)
if (i % 2) {
if (a[i][j] != c1) a1++;
if (a[i][j] != c2) a2++;
} else {
if (a[i][j] != c2) a1++;
if (a[i][j] != c1) a2++;
}
} else {
for (int i = 1; i <= n; ++i)
if (i % 2) {
if (a[i][j] != c3) a1++;
if (a[i][j] != c4) a2++;
} else {
if (a[i][j] != c4) a1++;
if (a[i][j] != c3) a2++;
}
}
ans += min(a1, a2);
}
return ans;
}
void get1(int n, int m, char c1, char c2, char c3, char c4) {
for (int i = 1; i <= n; ++i) {
int a1 = 0, a2 = 0;
if (i % 2) {
for (int j = 1; j <= m; ++j)
if (j % 2) {
if (a[i][j] != c1) a1++;
if (a[i][j] != c2) a2++;
} else {
if (a[i][j] != c2)
a1++;
else
a2++;
}
if (a1 > a2) swap(c1, c2);
for (int j = 1; j <= m; ++j)
if (j % 2 == 1)
sol[i][j] = c1;
else
sol[i][j] = c2;
} else {
for (int j = 1; j <= m; ++j)
if (j % 2) {
if (a[i][j] != c3) a1++;
if (a[i][j] != c4) a2++;
} else {
if (a[i][j] != c4) a1++;
if (a[i][j] != c3) a2++;
}
if (a1 > a2) swap(c3, c4);
for (int j = 1; j <= m; ++j)
if (j % 2 == 1)
sol[i][j] = c3;
else
sol[i][j] = c4;
}
}
}
void get2(int n, int m, char c1, char c2, char c3, char c4) {
for (int j = 1; j <= m; ++j) {
int a1 = 0, a2 = 0;
if (j % 2) {
for (int i = 1; i <= n; ++i)
if (i % 2) {
if (a[i][j] != c1) a1++;
if (a[i][j] != c2) a2++;
} else {
if (a[i][j] != c2) a1++;
if (a[i][j] != c1) a2++;
}
if (a1 > a2) swap(c1, c2);
for (int i = 1; i <= n; ++i)
if (i % 2 == 1)
sol[i][j] = c1;
else
sol[i][j] = c2;
} else {
for (int i = 1; i <= n; ++i)
if (i % 2) {
if (a[i][j] != c3) a1++;
if (a[i][j] != c4) a2++;
} else {
if (a[i][j] != c4) a1++;
if (a[i][j] != c3) a2++;
}
if (a1 > a2) swap(c3, c4);
for (int i = 1; i <= n; ++i)
if (i % 2 == 1)
sol[i][j] = c3;
else
sol[i][j] = c4;
}
}
}
int main() {
int n, m;
scanf("%d%d ", &n, &m);
a = new char *[n + 5];
sol = new char *[n + 5];
for (int i = 1; i <= n; ++i) {
a[i] = new char[m + 5];
sol[i] = new char[m + 5];
}
for (int i = 1; i <= n; ++i) scanf("%s", a[i] + 1);
int m1 = sol1(n, m, 'A', 'C', 'T', 'G');
int m2 = sol1(n, m, 'A', 'G', 'C', 'T');
int m3 = sol1(n, m, 'C', 'G', 'A', 'T');
int m4 = sol1(n, m, 'C', 'T', 'A', 'G');
int m5 = sol1(n, m, 'A', 'T', 'C', 'G');
int m6 = sol1(n, m, 'G', 'T', 'A', 'C');
int m7 = sol2(n, m, 'A', 'C', 'T', 'G');
int m8 = sol2(n, m, 'A', 'G', 'C', 'T');
int m9 = sol2(n, m, 'C', 'G', 'A', 'T');
int m10 = sol2(n, m, 'C', 'T', 'A', 'G');
int m11 = sol2(n, m, 'A', 'T', 'C', 'G');
int m12 = sol2(n, m, 'G', 'T', 'A', 'C');
int k1 = min(min(min(min(min(m1, m2), m3), m4), m5), m6);
int k2 = min(min(min(min(min(m7, m8), m9), m10), m11), m12);
int k = min(k1, k2);
if (m1 == k)
get1(n, m, 'A', 'C', 'T', 'G');
else if (m2 == k)
get1(n, m, 'A', 'G', 'C', 'T');
else if (m3 == k)
get1(n, m, 'C', 'G', 'A', 'T');
else if (m4 == k)
get1(n, m, 'C', 'T', 'A', 'G');
else if (m5 == k)
get1(n, m, 'A', 'T', 'C', 'G');
else if (m6 == k)
get1(n, m, 'G', 'T', 'A', 'C');
else if (m7 == k)
get2(n, m, 'A', 'C', 'T', 'G');
else if (m8 == k)
get2(n, m, 'A', 'G', 'C', 'T');
else if (m9 == k)
get2(n, m, 'C', 'G', 'A', 'T');
else if (m10 == k)
get2(n, m, 'C', 'T', 'A', 'G');
else if (m11 == k)
get2(n, m, 'A', 'T', 'C', 'G');
else
get2(n, m, 'G', 'T', 'A', 'C');
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) printf("%c", sol[i][j]);
printf("\n");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 150000;
int n, m;
string M[N], A[N];
char p[4] = {'A', 'C', 'G', 'T'};
int v(bool save = false) {
int res = 0;
for (int j = 0; j < m; j++) {
int bx = 0, bs = 3 * N;
for (int x = 0; x < 2; x++) {
int s = 0;
for (int i = 0; i < n; i++) {
s += M[i][j] != p[((i + x) % 2) + 2 * (j % 2)] ? 1 : 0;
}
if (s < bs) {
bx = x;
bs = s;
}
}
int s = 0;
for (int i = 0; i < n; i++) {
s += M[i][j] != p[((i + bx) % 2) + 2 * (j % 2)] ? 1 : 0;
if (save) A[i][j] = p[((i + bx) % 2) + 2 * (j % 2)];
}
res += s;
}
return res;
}
int h(bool save = false) {
int res = 0;
for (int i = 0; i < n; i++) {
int bx = 0, bs = 3 * N;
for (int x = 0; x < 2; x++) {
int s = 0;
for (int j = 0; j < m; j++) {
s += M[i][j] != p[2 * (i % 2) + ((j + x) % 2)] ? 1 : 0;
}
if (s < bs) {
bx = x;
bs = s;
}
}
int s = 0;
for (int j = 0; j < m; j++) {
s += M[i][j] != p[2 * (i % 2) + ((j + bx) % 2)] ? 1 : 0;
if (save) A[i][j] = p[2 * (i % 2) + ((j + bx) % 2)];
}
res += s;
}
return res;
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> M[i];
A[i] = M[i];
}
int ans = 3 * N;
do {
if (ans > v()) {
ans = v();
v(true);
}
if (ans > h()) {
ans = h();
h(true);
}
} while (next_permutation(p, p + 4));
for (int i = 0; i < n; i++) cout << A[i] << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 10;
int n, m;
char ma[maxn];
int hexma[maxn];
int ans[maxn], tmp[maxn];
int rcal(int r, int a, int b) {
int res = 0;
for (int i = 0; i < m; i++) {
tmp[(r * m + i)] = (i & 1) ? a : b;
if (tmp[(r * m + i)] == hexma[(r * m + i)]) res++;
}
return res;
}
int ccal(int c, int a, int b) {
int res = 0;
for (int i = 0; i < n; i++) {
tmp[(i * m + c)] = (i & 1) ? a : b;
if (tmp[(i * m + c)] == hexma[(i * m + c)]) res++;
}
return res;
}
int rwork(int a, int b) {
int c, d, t;
int res = 0;
a = 1 << a;
b = 1 << b;
t = (1 << 4) - 1 - a - b;
c = t & -t;
d = t - c;
for (int i = 0; i < m; i++) {
if (i & 1)
tmp[i] = a;
else
tmp[i] = b;
if (tmp[i] == hexma[i]) res++;
}
for (int i = 1; i < n; i++) {
int p, q;
if (i & 1)
p = c, q = d;
else
p = a, q = b;
int c1 = rcal(i, p, q), c2 = rcal(i, q, p);
if (c1 < c2) swap(p, q), swap(c1, c2);
;
res += c1;
for (int j = 0; j < m; j++) tmp[(i * m + j)] = (j & 1) ? p : q;
}
return res;
}
int cwork(int a, int b) {
int c, d, t;
int res = 0;
a = 1 << a;
b = 1 << b;
t = (1 << 4) - 1 - a - b;
c = t & -t;
d = t - c;
for (int i = 0; i < n; i++) {
if (i & 1)
tmp[(i * m + 0)] = a;
else
tmp[(i * m + 0)] = b;
if (tmp[(i * m + 0)] == hexma[(i * m + 0)]) res++;
}
for (int i = 1; i < m; i++) {
int p, q;
if (i & 1)
p = c, q = d;
else
p = a, q = b;
int c1 = ccal(i, p, q), c2 = ccal(i, q, p);
if (c1 < c2) swap(p, q), swap(c1, c2);
;
res += c1;
for (int j = 0; j < n; j++) tmp[(j * m + i)] = (j & 1) ? p : q;
}
return res;
}
char rev(int x) {
char res = 0;
if (x == 1) res = 'A';
if (x == 2) res = 'C';
if (x == 4) res = 'G';
if (x == 8) res = 'T';
return res;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) {
getchar();
for (int j = 0; j < m; j++) {
scanf("%c", &ma[(i * m + j)]);
if (ma[(i * m + j)] == 'A') hexma[(i * m + j)] = 1;
if (ma[(i * m + j)] == 'C') hexma[(i * m + j)] = 2;
if (ma[(i * m + j)] == 'G') hexma[(i * m + j)] = 4;
if (ma[(i * m + j)] == 'T') hexma[(i * m + j)] = 8;
}
}
int now = 0;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++)
if (i != j) {
int temp = rwork(i, j);
if (temp > now) {
memcpy(ans, tmp, sizeof(int) * (n * m));
now = temp;
}
}
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++)
if (i != j) {
int temp = cwork(i, j);
if (temp > now) {
memcpy(ans, tmp, sizeof(int) * (n * m));
now = temp;
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) printf("%c", rev(ans[(i * m + j)]));
puts("");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline bool upmin(T &x, T y) {
return y < x ? x = y, 1 : 0;
}
template <typename T>
inline bool upmax(T &x, T y) {
return x < y ? x = y, 1 : 0;
}
const long double eps = 1e-11;
const long double pi = acos(-1);
const long long oo = 1 << 30;
const long long loo = 1ll << 62;
const long long mods = 1e9 + 7;
const long long MAXN = 600005;
const long long INF = 0x3f3f3f3f;
inline long long read() {
long long f = 1, x = 0;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = (x << 3) + (x << 1) + (c ^ 48);
c = getchar();
}
return x * f;
}
char st[MAXN];
long long a[MAXN], c[MAXN], b[2][2], n, m, ans = INF, flag;
pair<long long, long long> f[MAXN], F[MAXN];
long long get(long long x, long long y) { return (x - 1) * m + y; }
void print(long long x) {
if (x == 3) putchar('A');
if (x == 2) putchar('C');
if (x == 1) putchar('G');
if (x == 0) putchar('T');
}
void solve1() {
long long num = 0;
for (long long i = 0; i < n; i++) {
if (a[get(i, 0)] != b[i & 1][0]) num++;
if (a[get(i, 1)] != b[i & 1][1]) num++;
}
for (long long i = 2; i < m; i++) {
long long p = 0, q = 0;
for (long long j = 0; j < n; j++) {
if (a[get(j, i)] != b[j & 1][i & 1]) p++;
if (a[get(j, i)] != b[(j & 1) ^ 1][i & 1]) q++;
}
if (p < q)
num += p, f[i] = make_pair(b[0][i & 1], b[1][i & 1]);
else
num += q, f[i] = make_pair(b[1][i & 1], b[0][i & 1]);
}
if (upmin(ans, num)) {
flag = 0;
F[0] = make_pair(b[0][0], b[1][0]);
F[1] = make_pair(b[0][1], b[1][1]);
for (long long i = 2; i < m; i++) F[i] = f[i];
}
}
void solve2() {
long long num = 0;
for (long long i = 0; i < m; i++) {
if (a[get(0, i)] != b[0][i & 1]) num++;
if (a[get(1, i)] != b[1][i & 1]) num++;
}
for (long long i = 2; i < n; i++) {
long long p = 0, q = 0;
for (long long j = 0; j < m; j++) {
if (a[get(i, j)] != b[i & 1][j & 1]) p++;
if (a[get(i, j)] != b[i & 1][(j & 1) ^ 1]) q++;
}
if (p < q)
num += p, f[i] = make_pair(b[i & 1][0], b[i & 1][1]);
else
num += q, f[i] = make_pair(b[i & 1][1], b[i & 1][0]);
}
if (upmin(ans, num)) {
flag = 1;
F[0] = make_pair(b[0][0], b[0][1]);
F[1] = make_pair(b[1][0], b[1][1]);
for (long long i = 2; i < n; i++) F[i] = f[i];
}
}
signed main() {
n = read(), m = read();
for (long long i = 0; i < n; i++) {
scanf("%s", st);
for (long long j = 0; j < m; j++)
a[get(i, j)] = (st[j] < 'G') + (st[j] < 'C') + (st[j] < 'T');
}
for (long long i = 0; i < 4; i++)
for (long long j = 0; j < 4; j++)
for (long long k = 0; k < 4; k++)
for (long long t = 0; t < 4; t++) {
if (i == j || i == k || i == t || j == k || j == t || k == t)
continue;
b[0][0] = i, b[0][1] = j, b[1][0] = k, b[1][1] = t, solve1(),
solve2();
}
if (!flag) {
for (long long i = 0; i < n; i++) {
for (long long j = 0; j < m; j++)
if (i & 1)
print(F[j].second);
else
print(F[j].first);
puts("");
}
} else {
for (long long i = 0; i < n; i++) {
for (long long j = 0; j < m; j++)
if (j & 1)
print(F[i].second);
else
print(F[i].first);
puts("");
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
string aux[300000], sol[300000];
long funcion(char x, char y, char w, char z, string pal[], long mayor, long n,
long m) {
pair<long, long> xx, yy, ww, zz;
long cont = 0;
for (long i = 0; i < n; i++) {
xx.first = 0;
yy.first = 0;
ww.first = 0;
zz.first = 0;
xx.second = 0;
yy.second = 0;
ww.second = 0;
zz.second = 0;
aux[i].clear();
for (long j = 0; j < m; j++) {
if (i % 2 == 0) {
if (pal[i][j] == x && j % 2 == 0) xx.first++;
if (pal[i][j] == y && j % 2 == 0) yy.first++;
if (pal[i][j] == x && j % 2 == 1) xx.second++;
if (pal[i][j] == y && j % 2 == 1) yy.second++;
}
if (i % 2 == 1) {
if (pal[i][j] == w && j % 2 == 0) ww.first++;
if (pal[i][j] == z && j % 2 == 0) zz.first++;
if (pal[i][j] == w && j % 2 == 1) ww.second++;
if (pal[i][j] == z && j % 2 == 1) zz.second++;
}
}
if (i % 2 == 0) {
if (xx.first + yy.second > xx.second + yy.first) {
for (long j = 0; j < m; j++) {
if (j % 2 == 0) aux[i].push_back(x);
if (j % 2 == 1) aux[i].push_back(y);
}
cont += xx.first + yy.second;
} else {
for (long j = 0; j < m; j++) {
if (j % 2 == 0) aux[i].push_back(y);
if (j % 2 == 1) aux[i].push_back(x);
}
cont += yy.first + xx.second;
}
}
if (i % 2 == 1) {
if (ww.first + zz.second > ww.second + zz.first) {
for (long j = 0; j < m; j++) {
if (j % 2 == 0) aux[i].push_back(w);
if (j % 2 == 1) aux[i].push_back(z);
}
cont += ww.first + zz.second;
} else {
for (long j = 0; j < m; j++) {
if (j % 2 == 0) aux[i].push_back(z);
if (j % 2 == 1) aux[i].push_back(w);
}
cont += zz.first + ww.second;
}
}
}
if (cont > mayor) {
for (long i = 0; i < n; i++) {
sol[i].clear();
for (long j = 0; j < m; j++) {
sol[i].push_back(aux[i][j]);
}
}
mayor = cont;
}
return mayor;
}
long funcion2(char x, char y, char w, char z, string pal[], long mayor, long n,
long m) {
pair<long, long> xx, yy, ww, zz;
long cont = 0;
for (long j = 0; j < n; j++) {
aux[j].clear();
}
for (long i = 0; i < m; i++) {
xx.first = 0;
yy.first = 0;
ww.first = 0;
zz.first = 0;
xx.second = 0;
yy.second = 0;
ww.second = 0;
zz.second = 0;
for (long j = 0; j < n; j++) {
if (i % 2 == 0) {
if (pal[j][i] == x && j % 2 == 0) xx.first++;
if (pal[j][i] == y && j % 2 == 0) yy.first++;
if (pal[j][i] == x && j % 2 == 1) xx.second++;
if (pal[j][i] == y && j % 2 == 1) yy.second++;
}
if (i % 2 == 1) {
if (pal[j][i] == w && j % 2 == 0) ww.first++;
if (pal[j][i] == z && j % 2 == 0) zz.first++;
if (pal[j][i] == w && j % 2 == 1) ww.second++;
if (pal[j][i] == z && j % 2 == 1) zz.second++;
}
}
if (i % 2 == 0) {
if (xx.first + yy.second > xx.second + yy.first) {
for (long j = 0; j < n; j++) {
if (j % 2 == 0) aux[j].push_back(x);
if (j % 2 == 1) aux[j].push_back(y);
}
cont += xx.first + yy.second;
} else {
for (long j = 0; j < n; j++) {
if (j % 2 == 0) aux[j].push_back(y);
if (j % 2 == 1) aux[j].push_back(x);
}
cont += yy.first + xx.second;
}
}
if (i % 2 == 1) {
if (ww.first + zz.second > ww.second + zz.first) {
for (long j = 0; j < n; j++) {
if (j % 2 == 0) aux[j].push_back(w);
if (j % 2 == 1) aux[j].push_back(z);
}
cont += ww.first + zz.second;
} else {
for (long j = 0; j < n; j++) {
if (j % 2 == 0) aux[j].push_back(z);
if (j % 2 == 1) aux[j].push_back(w);
}
cont += zz.first + ww.second;
}
}
}
if (cont > mayor) {
for (long i = 0; i < n; i++) {
sol[i].clear();
for (long j = 0; j < m; j++) {
sol[i].push_back(aux[i][j]);
}
}
mayor = cont;
}
return mayor;
}
int main() {
long n, m;
cin >> n >> m;
string pal[300000];
for (long i = 0; i < n; i++) {
cin >> pal[i];
}
long mayor = 0;
mayor = funcion('A', 'C', 'G', 'T', pal, mayor, n, m);
mayor = funcion('G', 'T', 'A', 'C', pal, mayor, n, m);
mayor = funcion('A', 'G', 'C', 'T', pal, mayor, n, m);
mayor = funcion('A', 'T', 'G', 'C', pal, mayor, n, m);
mayor = funcion('C', 'T', 'A', 'G', pal, mayor, n, m);
mayor = funcion('G', 'C', 'A', 'T', pal, mayor, n, m);
mayor = funcion2('A', 'C', 'G', 'T', pal, mayor, n, m);
mayor = funcion2('G', 'T', 'A', 'C', pal, mayor, n, m);
mayor = funcion2('A', 'G', 'C', 'T', pal, mayor, n, m);
mayor = funcion2('A', 'T', 'G', 'C', pal, mayor, n, m);
mayor = funcion2('C', 'T', 'A', 'G', pal, mayor, n, m);
mayor = funcion2('G', 'C', 'A', 'T', pal, mayor, n, m);
for (long i = 0; i < n; i++) {
cout << sol[i] << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
char ch = getchar();
long long p = 1, data = 0;
while (ch < '0' || ch > '9') {
if (ch == '-') p = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
data = data * 10 + (ch ^ 48);
ch = getchar();
}
return p * data;
}
inline long long qpow(long long a, long long b) {
long long r = 1;
while (b) {
if (b & 1) r = a * r;
a = a * a;
b >>= 1;
}
return r;
}
inline long long gcd(long long a, long long b) {
if (!a || !b) return (!a) ? b : a;
while (b ^= a ^= b ^= a %= b)
;
return a;
}
inline long long lcm(long long a, long long b) { return a * b / gcd(a, b); }
void exgcd(long long a, long long b, long long &x, long long &y) {
if (!b)
x = 1, y = 0;
else {
exgcd(b, a % b, y, x);
y -= x * (a / b);
}
}
char s[4] = {'A', 'G', 'C', 'T'};
const int mod = 1e9 + 7, maxn = 3e5 + 5;
int n, m, ans = 0x3f3f3f3f;
vector<vector<char> > mp, pm, tans;
void gao(int a, int b, int c, int d) {
int now = 0;
for (int i = 1; i <= n; i++) {
int tnow = 0, ttnow = 0;
for (int j = 1; j <= m; j++) {
char ch;
if (i & 1) {
if (j & 1)
ch = s[a];
else
ch = s[b];
} else {
if (j & 1)
ch = s[c];
else
ch = s[d];
}
tnow += ch != mp[i][j];
}
for (int j = 1; j <= m; j++) {
char ch;
if (i & 1) {
if (j & 1)
ch = s[b];
else
ch = s[a];
} else {
if (j & 1)
ch = s[d];
else
ch = s[c];
}
ttnow += ch != mp[i][j];
}
if (tnow < ttnow)
for (int j = 1; j <= m; j++) {
char ch;
if (i & 1) {
if (j & 1)
ch = s[a];
else
ch = s[b];
} else {
if (j & 1)
ch = s[c];
else
ch = s[d];
}
pm[i][j] = ch;
}
else
for (int j = 1; j <= m; j++) {
char ch;
if (i & 1) {
if (j & 1)
ch = s[b];
else
ch = s[a];
} else {
if (j & 1)
ch = s[d];
else
ch = s[c];
}
pm[i][j] = ch;
}
now += min(tnow, ttnow);
}
if (ans > now) {
ans = now;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) tans[i][j] = pm[i][j];
}
now = 0;
for (int j = 1; j <= m; j++) {
int tnow = 0, ttnow = 0;
for (int i = 1; i <= n; i++) {
char ch;
if (j & 1) {
if (i & 1)
ch = s[a];
else
ch = s[b];
} else {
if (i & 1)
ch = s[c];
else
ch = s[d];
}
tnow += ch != mp[i][j];
}
for (int i = 1; i <= n; i++) {
char ch;
if (j & 1) {
if (i & 1)
ch = s[b];
else
ch = s[a];
} else {
if (i & 1)
ch = s[d];
else
ch = s[c];
}
ttnow += ch != mp[i][j];
}
if (tnow < ttnow)
for (int i = 1; i <= n; i++) {
char ch;
if (j & 1) {
if (i & 1)
ch = s[a];
else
ch = s[b];
} else {
if (i & 1)
ch = s[c];
else
ch = s[d];
}
pm[i][j] = ch;
}
else
for (int i = 1; i <= n; i++) {
char ch;
if (j & 1) {
if (i & 1)
ch = s[b];
else
ch = s[a];
} else {
if (i & 1)
ch = s[d];
else
ch = s[c];
}
pm[i][j] = ch;
}
now += min(tnow, ttnow);
}
if (ans > now) {
ans = now;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) tans[i][j] = pm[i][j];
}
}
int main() {
n = read(), m = read();
mp.resize(n + 1), pm.resize(n + 1), tans.resize(n + 1);
for (int i = 1; i <= n; i++) {
mp[i].resize(m + 1), pm[i].resize(m + 1), tans[i].resize(m + 1);
for (int j = 1; j <= m; j++) {
char c;
scanf("%c", &c);
mp[i][j] = c;
}
getchar();
}
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++) {
if (i != j) {
for (int l = 0; l < 4; l++)
for (int r = 0; r < 4; r++) {
if (l == r || l == i || l == j || r == i || r == j) continue;
gao(i, j, l, r);
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) printf("%c", tans[i][j]);
puts("");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void out(T x) {
cout << x << endl;
exit(0);
}
const int maxn = 2005;
int n, m;
vector<string> g;
pair<int, string> solveRow(const string& row, char c1, char c2) {
pair<int, string> s1, s2;
for (size_t i = 0; i < row.size(); i++) {
s1.second += c1;
s1.first += s1.second[i] != row[i];
s2.second += c2;
s2.first += s2.second[i] != row[i];
swap(c1, c2);
}
assert(s1.second.size() == row.size());
assert(s2.second.size() == row.size());
return min(s1, s2);
}
pair<int, vector<string>> solveRows(vector<string> g) {
pair<int, vector<string>> best;
vector<char> lets = {'A', 'C', 'G', 'T'};
for (int i = 0; i < 4; i++) {
for (int j = i + 1; j < 4; j++) {
int a = i;
int b = j;
int c = 0;
while (c == a || c == b) c++;
int d = c + 1;
while (d == a || d == b || d == c) d++;
pair<int, vector<string>> cur;
for (auto r : g) {
auto res = solveRow(r, lets[a], lets[b]);
cur.first += res.first;
cur.second.push_back(res.second);
swap(a, c);
swap(b, d);
}
if (best.second.empty() || cur < best) {
best = cur;
}
}
}
assert(!best.second.empty());
return best;
}
vector<string> transpose(vector<string> g) {
int n = g.size();
int m = g[0].length();
vector<string> h(m, string(n, 'x'));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
h[j][i] = g[i][j];
}
}
return h;
}
pair<int, vector<string>> solveCols(vector<string> g) {
g = transpose(g);
pair<int, vector<string>> res = solveRows(g);
res.second = transpose(res.second);
return res;
}
pair<int, vector<string>> solve(vector<string> g) {
return min(solveRows(g), solveCols(g));
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 0; i < n; i++) {
string buf;
cin >> buf;
g.push_back(buf);
}
pair<int, vector<string>> res = solve(g);
cerr << res.first << endl;
for (auto r : res.second) {
cout << r << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m;
int maxval;
vector<int> agct;
int roc;
int encr[255];
int decr[255];
vector<vector<int> > mat;
int row[300000][4][2];
int col[300000][4][2];
static void determinemax(vector<int> v) {
int onrow = 0, oncol = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0)
onrow += max(row[i][v[0]][0] + row[i][v[1]][1],
row[i][v[0]][1] + row[i][v[1]][0]);
else
onrow += max(row[i][v[2]][0] + row[i][v[3]][1],
row[i][v[2]][1] + row[i][v[3]][0]);
}
for (int i = 0; i < m; i++) {
if (i % 2 == 0)
oncol += max(col[i][v[0]][0] + col[i][v[2]][1],
col[i][v[0]][1] + col[i][v[2]][0]);
else {
oncol += max(col[i][v[1]][0] + col[i][v[3]][1],
col[i][v[1]][1] + col[i][v[3]][0]);
}
}
if (onrow > maxval) {
maxval = onrow;
agct = v;
roc = 'r';
}
if (oncol > maxval) {
maxval = oncol;
agct = v;
roc = 'c';
}
if (next_permutation(v.begin(), v.end())) determinemax(v);
return;
}
int main() {
encr['A'] = 0;
encr['C'] = 1;
encr['G'] = 2;
encr['T'] = 3;
decr[0] = 'A';
decr[1] = 'C';
decr[2] = 'G';
decr[3] = 'T';
char ch;
cin >> n >> m;
mat.resize(n, vector<int>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> ch;
ch = encr[ch];
mat[i][j] = ch;
row[i][ch][j % 2]++;
col[j][ch][i % 2]++;
}
}
vector<int> v = {0, 1, 2, 3};
determinemax(v);
v = agct;
if (roc == 'r') {
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
if (row[i][v[0]][0] + row[i][v[1]][1] >
row[i][v[0]][1] + row[i][v[1]][0]) {
for (int j = 0; j < m; j++) {
mat[i][j] = v[j & 1];
}
} else {
for (int j = 0; j < m; j++) {
mat[i][j] = v[!(j & 1)];
}
}
} else {
if (row[i][v[2]][0] + row[i][v[3]][1] >
row[i][v[2]][1] + row[i][v[3]][0]) {
for (int j = 0; j < m; j++) {
mat[i][j] = v[2 + (j & 1)];
}
} else {
for (int j = 0; j < m; j++) {
mat[i][j] = v[2 + !(j & 1)];
}
}
}
}
}
if (roc == 'c') {
for (int i = 0; i < m; i++) {
if (i % 2 == 0) {
if (col[i][v[0]][0] + col[i][v[2]][1] >
col[i][v[0]][1] + col[i][v[2]][0])
for (int j = 0; j < n; j++) mat[j][i] = v[(j & 1) << 1];
else
for (int j = 0; j < n; j++) mat[j][i] = v[!(j & 1) << 1];
} else {
if (col[i][v[1]][0] + col[i][v[3]][1] >
col[i][v[1]][1] + col[i][v[3]][0])
for (int j = 0; j < n; j++) mat[j][i] = v[1 + (((j & 1)) << 1)];
else
for (int j = 0; j < n; j++) mat[j][i] = v[1 + ((!(j & 1)) << 1)];
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << (char)decr[mat[i][j]];
}
cout << '\n';
}
}
|
#include <bits/stdc++.h>
using namespace std;
using ull = uint64_t;
using ll = int64_t;
using ld = long double;
string st = "AGCT";
const int N = 300228;
string s[N];
string t[N];
string best[N];
string curr[N];
int n, m;
int ans = N;
bool ansinv = false;
bool cinv;
void solve() {
for (int iter = 0; iter < 24; ++iter) {
int cur = 0;
next_permutation(st.begin(), st.end());
for (int j = 0; j < n; ++j) {
int p = (j % 2) * 2;
int cv = N;
array<int, 2> cl{};
string str[2];
for (int l = 0; l < 2; ++l) {
for (int i = 0; i < m; ++i) {
char ch = st[p + (l ^ (i & 1))];
str[l].push_back(ch);
cl[l] += s[j][i] != str[l][i];
}
}
if (cl[0] < cl[1]) {
curr[j] = str[0];
cur += cl[0];
} else {
curr[j] = str[1];
cur += cl[1];
}
}
if (ans > cur) {
copy(curr, curr + n, best);
ansinv = cinv;
ans = cur;
}
}
}
void invert(decltype(t) s) {
for (int i = 0; i < m; ++i) {
t[i] = "";
t[i].reserve(n);
for (int j = 0; j < n; ++j) {
t[i] += s[j][i];
}
}
copy(t, t + m, s);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cout.setf(ios::fixed);
cout.precision(20);
cin >> n >> m;
for (int i = 0; i < n; ++i) {
cin >> s[i];
}
solve();
invert(s);
swap(n, m);
cinv = true;
solve();
if (ansinv) {
invert(best);
}
swap(n, m);
for (int i = 0; i < n; ++i) {
cout << best[i] << "\n";
}
cerr << ans << "\n";
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, mn, tmp, t1, t2, side;
char r, ch[5], ans[5];
vector<char> v[300005], a[300005];
int main() {
scanf("%d%d", &n, &m);
mn = n * m;
ch[1] = 'A';
ch[2] = 'C';
ch[3] = 'G';
ch[4] = 'T';
for (int i = 1; i <= n; i++) {
scanf("\n");
v[i].push_back(' ');
for (int j = 1; j <= m; j++) {
scanf("%c", &r);
v[i].push_back(r);
}
}
do {
tmp = 0;
for (int i = 1; i <= m; i++) {
t1 = t2 = 0;
if (i % 2)
for (int j = 1; j <= n; j++) {
if (j % 2) {
if (v[j][i] != ch[1]) t1++;
if (v[j][i] != ch[2]) t2++;
} else {
if (v[j][i] != ch[2]) t1++;
if (v[j][i] != ch[1]) t2++;
}
}
else
for (int j = 1; j <= n; j++) {
if (j % 2) {
if (v[j][i] != ch[3]) t1++;
if (v[j][i] != ch[4]) t2++;
} else {
if (v[j][i] != ch[4]) t1++;
if (v[j][i] != ch[3]) t2++;
}
}
tmp += min(t1, t2);
}
if (tmp < mn) {
mn = tmp;
for (int i = 1; i <= 4; i++) {
ans[i] = ch[i];
}
side = 1;
}
tmp = 0;
for (int i = 1; i <= n; i++) {
t1 = t2 = 0;
if (i % 2)
for (int j = 1; j <= m; j++) {
if (j % 2) {
if (v[i][j] != ch[1]) t1++;
if (v[i][j] != ch[2]) t2++;
} else {
if (v[i][j] != ch[2]) t1++;
if (v[i][j] != ch[1]) t2++;
}
}
else
for (int j = 1; j <= m; j++) {
if (j % 2) {
if (v[i][j] != ch[3]) t1++;
if (v[i][j] != ch[4]) t2++;
} else {
if (v[i][j] != ch[4]) t1++;
if (v[i][j] != ch[3]) t2++;
}
}
tmp += min(t1, t2);
}
if (tmp < mn) {
mn = tmp;
for (int i = 1; i <= 4; i++) {
ans[i] = ch[i];
}
side = 0;
}
} while (next_permutation(ch + 1, ch + 5));
for (int i = 1; i <= n; i++) {
a[i].push_back(' ');
}
if (side) {
for (int i = 1; i <= m; i++) {
t1 = t2 = 0;
if (i % 2)
for (int j = 1; j <= n; j++) {
if (j % 2) {
if (v[j][i] != ans[1]) t1++;
if (v[j][i] != ans[2]) t2++;
} else {
if (v[j][i] != ans[2]) t1++;
if (v[j][i] != ans[1]) t2++;
}
}
else
for (int j = 1; j <= n; j++) {
if (j % 2) {
if (v[j][i] != ans[3]) t1++;
if (v[j][i] != ans[4]) t2++;
} else {
if (v[j][i] != ans[4]) t1++;
if (v[j][i] != ans[3]) t2++;
}
}
if (t1 < t2) {
for (int j = 1; j <= n; j++) {
if (i % 2) {
if (j % 2)
a[j].push_back(ans[1]);
else
a[j].push_back(ans[2]);
} else {
if (j % 2)
a[j].push_back(ans[3]);
else
a[j].push_back(ans[4]);
}
}
} else {
for (int j = 1; j <= n; j++) {
if (i % 2) {
if (j % 2)
a[j].push_back(ans[2]);
else
a[j].push_back(ans[1]);
} else {
if (j % 2)
a[j].push_back(ans[4]);
else
a[j].push_back(ans[3]);
}
}
}
}
} else {
for (int i = 1; i <= n; i++) {
t1 = t2 = 0;
if (i % 2)
for (int j = 1; j <= m; j++) {
if (j % 2) {
if (v[i][j] != ans[1]) t1++;
if (v[i][j] != ans[2]) t2++;
} else {
if (v[i][j] != ans[2]) t1++;
if (v[i][j] != ans[1]) t2++;
}
}
else
for (int j = 1; j <= m; j++) {
if (j % 2) {
if (v[i][j] != ans[3]) t1++;
if (v[i][j] != ans[4]) t2++;
} else {
if (v[i][j] != ans[4]) t1++;
if (v[i][j] != ans[3]) t2++;
}
}
if (t1 < t2) {
for (int j = 1; j <= m; j++) {
if (i % 2) {
if (j % 2)
a[i].push_back(ans[1]);
else
a[i].push_back(ans[2]);
} else {
if (j % 2)
a[i].push_back(ans[3]);
else
a[i].push_back(ans[4]);
}
}
} else {
for (int j = 1; j <= m; j++) {
if (i % 2) {
if (j % 2)
a[i].push_back(ans[2]);
else
a[i].push_back(ans[1]);
} else {
if (j % 2)
a[i].push_back(ans[4]);
else
a[i].push_back(ans[3]);
}
}
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
printf("%c", a[i][j]);
}
printf("\n");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long n, m;
long long ans;
int os[300001];
int se[300001];
int sol[300001];
void ss(int a, int b, int c, int d) {
long long va = 0;
se[0] = a;
se[m] = b;
se[2 * m] = c;
se[1 + m] = d;
for (int i = 1; i < m; i++) {
for (int j = 1; j <= 4; j++) {
if (i != 1 && j != se[(i - 1)] && j != se[(i - 1) + m] &&
j != se[(i - 1) + 2 * m]) {
se[i + m] = j;
}
}
for (int j = 1; j <= 4; j++) {
if (j != se[(i - 1)] && j != se[(i - 1) + m] && j != se[i + m]) {
se[i] = j;
}
if (j != se[(i - 1) + 2 * m] && j != se[(i - 1) + m] && j != se[i + m]) {
se[i + 2 * m] = j;
}
}
}
for (int i = 3; i < n; i++) {
for (int j = 1; j <= 4; j++) {
if (j != se[(i - 1) * m] && j != se[(i - 1) * m + 1] &&
j != se[(i - 1) * m + 2]) {
se[i * m + 1] = j;
}
}
for (int j = 1; j <= 4; j++) {
if (j != se[(i - 1) * m] && j != se[(i - 1) * m + 1] &&
j != se[i * m + 1]) {
se[i * m] = j;
}
}
for (int k = 2; k < m; k++) {
bool f = true;
for (int j = 1; j <= 4; j++) {
if (j != se[i * m + k - 1] && j != se[(i - 1) * m + k] &&
j != se[(i - 1) * m + (k - 1)] &&
(k + 1 >= m || j != se[(i - 1) * m + (k + 1)])) {
se[i * m + k] = j;
f = false;
}
}
if (f) {
va = n * m + 1;
break;
}
}
}
if (va == 0) {
for (int i = 0; i < n * m; i++) {
if (se[i] != os[i]) va++;
}
}
if (va < ans) {
ans = va;
for (int i = 0; i < n * m; i++) {
sol[i] = se[i];
}
}
}
void s(int a, int b) {
int as = a, bs = b;
int c = 0;
int d = 0;
for (int i = 1; i <= 4; i++) {
if (i != a && i != b) {
if (c == 0) {
c = i;
} else {
d = i;
}
}
}
long long va = 0;
for (int i = 0; i < m; i++) {
if (i % 2 == 1) {
a = c;
b = d;
}
long long x = 0;
long long y = 0;
for (int j = 0; j < n; j++) {
if (os[j * m + i] == a && j % 2 == 0) x++;
if (os[j * m + i] == a && j % 2 == 1) y++;
if (os[j * m + i] == b && j % 2 == 0) y++;
if (os[j * m + i] == b && j % 2 == 1) x++;
}
if (x >= y) {
for (int j = 0; j < n; j++) {
if (j % 2 == 0) {
se[j * m + i] = a;
} else {
se[j * m + i] = b;
}
}
} else {
for (int j = 0; j < n; j++) {
if (j % 2 == 0) {
se[j * m + i] = b;
} else {
se[j * m + i] = a;
}
}
}
if (i % 2 == 1) {
a = as;
b = bs;
}
}
for (int i = 0; i < n * m; i++) {
if (os[i] != se[i]) va++;
}
if (va < ans) {
ans = va;
for (int i = 0; i < n * m; i++) {
sol[i] = se[i];
}
}
}
void o(int a, int b) {
int as = a;
int bs = b;
int c = 0;
int d = 0;
for (int i = 1; i <= 4; i++) {
if (i != a && i != b) {
if (c == 0) {
c = i;
} else {
d = i;
}
}
}
long long va = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 1) {
a = c;
b = d;
}
long long x = 0;
long long y = 0;
for (int j = 0; j < m; j++) {
if (os[j + i * m] == a && j % 2 == 0) x++;
if (os[j + i * m] == a && j % 2 == 1) y++;
if (os[j + i * m] == b && j % 2 == 0) y++;
if (os[j + i * m] == b && j % 2 == 1) x++;
}
if (x >= y) {
for (int j = 0; j < m; j++) {
if (j % 2 == 0) {
se[j + i * m] = a;
} else {
se[j + i * m] = b;
}
}
} else {
for (int j = 0; j < m; j++) {
if (j % 2 == 0) {
se[j + i * m] = b;
} else {
se[j + i * m] = a;
}
}
}
if (i % 2 == 1) {
a = as;
b = bs;
}
}
for (int i = 0; i < n * m; i++) {
if (os[i] != se[i]) va++;
}
if (va < ans) {
ans = va;
for (int i = 0; i < n * m; i++) {
sol[i] = se[i];
}
}
}
void sz(int a, int b) {
int c = 0;
int d = 0;
for (int i = 1; i <= 4; i++) {
if (i != a && i != b) {
if (c == 0) {
c = i;
} else {
d = i;
}
}
}
long long va = 0;
for (int i = 0; i < m; i++) {
if (i % 2 == 0) {
if ((os[i] == a && os[i + m] == b) || (os[i] == b && os[i + m] == a)) {
se[i] = os[i];
se[i + m] = os[i + m];
} else {
if (os[i] == a || os[i] == b) {
va++;
se[i] = os[i];
if (os[i] == a) {
se[i + m] = b;
} else {
se[i + m] = a;
}
} else {
if (os[i + m] == a || os[i + m] == b) {
va++;
se[i + m] = os[i + m];
if (os[i + m] == a) {
se[i] = b;
} else {
se[i] = a;
}
} else {
va += 2;
se[i] = a;
se[i + m] = b;
}
}
}
} else {
int as = a;
int bs = b;
a = c;
b = d;
if ((os[i] == a && os[i + m] == b) || (os[i] == b && os[i + m] == a)) {
se[i] = os[i];
se[i + m] = os[i + m];
} else {
if (os[i] == a || os[i] == b) {
va++;
se[i] = os[i];
if (os[i] == a) {
se[i + m] = b;
} else {
se[i + m] = a;
}
} else {
if (os[i + m] == a || os[i + m] == b) {
va++;
se[i + m] = os[i + m];
if (os[i + m] == a) {
se[i] = b;
} else {
se[i] = a;
}
} else {
va += 2;
se[i] = a;
se[i + m] = b;
}
}
}
a = as;
b = bs;
}
}
if (va < ans) {
ans = va;
for (int i = 0; i < n * m; i++) {
sol[i] = se[i];
}
}
}
void szb(int a, int b) {
int c = 0;
int d = 0;
for (int i = 1; i <= 4; i++) {
if (i != a && i != b) {
if (c == 0) {
c = i;
} else {
d = i;
}
}
}
long long va = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
if ((os[2 * i] == a && os[2 * i + 1] == b) ||
(os[2 * i] == b && os[2 * i + 1] == a)) {
se[2 * i] = os[2 * i];
se[2 * i + 1] = os[2 * i + 1];
} else {
if (os[2 * i] == a || os[2 * i] == b) {
va++;
se[2 * i] = os[2 * i];
if (os[2 * i] == a) {
se[2 * i + 1] = b;
} else {
se[2 * i + 1] = a;
}
} else {
if (os[2 * i + 1] == a || os[2 * i + 1] == b) {
va++;
se[2 * i + 1] = os[2 * i + 1];
if (os[2 * i + 1] == a) {
se[2 * i] = b;
} else {
se[2 * i] = a;
}
} else {
va += 2;
se[2 * i] = a;
se[2 * i + 1] = b;
}
}
}
} else {
int as = a;
int bs = b;
a = c;
b = d;
if ((os[2 * i] == a && os[2 * i + 1] == b) ||
(os[2 * i] == b && os[2 * i + 1] == a)) {
se[2 * i] = os[2 * i];
se[2 * i + 1] = os[2 * i + 1];
} else {
if (os[2 * i] == a || os[2 * i] == b) {
va++;
se[2 * i] = os[2 * i];
if (os[2 * i] == a) {
se[2 * i + 1] = b;
} else {
se[2 * i + 1] = a;
}
} else {
if (os[2 * i + 1] == a || os[2 * i + 1] == b) {
va++;
se[2 * i + 1] = os[2 * i + 1];
if (os[2 * i + 1] == a) {
se[2 * i] = b;
} else {
se[2 * i] = a;
}
} else {
va += 2;
se[2 * i] = a;
se[2 * i + 1] = b;
}
}
}
a = as;
b = bs;
}
}
if (va < ans) {
ans = va;
for (int i = 0; i < n * m; i++) {
sol[i] = se[i];
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> m;
ans = n * m + 1;
char c;
int a[n + 1][m + 1];
int b[n + 1][m + 1];
int kk = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> c;
if (c == 'A') a[i][j] = 1;
if (c == 'G') a[i][j] = 2;
if (c == 'C') a[i][j] = 3;
if (c == 'T') a[i][j] = 4;
b[i][j] = a[i][j];
os[kk] = a[i][j];
kk++;
}
}
if (n == 2) {
for (int i = 1; i <= 4; i++) {
for (int j = i + 1; j <= 4; j++) {
sz(i, j);
}
}
} else {
if (m == 2) {
for (int i = 1; i <= 4; i++) {
for (int j = i + 1; j <= 4; j++) {
szb(i, j);
}
}
} else {
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= 4; j++) {
if (i != j) {
s(i, j);
o(i, j);
}
}
}
}
}
kk = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (sol[kk] == 1) cout << 'A';
if (sol[kk] == 2) cout << 'G';
if (sol[kk] == 3) cout << 'C';
if (sol[kk] == 4) cout << 'T';
kk++;
}
cout << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m;
char ss[300005];
vector<char> s[300005];
vector<pair<char, char> > w = {{'A', 'T'}, {'A', 'C'}, {'A', 'G'},
{'T', 'C'}, {'T', 'G'}, {'C', 'G'}};
vector<pair<char, char> > rw = {{'C', 'G'}, {'T', 'G'}, {'C', 'T'},
{'A', 'G'}, {'A', 'C'}, {'T', 'A'}};
int co[300005];
int main() {
scanf("%d%d", &(n), &(m));
;
for (int i = (0); i < (n); ++i) {
scanf("%s", ss);
for (int j = (0); j < (m); ++j) s[i].push_back(ss[j]);
}
int best = 0;
int czyW = 1;
int xxx = 0;
for (int ii = (0); ii < (6); ++ii) {
int ile = 0;
for (int i = (0); i < (n); ++i) {
char L1 = w[ii].first, L2 = w[ii].second;
if (i % 2 == 1) {
L1 = rw[ii].first, L2 = rw[ii].second;
}
int x[2];
x[0] = x[1] = 0;
for (int j = (0); j < (m); ++j) {
if (s[i][j] == L1) x[j % 2]++;
if (s[i][j] == L2) x[1 - (j % 2)]++;
}
ile += max(x[0], x[1]);
}
if (ile > best) {
best = ile;
xxx = ii;
}
}
for (int ii = (0); ii < (6); ++ii) {
int ile = 0;
for (int i = (0); i < (m); ++i) {
char L1 = w[ii].first, L2 = w[ii].second;
if (i % 2 == 1) {
L1 = rw[ii].first, L2 = rw[ii].second;
}
int x[2];
x[0] = x[1] = 0;
for (int j = (0); j < (n); ++j) {
if (s[j][i] == L1) x[j % 2]++;
if (s[j][i] == L2) x[1 - (j % 2)]++;
}
ile += max(x[0], x[1]);
}
if (ile > best) {
best = ile;
xxx = ii;
czyW = 0;
}
}
if (czyW == 1) {
int ii = xxx;
for (int i = (0); i < (n); ++i) {
char L1 = w[ii].first, L2 = w[ii].second;
if (i % 2 == 1) {
L1 = rw[ii].first, L2 = rw[ii].second;
}
int x[2];
x[0] = x[1] = 0;
for (int j = (0); j < (m); ++j) {
if (s[i][j] == L1) x[j % 2]++;
if (s[i][j] == L2) x[1 - (j % 2)]++;
}
if (x[0] >= x[1]) {
for (int j = (0); j < (m); ++j) {
if (j % 2)
s[i][j] = L2;
else
s[i][j] = L1;
}
} else {
for (int j = (0); j < (m); ++j) {
if (j % 2)
s[i][j] = L1;
else
s[i][j] = L2;
}
}
}
} else {
int ii = xxx;
for (int i = (0); i < (m); ++i) {
char L1 = w[ii].first, L2 = w[ii].second;
if (i % 2 == 1) {
L1 = rw[ii].first, L2 = rw[ii].second;
}
int x[2];
x[0] = x[1] = 0;
for (int j = (0); j < (n); ++j) {
if (s[j][i] == L1) x[j % 2]++;
if (s[j][i] == L2) x[1 - (j % 2)]++;
}
if (x[0] >= x[1]) {
for (int j = (0); j < (n); ++j) {
if (j % 2)
s[j][i] = L2;
else
s[j][i] = L1;
}
} else {
for (int j = (0); j < (n); ++j) {
if (j % 2)
s[j][i] = L1;
else
s[j][i] = L2;
}
}
}
}
for (int i = (0); i < (n); ++i) {
for (int j = (0); j < (m); ++j) {
putchar(s[i][j]);
}
putchar('\n');
}
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, ans, flag;
string mat[300005];
char pr[10] = "ACGT", sol[10];
int solve_row(int d = 0) {
int res = 0;
for (int i = 0; i <= n - 1; ++i) {
char a[2];
if (i & 1) {
a[0] = pr[0];
a[1] = pr[1];
} else {
a[0] = pr[2];
a[1] = pr[3];
}
int sa = 0, sb = 0;
for (int j = 0; j <= m - 1; ++j) {
sa += mat[i][j] != a[j & 1];
sb += mat[i][j] != a[!(j & 1)];
}
if (sa < sb) {
res += sa;
if (d)
for (int j = 0; j <= m - 1; ++j) mat[i][j] = a[j & 1];
} else {
res += sb;
if (d)
for (int j = 0; j <= m - 1; ++j) mat[i][j] = a[!(j & 1)];
}
}
return res;
}
int solve_col(int d = 0) {
int res = 0;
for (int i = 0; i <= m - 1; ++i) {
char a[2];
if (i & 1) {
a[0] = pr[0];
a[1] = pr[1];
} else {
a[0] = pr[2];
a[1] = pr[3];
}
int sa = 0, sb = 0;
for (int j = 0; j <= n - 1; ++j) {
sa += mat[j][i] != a[j & 1];
sb += mat[j][i] != a[!(j & 1)];
}
if (sa < sb) {
res += sa;
if (d)
for (int j = 0; j <= n - 1; ++j) mat[j][i] = a[j & 1];
} else {
res += sb;
if (d)
for (int j = 0; j <= n - 1; ++j) mat[j][i] = a[!(j & 1)];
}
}
return res;
}
int main() {
ios::sync_with_stdio(false);
cin >> n >> m;
ans = n * m;
for (int i = 0; i <= n - 1; ++i) cin >> mat[i];
do {
int t;
if ((t = solve_row()) < ans) {
ans = t;
strcpy(sol, pr);
flag = 0;
}
if ((t = solve_col()) < ans) {
ans = t;
strcpy(sol, pr);
flag = 1;
}
} while (next_permutation(pr, pr + 4));
strcpy(pr, sol);
if (!flag)
solve_row(1);
else
solve_col(1);
for (int i = 0; i <= n - 1; ++i) cout << mat[i] << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int check(vector<char> &ai, string &temp) {
int res = 0, e = 0;
for (int i = 0; i < ai.size(); i++, e = 1 - e) {
res += (temp[e] != ai[i]);
}
return res;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m, mn = 1e8, now, J, K, e;
cin >> n >> m;
vector<vector<char>> a(n, vector<char>(m));
vector<vector<char>> b(m, vector<char>(n));
vector<vector<char>> c(n, vector<char>(m, '-'));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> a[i][j];
b[j][i] = a[i][j];
}
}
vector<vector<string>> alf = {{"AC", "CA", "GT", "TG"},
{"AT", "TA", "GC", "CG"},
{"AG", "GA", "TC", "CT"}};
for (int k = 0; k < 2; k++) {
for (int j = 0; j < 6; j++) {
now = 0;
for (int i = 0; i + 1 < (k ? n : m); i += 2) {
int ab = check(k ? a[i] : b[i], alf[j >> 1][0 + ((j & 1) << 1)]);
int ba = check(k ? a[i] : b[i], alf[j >> 1][1 + ((j & 1) << 1)]);
int cd =
check(k ? a[i + 1] : b[i + 1], alf[j >> 1][2 - ((j & 1) << 1)]);
int dc =
check(k ? a[i + 1] : b[i + 1], alf[j >> 1][3 - ((j & 1) << 1)]);
now += min(ab, ba) + min(cd, dc);
}
int ab = check(k ? a[n - 1] : b[m - 1], alf[j >> 1][0 + ((j & 1) << 1)]);
int ba = check(k ? a[n - 1] : b[m - 1], alf[j >> 1][1 + ((j & 1) << 1)]);
now += (((k ? n : m) % 2) ? min(ab, ba) : 0);
if (mn > now) {
mn = now;
J = j, K = k;
}
}
}
for (int i = 0; i + 1 < (K ? n : m); i += 2) {
int ab = check(K ? a[i] : b[i], alf[J >> 1][0 + ((J & 1) << 1)]);
int ba = check(K ? a[i] : b[i], alf[J >> 1][1 + ((J & 1) << 1)]);
int cd = check(K ? a[i + 1] : b[i + 1], alf[J >> 1][2 - ((J & 1) << 1)]);
int dc = check(K ? a[i + 1] : b[i + 1], alf[J >> 1][3 - ((J & 1) << 1)]);
for (int j = 0, e = 0; j < (K ? m : n); j++, e = 1 - e)
c[(K ? i : j)][(K ? j : i)] = alf[J >> 1][(ab > ba) + ((J & 1) << 1)][e];
for (int j = 0, e = 0; j < (K ? m : n); j++, e = 1 - e)
c[(K ? i + 1 : j)][(K ? j : i + 1)] =
alf[J >> 1][2 + (cd > dc) - ((J & 1) << 1)][e];
}
int ab = check(K ? a[n - 1] : b[m - 1], alf[J >> 1][0 + ((J & 1) << 1)]);
int ba = check(K ? a[n - 1] : b[m - 1], alf[J >> 1][1 + ((J & 1) << 1)]);
if ((K ? n : m) % 2) {
for (int j = 0, e = 0; j < (K ? m : n); j++, e = 1 - e)
c[(K ? n - 1 : j)][(K ? j : m - 1)] =
alf[J >> 1][(ab > ba) + ((J & 1) << 1)][e];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << c[i][j];
}
cout << '\n';
}
}
|
#include <bits/stdc++.h>
using namespace std;
vector<vector<char>> best;
long long best_res = 1e18;
string s = "ACGT";
vector<char> row1, row2;
vector<vector<char>> current;
void solve(vector<vector<char>> a, bool rev) {
long long n = a.size();
long long m = a[0].size();
current.resize(n, vector<char>(m));
row1.resize(m);
row2.resize(m);
for (long long n1 = 0; n1 < 3; n1++) {
for (long long n2 = n1 + 1; n2 < 4; n2++) {
for (long long n3 = 0; n3 < 3; n3++) {
if (n3 == n1 || n3 == n2) continue;
for (long long n4 = n3 + 1; n4 < 4; n4++) {
if (n4 == n1 || n4 == n2) continue;
char c[2][2] = {{s[n1], s[n2]}, {s[n3], s[n4]}};
long long res = 0;
for (long long i = 0; i < n; i++) {
long long res1 = 0, res2 = 0;
for (long long j = 0; j < m; j++) {
res1 += a[i][j] != c[i % 2][j % 2];
row1[j] = c[i % 2][j % 2];
res2 += a[i][j] != c[i % 2][(j + 1) % 2];
row2[j] = c[i % 2][(j + 1) % 2];
}
if (res1 < res2) {
res += res1;
current[i] = row1;
} else {
res += res2;
current[i] = row2;
}
}
if (res < best_res) {
best_res = res;
for (long long i = 0; i < n; i++) {
for (long long j = 0; j < m; j++) {
if (!rev) {
best[i][j] = current[i][j];
} else {
best[j][i] = current[i][j];
}
}
}
}
}
}
}
}
}
signed main() {
cin.tie(0);
cout.tie(0);
ios_base ::sync_with_stdio(0);
long long n, m;
cin >> n >> m;
best.resize(n, vector<char>(m));
vector<vector<char>> a(n, vector<char>(m));
for (long long i = 0; i < n; i++) {
for (long long j = 0; j < m; j++) {
cin >> a[i][j];
}
}
solve(a, false);
vector<vector<char>> b(m, vector<char>(n));
for (long long i = 0; i < n; i++) {
for (long long j = 0; j < m; j++) {
b[j][i] = a[i][j];
}
}
solve(b, true);
for (long long i = 0; i < n; i++) {
for (long long j = 0; j < m; j++) {
cout << best[i][j];
}
cout << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int M = 998244353;
int n, i, m, A, C, o[4], j, sum, an = 1 << 30;
string c[300005], ans[300005], p[300005];
int compare() {
int j, k, s = 0;
for (j = 0; j < n; j++)
for (k = 0; k < m; k++)
if (ans[j][k] != c[j][k]) s++;
return s;
}
void dfs(int i) {
int j, k, flag = 0;
if (i == 4) {
for (j = 0; j < 4; j++)
for (k = j + 1; k < 4; k++)
if (o[j] == o[k]) flag = 1;
if (flag == 0) {
for (j = 0; j < n; j++) ans[j] = c[j];
for (j = 0; j < n; j += 2) {
A = C = 0;
for (k = 0; k < m; k++) {
if (k % 2 == 0) {
if (c[j][k] != o[0]) A++;
if (c[j][k] != o[1]) C++;
} else {
if (c[j][k] != o[0]) A--;
if (c[j][k] != o[1]) C--;
}
}
if (A < C) {
for (k = 0; k < m; k += 2) ans[j][k] = o[0];
for (k = 1; k < m; k += 2) ans[j][k] = o[1];
} else {
for (k = 0; k < m; k += 2) ans[j][k] = o[1];
for (k = 1; k < m; k += 2) ans[j][k] = o[0];
}
}
for (j = 1; j < n; j += 2) {
A = C = 0;
for (k = 0; k < m; k++) {
if (k % 2 == 0) {
if (c[j][k] != o[2]) A++;
if (c[j][k] != o[3]) C++;
} else {
if (c[j][k] != o[2]) A--;
if (c[j][k] != o[3]) C--;
}
}
if (A < C) {
for (k = 0; k < m; k += 2) ans[j][k] = o[2];
for (k = 1; k < m; k += 2) ans[j][k] = o[3];
} else {
for (k = 0; k < m; k += 2) ans[j][k] = o[3];
for (k = 1; k < m; k += 2) ans[j][k] = o[2];
}
}
int s = compare();
if (s < an) {
an = s;
for (j = 0; j < n; j++) p[j] = ans[j];
}
for (j = 0; j < n; j++) ans[j] = c[j];
for (j = 0; j < m; j += 2) {
A = C = 0;
for (k = 0; k < n; k++) {
if (k % 2 == 0) {
if (c[k][j] != o[0]) A++;
if (c[k][j] != o[1]) C++;
} else {
if (c[k][j] != o[0]) A--;
if (c[k][j] != o[1]) C--;
}
}
if (A < C) {
for (k = 0; k < n; k += 2) ans[k][j] = o[0];
for (k = 1; k < n; k += 2) ans[k][j] = o[1];
} else {
for (k = 0; k < n; k += 2) ans[k][j] = o[1];
for (k = 1; k < n; k += 2) ans[k][j] = o[0];
}
}
for (j = 1; j < m; j += 2) {
A = C = 0;
for (k = 0; k < n; k++) {
if (k % 2 == 0) {
if (c[k][j] != o[2]) A++;
if (c[k][j] != o[3]) C++;
} else {
if (c[k][j] != o[2]) A--;
if (c[k][j] != o[3]) C--;
}
}
if (A < C) {
for (k = 0; k < n; k += 2) ans[k][j] = o[2];
for (k = 1; k < n; k += 2) ans[k][j] = o[3];
} else {
for (k = 0; k < n; k += 2) ans[k][j] = o[3];
for (k = 1; k < n; k += 2) ans[k][j] = o[2];
}
}
int ss = compare();
if (ss < an) {
an = ss;
for (j = 0; j < n; j++) p[j] = ans[j];
}
sum++;
}
return;
}
o[i] = 'A';
dfs(i + 1);
o[i] = 'T';
dfs(i + 1);
o[i] = 'G';
dfs(i + 1);
o[i] = 'C';
dfs(i + 1);
}
int main() {
scanf("%d %d\n", &n, &m);
for (i = 0; i < n; i++) cin >> c[i];
dfs(0);
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) printf("%c", p[i][j]);
printf("\n");
}
}
|
#include <bits/stdc++.h>
using namespace std;
int cmp_str(string &a, string &b) {
int s = 0;
for (int i = 0; i < a.size(); i++) s += (a[i] == b[i]);
return s;
}
string gen_str(int s, char a, char b) {
string res = "";
for (int i = 0; i < s; i++)
if (i % 2 == 1)
res += b;
else
res += a;
return res;
}
int solve(vector<string> &src, string pat, vector<string> &ans) {
ans.clear();
int res = 0;
string a1 = gen_str(src[0].size(), pat[0], pat[1]);
string a2 = gen_str(src[0].size(), pat[1], pat[0]);
string b1 = gen_str(src[0].size(), pat[2], pat[3]);
string b2 = gen_str(src[0].size(), pat[3], pat[2]);
for (int i = 0; i < src.size(); i++) {
if (i % 2 == 0) {
int x1 = cmp_str(src[i], a1);
int x2 = cmp_str(src[i], a2);
if (x1 > x2)
ans.push_back(a1);
else
ans.push_back(a2);
res += max(x1, x2);
} else {
int x1 = cmp_str(src[i], b1);
int x2 = cmp_str(src[i], b2);
if (x1 > x2)
ans.push_back(b1);
else
ans.push_back(b2);
res += max(x1, x2);
}
}
return res;
}
void trans(vector<string> &A, vector<string> &B) {
B.clear();
B.resize(A[0].size(), "");
for (int i = 0; i < A.size(); i++)
for (int j = 0; j < A[i].size(); j++) B[j] += A[i][j];
}
int main() {
int n, m;
cin >> n >> m;
vector<string> A(n), B, C, D;
for (int i = 0; i < n; i++) cin >> A[i];
trans(A, D);
vector<string> st = {"AGCT", "ACGT", "ATCG", "GCAT", "GTAC", "CTAG"};
int best = 0;
for (int i = 0; i < 6; i++) {
int r = solve(A, st[i], B);
if (r > best) {
best = r;
swap(C, B);
}
r = solve(D, st[i], B);
if (r > best) {
best = r;
trans(B, C);
}
}
for (int i = 0; i < n; i++) cout << C[i] << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 500000;
int n, m;
vector<char> board[300010];
vector<int> row[300010];
vector<int> col[300010];
string str[6];
int main() {
str[0] = "ACTG";
str[1] = "ATCG";
str[2] = "AGCT";
str[3] = "CTAG";
str[4] = "CGAT";
str[5] = "TGAC";
cin.tie(0);
ios_base::sync_with_stdio(false);
cin >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
char x;
cin >> x;
board[i].push_back(x);
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < 12; j++) row[i].push_back(0);
for (int j = 0; j < m; j++) {
if (j % 2 == 0) {
if (board[i][j] == 'A') {
row[i][0]++;
row[i][1]++;
row[i][2]++;
} else if (board[i][j] == 'C') {
row[i][3]++;
row[i][4]++;
row[i][6]++;
} else if (board[i][j] == 'T') {
row[i][5]++;
row[i][7]++;
row[i][9]++;
} else {
row[i][8]++;
row[i][10]++;
row[i][11]++;
}
} else {
if (board[i][j] == 'A') {
row[i][6]++;
row[i][7]++;
row[i][8]++;
} else if (board[i][j] == 'C') {
row[i][0]++;
row[i][9]++;
row[i][10]++;
} else if (board[i][j] == 'T') {
row[i][1]++;
row[i][3]++;
row[i][11]++;
} else {
row[i][2]++;
row[i][4]++;
row[i][5]++;
}
}
}
}
for (int j = 0; j < m; j++) {
for (int i = 0; i < 12; i++) col[j].push_back(0);
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
if (board[i][j] == 'A') {
col[j][0]++;
col[j][1]++;
col[j][2]++;
} else if (board[i][j] == 'C') {
col[j][3]++;
col[j][4]++;
col[j][6]++;
} else if (board[i][j] == 'T') {
col[j][5]++;
col[j][7]++;
col[j][9]++;
} else {
col[j][8]++;
col[j][10]++;
col[j][11]++;
}
} else {
if (board[i][j] == 'A') {
col[j][6]++;
col[j][7]++;
col[j][8]++;
} else if (board[i][j] == 'C') {
col[j][0]++;
col[j][9]++;
col[j][10]++;
} else if (board[i][j] == 'T') {
col[j][1]++;
col[j][3]++;
col[j][11]++;
} else {
col[j][2]++;
col[j][4]++;
col[j][5]++;
}
}
}
}
int r[6] = {};
int c[6] = {};
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
r[0] += max(row[i][0], row[i][6]);
r[1] += max(row[i][1], row[i][7]);
r[2] += max(row[i][2], row[i][8]);
r[3] += max(row[i][3], row[i][9]);
r[4] += max(row[i][4], row[i][10]);
r[5] += max(row[i][5], row[i][11]);
} else {
r[0] += max(row[i][5], row[i][11]);
r[1] += max(row[i][4], row[i][10]);
r[2] += max(row[i][3], row[i][9]);
r[3] += max(row[i][2], row[i][8]);
r[4] += max(row[i][1], row[i][7]);
r[5] += max(row[i][0], row[i][6]);
}
}
for (int i = 0; i < m; i++) {
if (i % 2 == 0) {
c[0] += max(col[i][0], col[i][6]);
c[1] += max(col[i][1], col[i][7]);
c[2] += max(col[i][2], col[i][8]);
c[3] += max(col[i][3], col[i][9]);
c[4] += max(col[i][4], col[i][10]);
c[5] += max(col[i][5], col[i][11]);
} else {
c[0] += max(col[i][5], col[i][11]);
c[1] += max(col[i][4], col[i][10]);
c[2] += max(col[i][3], col[i][9]);
c[3] += max(col[i][2], col[i][8]);
c[4] += max(col[i][1], col[i][7]);
c[5] += max(col[i][0], col[i][6]);
}
}
if (*max_element(r, r + 6) > *max_element(c, c + 6)) {
int id = max_element(r, r + 6) - r;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (i % 2 == 0 && j % 2 == 0)
board[i][j] =
((row[i][id] > row[i][id + 6]) ? str[id][0] : str[id][1]);
else if (i % 2 == 0 && j % 2 == 1)
board[i][j] =
((row[i][id] > row[i][id + 6]) ? str[id][1] : str[id][0]);
else if (i % 2 == 1 && j % 2 == 0)
board[i][j] =
((row[i][5 - id] > row[i][11 - id]) ? str[id][2] : str[id][3]);
else
board[i][j] =
((row[i][5 - id] > row[i][11 - id]) ? str[id][3] : str[id][2]);
}
}
} else {
int id = max_element(c, c + 6) - c;
for (int j = 0; j < m; j++) {
for (int i = 0; i < n; i++) {
if (j % 2 == 0 && i % 2 == 0)
board[i][j] =
((col[j][id] > col[j][id + 6]) ? str[id][0] : str[id][1]);
else if (j % 2 == 0 && i % 2 == 1)
board[i][j] =
((col[j][id] > col[j][id + 6]) ? str[id][1] : str[id][0]);
else if (j % 2 == 1 && i % 2 == 0)
board[i][j] =
((col[j][5 - id] > col[j][11 - id]) ? str[id][2] : str[id][3]);
else
board[i][j] =
((col[j][5 - id] > col[j][11 - id]) ? str[id][3] : str[id][2]);
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << board[i][j];
}
cout << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
string a[300010], b[300010], answer[300010];
char ch[5] = "AGCT";
int f[2][4][4][300010];
int ans, ans1, ans2, ans3;
inline void solve(int n, int m, int opt) {
for (int x = 0; x < 4; ++x)
for (int y = 0; y < 4; ++y)
if (x != y) {
char c1 = ch[x], c2 = ch[y];
for (int i = 1; i <= n; ++i) {
int s = 0;
for (int j = 1; j <= m; ++j)
if (j & 1)
s += a[i][j] != c1;
else
s += a[i][j] != c2;
f[opt][x][y][i] = s;
}
}
for (int x = 0; x < 4; ++x)
for (int y = 0; y < 4; ++y)
if (x != y) {
int z, w;
for (z = 0; z < 4; ++z)
if (z != x && z != y) break;
for (w = 0; w < 4; ++w)
if (w != x && w != y && w != z) break;
char c1 = ch[x], c2 = ch[y], c3 = ch[z], c4 = ch[w];
int s = f[opt][x][y][1];
for (int i = 2; i <= n; ++i)
if (i & 1)
s += min(f[opt][x][y][i], f[opt][y][x][i]);
else
s += min(f[opt][z][w][i], f[opt][w][z][i]);
if (s < ans) {
ans = s;
ans1 = x;
ans2 = y;
ans3 = opt;
}
}
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
ans = n * m;
for (int i = 1; i <= n; ++i) {
cin >> b[i];
a[i].resize(m + 5);
answer[i].resize(m + 5);
}
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j) a[i][j] = b[i][j - 1];
solve(n, m, 0);
for (int i = 1; i <= m; ++i) {
a[i].clear();
a[i].resize(n + 5);
}
for (int i = 1; i <= m; ++i)
for (int j = 1; j <= n; ++j) a[i][j] = b[j][i - 1];
solve(m, n, 1);
int x = ans1, y = ans2;
int z, w;
for (z = 0; z < 4; ++z)
if (z != x && z != y) break;
for (w = 0; w < 4; ++w)
if (w != x && w != y && w != z) break;
if (ans3) {
for (int i = 1; i <= m; ++i)
if (i & 1) {
if (f[ans3][x][y][i] > f[ans3][y][x][i]) swap(x, y);
for (int j = 1; j <= n; ++j) {
if (j & 1)
answer[j][i] = ch[x];
else
answer[j][i] = ch[y];
}
} else {
if (f[ans3][z][w][i] > f[ans3][w][z][i]) swap(z, w);
for (int j = 1; j <= n; ++j) {
if (j & 1)
answer[j][i] = ch[z];
else
answer[j][i] = ch[w];
}
}
} else {
for (int i = 1; i <= n; ++i)
if (i & 1) {
if (f[ans3][x][y][i] > f[ans3][y][x][i]) swap(x, y);
for (int j = 1; j <= m; ++j) {
if (j & 1)
answer[i][j] = ch[x];
else
answer[i][j] = ch[y];
}
} else {
if (f[ans3][z][w][i] > f[ans3][w][z][i]) swap(z, w);
for (int j = 1; j <= m; ++j) {
if (j & 1)
answer[i][j] = ch[z];
else
answer[i][j] = ch[w];
}
}
}
for (int i = 1; i <= n; ++i, printf("\n"))
for (int j = 1; j <= m; ++j) printf("%c", answer[i][j]);
}
|
#include <bits/stdc++.h>
using namespace std;
vector<char> w = {'A', 'T', 'G', 'C'};
pair<int, vector<vector<int> > > solve(const vector<string> &a) {
int n = a.size();
int m = a[0].size();
int cost[4][4][m][2];
memset(cost, 0, sizeof(cost));
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (i == j) continue;
for (int c = 0; c < m; c++) {
for (int r = 0; r < n; r++) {
if (r % 2 == 0) {
cost[i][j][c][0] += a[r][c] != w[i];
cost[i][j][c][1] += a[r][c] != w[j];
} else {
cost[i][j][c][0] += a[r][c] != w[j];
cost[i][j][c][1] += a[r][c] != w[i];
}
}
}
}
}
int ans = 1e9;
for (int a = 0; a < 4; a++) {
for (int b = 0; b < 4; b++) {
for (int c = 0; c < 4; c++) {
for (int d = 0; d < 4; d++) {
set<int> s = {a, b, c, d};
if (s.size() != 4) continue;
int res = 0;
for (int i = 0; i < m; i++) {
if (i % 2 == 0) {
if (cost[a][b][i][0] < cost[a][b][i][1])
res += cost[a][b][i][0];
else
res += cost[a][b][i][1];
} else {
if (cost[c][d][i][0] < cost[c][d][i][1])
res += cost[c][d][i][0];
else
res += cost[c][d][i][1];
}
}
ans = min(ans, res);
}
}
}
}
vector<vector<int> > mat(n, vector<int>(m));
for (int a = 0; a < 4; a++) {
for (int b = 0; b < 4; b++) {
for (int c = 0; c < 4; c++) {
for (int d = 0; d < 4; d++) {
set<int> s = {a, b, c, d};
if (s.size() != 4) continue;
int res = 0;
for (int i = 0; i < m; i++) {
if (i % 2 == 0) {
if (cost[a][b][i][0] < cost[a][b][i][1])
res += cost[a][b][i][0];
else
res += cost[a][b][i][1];
} else {
if (cost[c][d][i][0] < cost[c][d][i][1])
res += cost[c][d][i][0];
else
res += cost[c][d][i][1];
}
}
if (res == ans) {
for (int i = 0; i < m; i++) {
if (i % 2 == 0) {
if (cost[a][b][i][0] < cost[a][b][i][1]) {
for (int j = 0; j < n; j++) {
if (j % 2 == 0)
mat[j][i] = a;
else
mat[j][i] = b;
}
} else {
for (int j = 0; j < n; j++) {
if (j % 2 == 0)
mat[j][i] = b;
else
mat[j][i] = a;
}
}
} else {
if (cost[c][d][i][0] < cost[c][d][i][1]) {
for (int j = 0; j < n; j++) {
if (j % 2 == 0)
mat[j][i] = c;
else
mat[j][i] = d;
}
} else {
for (int j = 0; j < n; j++) {
if (j % 2 == 0)
mat[j][i] = d;
else
mat[j][i] = c;
}
}
}
}
}
}
}
}
}
return {ans, mat};
}
vector<string> rotate(const vector<string> &a) {
vector<string> b(a[0].size(), string(a.size(), 'A'));
for (int i = 0; i < a.size(); i++) {
for (int j = 0; j < a[0].size(); j++) {
b[j][i] = a[i][j];
}
}
return b;
}
int main() {
int n, m;
cin >> n >> m;
vector<string> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
auto x = solve(a);
a = rotate(a);
auto y = solve(a);
int flip = 0;
if (x.first > y.first) {
swap(x, y);
flip = 1;
}
auto mat = x.second;
vector<string> ans(mat.size(), string(mat[0].size(), 'A'));
for (int i = 0; i < mat.size(); i++)
for (int j = 0; j < mat[0].size(); j++) ans[i][j] = w[mat[i][j]];
if (flip) {
ans = rotate(ans);
ans = rotate(ans);
ans = rotate(ans);
}
for (int i = 0; i < ans.size(); i++) {
printf("%s\n", ans[i].c_str());
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<string> table;
vector<string> ftable;
int fans = INT_MAX;
void tryRows(string ch) {
int ans = 0;
vector<string> t;
for (int i = 0; i < n; i++) {
int cnt1 = 0, cnt2 = 0;
string s1 = "", s2 = "";
for (int j = 0; j < m; j++) {
if (table[i][j] != ch[j % 2 + 2 * (i % 2)]) cnt1++;
s1 += ch[j % 2 + 2 * (i % 2)];
}
for (int j = 0; j < m; j++) {
if (table[i][j] != ch[(j + 1) % 2 + 2 * (i % 2)]) cnt2++;
s2 += ch[(j + 1) % 2 + 2 * (i % 2)];
}
if (cnt1 < cnt2)
t.push_back(s1);
else
t.push_back(s2);
ans += min(cnt1, cnt2);
}
if (ans < fans) {
ftable = t;
fans = ans;
}
}
void tryCols(string ch) {
int ans = 0;
vector<string> t;
for (int i = 0; i < m; i++) {
int cnt1 = 0, cnt2 = 0;
string s1 = "", s2 = "";
for (int j = 0; j < n; j++) {
if (table[j][i] != ch[j % 2 + 2 * (i % 2)]) cnt1++;
s1 += ch[j % 2 + 2 * (i % 2)];
}
for (int j = 0; j < n; j++) {
if (table[j][i] != ch[(j + 1) % 2 + 2 * (i % 2)]) cnt2++;
s2 += ch[(j + 1) % 2 + 2 * (i % 2)];
}
if (cnt1 < cnt2)
t.push_back(s1);
else
t.push_back(s2);
ans += min(cnt1, cnt2);
}
if (ans < fans) {
ftable = t;
fans = ans;
for (int i = 0; i < n; i++) {
string s = "";
if (i >= ftable.size()) ftable.push_back(s);
for (int j = 0; j < m; j++) {
if (ftable[i].size() > j)
ftable[i][j] = t[j][i];
else
ftable[i] += t[j][i];
}
}
}
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> m;
for (int i = 0; i < n; i++) {
string t;
cin >> t;
table.push_back(t);
}
string ch = "AGCT";
int ans = INT_MAX;
do {
tryRows(ch);
tryCols(ch);
} while (next_permutation(ch.begin(), ch.end()));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << ftable[i][j];
}
cout << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<string> dna;
string c[] = {"ACGT", "AGCT", "ATCG", "CGAT", "CTAG", "GTAC"};
int main() {
cin >> n >> m;
dna.assign(n, "");
for (int i = 0; i < n; i++) cin >> dna[i];
int bestScore = -1;
int bestPair = 0;
bool rowBetter = true;
for (int k = 0; k < 6; k++) {
char c1 = c[k][0];
char c2 = c[k][1];
char c3 = c[k][2];
char c4 = c[k][3];
int scoreRow = 0;
for (int i = 0; i < n; i++) {
int matchA = 0;
int matchB = 0;
for (int j = 0; j < m; j++) {
if (i % 2 == 0) {
if (j % 2 == 0) {
if (dna[i][j] == c1) matchA++;
if (dna[i][j] == c2) matchB++;
} else {
if (dna[i][j] == c2) matchA++;
if (dna[i][j] == c1) matchB++;
}
} else {
if (j % 2 == 0) {
if (dna[i][j] == c3) matchA++;
if (dna[i][j] == c4) matchB++;
} else {
if (dna[i][j] == c4) matchA++;
if (dna[i][j] == c3) matchB++;
}
}
}
scoreRow += max(matchA, matchB);
}
int scoreCol = 0;
for (int j = 0; j < m; j++) {
int matchA = 0;
int matchB = 0;
for (int i = 0; i < n; i++) {
if (j % 2 == 0) {
if (i % 2 == 0) {
if (dna[i][j] == c1) matchA++;
if (dna[i][j] == c2) matchB++;
} else {
if (dna[i][j] == c2) matchA++;
if (dna[i][j] == c1) matchB++;
}
} else {
if (i % 2 == 0) {
if (dna[i][j] == c3) matchA++;
if (dna[i][j] == c4) matchB++;
} else {
if (dna[i][j] == c4) matchA++;
if (dna[i][j] == c3) matchB++;
}
}
}
scoreCol += max(matchA, matchB);
}
if (bestScore == -1 || scoreRow > bestScore) {
bestScore = scoreRow;
bestPair = k;
rowBetter = true;
}
if (bestScore == -1 || scoreCol > bestScore) {
bestScore = scoreCol;
bestPair = k;
rowBetter = false;
}
}
char ans[n][m];
char c1 = c[bestPair][0];
char c2 = c[bestPair][1];
char c3 = c[bestPair][2];
char c4 = c[bestPair][3];
if (rowBetter) {
for (int i = 0; i < n; i++) {
int matchA = 0;
int matchB = 0;
for (int j = 0; j < m; j++) {
if (i % 2 == 0) {
if (j % 2 == 0) {
if (dna[i][j] == c1) matchA++;
if (dna[i][j] == c2) matchB++;
} else {
if (dna[i][j] == c2) matchA++;
if (dna[i][j] == c1) matchB++;
}
} else {
if (j % 2 == 0) {
if (dna[i][j] == c3) matchA++;
if (dna[i][j] == c4) matchB++;
} else {
if (dna[i][j] == c4) matchA++;
if (dna[i][j] == c3) matchB++;
}
}
}
for (int j = 0; j < m; j++) {
if (i % 2 == 0) {
if (j % 2 == 0) {
if (matchA > matchB)
ans[i][j] = c1;
else
ans[i][j] = c2;
} else {
if (matchA > matchB)
ans[i][j] = c2;
else
ans[i][j] = c1;
}
} else {
if (j % 2 == 0) {
if (matchA > matchB)
ans[i][j] = c3;
else
ans[i][j] = c4;
} else {
if (matchA > matchB)
ans[i][j] = c4;
else
ans[i][j] = c3;
}
}
}
}
} else {
for (int j = 0; j < m; j++) {
int matchA = 0;
int matchB = 0;
for (int i = 0; i < n; i++) {
if (j % 2 == 0) {
if (i % 2 == 0) {
if (dna[i][j] == c1) matchA++;
if (dna[i][j] == c2) matchB++;
} else {
if (dna[i][j] == c2) matchA++;
if (dna[i][j] == c1) matchB++;
}
} else {
if (i % 2 == 0) {
if (dna[i][j] == c3) matchA++;
if (dna[i][j] == c4) matchB++;
} else {
if (dna[i][j] == c4) matchA++;
if (dna[i][j] == c3) matchB++;
}
}
}
for (int i = 0; i < n; i++) {
if (j % 2 == 0) {
if (i % 2 == 0) {
if (matchA > matchB)
ans[i][j] = c1;
else
ans[i][j] = c2;
} else {
if (matchA > matchB)
ans[i][j] = c2;
else
ans[i][j] = c1;
}
} else {
if (i % 2 == 0) {
if (matchA > matchB)
ans[i][j] = c3;
else
ans[i][j] = c4;
} else {
if (matchA > matchB)
ans[i][j] = c4;
else
ans[i][j] = c3;
}
}
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << ans[i][j];
}
cout << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int row[2][4][150000];
int col[2][4][150000];
int r, cc;
int go1() {
int ret = 0;
for (int a = 0; a < 4; a++) {
for (int b = 0; b < 4; b++) {
if (a == b) {
continue;
}
int now = 0;
vector<int> zz;
for (int i = 0; i < 4; i++) {
if (i != a && i != b) {
zz.push_back(i);
}
}
int c = zz[0];
int d = zz[1];
for (int i = 0; i < r; i += 2) {
int here =
max(row[0][a][i] + row[1][b][i], row[0][b][i] + row[1][a][i]);
now += here;
}
for (int i = 1; i < r; i += 2) {
int here =
max(row[0][c][i] + row[1][d][i], row[0][d][i] + row[1][c][i]);
now += here;
}
ret = max(ret, now);
}
}
return ret;
}
void build1(int ret) {
char ch[4] = {'A', 'G', 'C', 'T'};
for (int a = 0; a < 4; a++) {
for (int b = 0; b < 4; b++) {
if (a == b) {
continue;
}
int now = 0;
vector<int> zz;
for (int i = 0; i < 4; i++) {
if (i != a && i != b) {
zz.push_back(i);
}
}
int c = zz[0];
int d = zz[1];
for (int i = 0; i < r; i += 2) {
int here =
max(row[0][a][i] + row[1][b][i], row[0][b][i] + row[1][a][i]);
now += here;
}
for (int i = 1; i < r; i += 2) {
int here =
max(row[0][c][i] + row[1][d][i], row[0][d][i] + row[1][c][i]);
now += here;
}
if (now == ret) {
for (int i = 0; i < r; i++) {
if (i % 2 == 1) {
swap(a, c);
swap(b, d);
}
if (row[0][a][i] + row[1][b][i] > row[0][b][i] + row[1][a][i]) {
for (int j = 0; j < cc; j++) {
if (j % 2 == 0) {
cout << ch[a];
} else {
cout << ch[b];
}
}
} else {
for (int j = 0; j < cc; j++) {
if (j % 2 == 0) {
cout << ch[b];
} else {
cout << ch[a];
}
}
}
cout << endl;
if (i % 2 == 1) {
swap(a, c);
swap(b, d);
}
}
return;
}
}
}
}
int go2() {
int ret = 0;
for (int a = 0; a < 4; a++) {
for (int b = 0; b < 4; b++) {
if (a == b) {
continue;
}
int now = 0;
vector<int> zz;
for (int i = 0; i < 4; i++) {
if (i != a && i != b) {
zz.push_back(i);
}
}
int c = zz[0];
int d = zz[1];
for (int i = 0; i < cc; i += 2) {
int here =
max(col[0][a][i] + col[1][b][i], col[0][b][i] + col[1][a][i]);
now += here;
}
for (int i = 1; i < cc; i += 2) {
int here =
max(col[0][c][i] + col[1][d][i], col[0][d][i] + col[1][c][i]);
now += here;
}
ret = max(ret, now);
}
}
return ret;
}
void build2(int ret) {
char ch[4] = {'A', 'G', 'C', 'T'};
char ans[r][cc];
for (int a = 0; a < 4; a++) {
for (int b = 0; b < 4; b++) {
if (a == b) {
continue;
}
int now = 0;
vector<int> zz;
for (int i = 0; i < 4; i++) {
if (i != a && i != b) {
zz.push_back(i);
}
}
int c = zz[0];
int d = zz[1];
for (int i = 0; i < cc; i += 2) {
int here =
max(col[0][a][i] + col[1][b][i], col[0][b][i] + col[1][a][i]);
now += here;
}
for (int i = 1; i < cc; i += 2) {
int here =
max(col[0][c][i] + col[1][d][i], col[0][d][i] + col[1][c][i]);
now += here;
}
if (now == ret) {
for (int i = 0; i < cc; i++) {
if (i % 2 == 1) {
swap(a, c);
swap(b, d);
}
if (col[0][a][i] + col[1][b][i] > col[0][b][i] + col[1][a][i]) {
for (int j = 0; j < r; j++) {
if (j % 2 == 0) {
ans[j][i] = ch[a];
} else {
ans[j][i] = ch[b];
}
}
} else {
for (int j = 0; j < r; j++) {
if (j % 2 == 0) {
ans[j][i] = ch[b];
} else {
ans[j][i] = ch[a];
}
}
}
if (i % 2 == 1) {
swap(a, c);
swap(b, d);
}
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < cc; j++) {
cout << ans[i][j];
}
cout << endl;
}
return;
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> r >> cc;
int a[r][cc];
for (int i = 0; i < r; i++) {
string s;
cin >> s;
for (int j = 0; j < cc; j++) {
if (s[j] == 'A') {
a[i][j] = 0;
} else if (s[j] == 'G') {
a[i][j] = 1;
} else if (s[j] == 'C') {
a[i][j] = 2;
} else {
a[i][j] = 3;
}
}
}
for (int a = 0; a < 2; a++) {
for (int b = 0; b < 4; b++) {
for (int c = 0; c < 150000; c++) {
row[a][b][c] = 0;
col[a][b][c] = 0;
}
}
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < cc; j++) {
row[j % 2][a[i][j]][i]++;
col[i % 2][a[i][j]][j]++;
}
}
int aa = go1();
int bb = go2();
if (aa >= bb) {
build1(aa);
} else {
build2(bb);
}
}
|
#include <bits/stdc++.h>
using namespace std;
long long rr[] = {0, 1, 1, 1, 0, -1, -1, -1};
long long cc[] = {1, 1, 0, -1, -1, -1, 0, 1};
const long long inf = 1e18;
const long long mod = 1e9 + 7;
const long long N = 300050;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
long long test = 1;
while (test--) {
long long n, m;
cin >> n >> m;
string s[n], final[n];
for (long long i = 0; i < n; i++) {
cin >> s[i];
final[i] = s[i];
}
long long storey[m][2][26], storex[n][2][26];
memset(storex, 0, sizeof storex);
memset(storey, 0, sizeof storey);
for (long long i = 0; i < m; i++) {
for (long long j = 0; j < n; j++) {
storey[i][j % 2][s[j][i] - 'A']++;
}
}
for (long long i = 0; i < n; i++) {
for (long long j = 0; j < m; j++) {
storex[i][j % 2][s[i][j] - 'A']++;
}
}
string a = "ACGT";
function<void(void)> make_ans_y = [&]() {
for (long long i = 0; i < m; i++) {
if (i % 2 == 0) {
long long temp1 = n - n / 2 - storey[i][0][a[0] - 'A'] + n / 2 -
storey[i][1][a[2] - 'A'];
long long temp2 = n - n / 2 - storey[i][0][a[2] - 'A'] + n / 2 -
storey[i][1][a[0] - 'A'];
if (temp1 < temp2) {
final[0][i] = a[0];
final[1][i] = a[2];
} else {
final[0][i] = a[2];
final[1][i] = a[0];
}
} else {
long long temp1 = n - n / 2 - storey[i][0][a[1] - 'A'] + n / 2 -
storey[i][1][a[3] - 'A'];
long long temp2 = n - n / 2 - storey[i][0][a[3] - 'A'] + n / 2 -
storey[i][1][a[1] - 'A'];
if (temp1 < temp2) {
final[0][i] = a[1];
final[1][i] = a[3];
} else {
final[0][i] = a[3];
final[1][i] = a[1];
}
}
}
for (long long i = 0; i < m; i++) {
for (long long j = 2; j < n; j++) {
final[j][i] = final[j % 2][i];
}
}
return;
};
function<void(void)> make_ans_x = [&]() {
for (long long i = 0; i < n; i++) {
if (i % 2 == 0) {
long long temp1 = m - m / 2 - storex[i][0][a[0] - 'A'] + m / 2 -
storex[i][1][a[1] - 'A'];
long long temp2 = m - m / 2 - storex[i][0][a[1] - 'A'] + m / 2 -
storex[i][1][a[0] - 'A'];
if (temp1 < temp2) {
final[i][0] = a[0];
final[i][1] = a[1];
} else {
final[i][0] = a[1];
final[i][1] = a[0];
}
} else {
long long temp1 = m - m / 2 - storex[i][0][a[2] - 'A'] + m / 2 -
storex[i][1][a[3] - 'A'];
long long temp2 = m - m / 2 - storex[i][0][a[3] - 'A'] + m / 2 -
storex[i][1][a[2] - 'A'];
if (temp1 < temp2) {
final[i][0] = a[2];
final[i][1] = a[3];
} else {
final[i][0] = a[3];
final[i][1] = a[2];
}
}
}
for (long long i = 0; i < n; i++) {
for (long long j = 2; j < m; j++) {
final[i][j] = final[i][j % 2];
}
}
return;
};
long long ans = LLONG_MAX;
do {
long long tot = 0;
for (long long i = 0; i < m; i++) {
if (i % 2 == 0) {
tot += min(n - n / 2 - storey[i][0][a[0] - 'A'] + n / 2 -
storey[i][1][a[2] - 'A'],
n - n / 2 - storey[i][0][a[2] - 'A'] + n / 2 -
storey[i][1][a[0] - 'A']);
} else {
tot += min(n - n / 2 - storey[i][0][a[1] - 'A'] + n / 2 -
storey[i][1][a[3] - 'A'],
n - n / 2 - storey[i][0][a[3] - 'A'] + n / 2 -
storey[i][1][a[1] - 'A']);
}
}
ans = min(ans, tot);
tot = 0;
for (long long i = 0; i < n; i++) {
if (i % 2 == 0) {
tot += min(m - m / 2 - storex[i][0][a[0] - 'A'] + m / 2 -
storex[i][1][a[1] - 'A'],
m - m / 2 - storex[i][0][a[1] - 'A'] + m / 2 -
storex[i][1][a[0] - 'A']);
} else {
tot += min(m - m / 2 - storex[i][0][a[2] - 'A'] + m / 2 -
storex[i][1][a[3] - 'A'],
m - m / 2 - storex[i][0][a[3] - 'A'] + m / 2 -
storex[i][1][a[2] - 'A']);
}
}
ans = min(ans, tot);
} while (next_permutation(a.begin(), a.end()));
sort(a.begin(), a.end());
do {
long long tot = 0;
for (long long i = 0; i < m; i++) {
if (i % 2 == 0) {
tot += min(n - n / 2 - storey[i][0][a[0] - 'A'] + n / 2 -
storey[i][1][a[2] - 'A'],
n - n / 2 - storey[i][0][a[2] - 'A'] + n / 2 -
storey[i][1][a[0] - 'A']);
} else {
tot += min(n - n / 2 - storey[i][0][a[1] - 'A'] + n / 2 -
storey[i][1][a[3] - 'A'],
n - n / 2 - storey[i][0][a[3] - 'A'] + n / 2 -
storey[i][1][a[1] - 'A']);
}
}
if (tot == ans) {
make_ans_y();
break;
}
tot = 0;
for (long long i = 0; i < n; i++) {
if (i % 2 == 0) {
tot += min(m - m / 2 - storex[i][0][a[0] - 'A'] + m / 2 -
storex[i][1][a[1] - 'A'],
m - m / 2 - storex[i][0][a[1] - 'A'] + m / 2 -
storex[i][1][a[0] - 'A']);
} else {
tot += min(m - m / 2 - storex[i][0][a[2] - 'A'] + m / 2 -
storex[i][1][a[3] - 'A'],
m - m / 2 - storex[i][0][a[3] - 'A'] + m / 2 -
storex[i][1][a[2] - 'A']);
}
}
if (tot == ans) {
make_ans_x();
break;
}
} while (next_permutation(a.begin(), a.end()));
for (long long i = 0; i < n; i++) {
cout << final[i] << '\n';
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
const int inf = 0x3f3f3f3f;
long long gcd(long long x, long long y) {
if (y == 0)
return x;
else
return gcd(y, x % y);
}
long long ksm(long long a, long long b) {
long long r = 1;
while (b) {
if (b & 1) r = (r * a) % mod;
a = a * a % mod;
b >>= 1;
}
return r;
}
void exgcd(long long a, long long b, long long &x, long long &y, long long &d) {
if (!b)
d = a, x = 1, y = 0;
else
exgcd(b, a % b, y, x, d), y -= a / b * x;
}
long long inv(long long a, long long p) {
long long x, y, d;
exgcd(a, p, x, y, d);
x = (x % p + p) % p;
return x;
}
inline int read() {
char c = getchar();
int x = 0, f = 1;
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x * f;
}
const int maxn = 3e5 + 10;
int n, m, k, t, ans = 1e9, id = 0, best_id, best_type;
string s[maxn];
string ch[maxn];
char h[maxn];
inline void cou(int type) {
int tmp = 0;
for (int i = 0; i <= n - 1; i++)
for (int j = 0; j <= m - 1; j++)
if (s[i][j] != ch[i][j]) tmp++;
if (tmp < ans) {
ans = tmp;
best_id = id;
best_type = type;
}
}
inline int cou_h(int i) {
int res = 0;
for (int j = 0; j < m; j++) {
res += (s[i][j] != h[j]);
}
return res;
}
inline int cou_l(int j) {
int res = 0;
for (int i = 0; i < n; i++) {
res += (s[i][j] != h[i]);
}
return res;
}
inline void push_h(char a, char b, int n) {
for (int i = 0; i < n; i++) {
if (i % 2 == 0)
h[i] = a;
else
h[i] = b;
}
}
inline void hang(char a, char b, char c, char d) {
for (int i = 0; i < n; i++) {
int ans1, ans2;
if (i % 2 == 0) {
push_h(a, b, m);
ans1 = cou_h(i);
push_h(b, a, m);
ans2 = cou_h(i);
} else {
push_h(c, d, m);
ans1 = cou_h(i);
push_h(d, c, m);
ans2 = cou_h(i);
}
if (ans1 > ans2) {
swap(a, b);
swap(c, d);
}
for (int j = 0; j < m; j++) {
if (i % 2 == 0) {
if (j % 2 == 0)
ch[i][j] = a;
else
ch[i][j] = b;
} else {
if (j % 2 == 0)
ch[i][j] = c;
else
ch[i][j] = d;
}
}
}
cou(0);
}
inline void lie(char a, char b, char c, char d) {
for (int j = 0; j < m; j++) {
int ans1, ans2;
if (j % 2 == 0) {
push_h(a, b, n);
ans1 = cou_l(j);
push_h(b, a, n);
ans2 = cou_l(j);
} else {
push_h(c, d, n);
ans1 = cou_l(j);
push_h(d, c, n);
ans2 = cou_l(j);
}
if (ans1 > ans2) {
swap(a, b);
swap(c, d);
}
for (int i = 0; i < n; i++) {
if (j % 2 == 0) {
if (i % 2 == 0)
ch[i][j] = a;
else
ch[i][j] = b;
} else {
if (i % 2 == 0)
ch[i][j] = c;
else
ch[i][j] = d;
}
}
}
cou(1);
}
char g[4] = {'A', 'T', 'G', 'C'};
int main() {
cin >> n >> m;
for (int i = 0; i <= n - 1; i++) cin >> s[i];
for (int i = 0; i <= n - 1; i++) ch[i].resize(m + 10);
int cnt = 0;
for (int i = 1; i <= 24; i++) {
id++;
hang(g[0], g[1], g[2], g[3]);
lie(g[0], g[1], g[2], g[3]);
next_permutation(g, g + 4);
}
for (int i = 1; i <= 24; i++) {
if (i == best_id) {
if (best_type == 0)
hang(g[0], g[1], g[2], g[3]);
else
lie(g[0], g[1], g[2], g[3]);
break;
}
next_permutation(g, g + 4);
}
for (int i = 0; i <= n - 1; i++) {
for (int j = 0; j <= m - 1; j++) cout << ch[i][j];
cout << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
char c[] = {'A', 'G', 'C', 'T'};
vector<vector<char> > a, b, res;
int n, m, ans = (1 << 30);
void col() {
int cur = 0;
for (int j = 0; j < m; j++) {
int p = ((j & 1) ? 0 : 2), cnt[] = {0, 0};
for (int i = 0, q = 0; i < n; i++, q ^= 1) {
cnt[0] += (a[i][j] != c[p + q]);
cnt[1] += (a[i][j] != c[p + (q ^ 1)]);
}
cur += min(cnt[0], cnt[1]);
for (int i = 0, q = 0; i < n; i++, q ^= 1)
b[i][j] = c[p + (q ^ (cnt[0] < cnt[1] ? 0 : 1))];
}
if (cur < ans) {
ans = cur;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) res[i][j] = b[i][j];
}
}
void row() {
int cur = 0;
for (int i = 0; i < n; i++) {
int p = ((i & 1) ? 0 : 2), cnt[] = {0, 0};
for (int j = 0, q = 0; j < m; j++, q ^= 1) {
cnt[0] += (a[i][j] != c[p + q]);
cnt[1] += (a[i][j] != c[p + (q ^ 1)]);
}
cur += min(cnt[0], cnt[1]);
for (int j = 0, q = 0; j < m; j++, q ^= 1)
b[i][j] = c[p + (q ^ (cnt[0] < cnt[1] ? 0 : 1))];
}
if (cur < ans) {
ans = cur;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) res[i][j] = b[i][j];
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
a.resize(n), b.resize(n), res.resize(n);
for (int i = 0; i < n; i++) {
a[i].resize(m), b[i].resize(m), res[i].resize(m);
for (int j = 0; j < m; j++) cin >> a[i][j];
}
do {
col();
row();
} while (next_permutation(c, c + 4));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) cout << res[i][j];
cout << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
inline void fastio() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
long long n, m;
vector<vector<long long> > v;
void init() {
cin >> n >> m;
v.assign(n, vector<long long>(m, 0));
for (long long i = 0; i < n; ++i) {
for (long long j = 0; j < m; ++j) {
char c;
cin >> c;
if (c == 'A') {
v[i][j] = 0;
} else if (c == 'G') {
v[i][j] = 1;
} else if (c == 'T') {
v[i][j] = 2;
} else {
v[i][j] = 3;
}
}
}
}
long long answer = 0;
long long av, ah;
vector<vector<long long> > ans1;
vector<vector<long long> > ans2;
void tr(const vector<long long>& p, vector<vector<long long> >& ans,
long long& aa) {
long long ret = 0;
for (long long i = 0; i < n; ++i) {
if (i % 2 == 0) {
long long a1 = 0, a2 = 0;
for (long long j = 0; j < m; ++j) {
if (j % 2 == 0 && v[i][j] != p[0]) a1++;
if (j % 2 == 1 && v[i][j] != p[1]) a1++;
if (j % 2 == 0 && v[i][j] != p[1]) a2++;
if (j % 2 == 1 && v[i][j] != p[0]) a2++;
}
ret += min(a1, a2);
} else {
long long a1 = 0, a2 = 0;
for (long long j = 0; j < m; ++j) {
if (j % 2 == 0 && v[i][j] != p[2]) a1++;
if (j % 2 == 1 && v[i][j] != p[3]) a1++;
if (j % 2 == 0 && v[i][j] != p[3]) a2++;
if (j % 2 == 1 && v[i][j] != p[2]) a2++;
}
ret += min(a1, a2);
}
}
if (ret < aa) {
aa = ret;
for (long long i = 0; i < n; ++i) {
if (i % 2 == 0) {
long long a1 = 0, a2 = 0;
for (long long j = 0; j < m; ++j) {
if (j % 2 == 0 && v[i][j] != p[0]) a1++;
if (j % 2 == 1 && v[i][j] != p[1]) a1++;
if (j % 2 == 0 && v[i][j] != p[1]) a2++;
if (j % 2 == 1 && v[i][j] != p[0]) a2++;
}
if (a1 < a2) {
for (long long j = 0; j < m; ++j) {
if (j % 2 == 0) {
ans[i][j] = p[0];
} else {
ans[i][j] = p[1];
}
}
} else {
for (long long j = 0; j < m; ++j) {
if (j % 2 == 0) {
ans[i][j] = p[1];
} else {
ans[i][j] = p[0];
}
}
}
} else {
long long a1 = 0, a2 = 0;
for (long long j = 0; j < m; ++j) {
if (j % 2 == 0 && v[i][j] != p[2]) a1++;
if (j % 2 == 1 && v[i][j] != p[3]) a1++;
if (j % 2 == 0 && v[i][j] != p[3]) a2++;
if (j % 2 == 1 && v[i][j] != p[2]) a2++;
}
if (a1 < a2) {
for (long long j = 0; j < m; ++j) {
if (j % 2 == 0) {
ans[i][j] = p[2];
} else {
ans[i][j] = p[3];
}
}
} else {
for (long long j = 0; j < m; ++j) {
if (j % 2 == 0) {
ans[i][j] = p[3];
} else {
ans[i][j] = p[2];
}
}
}
}
}
}
}
map<long long, char> mp;
void solve() {
mp[0] = 'A';
mp[1] = 'G';
mp[2] = 'T';
mp[3] = 'C';
init();
answer = n * m;
av = n * m;
ah = n * m;
vector<long long> p = {0, 1, 2, 3};
vector<vector<long long> > vert = v;
do {
tr(p, vert, av);
} while (next_permutation(p.begin(), p.end()));
vector<vector<long long> > nv(m, vector<long long>(n, 0));
for (long long i = 0; i < n; ++i) {
for (long long j = 0; j < m; ++j) {
nv[j][i] = v[i][j];
}
}
swap(n, m);
v.swap(nv);
p = {0, 1, 2, 3};
vector<vector<long long> > hor = v;
do {
tr(p, hor, ah);
} while (next_permutation(p.begin(), p.end()));
swap(n, m);
if (av < ah) {
for (long long i = 0; i < n; ++i) {
for (long long j = 0; j < m; ++j) {
cout << mp[vert[i][j]];
}
cout << '\n';
}
} else {
for (long long i = 0; i < n; ++i) {
for (long long j = 0; j < m; ++j) {
cout << mp[hor[j][i]];
}
cout << '\n';
}
}
}
signed main() {
fastio();
long long t = 1;
while (t--) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
inline int mul(int a, int b) { return int(a * 1ll * b % 998244353); }
inline int norm(int a) {
if (a >= 998244353) a -= 998244353;
if (a < 0) a += 998244353;
return a;
}
inline int binPow(int a, int k) {
int ans = 1;
while (k > 0) {
if (k & 1) ans = mul(ans, a);
a = mul(a, a);
k >>= 1;
}
return ans;
}
inline int subtract(int a, int b) { return norm(a - b); }
inline int sum(int a, int b) { return int((a + b) % 998244353); }
inline int inv(int a) { return binPow(a, 998244353 - 2); }
int main() {
int n, m;
cin >> n >> m;
vector<string> table(n);
for (int i = 0; i < n; i++) {
cin >> table[i];
}
string temp = "AGCT";
int all = 15;
vector<vector<char>> ans(n, vector<char>(m));
vector<vector<char>> curr_ans(n, vector<char>(m));
int min_dif = 3000000;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < i; j++) {
int first_sym = i;
int second_sym = j;
int third_sym = 0;
int fourth_sym = 0;
while (third_sym == first_sym || third_sym == second_sym) third_sym++;
while (fourth_sym == third_sym || fourth_sym == second_sym ||
fourth_sym == first_sym)
fourth_sym++;
int curr_dif = 0;
for (int t = 0; t < n; t++) {
swap(first_sym, third_sym);
swap(second_sym, fourth_sym);
int transpos = 0;
int dif1 = 0, dif2 = 0;
for (int q = 0; q < m; q++) {
if (!(q % 2) && table[t][q] != temp[first_sym]) {
dif1++;
}
if ((q % 2) && table[t][q] != temp[second_sym]) {
dif1++;
}
if ((q % 2) && table[t][q] != temp[first_sym]) {
dif2++;
}
if (!(q % 2) && table[t][q] != temp[second_sym]) {
dif2++;
}
}
if (dif2 < dif1) {
swap(dif2, dif1);
transpos = 1;
}
curr_dif += dif1;
for (int q = 0; q < m; q++) {
if ((q % 2) == transpos) curr_ans[t][q] = temp[first_sym];
if ((q % 2) != transpos) curr_ans[t][q] = temp[second_sym];
}
}
if (curr_dif < min_dif) {
min_dif = curr_dif;
for (int t = 0; t < n; t++) {
for (int q = 0; q < m; q++) {
ans[t][q] = curr_ans[t][q];
}
}
}
curr_dif = 0;
for (int t = 0; t < m; t++) {
swap(first_sym, third_sym);
swap(second_sym, fourth_sym);
int transpos = 0;
int dif1 = 0, dif2 = 0;
for (int q = 0; q < n; q++) {
if (!(q % 2) && table[q][t] != temp[first_sym]) {
dif1++;
}
if ((q % 2) && table[q][t] != temp[second_sym]) {
dif1++;
}
if ((q % 2) && table[q][t] != temp[first_sym]) {
dif2++;
}
if (!(q % 2) && table[q][t] != temp[second_sym]) {
dif2++;
}
}
if (dif2 < dif1) {
swap(dif2, dif1);
transpos = 1;
}
curr_dif += dif1;
for (int q = 0; q < n; q++) {
if ((q % 2) == transpos) curr_ans[q][t] = temp[first_sym];
if ((q % 2) != transpos) curr_ans[q][t] = temp[second_sym];
}
}
if (curr_dif < min_dif) {
min_dif = curr_dif;
for (int t = 0; t < n; t++) {
for (int q = 0; q < m; q++) {
ans[t][q] = curr_ans[t][q];
}
}
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << ans[i][j];
}
cout << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
const int MAXCELLS = 300000;
const char CHARS[4] = {'A', 'C', 'G', 'T'};
int h, w;
char s[MAXCELLS + 1];
char ans[MAXCELLS + 1];
char t[MAXCELLS + 1];
void solve() {
int best = INT_MAX;
for (int i = (0); i < (h * w); ++i) ans[i] = '?';
t[h * w] = ans[h * w] = '\0';
for (int a = (0); a < (4); ++a)
for (int b = (a + 1); b < (4); ++b)
for (int c = (0); c < (4); ++c)
for (int d = (c + 1); d < (4); ++d) {
if (c == a || c == b || d == a || d == b) continue;
for (int x = (0); x < (h); ++x) {
int u = x % 2 == 0 ? a : c, v = x % 2 == 0 ? b : d;
int match[2];
match[0] = match[1] = 0;
for (int y = (0); y < (w); ++y)
if (s[x * w + y] == CHARS[u])
++match[y % 2];
else if (s[x * w + y] == CHARS[v])
++match[1 - y % 2];
if (match[1] > match[0]) swap(u, v);
for (int y = (0); y < (w); ++y)
t[x * w + y] = y % 2 == 0 ? CHARS[u] : CHARS[v];
}
{
int cur = 0;
for (int i = (0); i < (h * w); ++i)
if (s[i] != t[i]) ++cur;
if (cur < best) {
best = cur;
for (int i = (0); i < (h * w); ++i) ans[i] = t[i];
}
}
for (int y = (0); y < (w); ++y) {
int u = y % 2 == 0 ? a : c, v = y % 2 == 0 ? b : d;
int match[2];
match[0] = match[1] = 0;
for (int x = (0); x < (h); ++x)
if (s[x * w + y] == CHARS[u])
++match[x % 2];
else if (s[x * w + y] == CHARS[v])
++match[1 - x % 2];
if (match[1] > match[0]) swap(u, v);
for (int x = (0); x < (h); ++x)
t[x * w + y] = x % 2 == 0 ? CHARS[u] : CHARS[v];
}
{
int cur = 0;
for (int i = (0); i < (h * w); ++i)
if (s[i] != t[i]) ++cur;
if (cur < best) {
best = cur;
for (int i = (0); i < (h * w); ++i) ans[i] = t[i];
}
}
}
}
void run() {
scanf("%d%d", &h, &w);
for (int x = (0); x < (h); ++x) scanf("%s", s + x * w);
solve();
for (int x = (0); x < (h); ++x) printf("%.*s\n", w, ans + x * w);
}
int main() {
run();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
inline int in() {
int x, y;
y = scanf("%d", &x);
return x;
}
const int N = 300 * 1000 + 100;
string a[N];
char ch[] = {'A', 'C', 'T', 'G'};
int n, m;
int mnAns;
bool pr;
int state[N];
int solve_row() {
char c[2][2] = {{ch[0], ch[1]}, {ch[2], ch[3]}};
int sum = 0;
for (int i = 0; i < n; i++) {
char* d = c[i % 2];
int diff[2] = {0, 0};
for (int j = 0; j < m; j++) diff[0] += (a[i][j] != d[j % 2]);
for (int j = 0; j < m; j++) diff[1] += (a[i][j] != d[(1 + j) % 2]);
state[i] = (diff[0] >= diff[1]);
sum += min(diff[0], diff[1]);
}
if (pr && sum == mnAns) {
for (int i = 0; i < n; i++) {
char* d = c[i % 2];
for (int j = 0; j < m; j++) cout << d[(state[i] + j) % 2];
cout << '\n';
}
exit(0);
}
return sum;
}
int solve_col() {
char c[2][2] = {{ch[0], ch[1]}, {ch[2], ch[3]}};
int sum = 0;
for (int j = 0; j < m; j++) {
char* d = c[j % 2];
int diff[2] = {0, 0};
for (int i = 0; i < n; i++) diff[0] += (a[i][j] != d[i % 2]);
for (int i = 0; i < n; i++) diff[1] += (a[i][j] != d[(1 + i) % 2]);
state[j] = (diff[0] >= diff[1]);
sum += min(diff[0], diff[1]);
}
if (pr && sum == mnAns) {
for (int j = 0; j < m; j++) {
char* d = c[j % 2];
for (int i = 0; i < n; i++) a[i][j] = d[(state[j] + i) % 2];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) cout << a[i][j];
cout << '\n';
}
exit(0);
}
return sum;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> a[i];
mnAns = n * m + 1;
do {
mnAns = min(mnAns, solve_row());
mnAns = min(mnAns, solve_col());
} while (next_permutation(ch, ch + 4));
pr = true;
do {
solve_row();
solve_col();
} while (next_permutation(ch, ch + 4));
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<vector<char> > t;
void read() {
cin >> n >> m;
t.resize(n + 1);
for (int i = 1; i <= n; i++) {
t[i].resize(m + 1);
for (int j = 1; j <= m; j++) cin >> t[i][j];
}
}
vector<vector<char> > best_t;
int best_c = INT_MAX;
void solve() {
best_t.resize(n + 1);
for (int i = 1; i <= n; i++) best_t.resize(m + 1);
vector<char> p = {'A', 'C', 'G', 'T'};
do {
vector<vector<char> > curr_t;
curr_t.resize(n + 1);
for (int i = 1; i <= n; i++) curr_t[i].resize(m + 1);
char a = p[0], b = p[1], c = p[2], d = p[3];
int cnt_d = 0;
curr_t[1][1] = a, curr_t[1][2] = b, curr_t[2][1] = c, curr_t[2][2] = d;
cnt_d += (t[1][1] != a) + (t[1][2] != b) + (t[2][1] != c) + (t[2][2] != d);
for (int j = 3; j <= m; j++) {
if (j % 2 == 1) {
curr_t[1][j] = a;
cnt_d += (t[1][j] != a);
} else {
curr_t[1][j] = b;
cnt_d += (t[1][j] != b);
}
}
for (int j = 3; j <= m; j++) {
if (j % 2 == 1) {
curr_t[2][j] = c;
cnt_d += (t[2][j] != c);
} else {
curr_t[2][j] = d;
cnt_d += (t[2][j] != d);
}
}
for (int i = 3; i <= n; i++) {
int first = 0, second = 0;
if (i % 2 == 1) {
for (int j = 1; j <= m; j++) {
if (j % 2 == 1) {
first += (t[i][j] != a);
second += (t[i][j] != b);
} else {
first += (t[i][j] != b);
second += (t[i][j] != a);
}
}
if (first < second) {
cnt_d += first;
for (int j = 1; j <= m; j++) {
if (j % 2 == 1)
curr_t[i][j] = a;
else
curr_t[i][j] = b;
}
} else {
cnt_d += second;
for (int j = 1; j <= m; j++) {
if (j % 2 == 1)
curr_t[i][j] = b;
else
curr_t[i][j] = a;
}
}
} else {
for (int j = 1; j <= m; j++) {
if (j % 2 == 1) {
first += (t[i][j] != c);
second += (t[i][j] != d);
} else {
first += (t[i][j] != d);
second += (t[i][j] != c);
}
}
if (first < second) {
cnt_d += first;
for (int j = 1; j <= m; j++) {
if (j % 2 == 1)
curr_t[i][j] = c;
else
curr_t[i][j] = d;
}
} else {
cnt_d += second;
for (int j = 1; j <= m; j++) {
if (j % 2 == 1)
curr_t[i][j] = d;
else
curr_t[i][j] = c;
}
}
}
}
if (cnt_d < best_c) best_c = cnt_d, best_t = curr_t;
cnt_d = (t[1][1] != a) + (t[1][2] != b) + (t[2][1] != c) + (t[2][2] != d);
for (int i = 3; i <= n; i++) {
if (i % 2 == 1) {
curr_t[i][1] = a;
cnt_d += (t[i][1] != a);
} else {
curr_t[i][1] = c;
cnt_d += (t[i][1] != c);
}
}
for (int i = 3; i <= n; i++) {
if (i % 2 == 1) {
curr_t[i][2] = b;
cnt_d += (t[i][2] != b);
} else {
curr_t[i][2] = d;
cnt_d += (t[i][2] != d);
}
}
for (int j = 3; j <= m; j++) {
int first = 0, second = 0;
if (j % 2 == 1) {
for (int i = 1; i <= n; i++) {
if (i % 2 == 1) {
first += (t[i][j] != a);
second += (t[i][j] != c);
} else {
first += (t[i][j] != c);
second += (t[i][j] != a);
}
}
if (first < second) {
cnt_d += first;
for (int i = 1; i <= n; i++) {
if (i % 2 == 1)
curr_t[i][j] = a;
else
curr_t[i][j] = c;
}
} else {
cnt_d += second;
for (int i = 1; i <= n; i++) {
if (i % 2 == 1)
curr_t[i][j] = c;
else
curr_t[i][j] = a;
}
}
} else {
for (int i = 1; i <= n; i++) {
if (i % 2 == 1) {
first += (t[i][j] != b);
second += (t[i][j] != d);
} else {
first += (t[i][j] != d);
second += (t[i][j] != b);
}
}
if (first < second) {
cnt_d += first;
for (int i = 1; i <= n; i++) {
if (i % 2 == 1)
curr_t[i][j] = b;
else
curr_t[i][j] = d;
}
} else {
cnt_d += second;
for (int i = 1; i <= n; i++) {
if (i % 2 == 1)
curr_t[i][j] = d;
else
curr_t[i][j] = b;
}
}
}
}
if (cnt_d < best_c) best_c = cnt_d, best_t = curr_t;
} while (next_permutation(p.begin(), p.end()));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) cout << best_t[i][j];
cout << '\n';
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
read();
solve();
}
|
#include <bits/stdc++.h>
using namespace std;
ifstream fin("input.txt");
ofstream fout("output.txt");
bool isPrime(long long int x) {
if (x <= 4 || x % 2 == 0 || x % 3 == 0) return x == 2 || x == 3;
for (int i = 5; i * i <= x; i += 6)
if (x % i == 0 || x % (i + 2) == 0) return 0;
return 1;
}
int NumDigits(int x) {
return (
x < 10
? 1
: (x < 100
? 2
: (x < 1000
? 3
: (x < 10000
? 4
: (x < 100000
? 5
: (x < 1000000
? 6
: (x < 10000000
? 7
: (x < 100000000
? 8
: (x < 1000000000
? 9
: 10)))))))));
}
inline void boostIO() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout.precision(10);
}
inline long long int Pow10(int x) {
if (x == 0) return 1;
if (x == 1) return 10;
if (x == 2) return 100;
if (x == 3) return 1000;
if (x == 4) return 10000;
if (x == 5) return 100000;
if (x == 6) return 1000000;
if (x == 7) return 10000000;
if (x == 8) return 100000000;
if (x == 9) return 1000000000;
if (x == 10) return 10000000000;
}
long long int gcd(long long int a, long long int b) {
return b ? gcd(b, a % b) : a;
}
long long int lcm(long long int a, long long int b) {
return a * b / gcd(a, b);
}
long long MOD = 1e9 + 7;
long long n, m;
long long f(char a, char b, char c, char d, vector<vector<char>> &A) {
char V[4][2] = {{a, b}, {c, d}, {b, a}, {d, c}};
char z;
long long dif = 0;
for (long long i = 0; i < (n); ++i) {
long long case0 = 0;
long long case1 = 0;
for (long long j = 0; j < (m); ++j) {
z = V[i % 4][j % 2];
if (A[i][j] != z) case0++;
}
for (long long j = 0; j < (m); ++j) {
z = V[i % 4][(j + 1) % 2];
if (A[i][j] != z) case1++;
}
if (case0 <= case1) {
dif += case0;
} else {
dif += case1;
}
}
return dif;
}
long long f2(char a, char b, char c, char d, vector<vector<char>> &A) {
char V[4][2] = {{a, b}, {c, d}, {b, a}, {d, c}};
char z;
long long dif = 0;
for (long long i = 0; i < (m); ++i) {
long long case0 = 0;
long long case1 = 0;
for (long long j = 0; j < (n); ++j) {
z = V[i % 4][j % 2];
if (A[j][i] != z) case0++;
}
for (long long j = 0; j < (n); ++j) {
z = V[i % 4][(j + 1) % 2];
if (A[j][i] != z) case1++;
}
if (case0 <= case1) {
dif += case0;
} else {
dif += case1;
}
}
return dif;
}
void Out(char a, char b, char c, char d, vector<vector<char>> &Q,
vector<vector<char>> &A) {
char V[4][2] = {{a, b}, {c, d}, {b, a}, {d, c}};
char z;
long long dif = 0;
for (long long i = 0; i < (n); ++i) {
long long case0 = 0;
long long case1 = 0;
for (long long j = 0; j < (m); ++j) {
z = V[i % 4][j % 2];
if (A[i][j] != z) case0++;
}
for (long long j = 0; j < (m); ++j) {
z = V[i % 4][(j + 1) % 2];
if (A[i][j] != z) case1++;
}
if (case0 <= case1) {
for (long long j = 0; j < (m); ++j) {
z = V[i % 4][j % 2];
Q[i][j] = z;
}
} else {
for (long long j = 0; j < (m); ++j) {
z = V[i % 4][(j + 1) % 2];
Q[i][j] = z;
}
}
}
}
void Out2(char a, char b, char c, char d, vector<vector<char>> &Q,
vector<vector<char>> &A) {
char V[4][2] = {{a, b}, {c, d}, {b, a}, {d, c}};
char z;
long long dif = 0;
for (long long i = 0; i < (m); ++i) {
long long case0 = 0;
long long case1 = 0;
for (long long j = 0; j < (n); ++j) {
z = V[i % 4][j % 2];
if (A[j][i] != z) case0++;
}
for (long long j = 0; j < (n); ++j) {
z = V[i % 4][(j + 1) % 2];
if (A[j][i] != z) case1++;
}
if (case0 <= case1) {
for (long long j = 0; j < (n); ++j) {
z = V[i % 4][j % 2];
Q[j][i] = z;
}
} else {
for (long long j = 0; j < (n); ++j) {
z = V[i % 4][(j + 1) % 2];
Q[j][i] = z;
}
}
}
}
bool chosen[4];
vector<char> p;
char W[4];
bool ot = 0;
long long Min = LONG_LONG_MAX;
vector<char> e;
void search(vector<vector<char>> &A) {
if (p.size() == 4) {
long long v = f(p[0], p[1], p[2], p[3], A);
if (v < Min) {
Min = v;
e = p;
ot = 0;
}
v = f2(p[0], p[1], p[2], p[3], A);
if (v < Min) {
Min = v;
e = p;
ot = 1;
}
} else {
for (long long i = 0; i < 4; ++i) {
if (chosen[i]) continue;
chosen[i] = true;
p.push_back(W[i]);
search(A);
chosen[i] = false;
p.pop_back();
}
}
}
int32_t main() {
boostIO();
cin >> n >> m;
vector<vector<char>> A(n, vector<char>(m));
for (long long i = 0; i < (n); ++i) {
for (long long j = 0; j < (m); ++j) {
cin >> A[i][j];
}
}
W[0] = 'A';
W[1] = 'G';
W[2] = 'C';
W[3] = 'T';
search(A);
vector<vector<char>> Q(n, vector<char>(m));
if (ot == 0)
Out(e[0], e[1], e[2], e[3], Q, A);
else {
Out2(e[0], e[1], e[2], e[3], Q, A);
}
for (long long i = 0; i < (n); ++i) {
for (long long j = 0; j < (m); ++j) {
cout << Q[i][j];
}
cout << '\n';
}
cerr << '\n' << "RUNTIME: " << 1.0 * clock() / CLOCKS_PER_SEC << " s" << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const char mat[] = "AGCT";
int n, m;
int go(vector<string> &lis, int n, int m, char ch1, char ch2,
vector<string> &ret) {
vector<char> mm[2];
mm[0] = {ch1, ch2};
for (int(i) = (0); (i) <= ((4) - 1); (i)++)
if (mat[i] != mm[0][0] && mat[i] != mm[0][1]) mm[1].emplace_back(mat[i]);
int res = 0;
for (int(i) = (0); (i) <= ((n)-1); (i)++) {
int now[2] = {};
for (int(j) = (0); (j) <= ((m)-1); (j)++)
if (lis[i][j] != mm[i & 1][j & 1]) now[0]++;
for (int(j) = (0); (j) <= ((m)-1); (j)++)
if (lis[i][j] != mm[i & 1][j & 1 ^ 1]) now[1]++;
if (now[0] < now[1]) {
for (int(j) = (0); (j) <= ((m)-1); (j)++) ret[i][j] = mm[i & 1][j & 1];
res += now[0];
} else {
for (int(j) = (0); (j) <= ((m)-1); (j)++)
ret[i][j] = mm[i & 1][j & 1 ^ 1];
res += now[1];
}
}
return res;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin >> n >> m;
vector<string> lis(n);
for (int(i) = (0); (i) <= ((n)-1); (i)++) cin >> lis[i];
int res = n * m;
vector<string> ans;
bool rev = false;
for (int(i) = (0); (i) <= ((4) - 1); (i)++)
for (int(j) = (i + 1); (j) <= (3); (j)++) {
vector<string> tmp(n, string(m, '\0'));
int cur = go(lis, n, m, mat[i], mat[j], tmp);
if (cur < res) {
ans = tmp;
res = cur;
}
}
vector<string> lis2(m);
for (int(i) = (0); (i) <= ((m)-1); (i)++)
for (int(j) = (0); (j) <= ((n)-1); (j)++) lis2[i] += lis[j][i];
for (int(i) = (0); (i) <= ((4) - 1); (i)++)
for (int(j) = (i + 1); (j) <= (3); (j)++) {
vector<string> tmp(m, string(n, '\0'));
int cur = go(lis2, m, n, mat[i], mat[j], tmp);
if (cur < res) {
ans = tmp;
rev = true;
res = cur;
}
}
if (rev) {
for (int(i) = (0); (i) <= ((n)-1); (i)++) {
for (int(j) = (0); (j) <= ((m)-1); (j)++) cout << ans[j][i];
cout << '\n';
}
} else {
for (int(i) = (0); (i) <= ((n)-1); (i)++) cout << ans[i] << '\n';
}
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m;
char x[300005];
int ca[300005][2][4];
int cb[300005][2][4];
int aa[256];
char bbb[4];
int y[4], z[4], best = -1, bb, cur;
bool u[4];
vector<vector<char>> ans;
int ta, tb;
void go(int a) {
if (a == 4) {
cur = 0;
for (int i = 0; i < n; i++) {
ta = ca[i][0][y[0 + ((i & 1) << 1)]] + ca[i][1][y[1 + ((i & 1) << 1)]];
tb = ca[i][0][y[1 + ((i & 1) << 1)]] + ca[i][1][y[0 + ((i & 1) << 1)]];
cur += max(ta, tb);
}
if (cur > best) {
best = cur;
bb = 0;
memcpy(z, y, sizeof(y[0]) * 4);
}
cur = 0;
for (int i = 0; i < m; i++) {
ta = cb[i][0][y[0 + ((i & 1) << 0)]] + cb[i][1][y[2 + ((i & 1) << 0)]];
tb = cb[i][0][y[2 + ((i & 1) << 0)]] + cb[i][1][y[0 + ((i & 1) << 0)]];
cur += max(ta, tb);
}
if (cur > best) {
best = cur;
bb = 1;
memcpy(z, y, sizeof(y[0]) * 4);
}
} else
for (int i = 0; i < 4; i++)
if (!u[i]) {
u[i] = 1;
y[a] = i;
go(a + 1);
u[i] = 0;
}
}
int main() {
aa[bbb[0] = 'A'] = 0;
aa[bbb[1] = 'G'] = 1;
aa[bbb[2] = 'C'] = 2;
aa[bbb[3] = 'T'] = 3;
scanf("%d%d", &n, &m);
ans = vector<vector<char>>(n, vector<char>(m));
for (int i = 0; i < n; i++) {
scanf("%s", x);
for (int j = 0; j < m; j++) {
x[j] = aa[x[j]];
ca[i][j & 1][x[j]]++;
cb[j][i & 1][x[j]]++;
}
}
go(0);
memcpy(y, z, sizeof(y[0]) * 4);
if (bb == 0)
for (int i = 0; i < n; i++) {
ta = ca[i][0][y[0 + ((i & 1) << 1)]] + ca[i][1][y[1 + ((i & 1) << 1)]];
tb = ca[i][0][y[1 + ((i & 1) << 1)]] + ca[i][1][y[0 + ((i & 1) << 1)]];
if (ta > tb)
for (int j = 0; j < m; j++)
ans[i][j] = (j & 1) ? y[1 + ((i & 1) << 1)] : y[0 + ((i & 1) << 1)];
else
for (int j = 0; j < m; j++)
ans[i][j] = (j & 1) ? y[0 + ((i & 1) << 1)] : y[1 + ((i & 1) << 1)];
}
else
for (int i = 0; i < m; i++) {
ta = cb[i][0][y[0 + ((i & 1) << 0)]] + cb[i][1][y[2 + ((i & 1) << 0)]];
tb = cb[i][0][y[2 + ((i & 1) << 0)]] + cb[i][1][y[0 + ((i & 1) << 0)]];
if (ta > tb)
for (int j = 0; j < n; j++)
ans[j][i] = (j & 1) ? y[2 + ((i & 1) << 0)] : y[0 + ((i & 1) << 0)];
else
for (int j = 0; j < n; j++)
ans[j][i] = (j & 1) ? y[0 + ((i & 1) << 0)] : y[2 + ((i & 1) << 0)];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) printf("%c", bbb[ans[i][j]]);
puts("");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int perm[30][5];
int n, fv[11], vv[11], x;
void bck(int poz) {
if (poz > 4) {
++x;
for (int i = 1; i <= 4; ++i) perm[x][i] = vv[i];
return;
}
for (int i = 1; i <= 4; ++i)
if (!fv[i]) {
fv[i] = 1;
vv[poz] = i;
bck(poz + 1);
fv[i] = 0;
}
}
char msk[10];
int main() {
ios_base::sync_with_stdio(0);
cin.tie();
cout.tie();
long long ci, cc, i, j, s = 0, cnt0, n, a, k, ok = 0, m, mn = 30000003, cnt1,
cnt;
cin >> n >> m;
char v[n + 3][m + 3], bst[n + 3][m + 3], cur[n + 3][m + 3];
for (i = 1; i <= n; ++i) cin >> (v[i] + 1);
bck(1);
msk[1] = 'A';
msk[2] = 'G';
msk[3] = 'C';
msk[4] = 'T';
for (int y = 1; y <= 24; ++y) {
cnt = 0;
for (i = 0; i < n; i += 2) {
cur[i + 1][1] = msk[perm[y][1]];
cur[i + 1][2] = msk[perm[y][2]];
cur[i + 2][1] = msk[perm[y][3]];
cur[i + 2][2] = msk[perm[y][4]];
if (cur[i + 1][1] != v[i + 1][1]) ++cnt;
if (cur[i + 1][2] != v[i + 1][2]) ++cnt;
if (cur[i + 2][1] != v[i + 2][1]) ++cnt;
if (cur[i + 2][2] != v[i + 2][2]) ++cnt;
}
for (j = 3; j <= m; ++j) {
cnt0 = cnt1 = 0;
if (j % 2) {
for (i = 1; i <= n; ++i) {
if (i % 2) {
cnt0 += (cur[1][1] != v[i][j]);
cnt1 += (cur[2][1] != v[i][j]);
} else {
cnt1 += (cur[1][1] != v[i][j]);
cnt0 += (cur[2][1] != v[i][j]);
}
}
} else {
for (i = 1; i <= n; ++i) {
if (i % 2) {
cnt0 += (cur[1][2] != v[i][j]);
cnt1 += (cur[2][2] != v[i][j]);
} else {
cnt1 += (cur[1][2] != v[i][j]);
cnt0 += (cur[2][2] != v[i][j]);
}
}
}
cnt += min(cnt0, cnt1);
if (cnt0 < cnt1)
for (i = 1; i <= n; ++i)
cur[i][j] = cur[(i - 1) % 2 + 1][(j - 1) % 2 + 1];
else
for (i = 1; i <= n; ++i)
cur[i][j] = cur[3 - ((i - 1) % 2 + 1)][(j - 1) % 2 + 1];
}
if (cnt < mn) {
for (i = 1; i <= n; ++i)
for (j = 1; j <= m; ++j) bst[i][j] = cur[i][j];
mn = cnt;
}
cnt = 0;
for (j = 0; j < m; j += 2) {
cur[1][j + 1] = msk[perm[y][1]];
cur[1][j + 2] = msk[perm[y][2]];
cur[2][j + 1] = msk[perm[y][3]];
cur[2][j + 2] = msk[perm[y][4]];
if (cur[1][1 + j] != v[1][1 + j]) ++cnt;
if (cur[1][2 + j] != v[1][2 + j]) ++cnt;
if (cur[2][1 + j] != v[2][1 + j]) ++cnt;
if (cur[2][2 + j] != v[2][2 + j]) ++cnt;
}
for (i = 3; i <= n; ++i) {
cnt0 = cnt1 = 0;
if (i % 2) {
for (j = 1; j <= m; ++j) {
if (j % 2) {
cnt0 += (cur[1][1] != v[i][j]);
cnt1 += (cur[1][2] != v[i][j]);
} else {
cnt1 += (cur[1][1] != v[i][j]);
cnt0 += (cur[1][2] != v[i][j]);
}
}
} else {
for (j = 1; j <= m; ++j) {
if (j % 2) {
cnt0 += (cur[2][1] != v[i][j]);
cnt1 += (cur[2][2] != v[i][j]);
} else {
cnt1 += (cur[2][1] != v[i][j]);
cnt0 += (cur[2][2] != v[i][j]);
}
}
}
cnt += min(cnt0, cnt1);
if (cnt0 < cnt1)
for (j = 1; j <= m; ++j)
cur[i][j] = cur[(i - 1) % 2 + 1][(j - 1) % 2 + 1];
else
for (j = 1; j <= m; ++j)
cur[i][j] = cur[(i - 1) % 2 + 1][3 - ((j - 1) % 2 + 1)];
}
if (cnt < mn) {
for (i = 1; i <= n; ++i)
for (j = 1; j <= m; ++j) bst[i][j] = cur[i][j];
mn = cnt;
}
}
for (i = 1; i <= n; ++i) {
for (j = 1; j <= m; ++j) cout << bst[i][j];
cout << '\n';
}
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vl = vector<long long>;
using pii = pair<int, int>;
using vpii = vector<pair<int, int>>;
template <class T>
inline bool ckmin(T& a, T b) {
return b < a ? a = b, 1 : 0;
}
template <class T>
inline bool ckmax(T& a, T b) {
return b > a ? a = b, 1 : 0;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const char nl = '\n';
const int mxN = 2e5 + 10;
const int MOD = 1e9 + 7;
const long long infLL = 1e18;
int n, m, bst = MOD;
vector<string> ans;
vector<string> G(n, "");
void runhoriz(char a, char c, char g, char t) {
vector<string> cur(n);
for (int i = (0); i < (n); i++) {
if (i & 1) {
for (int j = (0); j < (m); j++) {
if (j & 1)
cur[i] += a;
else
cur[i] += c;
}
int dif1 = 0, dif2 = 0;
for (int j = (0); j < (m); j++) {
if (cur[i][j] != G[i][j]) dif1++;
}
for (int j = (0); j < (m); j++) {
if (j & 1)
cur[i][j] = c;
else
cur[i][j] = a;
}
for (int j = (0); j < (m); j++) {
if (cur[i][j] != G[i][j]) dif2++;
}
if (dif1 < dif2) {
for (int j = (0); j < (m); j++) {
if (j & 1)
cur[i][j] = a;
else
cur[i][j] = c;
}
}
} else {
for (int j = (0); j < (m); j++) {
if (j & 1)
cur[i] += g;
else
cur[i] += t;
}
int dif1 = 0, dif2 = 0;
for (int j = (0); j < (m); j++) {
if (cur[i][j] != G[i][j]) dif1++;
}
for (int j = (0); j < (m); j++) {
if (j & 1)
cur[i][j] = t;
else
cur[i][j] = g;
}
for (int j = (0); j < (m); j++) {
if (cur[i][j] != G[i][j]) dif2++;
}
if (dif1 < dif2) {
for (int j = (0); j < (m); j++) {
if (j & 1)
cur[i][j] = g;
else
cur[i][j] = t;
}
}
}
}
int dif = 0;
for (int i = (0); i < (n); i++) {
for (int j = (0); j < (m); j++) {
if (cur[i][j] != G[i][j]) {
dif++;
}
}
}
if (ckmin(bst, dif)) {
ans = cur;
}
}
void runvert(char a, char c, char g, char t) {
vector<string> cur(n, string('.', m));
for (int i = (0); i < (n); i++) cur[i].resize(m);
for (int j = (0); j < (m); j++) {
if (j & 1) {
for (int i = (0); i < (n); i++) {
if (i & 1)
cur[i][j] = a;
else
cur[i][j] = c;
}
int dif1 = 0, dif2 = 0;
for (int i = (0); i < (n); i++) {
if (cur[i][j] != G[i][j]) dif1++;
}
for (int i = (0); i < (n); i++) {
if (i & 1)
cur[i][j] = c;
else
cur[i][j] = a;
}
for (int i = (0); i < (n); i++) {
if (cur[i][j] != G[i][j]) dif2++;
}
if (dif1 < dif2) {
for (int i = (0); i < (n); i++) {
if (i & 1)
cur[i][j] = a;
else
cur[i][j] = c;
}
}
} else {
for (int i = (0); i < (n); i++) {
if (i & 1)
cur[i][j] = g;
else
cur[i][j] = t;
}
int dif1 = 0, dif2 = 0;
for (int i = (0); i < (n); i++) {
if (cur[i][j] != G[i][j]) dif1++;
}
for (int i = (0); i < (n); i++) {
if (i & 1)
cur[i][j] = t;
else
cur[i][j] = g;
}
for (int i = (0); i < (n); i++) {
if (cur[i][j] != G[i][j]) dif2++;
}
if (dif1 < dif2) {
for (int i = (0); i < (n); i++) {
if (i & 1)
cur[i][j] = g;
else
cur[i][j] = t;
}
}
}
}
int dif = 0;
for (int i = (0); i < (n); i++) {
for (int j = (0); j < (m); j++) {
if (cur[i][j] != G[i][j]) {
dif++;
}
}
}
if (ckmin(bst, dif)) {
ans = cur;
}
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
char c[] = {'A', 'C', 'G', 'T'};
cin >> n >> m;
G.resize(n);
for (int i = (0); i < (n); i++) {
cin >> G[i];
}
for (int i = (0); i < (4); i++) {
for (int j = (0); j < (4); j++) {
for (int k = (0); k < (4); k++) {
for (int l = (0); l < (4); l++) {
if (l != i && l != j && l != k && k != j && k != i && j != i) {
runhoriz(c[i], c[j], c[k], c[l]);
runvert(c[i], c[j], c[k], c[l]);
}
}
}
}
}
for (int i = (0); i < (n); i++) {
for (int j = (0); j < (m); j++) {
cout << ans[i][j];
}
cout << nl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
struct _IO {
_IO() {
ios::sync_with_stdio(0);
cin.tie(0);
}
} _io;
void uni(vector<int> &v) {
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
}
long double rd() {
long double x;
cin >> x;
return x;
}
long long ri() {
long long x;
cin >> x;
return x;
}
string rs() {
string s;
cin >> s;
return s;
}
pair<int, int> rpi() {
pair<int, int> x;
cin >> x.first >> x.second;
return x;
}
vector<int> rvi(int n, int f = 0) {
vector<int> v(n + f);
for (int i = f; i < v.size(); i++) cin >> v[i];
return v;
}
vector<vector<int>> rvii(int n, int m, int f = 0) {
vector<vector<int>> v(n + f, vector<int>(n + f));
for (int i = f; i < v.size(); i++)
for (int j = f; j < v[0].size(); j++) cin >> v[i][j];
return v;
}
void pr(){};
template <class T, class... U>
void pr(T x, U... y) {
cout << x << ' ', pr(y...);
}
void prl() { cout << '\n'; };
template <class T, class... U>
void prl(T x, U... y) {
cout << x << ' ', prl(y...);
}
void pr(const vector<int> &v, int f = 0) {
for (int i = f; i < v.size(); i++) cout << v[i] << ' ';
}
void prl(const vector<int> &v, int f = 0) {
pr(v, f);
cout << '\n';
}
void pr(const pair<int, int> &x) { cout << x.first << ' ' << x.second; };
void prl(const pair<int, int> &x) {
pr(x);
cout << '\n';
}
const int N = 2e5 + 5, M = 1e9 + 7;
int dp[N][16], lst[N][16];
pair<int, vector<string>> solve(int n, int m, vector<string> vs) {
vector<int> sta;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (i != j) sta.push_back(i * 10 + j);
}
}
string t = "ATCG";
for (int i = 0; i < m; i++) {
for (int s = 0; s < sta.size(); s++) {
dp[i + 1][s] = n * m, lst[i + 1][s] = -1;
int mask = sta[s], x = 0;
string p;
p.push_back(t[mask / 10]);
p.push_back(t[mask % 10]);
for (int j = 0; j < n; j++) {
x += vs[j][i] != p[j % 2];
}
for (int ls = 0; ls < sta.size(); ls++) {
int u = sta[ls] / 10, v = sta[ls] % 10;
if (u == mask / 10 || u == mask % 10 || v == mask / 10 ||
v == mask % 10)
continue;
if (dp[i + 1][s] > dp[i][ls] + x) {
dp[i + 1][s] = dp[i][ls] + x;
lst[i + 1][s] = ls;
}
}
}
}
vector<int> ans;
int cur = 0, ansi = 0;
for (int i = 0; i < sta.size(); i++) {
if (dp[m][i] < dp[m][cur]) cur = i;
}
ansi = cur;
for (int i = m; i >= 1; i--) {
ans.push_back(sta[cur]);
cur = lst[i][cur];
}
reverse(ans.begin(), ans.end());
vector<string> ans2(n);
for (int j = 0; j < m; j++) {
for (int i = 0; i < n; i++) {
string p;
p.push_back(t[ans[j] / 10]);
p.push_back(t[ans[j] % 10]);
ans2[i].push_back(p[i % 2]);
}
}
return make_pair(dp[m][ansi], ans2);
}
int main() {
int n = ri(), m = ri();
vector<string> vs(n);
for (int i = 0; i < n; i++) vs[i] = rs();
auto ans1 = solve(n, m, vs);
{
vector<string> tmp(m);
for (int j = 0; j < m; j++)
for (int i = 0; i < n; i++) tmp[j].push_back(vs[i][j]);
swap(vs, tmp);
swap(n, m);
}
auto ans2 = solve(n, m, vs);
{
vector<string> tmp(m);
for (int j = 0; j < m; j++)
for (int i = 0; i < n; i++) tmp[j].push_back(ans2.second[i][j]);
swap(ans2.second, tmp);
swap(n, m);
}
for (int i = 0; i < n; i++)
prl(ans1.first < ans2.first ? ans1.second[i] : ans2.second[i]);
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, ans;
vector<int> h[150010], t[150010], s[150010];
void cal1(int A, int B, int C, int D) {
int res = 0;
for (int i = 1; i <= n; i++) {
int sum1 = 0, sum2 = 0;
for (int j = 1; j <= m; j += 2) {
if (i & 1) {
sum1 += (s[i][j] != A) + (j < m && s[i][j + 1] != B);
sum2 += (s[i][j] != B) + (j < m && s[i][j + 1] != A);
} else {
sum1 += (s[i][j] != C) + (j < m && s[i][j + 1] != D);
sum2 += (s[i][j] != D) + (j < m && s[i][j + 1] != C);
}
}
if (sum1 < sum2) {
for (int j = 1; j <= m; j += 2) {
if (i & 1)
t[i][j] = A, t[i][j + 1] = B;
else
t[i][j] = C, t[i][j + 1] = D;
}
res += sum1;
} else {
for (int j = 1; j <= m; j += 2) {
if (i & 1)
t[i][j] = B, t[i][j + 1] = A;
else
t[i][j] = D, t[i][j + 1] = C;
}
res += sum2;
}
}
if (res < ans) {
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) h[i][j] = t[i][j];
ans = res;
}
}
void cal2(int A, int B, int C, int D) {
int res = 0;
for (int i = 1; i <= m; i++) {
int sum1 = 0, sum2 = 0;
for (int j = 1; j <= n; j += 2) {
if (i & 1) {
sum1 += (s[j][i] != A) + (j < n && s[j + 1][i] != B);
sum2 += (s[j][i] != B) + (j < n && s[j + 1][i] != A);
} else {
sum1 += (s[j][i] != C) + (j < n && s[j + 1][i] != D);
sum2 += (s[j][i] != D) + (j < n && s[j + 1][i] != C);
}
}
if (sum1 < sum2) {
for (int j = 1; j <= n; j += 2) {
if (i & 1)
t[j][i] = A, t[j + 1][i] = B;
else
t[j][i] = C, t[j + 1][i] = D;
}
res += sum1;
} else {
for (int j = 1; j <= n; j += 2) {
if (i & 1)
t[j][i] = B, t[j + 1][i] = A;
else
t[j][i] = D, t[j + 1][i] = C;
}
res += sum2;
}
}
if (res < ans) {
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) h[i][j] = t[i][j];
ans = res;
}
}
int get(char ch) {
if (ch == 'A') return 0;
if (ch == 'G') return 1;
if (ch == 'T') return 2;
return 3;
}
int C(int x) {
if (x == 0) return 'A';
if (x == 1) return 'G';
if (x == 2) return 'T';
return 'C';
}
int main() {
char ch;
int a, b;
scanf("%d%d", &n, &m);
ans = n * m;
for (int i = 0; i <= n + 5; i++)
for (int j = 0; j <= m + 5; j++)
h[i].push_back(0), t[i].push_back(0), s[i].push_back(0);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) scanf(" %c", &ch), s[i][j] = get(ch);
for (int i = 0; i <= 3; i++)
for (int j = 0; j <= 3; j++) {
if (i == j) continue;
for (a = 0; a <= 3; a++)
if (i != a && j != a) break;
for (b = a + 1; b <= 3; b++)
if (i != b && j != b) break;
cal1(i, j, a, b);
cal1(i, j, b, a);
cal2(i, j, a, b);
cal2(i, j, b, a);
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) printf("%c", C(h[i][j]));
printf("\n");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const char c[4] = {'A', 'C', 'G', 'T'};
const int INF = 1e9;
int n, m;
vector<string> a;
struct plan {
int ch1, ch2, ch3, ch4, md;
vector<int> cases;
int cost;
} ans;
signed main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
string s;
cin >> s;
a.push_back(s);
}
ans.cost = INF;
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
if (i != j) {
plan now;
now.ch1 = now.ch2 = now.ch3 = now.ch4 = now.cost = 0;
now.cases.clear();
int k = -1, t = -1;
for (int ww = 0; ww < 4; ww++)
if (ww != i && ww != j) {
if (k == -1)
k = ww;
else
t = ww;
}
now.ch1 = i;
now.ch2 = j;
now.ch3 = k;
now.ch4 = t;
now.md = 0;
for (int x = 0; x < n; x++) {
int now1 = 0, now2 = 0;
for (int y = 0; y < m; y++)
if ((x & 1) == 0) {
now1 += (y & 1) ? (!(c[j] == a[x][y])) : (!(c[i] == a[x][y]));
now2 += (y & 1) ? (!(c[i] == a[x][y])) : (!(c[j] == a[x][y]));
} else {
now1 += (y & 1) ? (!(c[t] == a[x][y])) : (!(c[k] == a[x][y]));
now2 += (y & 1) ? (!(c[k] == a[x][y])) : (!(c[t] == a[x][y]));
}
if (now1 < now2)
now.cases.push_back(0);
else
now.cases.push_back(1);
now.cost += min(now1, now2);
}
if (now.cost < ans.cost) ans = now;
}
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
if (i != j) {
plan now;
now.ch1 = now.ch2 = now.ch3 = now.ch4 = now.cost = 0;
now.cases.clear();
int k = -1, t = -1;
for (int ww = 0; ww < 4; ww++)
if (ww != i && ww != j) {
if (k == -1)
k = ww;
else
t = ww;
}
now.ch1 = i;
now.ch2 = j;
now.ch3 = k;
now.ch4 = t;
now.md = 1;
for (int y = 0; y < m; y++) {
int now1 = 0, now2 = 0;
for (int x = 0; x < n; x++)
if ((y & 1) == 0) {
now1 += (x & 1) ? (!(c[j] == a[x][y])) : (!(c[i] == a[x][y]));
now2 += (x & 1) ? (!(c[i] == a[x][y])) : (!(c[j] == a[x][y]));
} else {
now1 += (x & 1) ? (!(c[t] == a[x][y])) : (!(c[k] == a[x][y]));
now2 += (x & 1) ? (!(c[k] == a[x][y])) : (!(c[t] == a[x][y]));
}
if (now1 < now2)
now.cases.push_back(0);
else
now.cases.push_back(1);
now.cost += min(now1, now2);
}
if (now.cost < ans.cost) ans = now;
}
if (ans.md == 0) {
int i = ans.ch1, j = ans.ch2, k = ans.ch3, t = ans.ch4;
for (int x = 0; x < n; x++) {
for (int y = 0; y < m; y++)
if ((x & 1) == 0) {
if (ans.cases[x] == 0)
putchar((y & 1) ? c[j] : c[i]);
else if (ans.cases[x] == 1)
putchar((y & 1) ? c[i] : c[j]);
} else {
if (ans.cases[x] == 0)
putchar((y & 1) ? c[t] : c[k]);
else if (ans.cases[x] == 1)
putchar((y & 1) ? c[k] : c[t]);
}
printf("\n");
}
} else {
int i = ans.ch1, j = ans.ch2, k = ans.ch3, t = ans.ch4;
for (int x = 0; x < n; x++) {
for (int y = 0; y < m; y++)
if ((y & 1) == 0) {
if (ans.cases[y] == 0)
putchar((x & 1) ? c[j] : c[i]);
else if (ans.cases[y] == 1)
putchar((x & 1) ? c[i] : c[j]);
} else {
if (ans.cases[y] == 0)
putchar((x & 1) ? c[t] : c[k]);
else if (ans.cases[y] == 1)
putchar((x & 1) ? c[k] : c[t]);
}
printf("\n");
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
struct mat {
int n, m;
vector<char> data;
mat(int n, int m) : n(n), m(m), data(n * m) {}
char& get(int i, int j, int rot = 0) {
return data[!rot ? (i * m + j) : (j * m + i)];
}
void dump() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << get(i, j);
}
cout << "\n";
}
}
};
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
int n, m;
cin >> n >> m;
mat g(n, m);
for (int i = 0; i < n; i++) {
string s;
cin >> s;
for (int j = 0; j < m; j++) {
g.get(i, j) = s[j];
}
}
string seed = "ACGT";
mat best = g;
int bestv = -1;
mat cur(n, m);
do {
int N = n, M = m;
for (int rot = 0; rot < 2; rot++) {
int val = 0;
for (int i = 0; i < N; i++) {
int c[2];
c[0] = c[1] = 0;
for (int j = 0; j < M; j++) {
for (int k = 0; k < 2; k++) {
c[k] += seed[(i % 2) * 2 + (j + k) % 2] == g.get(i, j, rot);
}
}
int k = c[1] > c[0];
for (int j = 0; j < M; j++) {
cur.get(i, j, rot) = seed[(i % 2) * 2 + (j + k) % 2];
}
if (0) cout << c[k] << endl;
val += c[k];
}
if (0) {
cur.dump();
cout << val << "\n" << endl;
}
if (val > bestv) {
bestv = val;
best = cur;
}
swap(N, M);
}
} while (next_permutation(seed.begin(), seed.end()));
best.dump();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, m;
cin >> n >> m;
char table[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> table[i][j];
}
}
char s[6][4] = {{'A', 'C', 'G', 'T'}, {'G', 'T', 'A', 'C'},
{'A', 'G', 'C', 'T'}, {'T', 'C', 'G', 'A'},
{'A', 'T', 'G', 'C'}, {'G', 'C', 'A', 'T'}};
char best[n][m];
int mmin = 3e5 + 5;
for (int t = 0; t < 6; t++) {
int cur = 0;
char temp[n][m];
for (int i = 0; i < n; i++) {
int shift = (i % 2) * 2;
int first = 0;
int second = 0;
for (int j = 0; j < m; j++) {
if (s[t][(j % 2) + shift] != table[i][j]) first++;
if (s[t][((j + 1) % 2) + shift] != table[i][j]) second++;
}
int x = 0;
if (second < first) x = 1;
for (int j = 0; j < m; j++) {
temp[i][j] = s[t][((j + x) % 2) + shift];
}
cur += min(first, second);
}
if (cur < mmin) {
mmin = cur;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
best[i][j] = temp[i][j];
}
}
}
cur = 0;
temp[n][m];
for (int i = 0; i < m; i++) {
int shift = (i % 2) * 2;
int first = 0;
int second = 0;
for (int j = 0; j < n; j++) {
if (s[t][(j % 2) + shift] != table[j][i]) first++;
if (s[t][((j + 1) % 2) + shift] != table[j][i]) second++;
}
int x = 0;
if (second < first) x = 1;
for (int j = 0; j < n; j++) {
temp[j][i] = s[t][((j + x) % 2) + shift];
}
cur += min(first, second);
}
if (cur < mmin) {
mmin = cur;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
best[i][j] = temp[i][j];
}
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
string t(1, best[i][j]);
cout << t;
}
cout << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long qpow(long long a, long long b, long long m) {
long long r = 1;
a %= m;
for (; b; b >>= 1) {
if (b & 1) r = r * a % m;
a = a * a % m;
}
return r;
}
const int inf = 0x7fffffff;
int cnt[40];
char aa[] = "ATCG";
string s[300005];
string ans[300005];
int n, m;
int main() {
ios::sync_with_stdio(false);
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> s[i];
int tt = 0, pos = 0, mi = inf;
for (int i1 = 0; i1 < 4; i1++) {
for (int i2 = i1 + 1; i2 < 4; i2++) {
vector<int> ve[2];
ve[0].push_back(i1);
ve[0].push_back(i2);
for (int i = 0; i < 4; i++)
if (i != i1 && i != i2) ve[1].push_back(i);
tt++;
for (int i = 0; i < n; i++) {
int p = i % 2;
int x = 0, cnt1 = 0, cnt2 = 0;
for (int j = 0; j < m; j++) {
if (aa[ve[p][x]] != s[i][j]) cnt1++;
x ^= 1;
}
x = 1;
for (int j = 0; j < m; j++) {
if (aa[ve[p][x]] != s[i][j]) cnt2++;
x ^= 1;
}
if (cnt1 < cnt2)
cnt[tt] += cnt1;
else
cnt[tt] += cnt2;
}
if (cnt[tt] < mi) {
mi = cnt[tt];
pos = tt;
}
tt++;
for (int i = 0; i < n; i++) {
int p = (i + 1) % 2;
int x = 0, cnt1 = 0, cnt2 = 0;
for (int j = 0; j < m; j++) {
if (aa[ve[p][x]] != s[i][j]) cnt1++;
x ^= 1;
}
x = 1;
for (int j = 0; j < m; j++) {
if (aa[ve[p][x]] != s[i][j]) cnt2++;
x ^= 1;
}
if (cnt1 < cnt2)
cnt[tt] += cnt1;
else
cnt[tt] += cnt2;
}
if (cnt[tt] < mi) {
mi = cnt[tt];
pos = tt;
}
tt++;
for (int i = 0; i < m; i++) {
int p = i % 2;
int x = 0, cnt1 = 0, cnt2 = 0;
for (int j = 0; j < n; j++) {
if (aa[ve[p][x]] != s[j][i]) cnt1++;
x ^= 1;
}
x = 1;
for (int j = 0; j < n; j++) {
if (aa[ve[p][x]] != s[j][i]) cnt2++;
x ^= 1;
}
if (cnt1 < cnt2)
cnt[tt] += cnt1;
else
cnt[tt] += cnt2;
}
if (cnt[tt] < mi) {
mi = cnt[tt];
pos = tt;
}
tt++;
for (int i = 0; i < m; i++) {
int p = (i + 1) % 2;
int x = 0, cnt1 = 0, cnt2 = 0;
for (int j = 0; j < n; j++) {
if (aa[ve[p][x]] != s[j][i]) cnt1++;
x ^= 1;
}
x = 1;
for (int j = 0; j < n; j++) {
if (aa[ve[p][x]] != s[j][i]) cnt2++;
x ^= 1;
}
if (cnt1 < cnt2)
cnt[tt] += cnt1;
else
cnt[tt] += cnt2;
}
if (cnt[tt] < mi) {
mi = cnt[tt];
pos = tt;
}
}
}
int ttt = 0;
for (int i1 = 0; i1 < 4; i1++) {
for (int i2 = i1 + 1; i2 < 4; i2++) {
vector<int> ve[2];
ve[0].push_back(i1);
ve[0].push_back(i2);
for (int i = 0; i < 4; i++)
if (i != i1 && i != i2) ve[1].push_back(i);
ttt++;
if (ttt == pos) {
for (int i = 0; i < n; i++) {
int p = i % 2;
int x = 0, cnt1 = 0, cnt2 = 0;
string s1 = "", s2 = "";
for (int j = 0; j < m; j++) {
s1 += aa[ve[p][x]];
if (aa[ve[p][x]] != s[i][j]) cnt1++;
x ^= 1;
}
x = 1;
for (int j = 0; j < m; j++) {
s2 += aa[ve[p][x]];
if (aa[ve[p][x]] != s[i][j]) cnt2++;
x ^= 1;
}
if (cnt1 < cnt2)
cout << s1 << endl;
else
cout << s2 << endl;
}
return 0;
}
ttt++;
if (ttt == pos) {
for (int i = 0; i < n; i++) {
int p = (i + 1) % 2;
int x = 0, cnt1 = 0, cnt2 = 0;
string s1 = "", s2 = "";
for (int j = 0; j < m; j++) {
s1 += aa[ve[p][x]];
if (aa[ve[p][x]] != s[i][j]) cnt1++;
x ^= 1;
}
x = 1;
for (int j = 0; j < m; j++) {
s2 += aa[ve[p][x]];
if (aa[ve[p][x]] != s[i][j]) cnt2++;
x ^= 1;
}
if (cnt1 < cnt2)
cout << s1 << endl;
else
cout << s2 << endl;
}
return 0;
}
ttt++;
if (ttt == pos) {
for (int i = 0; i < m; i++) {
int p = i % 2;
int x = 0, cnt1 = 0, cnt2 = 0;
string s1 = "", s2 = "";
for (int j = 0; j < n; j++) {
s1 += aa[ve[p][x]];
if (aa[ve[p][x]] != s[j][i]) cnt1++;
x ^= 1;
}
x = 1;
for (int j = 0; j < n; j++) {
s2 += aa[ve[p][x]];
if (aa[ve[p][x]] != s[j][i]) cnt2++;
x ^= 1;
}
if (cnt1 < cnt2) {
for (int j = 0; j < n; j++) ans[j] += s1[j];
} else {
for (int j = 0; j < n; j++) ans[j] += s2[j];
}
}
for (int i = 0; i < n; i++) cout << ans[i] << endl;
return 0;
}
ttt++;
if (ttt == pos) {
for (int i = 0; i < m; i++) {
int p = (i + 1) % 2;
int x = 0, cnt1 = 0, cnt2 = 0;
string s1 = "", s2 = "";
for (int j = 0; j < n; j++) {
s1 += aa[ve[p][x]];
if (aa[ve[p][x]] != s[j][i]) cnt1++;
x ^= 1;
}
x = 1;
for (int j = 0; j < n; j++) {
s2 += aa[ve[p][x]];
if (aa[ve[p][x]] != s[j][i]) cnt2++;
x ^= 1;
}
if (cnt1 < cnt2) {
for (int j = 0; j < n; j++) ans[j] += s1[j];
} else {
for (int j = 0; j < n; j++) ans[j] += s2[j];
}
}
for (int i = 0; i < n; i++) cout << ans[i] << endl;
return 0;
}
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 7;
const int maxN = 3e5 + 5;
const int Q = 998244353;
const double eps = 1e-9;
int n, m;
string s[maxN];
struct Cost {
int cost;
char a;
char b;
};
struct Cost rowCost(int row, char a, char b) {
int ans = 0;
int ans2 = 0;
for (int i = 0; i < m; i++) {
if (i % 2 == 0) {
ans += (s[row][i] != a);
ans2 += (s[row][i] != b);
} else {
ans += (s[row][i] != b);
ans2 += (s[row][i] != a);
}
}
if (ans < ans2) {
return Cost{ans, a, b};
}
return Cost{ans2, b, a};
}
struct Cost columnCost(int col, char a, char b) {
int ans = 0;
int ans2 = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
ans += (s[i][col] != a);
ans2 += (s[i][col] != b);
} else {
ans += (s[i][col] != b);
ans2 += (s[i][col] != a);
}
}
if (ans < ans2) {
return Cost{ans, a, b};
}
return Cost{ans2, b, a};
}
int getRowCost(char a, char b, char c, char d) {
set<char> s = {a, b, c, d};
if ((int)s.size() != 4) return inf;
int ans = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
Cost cost = rowCost(i, a, b);
ans += cost.cost;
} else {
Cost cost = rowCost(i, c, d);
ans += cost.cost;
}
}
return ans;
}
int getColumnCost(char a, char b, char c, char d) {
set<char> s = {a, b, c, d};
if ((int)s.size() != 4) return inf;
int ans = 0;
for (int i = 0; i < m; i++) {
if (i % 2 == 0) {
Cost cost = columnCost(i, a, b);
ans += cost.cost;
} else {
Cost cost = columnCost(i, c, d);
ans += cost.cost;
}
}
return ans;
}
void newRow(int i, char a, char b) {
for (int j = 0; j < m; j++) {
if (j % 2 == 0)
s[i][j] = a;
else
s[i][j] = b;
}
}
void newColumn(int i, char a, char b) {
for (int j = 0; j < n; j++) {
if (j % 2 == 0)
s[j][i] = a;
else
s[j][i] = b;
}
}
void newColAnswer(vector<char> answer) {
char a = answer[0];
char b = answer[1];
char c = answer[2];
char d = answer[3];
for (int i = 0; i < m; i += 2) {
Cost cost = columnCost(i, a, b);
newColumn(i, cost.a, cost.b);
}
for (int i = 1; i < m; i += 2) {
Cost cost = columnCost(i, c, d);
newColumn(i, cost.a, cost.b);
}
}
void newRowAnswer(vector<char> answer) {
char a = answer[0];
char b = answer[1];
char c = answer[2];
char d = answer[3];
for (int i = 0; i < n; i += 2) {
Cost cost = rowCost(i, a, b);
newRow(i, cost.a, cost.b);
}
for (int i = 1; i < n; i += 2) {
Cost cost = rowCost(i, c, d);
newRow(i, cost.a, cost.b);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin >> n >> m;
for (int i = (0); i <= ((n)-1); ++i) {
cin >> s[i];
}
int minim = n * m + 1;
vector<char> answer;
bool column = false;
vector<string> t = {"ACGT", "AGCT", "ATCG", "GTAC", "CTAG", "CGAT"};
for (auto s : t) {
int x = getRowCost(s[0], s[1], s[2], s[3]);
if (x < minim) {
answer = {s[0], s[1], s[2], s[3]};
minim = x;
column = false;
}
x = getColumnCost(s[0], s[1], s[2], s[3]);
if (x < minim) {
answer = {s[0], s[1], s[2], s[3]};
minim = x;
column = true;
}
}
if (column)
newColAnswer(answer);
else
newRowAnswer(answer);
for (int i = (0); i <= ((n)-1); ++i) {
cout << s[i] << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 4e5 + 5;
int n, m, t;
string s[maxn], ans[maxn];
int tonum(char ch) {
if (ch == 'A') return 1;
if (ch == 'G') return 2;
if (ch == 'C') return 3;
if (ch == 'T') return 4;
}
char tochar(int x) {
if (x == 1) return 'A';
if (x == 2) return 'G';
if (x == 3) return 'C';
if (x == 4) return 'T';
}
vector<int> a[maxn], temp[maxn];
int md[30][10];
int mmin = maxn;
int main() {
std::ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> s[i];
for (int j = 0; j < m; j++) {
a[i].push_back(tonum(s[i][j]));
}
}
int index = 0;
for (int c1 = 1; c1 <= 4; c1++) {
for (int c2 = 1; c2 <= 4; c2++) {
for (int c3 = 1; c3 <= 4; c3++) {
if (c1 == c2 || c1 == c3 || c2 == c3) continue;
int c4 = 10 - c1 - c2 - c3;
md[++index][1] = c1;
md[index][2] = c2;
md[index][3] = c3;
md[index][4] = c4;
}
}
}
for (int k = 1; k <= index; k++) {
int c1 = md[k][1], c2 = md[k][2], c3 = md[k][3], c4 = md[k][4];
for (int dire = 0; dire <= 1; dire++) {
int change = 0;
if (dire == 0) {
for (int i = 0; i < n; i++) temp[i].clear();
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
if (i == 0) {
for (int j = 0; j < m; j++) {
if (j % 2 == 0) {
if (c1 != a[i][j]) change++;
temp[i].push_back(c1);
} else {
if (c2 != a[i][j]) change++;
temp[i].push_back(c2);
}
}
} else {
int change1 = 0, change2 = 0;
for (int j = 0; j < m; j++) {
if (j % 2 == 0) {
if (c1 != a[i][j]) change1++;
if (c2 != a[i][j]) change2++;
} else {
if (c1 != a[i][j]) change2++;
if (c2 != a[i][j]) change1++;
}
}
if (change1 <= change2) {
change += change1;
for (int j = 0; j < m; j++) {
if (j % 2 == 0)
temp[i].push_back(c1);
else
temp[i].push_back(c2);
}
} else {
change += change2;
for (int j = 0; j < m; j++) {
if (j % 2 == 0)
temp[i].push_back(c2);
else
temp[i].push_back(c1);
}
}
}
} else {
if (i == 1) {
for (int j = 0; j < m; j++) {
if (j % 2 == 0) {
if (c3 != a[i][j]) change++;
temp[i].push_back(c3);
} else {
if (c4 != a[i][j]) change++;
temp[i].push_back(c4);
}
}
} else {
int change1 = 0, change2 = 0;
for (int j = 0; j < m; j++) {
if (j % 2 == 0) {
if (c3 != a[i][j]) change1++;
if (c4 != a[i][j]) change2++;
} else {
if (c3 != a[i][j]) change2++;
if (c4 != a[i][j]) change1++;
}
}
if (change1 <= change2) {
change += change1;
for (int j = 0; j < m; j++) {
if (j % 2 == 0)
temp[i].push_back(c3);
else
temp[i].push_back(c4);
}
} else {
change += change2;
for (int j = 0; j < m; j++) {
if (j % 2 == 0)
temp[i].push_back(c4);
else
temp[i].push_back(c3);
}
}
}
}
}
if (change < mmin) {
mmin = change;
for (int i = 0; i < n; i++) ans[i].clear();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
ans[i].push_back(tochar(temp[i][j]));
}
}
}
} else {
for (int j = 0; j < m; j++) temp[j].clear();
for (int j = 0; j < m; j++) {
if (j % 2 == 0) {
if (j == 0) {
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
if (c1 != a[i][j]) change++;
temp[j].push_back(c1);
} else {
if (c3 != a[i][j]) change++;
temp[j].push_back(c3);
}
}
} else {
int change1 = 0, change2 = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
if (c1 != a[i][j]) change1++;
if (c3 != a[i][j]) change2++;
} else {
if (c1 != a[i][j]) change2++;
if (c3 != a[i][j]) change1++;
}
}
if (change1 <= change2) {
change += change1;
for (int i = 0; i < n; i++) {
if (i % 2 == 0)
temp[j].push_back(c1);
else
temp[j].push_back(c3);
}
} else {
change += change2;
for (int i = 0; i < n; i++) {
if (i % 2 == 0)
temp[j].push_back(c3);
else
temp[j].push_back(c1);
}
}
}
} else {
if (j == 1) {
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
if (c2 != a[i][j]) change++;
temp[j].push_back(c2);
} else {
if (c4 != a[i][j]) change++;
temp[j].push_back(c4);
}
}
} else {
int change1 = 0, change2 = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
if (c2 != a[i][j]) change1++;
if (c4 != a[i][j]) change2++;
} else {
if (c2 != a[i][j]) change2++;
if (c4 != a[i][j]) change1++;
}
}
if (change1 <= change2) {
change += change1;
for (int i = 0; i < n; i++) {
if (i % 2 == 0)
temp[j].push_back(c2);
else
temp[j].push_back(c4);
}
} else {
change += change2;
for (int i = 0; i < n; i++) {
if (i % 2 == 0)
temp[j].push_back(c4);
else
temp[j].push_back(c2);
}
}
}
}
}
if (change < mmin) {
mmin = change;
for (int i = 0; i < n; i++) ans[i].clear();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
ans[i].push_back(tochar(temp[j][i]));
}
}
}
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << ans[i][j];
}
cout << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 300003;
const int M = 12;
char pat[M][2] = {'A', 'G', 'C', 'T', 'G', 'A', 'T', 'C', 'A', 'C', 'G', 'T',
'C', 'A', 'T', 'G', 'A', 'T', 'C', 'G', 'T', 'A', 'G', 'C'};
int from[M][2] = {1, 3, 0, 2, 1, 3, 0, 2, 5, 7, 4, 6,
5, 7, 4, 6, 9, 11, 8, 10, 9, 11, 8, 10};
int n, m, ans, w0, w1, o;
int an[N][M], am[N][M], pre[N][M], b[2][M], con[N];
char s[N];
int main() {
scanf("%d%d", &n, &m);
ans = m * n;
for (int i = 0; i < n; i++) {
scanf("%s", s);
for (int j = 0; j < m; j++)
for (int k = 0; k < M; k++) {
if (s[j] != pat[k][j & 1]) an[i][k]++;
if (s[j] != pat[k][i & 1]) am[j][k]++;
}
}
for (int i = 0; i < M; i++) b[0][i] = an[0][i];
o = 0;
for (int i = 1; i < n; i++) {
o = 1 - o;
for (int k = 0; k < M; k++) {
w0 = from[k][0];
w1 = from[k][1];
if (b[1 - o][w0] <= b[1 - o][w1]) {
b[o][k] = b[1 - o][w0] + an[i][k];
pre[i][k] = w0;
} else {
b[o][k] = b[1 - o][w1] + an[i][k];
pre[i][k] = w1;
}
}
}
for (int i = 0; i < M; i++)
if (ans > b[o][i]) {
ans = b[o][i];
w0 = i;
}
for (int i = n - 1; i >= 0; i--) {
con[i] = w0;
w0 = pre[i][w0];
}
for (int i = 0; i < M; i++) b[0][i] = am[0][i];
o = 0;
for (int i = 1; i < m; i++) {
o = 1 - o;
for (int k = 0; k < M; k++) {
w0 = from[k][0];
w1 = from[k][1];
if (b[1 - o][w0] <= b[1 - o][w1]) {
b[o][k] = b[1 - o][w0] + am[i][k];
pre[i][k] = w0;
} else {
b[o][k] = b[1 - o][w1] + am[i][k];
pre[i][k] = w1;
}
}
}
w1 = 0;
for (int i = 0; i < M; i++)
if (ans > b[o][i]) {
ans = b[o][i];
w0 = i;
w1 = 1;
}
if (w1) {
for (int i = m - 1; i >= 0; i--) {
con[i] = w0;
w0 = pre[i][w0];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) printf("%c", pat[con[j]][i & 1]);
printf("\n");
}
} else {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) printf("%c", pat[con[i]][j & 1]);
printf("\n");
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 17;
int n, m, ans = maxn;
string c[maxn], cer[maxn];
char per[] = "ACGT";
bool w[maxn];
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> c[i], cer[i] = c[i];
do {
int cur = 0;
for (int i = 0; i < n; i++) {
int c1 = 0, c2 = 0;
for (int j = 0; j < m; j++) {
c1 += per[(i & 1) * 2 + (j & 1)] != c[i][j];
c2 += per[(i & 1) * 2 + !(j & 1)] != c[i][j];
}
w[i] = c1 < c2;
cur += min(c1, c2);
}
if (cur < ans) {
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cer[i][j] =
w[i] ? per[(i & 1) * 2 + (j & 1)] : per[(i & 1) * 2 + !(j & 1)];
ans = cur;
}
cur = 0;
for (int i = 0; i < m; i++) {
int c1 = 0, c2 = 0;
for (int j = 0; j < n; j++) {
c1 += per[(i & 1) * 2 + (j & 1)] != c[j][i];
c2 += per[(i & 1) * 2 + !(j & 1)] != c[j][i];
}
w[i] = c1 < c2;
cur += min(c1, c2);
}
if (cur < ans) {
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
cer[j][i] =
w[i] ? per[(i & 1) * 2 + (j & 1)] : per[(i & 1) * 2 + !(j & 1)];
ans = cur;
}
} while (next_permutation(per, per + 4));
cerr << ans << '\n';
for (int i = 0; i < n; i++) cout << cer[i] << '\n';
}
|
#include <bits/stdc++.h>
using namespace std;
vector<vector<char> > a, b;
long long n, m, res = 1e9;
void init(vector<vector<char> > &a) {
a.assign(n + 1, vector<char>());
for (long long i = 1; i <= n; i++) a[i].assign(m + 1, char());
}
void solve(char A, char B, char C, char D, bool opt) {
long long ans = 0;
vector<vector<char> > tmp;
init(tmp);
if (!opt) {
for (long long i = 1; i <= n; i++) {
if (i % 2 == 1) {
long long tmp1 = 0, tmp2 = 0;
for (long long j = 1; j <= m; j++) {
if (j % 2 == 1 and a[i][j] != A) tmp1++;
if (j % 2 == 0 and a[i][j] != B) tmp1++;
}
for (long long j = 1; j <= m; j++) {
if (j % 2 == 1 and a[i][j] != B) tmp2++;
if (j % 2 == 0 and a[i][j] != A) tmp2++;
}
if (tmp1 < tmp2) {
ans += tmp1;
for (long long j = 1; j <= m; j++) tmp[i][j] = (j % 2 == 1 ? A : B);
} else {
ans += tmp2;
for (long long j = 1; j <= m; j++) tmp[i][j] = (j % 2 == 1 ? B : A);
}
} else {
long long tmp1 = 0, tmp2 = 0;
for (long long j = 1; j <= m; j++) {
if (j % 2 == 1 and a[i][j] != C) tmp1++;
if (j % 2 == 0 and a[i][j] != D) tmp1++;
}
for (long long j = 1; j <= m; j++) {
if (j % 2 == 1 and a[i][j] != D) tmp2++;
if (j % 2 == 0 and a[i][j] != C) tmp2++;
}
if (tmp1 < tmp2) {
ans += tmp1;
for (long long j = 1; j <= m; j++) tmp[i][j] = (j % 2 == 1 ? C : D);
} else {
ans += tmp2;
for (long long j = 1; j <= m; j++) tmp[i][j] = (j % 2 == 1 ? D : C);
}
}
}
} else {
for (long long i = 1; i <= m; i++) {
if (i % 2 == 1) {
long long tmp1 = 0, tmp2 = 0;
for (long long j = 1; j <= n; j++) {
if (j % 2 == 1 and a[j][i] != A) tmp1++;
if (j % 2 == 0 and a[j][i] != B) tmp1++;
}
for (long long j = 1; j <= n; j++) {
if (j % 2 == 1 and a[j][i] != B) tmp2++;
if (j % 2 == 0 and a[j][i] != A) tmp2++;
}
if (tmp1 < tmp2) {
ans += tmp1;
for (long long j = 1; j <= n; j++) tmp[j][i] = (j % 2 == 1 ? A : B);
} else {
ans += tmp2;
for (long long j = 1; j <= n; j++) tmp[j][i] = (j % 2 == 1 ? B : A);
}
} else {
long long tmp1 = 0, tmp2 = 0;
for (long long j = 1; j <= n; j++) {
if (j % 2 == 1 and a[j][i] != C) tmp1++;
if (j % 2 == 0 and a[j][i] != D) tmp1++;
}
for (long long j = 1; j <= n; j++) {
if (j % 2 == 1 and a[j][i] != D) tmp2++;
if (j % 2 == 0 and a[j][i] != C) tmp2++;
}
if (tmp1 < tmp2) {
ans += tmp1;
for (long long j = 1; j <= n; j++) tmp[j][i] = (j % 2 == 1 ? C : D);
} else {
ans += tmp2;
for (long long j = 1; j <= n; j++) tmp[j][i] = (j % 2 == 1 ? D : C);
}
}
}
}
if (res > ans) {
res = ans;
b = tmp;
}
}
signed main() {
cin >> n >> m;
init(a);
init(b);
for (long long i = 1; i <= n; i++) {
string s;
cin >> s;
for (long long j = 1; j <= m; j++) a[i][j] = s[j - 1];
}
char c[4] = {'A', 'T', 'G', 'C'};
for (long long i = 1; i <= (1 << 4); i++) {
if (__builtin_popcount(i) != 2) continue;
vector<char> up, dwn;
for (long long j = 0; j < 4; j++) {
if (i & (1 << j))
up.push_back(c[j]);
else
dwn.push_back(c[j]);
}
solve(up[0], up[1], dwn[0], dwn[1], 0);
solve(up[0], up[1], dwn[0], dwn[1], 1);
}
for (long long i = 1; i <= n; i++) {
for (long long j = 1; j <= m; j++) cout << b[i][j];
cout << '\n';
}
}
|
#include <bits/stdc++.h>
using namespace std;
char ele[4] = {'A', 'C', 'G', 'T'};
int id[6][4] = {{0, 1, 2, 3}, {0, 2, 1, 3}, {0, 3, 1, 2},
{1, 2, 0, 3}, {1, 3, 0, 2}, {2, 3, 0, 1}};
int n, m;
int solve1(vector<vector<char> > &A, vector<vector<char> > &ans) {
int res = n * m + 1;
for (int ti = 0; ti < 6; ti++) {
vector<vector<char> > B = A;
int cnt = 0;
for (int i = 0; i < n; i++) {
int tmp[2] = {0};
for (int j = 0; j < m; j++)
for (int k = 0; k < 2; k++) {
tmp[k] += (A[i][j] != ele[id[ti][2 * (i & 1) + ((j & 1) ^ k)]]);
}
int select = tmp[0] < tmp[1] ? 0 : 1;
cnt += tmp[select];
for (int j = 0; j < m; j++)
B[i][j] = ele[id[ti][2 * (i & 1) + ((j & 1) ^ select)]];
}
if (cnt < res) {
res = cnt;
ans = B;
}
}
return res;
}
int solve2(vector<vector<char> > &A, vector<vector<char> > &ans) {
int res = n * m + 1;
for (int ti = 0; ti < 6; ti++) {
vector<vector<char> > B = A;
int cnt = 0;
for (int i = 0; i < m; i++) {
int tmp[2] = {0};
for (int j = 0; j < n; j++)
for (int k = 0; k < 2; k++) {
tmp[k] += (A[j][i] != ele[id[ti][2 * (i & 1) + ((j & 1) ^ k)]]);
}
int select = tmp[0] < tmp[1] ? 0 : 1;
cnt += tmp[select];
for (int j = 0; j < n; j++)
B[j][i] = ele[id[ti][2 * (i & 1) + ((j & 1) ^ select)]];
}
if (cnt < res) {
res = cnt;
ans = B;
}
}
return res;
}
const int N = 3e5 + 10;
char s[N];
void print(vector<vector<char> > &A) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) printf("%c", A[i][j]);
puts("");
}
}
int main() {
scanf("%d%d", &n, &m);
vector<vector<char> > A(n, vector<char>(m));
for (int i = 0; i < n; i++) {
scanf("%s", s);
for (int j = 0; j < m; j++) A[i][j] = s[j];
}
vector<vector<char> > ans1, ans2;
int res1 = solve1(A, ans1), res2 = solve2(A, ans2);
if (res1 < res2)
print(ans1);
else
print(ans2);
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 3e5 + 15, MOD = 1e9 + 7, inf = 1e9 + 7;
int n, m;
char A, B, C, D;
char c[] = {'A', 'G', 'C', 'T'};
vector<string> table, ans;
int ans1 = inf, ans2 = inf;
char A1, B1, C1, D1;
char A2, B2, C2, D2;
int calc1(char a, char b, char c, char d) {
int sum = 0;
for (int i = 0; i < n; i++) {
if (i & 1) {
int res1 = 0;
int res2 = 0;
for (int j = 0; j < m; j++) res1 += (table[i][j] != (j & 1 ? c : d));
for (int j = 0; j < m; j++) res2 += (table[i][j] != (j & 1 ? d : c));
sum += min(res1, res2);
} else {
int res1 = 0;
int res2 = 0;
for (int j = 0; j < m; j++) res1 += (table[i][j] != (j & 1 ? a : b));
for (int j = 0; j < m; j++) res2 += (table[i][j] != (j & 1 ? b : a));
sum += min(res1, res2);
}
}
return sum;
}
int calc2(char a, char b, char c, char d) {
int sum = 0;
for (int i = 0; i < m; i++) {
if (i & 1) {
int res1 = 0;
int res2 = 0;
for (int j = 0; j < n; j++) res1 += (table[j][i] != (j & 1 ? c : d));
for (int j = 0; j < n; j++) res2 += (table[j][i] != (j & 1 ? d : c));
sum += min(res1, res2);
} else {
int res1 = 0;
int res2 = 0;
for (int j = 0; j < n; j++) res1 += (table[j][i] != (j & 1 ? a : b));
for (int j = 0; j < n; j++) res2 += (table[j][i] != (j & 1 ? b : a));
sum += min(res1, res2);
}
}
return sum;
}
void upd1(char a, char b, char c, char d) {
int res = calc1(a, b, c, d);
if (res < ans1) {
ans1 = res;
A1 = a, B1 = b, C1 = c, D1 = d;
}
}
void upd2(char a, char b, char c, char d) {
int res = calc2(a, b, c, d);
if (res < ans2) {
ans2 = res;
A2 = a, B2 = b, C2 = c, D2 = d;
}
}
void build1(char a, char b, char c, char d) {
for (int i = 0; i < n; i++) {
if (i & 1) {
int res1 = 0;
int res2 = 0;
for (int j = 0; j < m; j++) res1 += (table[i][j] != (j & 1 ? c : d));
for (int j = 0; j < m; j++) res2 += (table[i][j] != (j & 1 ? d : c));
string s;
if (res1 < res2) {
for (int j = 0; j < m; j++) s += (j & 1 ? c : d);
} else {
for (int j = 0; j < m; j++) s += (j & 1 ? d : c);
}
cout << s << "\n";
} else {
int res1 = 0;
int res2 = 0;
for (int j = 0; j < m; j++) res1 += (table[i][j] != (j & 1 ? a : b));
for (int j = 0; j < m; j++) res2 += (table[i][j] != (j & 1 ? b : a));
string s;
if (res1 < res2) {
for (int j = 0; j < m; j++) s += (j & 1 ? a : b);
} else {
for (int j = 0; j < m; j++) s += (j & 1 ? b : a);
}
cout << s << "\n";
}
}
}
void build2(char a, char b, char c, char d) {
for (int i = 0; i < m; i++) {
if (i & 1) {
int res1 = 0;
int res2 = 0;
for (int j = 0; j < n; j++) res1 += (table[j][i] != (j & 1 ? c : d));
for (int j = 0; j < n; j++) res2 += (table[j][i] != (j & 1 ? d : c));
string s;
if (res1 < res2) {
for (int j = 0; j < n; j++) s += (j & 1 ? c : d);
} else {
for (int j = 0; j < n; j++) s += (j & 1 ? d : c);
}
ans.push_back(s);
} else {
int res1 = 0;
int res2 = 0;
for (int j = 0; j < n; j++) res1 += (table[j][i] != (j & 1 ? a : b));
for (int j = 0; j < n; j++) res2 += (table[j][i] != (j & 1 ? b : a));
string s;
if (res1 < res2) {
for (int j = 0; j < n; j++) s += (j & 1 ? a : b);
} else {
for (int j = 0; j < n; j++) s += (j & 1 ? b : a);
}
ans.push_back(s);
}
}
for (int i = 0; i < n; i++) {
string s;
for (int j = 0; j < m; j++) s += ans[j][i];
cout << s << "\n";
}
}
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n >> m;
for (int i = 0; i < n; i++) {
string s;
cin >> s;
table.push_back(s);
}
int P1[] = {0, 1, 2, 3};
do {
upd1(c[P1[0]], c[P1[1]], c[P1[2]], c[P1[3]]);
} while (next_permutation(P1, P1 + 4));
int P2[] = {0, 1, 2, 3};
do {
upd2(c[P2[0]], c[P2[1]], c[P2[2]], c[P2[3]]);
} while (next_permutation(P2, P2 + 4));
if (ans1 < ans2)
build1(A1, B1, C1, D1);
else
build2(A2, B2, C2, D2);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long MAX = (long long)2e5 + 100;
long long mod = (long long)1e9 + 7, mod2 = (long long)1e9 + 9, p = 151;
long long n, m;
void solve() {
cin >> n >> m;
vector<string> t(n);
vector<pair<long long, vector<string>>> ans;
for (int i = 0; i < n; i++) cin >> t[i];
vector<char> kek = {'A', 'G', 'C', 'T'};
for (int i = 0; i < 24; i++) {
vector<string> keks;
long long price = 0;
for (int j = 0; j < n; j++) {
string a, b;
if (j % 2 == 0) {
for (int z = 0; z < m; z++) {
if (z % 2 == 0) {
a.push_back(kek[0]);
b.push_back(kek[1]);
} else {
a.push_back(kek[1]);
b.push_back(kek[0]);
}
}
} else {
for (int z = 0; z < m; z++) {
if (z % 2 == 0) {
a.push_back(kek[2]);
b.push_back(kek[3]);
} else {
a.push_back(kek[3]);
b.push_back(kek[2]);
}
}
}
long long va = 0, vb = 0;
for (int z = 0; z < m; z++) {
if (t[j][z] != a[z]) va++;
if (t[j][z] != b[z]) vb++;
}
if (va <= vb) {
keks.push_back(a);
price += va;
} else {
keks.push_back(b);
price += vb;
}
}
ans.push_back({price, keks});
keks.clear();
price = 0;
for (int j = 0; j < m; j++) {
string a, b;
if (j % 2 == 0) {
for (int z = 0; z < n; z++) {
if (z % 2 == 0) {
a.push_back(kek[0]);
b.push_back(kek[1]);
} else {
a.push_back(kek[1]);
b.push_back(kek[0]);
}
}
} else {
for (int z = 0; z < n; z++) {
if (z % 2 == 0) {
a.push_back(kek[2]);
b.push_back(kek[3]);
} else {
a.push_back(kek[3]);
b.push_back(kek[2]);
}
}
}
long long va = 0, vb = 0;
for (int z = 0; z < n; z++) {
if (t[z][j] != a[z]) va++;
if (t[z][j] != b[z]) vb++;
}
if (va <= vb) {
keks.push_back(a);
price += va;
} else {
keks.push_back(b);
price += vb;
}
}
vector<string> tmp;
for (int i = 0; i < n; i++) {
string mem;
for (int j = 0; j < m; j++) {
mem.push_back(keks[j][i]);
}
tmp.push_back(mem);
}
keks = tmp;
ans.push_back({price, keks});
next_permutation(kek.begin(), kek.end());
}
sort(ans.begin(), ans.end());
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << ans[0].second[i][j];
}
cout << "\n";
}
}
int main() {
ios::sync_with_stdio(0);
long long q;
q = 1;
while (q--) solve();
}
|
#include <bits/stdc++.h>
int N, M, Flag, Min;
std::string S[300005], B[300005], Ans[300005];
std::string Sol;
std::bitset<300005> Bits;
void Solve(const std::string& Pattern, bool Part) {
std::bitset<300005> Solution;
int Count = 0, c1, c2;
for (int i = 0, j, c1, c2; i < N; ++i) {
c1 = c2 = 0;
for (j = 0; j < M; ++j)
c1 += (S[i][j] != Pattern[((i & 1) * 2 + (j & 1))]),
c2 += (S[i][j] != Pattern[((i & 1) * 2 + (j & 1) ^ 1)]);
if (c2 < c1)
Solution[i] = 1, Count += c2;
else
Count += c1;
}
if (Count < Min) {
Min = Count;
Sol = Pattern;
Bits = Solution;
Flag = Part;
}
}
void SolvePart(bool Part) {
std::string Pattern = "ACGT";
do {
Solve(Pattern, Part);
} while (std::next_permutation(Pattern.begin(), Pattern.end()));
}
void RotateMatrix(std::string (&A)[300005]) {
for (int i = 0; i < M; ++i) B[i].clear();
for (int i = 0, j; i < M; ++i)
for (j = 0; j < N; ++j) B[i] += A[j][i];
std::swap(N, M);
for (int i = 0; i < N; ++i) A[i] = B[i];
}
void Citire() {
std::cin >> N >> M;
for (int i = 0; i < N; ++i) std::cin >> S[i];
}
void Rezolvare() {
Min = 300005 + 10;
SolvePart(false);
RotateMatrix(S);
SolvePart(true);
if (!Flag) std::swap(N, M);
for (int i = 0, j; i < N; ++i)
for (j = 0; j < M; ++j) Ans[i] += Sol[(i & 1) * 2 + (j & 1) ^ Bits[i]];
if (Flag) RotateMatrix(Ans);
for (int i = 0; i < N; ++i, std::cout << '\n') std::cout << Ans[i];
}
int main() {
Citire();
Rezolvare();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<string> ans;
char v[4] = {'A', 'G', 'C', 'T'};
int M = 1e9;
bool type;
int Test(string s, char a, char b, string &res) {
int cnt = 0;
for (int i = 0; i < (s.size()); i++) {
if (i & 1) {
res += b;
cnt += (s[i] != b);
} else {
res += a;
cnt += (s[i] != a);
}
}
return cnt;
}
void Solve(vector<string> &table, int n, int m, bool flag) {
vector<bool> free(4, true);
for (int i = 0; i < (3); i++) {
free[i] = false;
for (int j = i + 1; j < (4); j++) {
free[j] = false;
char a, b, c, d;
for (int k = 0; k < (4); k++)
if (free[k]) {
a = v[k];
free[a] = false;
break;
}
for (int k = 0; k < (4); k++)
if (free[k]) b = v[k];
c = v[i], d = v[j];
vector<string> T(n);
int tot = 0;
for (int k = 0; k < n; k += 2) {
string t1, t2;
int x = Test(table[k], a, b, t1);
int y = Test(table[k], b, a, t2);
T[k] = x < y ? t1 : t2;
tot += min(x, y);
}
for (int k = 1; k < n; k += 2) {
string t1, t2;
int x = Test(table[k], c, d, t1);
int y = Test(table[k], d, c, t2);
T[k] = x < y ? t1 : t2;
tot += min(x, y);
}
if (tot < M) {
M = tot;
ans = T;
type = flag;
}
for (int k = 0; k < (4); k++)
if (k != i) free[k] = true;
}
free[i] = true;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
cin >> n >> m;
vector<string> T1(n), T2(m);
for (int i = 0; i < (n); i++) {
cin >> T1[i];
for (int j = 0; j < (m); j++) T2[j] += T1[i][j];
}
Solve(T1, n, m, true);
Solve(T2, m, n, false);
if (type) {
for (int i = 0; i < (n); i++) cout << ans[i] << endl;
} else {
for (int i = 0; i < (n); i++) {
for (int j = 0; j < (m); j++) cout << ans[j][i];
cout << endl;
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
char mp[4];
mp[0] = 'A';
mp[1] = 'C';
mp[2] = 'G';
mp[3] = 'T';
int n, m;
cin >> n >> m;
int dp[n + 10][1 << 4];
int dp2[m + 10][1 << 4];
for (int i = 0; i < n + 10; ++i)
for (int j = 0; j < (1 << 4); ++j) dp[i][j] = 1e9;
for (int i = 0; i < (1 << 4); ++i) dp[n][i] = 0;
for (int i = 0; i < m + 10; ++i)
for (int j = 0; j < (1 << 4); ++j) dp2[i][j] = 1e9;
for (int i = 0; i < (1 << 4); ++i) dp2[m][i] = 0;
vector<string> v(n + 1);
for (int i = 0; i < n; ++i) cin >> v[i];
for (int i = n - 1; i >= 0; i--) {
for (int j = 0; j < (1 << 4); ++j) {
if (__builtin_popcount(j) % 2 != 0) continue;
for (int k = 0; k < (1 << 4); ++k) {
if (__builtin_popcount(j | k) % 2 != 0 or j == k or
__builtin_popcount(k) != 2)
continue;
vector<char> c;
for (int l = 0; l < 4; ++l)
if (k & (1 << l)) c.push_back(mp[l]);
int cnt = 0;
int ini = 0;
for (int l = 0; l < m; ++l, ini = 1 - ini) cnt += v[i][l] != c[ini];
int cnt2 = 0;
ini = 1;
for (int l = 0; l < m; ++l, ini = 1 - ini) cnt2 += v[i][l] != c[ini];
dp[i][j] = min(dp[i][j], dp[i + 1][k] + min(cnt, cnt2));
}
}
}
for (int i = m - 1; i >= 0; --i) {
for (int j = 0; j < (1 << 4); ++j) {
if (__builtin_popcount(j) % 2 != 0) continue;
for (int k = 0; k < (1 << 4); ++k) {
if (__builtin_popcount(j | k) % 2 != 0 or j == k or
__builtin_popcount(k) != 2)
continue;
vector<char> c;
for (int l = 0; l < 4; ++l)
if (k & (1 << l)) c.push_back(mp[l]);
int cnt = 0;
int ini = 0;
for (int l = 0; l < n; ++l, ini = 1 - ini) cnt += v[l][i] != c[ini];
int cnt2 = 0;
ini = 1;
for (int l = 0; l < n; ++l, ini = 1 - ini) cnt2 += v[l][i] != c[ini];
dp2[i][j] = min(dp2[i][j], dp2[i + 1][k] + min(cnt, cnt2));
}
}
}
if (dp2[0][0] < dp[0][0]) {
int current = 0;
for (int i = 0; i < m; ++i) {
vector<char> ans;
int rini = -1;
int a = 1e9;
int mask = 0;
for (int k = 0; k < (1 << 4); ++k) {
if (__builtin_popcount(current | k) % 2 != 0 or current == k or
__builtin_popcount(k) != 2)
continue;
vector<char> c;
for (int l = 0; l < 4; ++l)
if (k & (1 << l)) c.push_back(mp[l]);
int cnt = 0;
int ini = 0;
for (int l = 0; l < n; ++l, ini = 1 - ini) cnt += v[l][i] != c[ini];
int cnt2 = 0;
ini = 1;
for (int l = 0; l < n; ++l, ini = 1 - ini) cnt2 += v[l][i] != c[ini];
if (dp2[i + 1][k] + min(cnt, cnt2) < a) {
a = dp2[i + 1][k] + min(cnt, cnt2);
ans = c;
mask = k;
if (cnt < cnt2) {
rini = 0;
} else {
rini = 1;
}
}
}
current = mask;
for (int l = 0; l < n; ++l, rini = 1 - rini) v[l][i] = ans[rini];
}
} else {
int current = 0;
for (int i = 0; i < n; ++i) {
vector<char> ans;
int rini = -1;
int a = 1e9;
int mask = 0;
for (int k = 0; k < (1 << 4); ++k) {
if (__builtin_popcount(current | k) % 2 != 0 or current == k or
__builtin_popcount(k) != 2)
continue;
vector<char> c;
for (int l = 0; l < 4; ++l)
if (k & (1 << l)) c.push_back(mp[l]);
int cnt = 0;
int ini = 0;
for (int l = 0; l < m; ++l, ini = 1 - ini) cnt += v[i][l] != c[ini];
int cnt2 = 0;
ini = 1;
for (int l = 0; l < m; ++l, ini = 1 - ini) cnt2 += v[i][l] != c[ini];
if (dp[i + 1][k] + min(cnt, cnt2) < a) {
a = dp[i + 1][k] + min(cnt, cnt2);
ans = c;
mask = k;
if (cnt < cnt2) {
rini = 0;
} else {
rini = 1;
}
}
}
current = mask;
for (int l = 0; l < m; ++l, rini = 1 - rini) v[i][l] = ans[rini];
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) cout << v[i][j];
cout << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<string> arr;
int rowcount[4][2][300008] = {0}, colcount[4][2][300008] = {0};
int num(char c) {
if (c == 'A') return 0;
if (c == 'T') return 1;
if (c == 'G') return 2;
if (c == 'C') return 3;
}
int main() {
int n, m;
cin >> n >> m;
arr.resize(n);
for (int i = 0; i < n; i++) cin >> arr[i];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
char c = arr[i][j];
rowcount[num(c)][j % 2][i]++;
}
}
for (int j = 0; j < m; j++) {
for (int i = 0; i < n; i++) {
char c = arr[i][j];
colcount[num(c)][i % 2][j]++;
}
}
string patterns[] = {"ATGC", "AGTC", "ACTG", "GCAT", "TCAG", "TGAC"};
int mini = 300008, pat, row;
vector<int> ori;
for (int i = 0; i < 6; i++) {
string ss = patterns[i];
char a = ss[0], b = ss[1], c = ss[2], d = ss[3];
vector<int> tmp;
int tmptotal = 0;
for (int j = 0; j < n; j++) {
int p, q, r, s;
if (j % 2 == 0) {
p = rowcount[num(a)][0][j];
q = rowcount[num(b)][1][j];
r = rowcount[num(b)][0][j];
s = rowcount[num(a)][1][j];
} else {
p = rowcount[num(c)][0][j];
q = rowcount[num(d)][1][j];
r = rowcount[num(d)][0][j];
s = rowcount[num(c)][1][j];
}
if (p + q > r + s) {
tmp.push_back(0);
tmptotal += m - p - q;
} else {
tmp.push_back(1);
tmptotal += m - r - s;
}
}
if (tmptotal < mini) {
mini = tmptotal;
pat = i;
row = 1;
ori = tmp;
}
}
for (int i = 0; i < 6; i++) {
string ss = patterns[i];
char a = ss[0], b = ss[1], c = ss[2], d = ss[3];
vector<int> tmp;
int tmptotal = 0;
for (int j = 0; j < m; j++) {
int p, q, r, s;
if (j % 2 == 0) {
p = colcount[num(a)][0][j];
q = colcount[num(b)][1][j];
r = colcount[num(b)][0][j];
s = colcount[num(a)][1][j];
} else {
p = colcount[num(c)][0][j];
q = colcount[num(d)][1][j];
r = colcount[num(d)][0][j];
s = colcount[num(c)][1][j];
}
if (p + q > r + s) {
tmp.push_back(0);
tmptotal += n - p - q;
} else {
tmp.push_back(1);
tmptotal += n - r - s;
}
}
if (tmptotal < mini) {
mini = tmptotal;
pat = i;
row = 0;
ori = tmp;
}
}
vector<vector<char> > out(n);
for (int i = 0; i < n; i++) out[i].resize(m);
if (row) {
string pp = patterns[pat];
for (int i = 0; i < n; i++) {
char a, b;
if (i % 2 == 0) {
a = pp[0], b = pp[1];
} else {
a = pp[2], b = pp[3];
}
string ss = "";
if (ori[i] % 2 == 0) {
ss.push_back(a);
ss.push_back(b);
} else {
ss.push_back(b);
ss.push_back(a);
}
for (int j = 0; j < m; j++) out[i][j] = ss[j % 2];
}
} else {
string pp = patterns[pat];
for (int j = 0; j < m; j++) {
char a, b;
if (j % 2 == 0) {
a = pp[0], b = pp[1];
} else {
a = pp[2], b = pp[3];
}
string ss = "";
if (ori[j] % 2 == 0) {
ss.push_back(a);
ss.push_back(b);
} else {
ss.push_back(b);
ss.push_back(a);
}
for (int i = 0; i < n; i++) out[i][j] = ss[i % 2];
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) cout << out[i][j];
cout << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, Max;
const char let[9] = "ATCG";
char s[300000 + 5], t[300000 + 5], ans[300000 + 5];
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) scanf("%s", s + i * m);
for (int a = 0; a < 4; a++)
for (int b = 0; b < 4; b++)
if (a != b) {
char x1 = let[a], x2 = let[b], y1, y2;
for (int c = 0; c < 4; c++)
if (c != a && c != b) {
y1 = let[c];
break;
}
for (int c = 3; c >= 0; c--)
if (c != a && c != b) {
y2 = let[c];
break;
}
int tot_r = 0, tot_c = 0;
for (int i = 0; i < n; i++) {
char z0 = (i % 2 == 0 ? x1 : y1);
char z1 = (i % 2 == 0 ? x2 : y2);
int cnt_0 = 0, cnt_1 = 0;
for (int j = 0; j < m; j++) {
if ((j % 2 == 0 && s[i * m + j] == z0) ||
(j % 2 == 1 && s[i * m + j] == z1))
cnt_0++;
if ((j % 2 == 0 && s[i * m + j] == z1) ||
(j % 2 == 1 && s[i * m + j] == z0))
cnt_1++;
}
if (cnt_0 >= cnt_1) {
tot_r += cnt_0;
for (int j = 0; j < m; j++) t[i * m + j] = j % 2 == 0 ? z0 : z1;
} else {
tot_r += cnt_1;
for (int j = 0; j < m; j++) t[i * m + j] = j % 2 == 0 ? z1 : z0;
}
}
if (tot_r > Max) {
for (int i = 0; i < n * m; i++) ans[i] = t[i];
Max = tot_r;
}
for (int j = 0; j < m; j++) {
char z0 = (j % 2 == 0 ? x1 : y1);
char z1 = (j % 2 == 0 ? x2 : y2);
int cnt_0 = 0, cnt_1 = 0;
for (int i = 0; i < n; i++) {
if ((i % 2 == 0 && s[i * m + j] == z0) ||
(i % 2 == 1 && s[i * m + j] == z1))
cnt_0++;
if ((i % 2 == 0 && s[i * m + j] == z1) ||
(i % 2 == 1 && s[i * m + j] == z0))
cnt_1++;
}
if (cnt_0 >= cnt_1) {
tot_c += cnt_0;
for (int i = 0; i < n; i++) t[i * m + j] = i % 2 == 0 ? z0 : z1;
} else {
tot_c += cnt_1;
for (int i = 0; i < n; i++) t[i * m + j] = i % 2 == 0 ? z1 : z0;
}
}
if (tot_c > Max) {
for (int i = 0; i < n * m; i++) ans[i] = t[i];
Max = tot_c;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) putchar(ans[i * m + j]);
putchar('\n');
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
char s[300010], cur[5], hang[2][300010];
int num[2][5][300010], mx = -1, n, m;
vector<char> mp[300010], res[300010], now[300010];
void solve() {
int sum = 0;
for (int i = 1; i <= n; i++) {
int t0 = 0, t1 = 0;
for (int j = 1; j <= m; j++) {
char a, b;
if (i % 2) {
a = cur[1];
b = cur[2];
} else {
a = cur[3];
b = cur[4];
}
if (j % 2 == 0) swap(a, b);
if (mp[i][j] == a)
t0++;
else if (mp[i][j] == b)
t1++;
}
for (int j = 1; j <= m; j++) {
char a, b;
if (i % 2) {
a = cur[1];
b = cur[2];
} else {
a = cur[3];
b = cur[4];
}
if (j % 2 == 0) swap(a, b);
if (t0 > t1)
now[i][j] = a;
else
now[i][j] = b;
}
sum += max(t0, t1);
}
if (sum > mx) {
swap(res, now);
mx = sum;
}
for (int c = 0; c <= 1; c++)
for (int x = 1; x <= 4; x++)
for (int i = 1; i <= m; i++) num[c][x][i] = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
for (int x = 1; x <= 4; x++)
if (mp[i][j] == cur[x]) num[i % 2][x][j]++;
sum = 0;
for (int i = 1; i <= m; i++) {
int t1, t2, a, b;
if (i % 2) {
a = 1;
b = 3;
} else {
a = 2;
b = 4;
}
t1 = num[1][a][i] + num[0][b][i];
t2 = num[1][b][i] + num[0][a][i];
if (t2 > t1) swap(a, b);
sum += max(t1, t2);
hang[1][i] = cur[a];
hang[0][i] = cur[b];
}
if (sum > mx) {
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) res[i][j] = hang[i % 2][j];
mx = sum;
}
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
for (int j = 0; j <= m; j++) {
mp[i].push_back('a');
res[i].push_back('a');
now[i].push_back('a');
}
for (int i = 1; i <= n; i++) {
scanf("%s", s + 1);
for (int j = 1; j <= m; j++) mp[i][j] = s[j];
}
cur[1] = 'A';
cur[2] = 'C';
cur[3] = 'G';
cur[4] = 'T';
for (int i = 1; i <= 24; i++) {
solve();
next_permutation(cur + 1, cur + 5);
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) printf("%c", res[i][j]);
puts("");
}
return 0;
}
|
#include <bits/stdc++.h>
const int MAXC = 4;
const int MAXN = 3e5 + 5;
const char* acgt = "ACGT";
char buff[MAXN];
std::string ans[MAXN], s[MAXN];
int m, n;
std::string dyn;
int best;
std::string ret;
bool cr;
void augment(int sum, bool flag) {
if (sum >= best) return;
best = sum;
ret = dyn;
cr = flag;
}
void format(int a, int end, bool second, int row) {
for (int b = 0; b < end; ++b) {
ans[(row < 0) ? b : row] += dyn[(a & 1) * 2 + ((b & 1) ^ second)];
}
}
void solve_cols(bool out) {
int sum = 0;
for (int j = 0; j < m; ++j) {
int cnt[2]{};
for (int i = 0; i < n; ++i) {
if (s[i][j] != dyn[(j & 1) * 2 + (i & 1)]) ++cnt[0];
if (s[i][j] != dyn[(j & 1) * 2 + ((i & 1) ^ 1)]) ++cnt[1];
}
sum += std::min(cnt[0], cnt[1]);
if (out) format(j, n, (cnt[1] <= cnt[0]), -1);
}
augment(sum, true);
}
void solve_rows(bool out) {
int sum = 0;
for (int i = 0; i < n; ++i) {
int cnt[2]{};
for (int j = 0; j < m; ++j) {
if (s[i][j] != dyn[(i & 1) * 2 + (j & 1)]) ++cnt[0];
if (s[i][j] != dyn[(i & 1) * 2 + ((j & 1) ^ 1)]) ++cnt[1];
}
sum += std::min(cnt[0], cnt[1]);
if (out) format(i, m, (cnt[1] <= cnt[0]), i);
}
augment(sum, false);
}
int main() {
scanf(" %d%d", &n, &m);
for (int i = 0; i < n; ++i) {
scanf(" %s", buff);
s[i] = buff;
}
best = m * n;
dyn = ret = acgt;
cr = false;
do {
solve_cols(false);
solve_rows(false);
} while (std::next_permutation((dyn).begin(), (dyn).end()));
dyn = ret;
if (cr) {
solve_cols(true);
} else {
solve_rows(true);
}
for (int i = 0; i < n; ++i) {
puts(ans[i].c_str());
}
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
using namespace std;
const long long MAX = (long long)2e5 + 100;
long long mod = (long long)1e9 + 7, mod2 = (long long)1e9 + 9, p = 151;
long long n, m;
void solve() {
cin >> n >> m;
vector<string> t(n);
vector<pair<long long, vector<string>>> ans;
for (int i = 0; i < n; i++) cin >> t[i];
vector<char> kek = {'A', 'G', 'C', 'T'};
for (int i = 0; i < 24; i++) {
vector<string> keks;
long long price = 0;
for (int j = 0; j < n; j++) {
string a, b;
if (j % 2 == 0) {
for (int z = 0; z < m; z++) {
if (z % 2 == 0) {
a.push_back(kek[0]);
b.push_back(kek[1]);
} else {
a.push_back(kek[1]);
b.push_back(kek[0]);
}
}
} else {
for (int z = 0; z < m; z++) {
if (z % 2 == 0) {
a.push_back(kek[2]);
b.push_back(kek[3]);
} else {
a.push_back(kek[3]);
b.push_back(kek[2]);
}
}
}
long long va = 0, vb = 0;
for (int z = 0; z < m; z++) {
if (t[j][z] != a[z]) va++;
if (t[j][z] != b[z]) vb++;
}
if (va <= vb) {
keks.push_back(a);
price += va;
} else {
keks.push_back(b);
price += vb;
}
}
ans.push_back({price, keks});
keks.clear();
price = 0;
for (int j = 0; j < m; j++) {
string a, b;
if (j % 2 == 0) {
for (int z = 0; z < n; z++) {
if (z % 2 == 0) {
a.push_back(kek[0]);
b.push_back(kek[1]);
} else {
a.push_back(kek[1]);
b.push_back(kek[0]);
}
}
} else {
for (int z = 0; z < n; z++) {
if (z % 2 == 0) {
a.push_back(kek[2]);
b.push_back(kek[3]);
} else {
a.push_back(kek[3]);
b.push_back(kek[2]);
}
}
}
long long va = 0, vb = 0;
for (int z = 0; z < n; z++) {
if (t[z][j] != a[z]) va++;
if (t[z][j] != b[z]) vb++;
}
if (va <= vb) {
keks.push_back(a);
price += va;
} else {
keks.push_back(b);
price += vb;
}
}
vector<string> tmp;
for (int i = 0; i < n; i++) {
string mem;
for (int j = 0; j < m; j++) {
mem.push_back(keks[j][i]);
}
tmp.push_back(mem);
}
keks = tmp;
ans.push_back({price, keks});
next_permutation(kek.begin(), kek.end());
}
sort(ans.begin(), ans.end());
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << ans[0].second[i][j];
}
cout << "\n";
}
}
int main() {
ios::sync_with_stdio(0);
long long q;
q = 1;
while (q--) solve();
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 10, M = 15;
int jl[M][N], zf[M][N], ans = 3e5 + 10, res = 0, fk = -1, cnt = 0, n, m;
int fa[6][2] = {{1, 2}, {1, 3}, {1, 4}, {2, 3}, {2, 4}, {3, 4}};
vector<int> qx[N];
string lx[N];
char zh(int z) {
if (z == 1) return 'A';
if (z == 2) return 'G';
if (z == 3) return 'T';
if (z == 4) return 'C';
}
int hz(char ch) {
if (ch == 'A') return 1;
if (ch == 'G') return 2;
if (ch == 'T') return 3;
if (ch == 'C') return 4;
}
void out() {
if (fk) {
for (int i = 1; i <= n; ++i) {
int nw = jl[res][i];
for (int j = 0; j <= m - 1; ++j) {
int kk = j & 1;
if (zf[res][i] < 0) kk = 1 - kk;
qx[i].push_back(fa[nw][kk]);
}
}
} else {
for (int i = 0; i <= m - 1; ++i) {
int nw = jl[res][i];
for (int j = 1; j <= n; ++j) {
int kk = j & 1;
if (!zf[res][i]) kk = 1 - kk;
qx[j].push_back(fa[nw][kk]);
}
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 0; j <= m - 1; ++j) cout << zh(qx[i][j]);
cout << "\n";
}
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; ++i) cin >> lx[i];
for (int i = 0; i <= 5; ++i) {
++cnt;
int pos = 0, ds = i, fs = 5 - i;
for (int j = 1; j <= n; ++j) {
if (j & 1) {
int zx = 0, nx = 0;
for (int k = 0; k <= m - 1; ++k) {
int nw = hz(lx[j][k]), zz = k & 1;
if (fa[ds][zz] != nw) zx++;
if (fa[ds][1 - zz] != nw) nx++;
}
pos += min(zx, nx);
if (zx < nx)
jl[cnt][j] = ds;
else
jl[cnt][j] = ds, zf[cnt][j] = -1;
} else {
int zx = 0, nx = 0;
for (int k = 0; k <= m - 1; ++k) {
int nw = hz(lx[j][k]), zz = k & 1;
if (fa[fs][zz] != nw) zx++;
if (fa[fs][1 - zz] != nw) nx++;
}
pos += min(zx, nx);
if (zx < nx)
jl[cnt][j] = fs;
else
jl[cnt][j] = fs, zf[cnt][j] = -1;
}
}
if (pos < ans) ans = pos, fk = 1, res = cnt;
}
for (int i = 0; i <= 5; ++i) {
++cnt;
int pos = 0, ds = i, fs = 5 - i;
for (int j = 0; j <= m - 1; ++j) {
if (j & 1) {
int zx = 0, nx = 0;
for (int k = 1; k <= n; ++k) {
int nw = hz(lx[k][j]), zz = k & 1;
if (fa[ds][zz] != nw) nx++;
if (fa[ds][zz ^ 1] != nw) zx++;
}
pos += min(zx, nx);
if (zx < nx)
jl[cnt][j] = ds;
else
jl[cnt][j] = ds, zf[cnt][j] = -1;
} else {
int zx = 0, nx = 0;
for (int k = 1; k <= n; ++k) {
int nw = hz(lx[k][j]), zz = k & 1;
if (fa[fs][zz] != nw) nx++;
if (fa[fs][zz ^ 1] != nw) zx++;
}
pos += min(zx, nx);
if (zx < nx)
jl[cnt][j] = fs;
else
jl[cnt][j] = fs, zf[cnt][j] = -1;
}
}
if (pos < ans) ans = pos, res = cnt, fk = 0;
}
out();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 5;
const string DNA = "ACGT";
int N, M;
vector<string> table;
int best = INF;
vector<string> best_nice;
vector<string> rotate(const vector<string> &v) {
int rows = v.size();
int cols = v[0].size();
vector<string> rotated(cols, string(rows, '.'));
for (int r = 0; r < rows; r++)
for (int c = 0; c < cols; c++) rotated[c][r] = v[r][c];
return rotated;
}
int cost;
int difference(const string &a, const string &b) {
assert(a.size() == b.size());
int n = a.size();
int count = 0;
for (int i = 0; i < n; i++)
if (a[i] != b[i]) count++;
return count;
}
string best_row(const string &row, char first, char second) {
int n = row.size();
string candidate1 = "", candidate2 = "";
for (int i = 0; i < n; i++) {
candidate1 += i % 2 == 0 ? first : second;
candidate2 += i % 2 == 0 ? second : first;
}
int cost1 = difference(candidate1, row);
int cost2 = difference(candidate2, row);
cost += min(cost1, cost2);
return cost1 < cost2 ? candidate1 : candidate2;
}
void attempt(char first, char second, bool needs_rotate) {
char third = '\0', fourth = '\0';
for (int i = 0; i < 4; i++)
if (DNA[i] != first && DNA[i] != second) {
if (third == '\0')
third = DNA[i];
else
fourth = DNA[i];
}
vector<string> current(N);
cost = 0;
for (int i = 0; i < N; i++) {
current[i] = best_row(table[i], first, second);
swap(first, third);
swap(second, fourth);
}
if (cost < best) {
best = cost;
best_nice = needs_rotate ? rotate(current) : current;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> N >> M;
table.resize(N);
for (int i = 0; i < N; i++) cin >> table[i];
for (int first = 0; first < 4; first++)
for (int second = first + 1; second < 4; second++)
attempt(DNA[first], DNA[second], false);
table = rotate(table);
swap(N, M);
for (int first = 0; first < 4; first++)
for (int second = first + 1; second < 4; second++)
attempt(DNA[first], DNA[second], true);
table = rotate(table);
swap(N, M);
cerr << best << '\n';
for (int i = 0; i < N; i++) cout << best_nice[i] << '\n';
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.