text
stringlengths 49
983k
|
|---|
#include <bits/stdc++.h>
using namespace std;
long long best = 9223372036854775807;
long long n, m;
void horizontal(vector<vector<char> > &tab, vector<vector<char> > &rep, char a,
char b, char c, char d) {
vector<vector<char> > tempo(n, vector<char>(m));
long long tot = 0;
for (long long i = 0; i < n; i++) {
if (tot >= best) return;
long long da = 0;
long long db = 0;
if (i % 2 == 0) {
for (long long j = 0; j < m; j++) {
if (j % 2 == 0 && tab[i][j] != a)
da++;
else if (j % 2 == 1 && tab[i][j] != b) {
da++;
}
if (j % 2 == 0 && tab[i][j] != b)
db++;
else if (j % 2 == 1 && tab[i][j] != a) {
db++;
}
}
} else {
for (long long j = 0; j < m; j++) {
if (j % 2 == 0 && tab[i][j] != c)
da++;
else if (j % 2 == 1 && tab[i][j] != d) {
da++;
}
if (j % 2 == 0 && tab[i][j] != d)
db++;
else if (j % 2 == 1 && tab[i][j] != c) {
db++;
}
}
}
if (da > db) {
tot += db;
if (i % 2 == 0) {
for (long long j = 0; j < m; j++) {
if (j % 2 == 0)
tempo[i][j] = b;
else
tempo[i][j] = a;
}
} else {
for (long long j = 0; j < m; j++) {
if (j % 2 == 0)
tempo[i][j] = d;
else
tempo[i][j] = c;
}
}
} else {
tot += da;
if (i % 2 == 0) {
for (long long j = 0; j < m; j++) {
if (j % 2 == 0)
tempo[i][j] = a;
else
tempo[i][j] = b;
}
} else {
for (long long j = 0; j < m; j++) {
if (j % 2 == 0)
tempo[i][j] = c;
else
tempo[i][j] = d;
}
}
}
}
if (tot < best) {
best = tot;
rep = tempo;
}
}
void vertical(vector<vector<char> > &tab, vector<vector<char> > &rep, char a,
char b, char c, char d) {
vector<vector<char> > tempo(n, vector<char>(m));
long long tot = 0;
for (long long j = 0; j < m; j++) {
if (tot >= best) return;
long long da = 0;
long long db = 0;
if (j % 2 == 0) {
for (long long i = 0; i < n; i++) {
if (i % 2 == 0 && tab[i][j] != a)
da++;
else if (i % 2 == 1 && tab[i][j] != b) {
da++;
}
if (i % 2 == 0 && tab[i][j] != b)
db++;
else if (i % 2 == 1 && tab[i][j] != a) {
db++;
}
}
} else {
for (long long i = 0; i < n; i++) {
if (i % 2 == 0 && tab[i][j] != c)
da++;
else if (i % 2 == 1 && tab[i][j] != d) {
da++;
}
if (i % 2 == 0 && tab[i][j] != d)
db++;
else if (i % 2 == 1 && tab[i][j] != c) {
db++;
}
}
}
if (da > db) {
tot += db;
if (j % 2 == 0) {
for (long long i = 0; i < n; i++) {
if (i % 2 == 0)
tempo[i][j] = b;
else
tempo[i][j] = a;
}
} else {
for (long long i = 0; i < n; i++) {
if (i % 2 == 0)
tempo[i][j] = d;
else
tempo[i][j] = c;
}
}
} else {
tot += da;
if (j % 2 == 0) {
for (long long i = 0; i < n; i++) {
if (i % 2 == 0)
tempo[i][j] = a;
else
tempo[i][j] = b;
}
} else {
for (long long i = 0; i < n; i++) {
if (i % 2 == 0)
tempo[i][j] = c;
else
tempo[i][j] = d;
}
}
}
}
if (tot < best) {
best = tot;
rep = tempo;
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
vector<vector<char> > tab(n, vector<char>(m));
for (long long i = 0; i < n; i++) {
for (long long j = 0; j < m; j++) {
cin >> tab[i][j];
}
}
vector<vector<char> > ans(n, vector<char>(m));
vector<char> liste(0);
liste.push_back('A');
liste.push_back('C');
liste.push_back('G');
liste.push_back('T');
liste.push_back('G');
liste.push_back('T');
liste.push_back('A');
liste.push_back('C');
liste.push_back('A');
liste.push_back('G');
liste.push_back('C');
liste.push_back('T');
liste.push_back('C');
liste.push_back('T');
liste.push_back('A');
liste.push_back('G');
liste.push_back('A');
liste.push_back('T');
liste.push_back('C');
liste.push_back('G');
liste.push_back('C');
liste.push_back('G');
liste.push_back('A');
liste.push_back('T');
for (long long i = 0; i < liste.size(); i += 4) {
horizontal(tab, ans, liste[i + 0], liste[i + 1], liste[i + 2],
liste[i + 3]);
vertical(tab, ans, liste[i + 0], liste[i + 1], liste[i + 2], liste[i + 3]);
}
for (long long i = 0; i < n; i++) {
for (long long j = 0; j < m; j++) {
cout << ans[i][j];
}
cout << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
char s[300005];
int cnt[2][300005][5];
int id(char c) {
if (c == 'A') return 0;
if (c == 'C') return 1;
if (c == 'G') return 2;
if (c == 'T') return 3;
}
pair<int, vector<string>> work(vector<string> a) {
vector<int> p = {0, 1, 2, 3};
int step = 1e9;
memset(cnt, 0, sizeof(cnt));
int n = a.size();
int m = a[0].length();
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
cnt[i % 2][j][id(a[i][j])] += 1;
cnt[i % 2][j][4] += 1;
}
}
vector<string> ans;
char di[5] = "ACGT";
do {
int tmp = 0;
tmp += cnt[0][0][4] - cnt[0][0][p[0]];
tmp += cnt[0][1][4] - cnt[0][1][p[1]];
tmp += cnt[1][0][4] - cnt[1][0][p[2]];
tmp += cnt[1][1][4] - cnt[1][1][p[3]];
string a0, a1;
a0 += di[p[0]];
a0 += di[p[1]];
a1 += di[p[2]];
a1 += di[p[3]];
for (int j = 2; j < m; ++j) {
if (j % 2) {
int cho1 =
cnt[0][j][4] - cnt[0][j][p[1]] + cnt[1][j][4] - cnt[1][j][p[3]];
int cho2 =
cnt[0][j][4] - cnt[0][j][p[3]] + cnt[1][j][4] - cnt[1][j][p[1]];
tmp += min(cho1, cho2);
if (cho1 < cho2) {
a0 += di[p[1]];
a1 += di[p[3]];
} else {
a0 += di[p[3]];
a1 += di[p[1]];
}
} else {
int cho1 =
cnt[0][j][4] - cnt[0][j][p[0]] + cnt[1][j][4] - cnt[1][j][p[2]];
int cho2 =
cnt[0][j][4] - cnt[0][j][p[2]] + cnt[1][j][4] - cnt[1][j][p[0]];
tmp += min(cho1, cho2);
if (cho1 < cho2) {
a0 += di[p[0]];
a1 += di[p[2]];
} else {
a0 += di[p[2]];
a1 += di[p[0]];
}
}
}
if (step > tmp) {
step = tmp;
ans = (vector<string>){a0, a1};
}
} while (next_permutation(p.begin(), p.end()));
for (int i = 2; i < n; ++i) {
ans.push_back(ans[i % 2]);
}
return make_pair(step, ans);
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
vector<string> a;
for (int i = 0; i < n; ++i) {
scanf("%s", s);
string tmp = s;
a.push_back(s);
}
vector<string> a0, a1, b, b1;
int s0, s1;
tie(s0, a0) = work(a);
for (int i = 0; i < m; ++i) {
string tmp;
for (int j = 0; j < n; ++j) {
tmp += a[j][i];
}
b.push_back(tmp);
}
tie(s1, b1) = work(b);
for (int i = 0; i < n; ++i) {
string tmp;
for (int j = 0; j < m; ++j) {
tmp += b1[j][i];
}
a1.push_back(tmp);
}
if (s1 < s0) {
a0.swap(a1);
swap(s1, s0);
}
for (string s : a0) {
printf("%s\n", s.c_str());
}
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T &x) {
x = 0;
char ch = getchar();
int fh = 1;
while (ch < '0' || ch > '9') {
if (ch == '-') fh = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
x = x * fh;
}
char ch[300010], an[300010], p[10], no[300010];
int m, n, ansn;
int main() {
cin >> n >> m;
p[1] = 'A';
p[2] = 'C';
p[3] = 'T';
p[4] = 'G';
for (int i = 1; i <= n * m; i++) cin >> ch[i];
for (int n1 = 1; n1 < 4; n1++) {
for (int n2 = n1 + 1; n2 <= 4; n2++) {
int n3, n4;
if (n1 == 1 && n2 == 2) n3 = 3, n4 = 4;
if (n1 == 1 && n2 == 3) n3 = 2, n4 = 4;
if (n1 == 1 && n2 == 4) n3 = 2, n4 = 3;
if (n1 == 2 && n2 == 3) n3 = 1, n4 = 4;
if (n1 == 2 && n2 == 4) n3 = 1, n4 = 3;
if (n1 == 3 && n2 == 4) n3 = 1, n4 = 2;
for (int i = 1; i <= n; i++) {
if (i % 2 == 1) {
int an1 = 0, an2 = 0;
for (int j = 1; j <= m; j++) {
if (j % 2 == 1 && ch[(i - 1) * m + j] == p[n1]) an1++;
if (j % 2 == 0 && ch[(i - 1) * m + j] == p[n2]) an1++;
if (j % 2 == 0 && ch[(i - 1) * m + j] == p[n1]) an2++;
if (j % 2 == 1 && ch[(i - 1) * m + j] == p[n2]) an2++;
}
if (an1 >= an2)
for (int j = 1; j <= m; j++)
if (j % 2 == 1)
no[(i - 1) * m + j] = p[n1];
else
no[(i - 1) * m + j] = p[n2];
else
for (int j = 1; j <= m; j++)
if (j % 2 == 1)
no[(i - 1) * m + j] = p[n2];
else
no[(i - 1) * m + j] = p[n1];
} else {
int an1 = 0, an2 = 0;
for (int j = 1; j <= m; j++) {
if (j % 2 == 1 && ch[(i - 1) * m + j] == p[n3]) an1++;
if (j % 2 == 0 && ch[(i - 1) * m + j] == p[n4]) an1++;
if (j % 2 == 0 && ch[(i - 1) * m + j] == p[n3]) an2++;
if (j % 2 == 1 && ch[(i - 1) * m + j] == p[n4]) an2++;
}
if (an1 >= an2)
for (int j = 1; j <= m; j++)
if (j % 2 == 1)
no[(i - 1) * m + j] = p[n3];
else
no[(i - 1) * m + j] = p[n4];
else
for (int j = 1; j <= m; j++)
if (j % 2 == 1)
no[(i - 1) * m + j] = p[n4];
else
no[(i - 1) * m + j] = p[n3];
}
}
int su = 0;
for (int i = 1; i <= n * m; i++)
if (ch[i] == no[i]) su++;
if (su > ansn) {
ansn = su;
for (int i = 1; i <= n * m; i++) an[i] = no[i];
}
for (int i = 1; i <= m; i++) {
if (i % 2 == 1) {
int an1 = 0, an2 = 0;
for (int j = 1; j <= n; j++) {
if (j % 2 == 1 && ch[(j - 1) * m + i] == p[n1]) an1++;
if (j % 2 == 0 && ch[(j - 1) * m + i] == p[n2]) an1++;
if (j % 2 == 0 && ch[(j - 1) * m + i] == p[n1]) an2++;
if (j % 2 == 1 && ch[(j - 1) * m + i] == p[n2]) an2++;
}
if (an1 >= an2)
for (int j = 1; j <= n; j++)
if (j % 2 == 1)
no[(j - 1) * m + i] = p[n1];
else
no[(j - 1) * m + i] = p[n2];
else
for (int j = 1; j <= n; j++)
if (j % 2 == 1)
no[(j - 1) * m + i] = p[n2];
else
no[(j - 1) * m + i] = p[n1];
} else {
int an1 = 0, an2 = 0;
for (int j = 1; j <= n; j++) {
if (j % 2 == 1 && ch[(j - 1) * m + i] == p[n3]) an1++;
if (j % 2 == 0 && ch[(j - 1) * m + i] == p[n4]) an1++;
if (j % 2 == 0 && ch[(j - 1) * m + i] == p[n3]) an2++;
if (j % 2 == 1 && ch[(j - 1) * m + i] == p[n4]) an2++;
}
if (an1 >= an2)
for (int j = 1; j <= n; j++)
if (j % 2 == 1)
no[(j - 1) * m + i] = p[n3];
else
no[(j - 1) * m + i] = p[n4];
else
for (int j = 1; j <= n; j++)
if (j % 2 == 1)
no[(j - 1) * m + i] = p[n4];
else
no[(j - 1) * m + i] = p[n3];
}
}
su = 0;
for (int i = 1; i <= n * m; i++)
if (ch[i] == no[i]) su++;
if (su > ansn) {
ansn = su;
for (int i = 1; i <= n * m; i++) an[i] = no[i];
}
}
}
for (int i = 1; i <= n * m; i++) {
cout << an[i];
if (i % m == 0) puts("");
}
return 0;
}
|
#include <bits/stdc++.h>
std::string c = "ATGC";
std::vector<std::string> ans, tmp, s;
int min = INT_MAX, now = 0;
int main(int argc, char *argv[]) {
int n, m;
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++) {
std::string str;
std::cin >> str;
s.push_back(str);
}
for (int c1 = 0; c1 < 4; c1++) {
for (int c2 = c1 + 1; c2 < 4; c2++) {
std::string t;
std::set<int> set;
for (int i = 0; i < 4; i++) set.insert(i);
set.erase(c1), set.erase(c2);
std::set<int>::iterator it = set.begin();
t += c[c1], t += c[c2], t += c[*(it++)], t += c[*it];
now = 0;
tmp.clear();
for (int i = 0; i < n; i++) {
tmp.push_back("");
int b = (i & 1) << 1;
int cnt1 = 0, cnt2 = 0;
for (int j = 0; j < m; j++) {
if (s[i][j] != t[b + (j & 1)]) cnt1++;
if (s[i][j] != t[b + ((j & 1) ^ 1)]) cnt2++;
}
if (cnt1 < cnt2) {
for (int j = 0; j < m; j++) {
tmp[i] += t[b + (j & 1)];
}
} else {
for (int j = 0; j < m; j++) {
tmp[i] += t[b + ((j & 1) ^ 1)];
}
}
now += std::min(cnt1, cnt2);
}
if (now < min) {
min = now;
ans = tmp;
}
now = 0;
tmp.clear();
for (int i = 0; i < n; i++) tmp.push_back("");
for (int j = 0; j < m; j++) {
int b = (j & 1) << 1;
int cnt1 = 0, cnt2 = 0;
for (int i = 0; i < n; i++) {
if (s[i][j] != t[b + (i & 1)]) cnt1++;
if (s[i][j] != t[b + ((i & 1) ^ 1)]) cnt2++;
}
if (cnt1 < cnt2) {
for (int i = 0; i < n; i++) {
tmp[i] += t[b + (i & 1)];
}
} else {
for (int i = 0; i < n; i++) {
tmp[i] += t[b + ((i & 1) ^ 1)];
}
}
now += std::min(cnt1, cnt2);
}
if (now < min) {
min = now;
ans = tmp;
}
}
}
for (int i = 0; i < n; i++) {
std::cout << ans[i] << std::endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 300010;
const int INF = 1000000010;
char letters[] = {'A', 'T', 'G', 'C'};
int n, m;
int costLine[MAXN][4][4];
string v[MAXN];
string ans[MAXN];
string aux[MAXN];
void rotate() {
for (int i = 0; i < n; i++) {
aux[i] = v[i];
v[i].clear();
}
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) v[j].push_back(aux[i][j]);
swap(n, m);
}
int decode(char a) {
for (int i = 0; i < 4; i++)
if (letters[i] == a) return i;
}
char encode(int a) { return letters[a]; }
int calculate() {
int c[4];
for (int i = 0; i < n; i++) {
for (c[0] = 0; c[0] < 4; c[0]++) {
for (c[1] = 0; c[1] < 4; c[1]++) {
int& curAns = costLine[i][c[0]][c[1]];
curAns = 0;
for (int j = 0; j < m; j++)
if (decode(v[i][j]) != c[j % 2]) curAns++;
}
}
}
int optC[4];
int optValue = INF;
for (c[0] = 0; c[0] < 4; c[0]++) {
for (c[1] = c[0] + 1; c[1] < 4; c[1]++) {
if (c[0] > 0)
c[2] = 0;
else if (c[1] == 1)
c[2] = 2;
else
c[2] = 1;
c[3] = 6 - c[0] - c[1] - c[2];
int sum = 0;
for (int i = 0; i < n; i++) {
int ind = 2 * (i % 2);
int val = costLine[i][c[ind]][c[ind + 1]];
val = min(val, costLine[i][c[ind + 1]][c[ind]]);
sum += val;
}
if (sum < optValue) {
for (int i = 0; i < 4; i++) optC[i] = c[i];
optValue = sum;
}
}
}
for (int i = 0; i < n; i++) {
int ind = 2 * (i % 2);
if (costLine[i][optC[ind]][optC[ind + 1]] >
costLine[i][optC[ind + 1]][optC[ind]])
swap(optC[ind], optC[ind + 1]);
ans[i].clear();
for (int j = 0; j < m; j++) ans[i].push_back(encode(optC[ind + j % 2]));
}
return optValue;
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++) cin >> v[i];
int ansLine = calculate();
rotate();
int ansColumn = calculate();
if (ansLine < ansColumn) rotate();
calculate();
if (ansLine < ansColumn) {
for (int i = 0; i < n; i++, printf("\n"))
for (int j = 0; j < m; j++) printf("%c", ans[i][j]);
} else {
for (int i = 0; i < m; i++, printf("\n"))
for (int j = 0; j < n; j++) printf("%c", ans[j][i]);
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const string str = "ATGC";
vector<string> s, tmp, ans;
int main() {
ios::sync_with_stdio(0);
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) {
string st;
cin >> st;
s.push_back(st);
}
int ansmin = INF, now;
for (int c1 = 0; c1 < 4; c1++)
for (int c2 = c1 + 1; c2 < 4; c2++) {
string t;
set<int> st;
for (int i = 0; i < 4; i++) st.insert(i);
st.erase(c1);
st.erase(c2);
set<int>::iterator it = st.begin();
t.push_back(str[c1]);
t.push_back(str[c2]);
t.push_back(str[*(it++)]);
t.push_back(str[*it]);
now = 0;
tmp.clear();
for (int i = 0; i < n; i++) {
tmp.push_back("");
int b = (i & 1) << 1;
int cnt1 = 0, cnt2 = 0;
for (int j = 0; j < m; j++) {
if (s[i][j] != t[b + (j & 1)]) ++cnt1;
if (s[i][j] != t[b + ((j & 1) ^ 1)]) ++cnt2;
}
if (cnt1 < cnt2)
for (int j = 0; j < m; j++) tmp[i].push_back(t[b + (j & 1)]);
else
for (int j = 0; j < m; j++) tmp[i].push_back(t[b + ((j & 1) ^ 1)]);
now += min(cnt1, cnt2);
}
if (now < ansmin) {
ans = tmp;
ansmin = now;
}
now = 0;
tmp.clear();
for (int i = 0; i < n; i++) tmp.push_back("");
for (int j = 0; j < m; j++) {
int b = (j & 1) << 1;
int cnt1 = 0, cnt2 = 0;
for (int i = 0; i < n; i++) {
if (s[i][j] != t[b + (i & 1)]) ++cnt1;
if (s[i][j] != t[b + ((i & 1) ^ 1)]) ++cnt2;
}
if (cnt1 < cnt2)
for (int i = 0; i < n; i++) tmp[i].push_back(t[b + (i & 1)]);
else
for (int i = 0; i < n; i++) tmp[i].push_back(t[b + ((i & 1) ^ 1)]);
now += min(cnt1, cnt2);
}
if (now < ansmin) {
ans = tmp;
ansmin = now;
}
}
for (int i = 0; i < n; i++) cout << ans[i] << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 100;
int n, m;
string s[maxn], ss[maxn], ans[maxn];
string ns = "AGCT";
string tmp[2];
int tolnum = maxn;
void init(string ss[], string s[]) {
for (int i = 0; i < n; ++i) {
ss[i] = s[i];
}
}
void pt(string ans[]) {
for (int i = 0; i < n; ++i) {
cout << ans[i] << endl;
}
}
void solve_raw() {
init(ss, s);
int t_ans = 0;
for (int i = 0; i < n; ++i) {
int ind = i % 2;
int num1 = 0, num2 = 0;
for (int j = 0; j < m; ++j) {
if (ss[i][j] != tmp[ind][j % 2]) {
++num1;
}
}
swap(tmp[ind][0], tmp[ind][1]);
for (int j = 0; j < m; ++j) {
if (ss[i][j] != tmp[ind][j % 2]) {
++num2;
}
}
if (num2 > num1) {
swap(tmp[ind][0], tmp[ind][1]);
} else
swap(num1, num2);
for (int j = 0; j < m; ++j) {
if (ss[i][j] != tmp[ind][j % 2]) {
ss[i][j] = tmp[ind][j % 2];
}
}
t_ans += num1;
}
if (t_ans < tolnum) {
init(ans, ss);
tolnum = t_ans;
}
}
void solve_con() {
init(ss, s);
int t_ans = 0;
for (int i = 0; i < m; ++i) {
int ind = i % 2;
int num1 = 0, num2 = 0;
for (int j = 0; j < n; ++j) {
if (ss[j][i] != tmp[ind][j % 2]) {
++num1;
}
}
swap(tmp[ind][0], tmp[ind][1]);
for (int j = 0; j < n; ++j) {
if (ss[j][i] != tmp[ind][j % 2]) {
++num2;
}
}
if (num2 > num1) {
swap(tmp[ind][0], tmp[ind][1]);
} else
swap(num1, num2);
for (int j = 0; j < n; ++j) {
if (ss[j][i] != tmp[ind][j % 2]) {
ss[j][i] = tmp[ind][j % 2];
}
}
t_ans += num1;
}
if (t_ans < tolnum) {
init(ans, ss);
tolnum = t_ans;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for (int i = 0; i < n; ++i) {
cin >> s[i];
}
for (int i = 0; i < (1 << 4); ++i) {
if (__builtin_popcount(i) == 2) {
tmp[0] = tmp[1] = "";
for (int j = 0; j < 4; ++j) {
if ((i >> j) & 1) {
tmp[0] += ns[j];
} else {
tmp[1] += ns[j];
}
}
solve_raw();
solve_con();
}
}
for (int i = 0; i < n; ++i) {
cout << ans[i] << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5, inf = 1e9;
int n, m, t, ans, anst;
int g[300], d[4];
char ch[N], c[4];
vector<int> a[2][N], b[2][N], as[2][N];
void solve(int t) {
int i, j, k, l, s, x, y, res;
for (i = 0; i < n; i++)
for (j = 0; j <= m; j++) as[t][i].push_back(0), b[t][i].push_back(0);
for (x = 0; x < 4; x++)
for (y = x + 1; y < 4; y++) {
res = 0;
d[0] = x, d[1] = y;
for (k = 2, i = 0; i < 4; i++)
if (i != x && i != y) d[k++] = i;
for (i = 0; i < n; i++) {
for (s = inf, l = 0; l < 2; l++) {
k = 0;
for (j = 0; j < m; j++) k += d[2 * (i & 1) + (j & 1)] != a[t][i][j];
if (s > k) {
for (s = k, j = 0; j < m; j++)
b[t][i][j] = d[2 * (i & 1) + (j & 1)];
}
swap(d[2 * (i & 1)], d[2 * (i & 1) + 1]);
}
res += s;
}
if (ans > res) {
ans = res, anst = t;
for (i = 0; i < n; i++)
for (j = 0; j < m; j++) as[t][i][j] = b[t][i][j];
}
}
}
int main() {
c[0] = 'A', c[1] = 'T', c[2] = 'C', c[3] = 'G';
g['A'] = 0, g['T'] = 1, g['C'] = 2, g['G'] = 3;
int i, j, k;
scanf("%d%d", &n, &m);
for (i = 0; i < n; i++)
for (scanf("%s", &ch), j = 0; j < m; j++) a[0][i].push_back(g[ch[j]]);
for (j = 0; j < m; j++)
for (i = 0; i < n; i++) a[1][j].push_back(a[0][i][j]);
ans = n * m, solve(0);
swap(n, m), solve(1);
swap(n, m);
if (!anst) {
for (i = 0; i < n; i++, puts(""))
for (j = 0; j < m; j++) printf("%c", c[as[0][i][j]]);
} else {
for (i = 0; i < n; i++, puts(""))
for (j = 0; j < m; j++) printf("%c", c[as[1][j][i]]);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
string ATGC = "ATGC";
int row[300001][4][2];
int column[300001][4][2];
int initial[5];
int result[5];
string field[300001];
bool same() {
for (int i = 0; i < 4; i++) {
for (int j = i + 1; j < 4; j++) {
if (initial[i] == initial[j]) return true;
}
}
return false;
}
void add(int index) {
initial[index]++;
if (initial[index] == 4) {
initial[index] = 0;
add(index + 1);
}
}
int main() {
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> field[i];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
for (int moji = 0; moji < 4; moji++) {
if (ATGC[moji] == field[i][j]) continue;
row[i][moji][j % 2]++;
column[j][moji][i % 2]++;
}
}
}
for (int i = 0; i < 5; i++) {
initial[i] = 0;
}
int ans = 300000;
vector<pair<int, int> > ANS(300000);
bool checker;
while (true) {
add(0);
while (same()) {
add(0);
}
if (initial[4] != 0) break;
int now1 = 0;
vector<pair<int, int> > NOW(300000);
for (int i = 0; i < n; i += 2) {
if (row[i][initial[0]][0] + row[i][initial[1]][1] <
row[i][initial[1]][0] + row[i][initial[0]][1]) {
NOW[i] = {initial[0], initial[1]};
} else {
NOW[i] = {initial[1], initial[0]};
}
now1 += min(row[i][initial[0]][0] + row[i][initial[1]][1],
row[i][initial[1]][0] + row[i][initial[0]][1]);
}
for (int i = 1; i < n; i += 2) {
if (row[i][initial[2]][0] + row[i][initial[3]][1] <
row[i][initial[3]][0] + row[i][initial[2]][1]) {
NOW[i] = {initial[2], initial[3]};
} else {
NOW[i] = {initial[3], initial[2]};
}
now1 += min(row[i][initial[2]][0] + row[i][initial[3]][1],
row[i][initial[3]][0] + row[i][initial[2]][1]);
}
if (now1 < ans) {
swap(NOW, ANS);
checker = true;
}
ans = min(ans, now1);
int now2 = 0;
for (int i = 0; i < m; i += 2) {
if (column[i][initial[0]][0] + column[i][initial[2]][1] <
column[i][initial[2]][0] + column[i][initial[0]][1]) {
NOW[i] = {initial[0], initial[2]};
} else {
NOW[i] = {initial[2], initial[0]};
}
now2 += min(column[i][initial[0]][0] + column[i][initial[2]][1],
column[i][initial[2]][0] + column[i][initial[0]][1]);
}
for (int i = 1; i < m; i += 2) {
if (column[i][initial[1]][0] + column[i][initial[3]][1] <
column[i][initial[3]][0] + column[i][initial[1]][1]) {
NOW[i] = {initial[1], initial[3]};
} else {
NOW[i] = {initial[3], initial[1]};
}
now2 += min(column[i][initial[1]][0] + column[i][initial[3]][1],
column[i][initial[3]][0] + column[i][initial[1]][1]);
}
if (now2 < ans) {
checker = false;
swap(NOW, ANS);
}
ans = min(ans, now2);
}
if (checker) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if ((j % 2) == 0) {
cout << ATGC[ANS[i].first];
} else {
cout << ATGC[ANS[i].second];
}
}
cout << endl;
}
} else {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (i % 2 == 0) {
cout << ATGC[ANS[j].first];
} else {
cout << ATGC[ANS[j].second];
}
}
cout << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m;
char mat[300005];
char mat1[300005];
char mat2[300005];
char matans[300005];
bool satir;
char matt1[300005];
char matt2[300005];
long long ansmin = 3000000;
void f(char c1, char c2, char c3, char c4) {
int ans = 0;
for (int i = 0; i < n; i++) {
int ans1 = 0;
int ans2 = 0;
for (int j = 0; j < m; j++) {
if (i % 2 == 0) {
if (j % 2 == 0) {
if (mat[i * m + j] != c1) ans1++;
matt1[i * m + j] = c1;
if (mat[i * m + j] != c2) ans2++;
matt2[i * m + j] = c2;
} else {
if (mat[i * m + j] != c1) ans2++;
matt2[i * m + j] = c1;
if (mat[i * m + j] != c2) ans1++;
matt1[i * m + j] = c2;
}
} else {
if (j % 2 == 0) {
if (mat[i * m + j] != c3) ans1++;
matt1[i * m + j] = c3;
if (mat[i * m + j] != c4) ans2++;
matt2[i * m + j] = c4;
} else {
if (mat[i * m + j] != c3) ans2++;
matt2[i * m + j] = c3;
if (mat[i * m + j] != c4) ans1++;
matt1[i * m + j] = c4;
}
}
}
if (ans2 < ans1) {
for (int j = 0; j < m; j++) {
int tmp = matt1[i * m + j];
matt1[i * m + j] = matt2[i * m + j];
matt2[i * m + j] = tmp;
}
}
ans += min(ans1, ans2);
}
if (ans < ansmin) {
ansmin = ans;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
matans[i * m + j] = matt1[i * m + j];
}
}
}
ans = 0;
for (int j = 0; j < m; j++) {
int ans1 = 0;
int ans2 = 0;
for (int i = 0; i < n; i++) {
if (j % 2 == 0) {
if (i % 2 == 0) {
if (mat[i * m + j] != c1) ans1++;
matt1[i * m + j] = c1;
if (mat[i * m + j] != c2) ans2++;
matt2[i * m + j] = c2;
} else {
if (mat[i * m + j] != c1) ans2++;
matt2[i * m + j] = c1;
if (mat[i * m + j] != c2) ans1++;
matt1[i * m + j] = c2;
}
} else {
if (i % 2 == 0) {
if (mat[i * m + j] != c3) ans1++;
matt1[i * m + j] = c3;
if (mat[i * m + j] != c4) ans2++;
matt2[i * m + j] = c4;
} else {
if (mat[i * m + j] != c3) ans2++;
matt2[i * m + j] = c3;
if (mat[i * m + j] != c4) ans1++;
matt1[i * m + j] = c4;
}
}
}
if (ans2 < ans1) {
for (int i = 0; i < n; i++) {
int tmp = matt1[i * m + j];
matt1[i * m + j] = matt2[i * m + j];
matt2[i * m + j] = tmp;
}
}
ans += min(ans1, ans2);
}
if (ans < ansmin) {
ansmin = ans;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
matans[i * m + j] = matt1[i * m + j];
}
}
}
}
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) cin >> mat[i * m + j];
f('A', 'G', 'C', 'T');
f('A', 'C', 'T', 'G');
f('A', 'T', 'C', 'G');
f('G', 'C', 'A', 'T');
f('G', 'T', 'A', 'C');
f('C', 'T', 'A', 'G');
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << matans[i * m + j];
}
cout << endl;
}
cout << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long N = 300100, mod = 1e9 + 7, mod2 = 1e9 + 9, mod3 = 998244353,
sq = 450, base = 727, lg = 30;
long long n, m, x, y, z, w, k, ans = 1e10, t;
string s[N], f[N];
string ans1, ans2;
long long sat(long long xx, char f0, char f1) {
long long yy = 0, zz = 0;
for (int i = 0; i < m; i++) {
if (i % 2 && s[xx][i] != f1) yy++;
if (!(i % 2) && s[xx][i] != f0) yy++;
}
for (int i = 0; i < m; i++) {
if (i % 2 && s[xx][i] != f0) zz++;
if (!(i % 2) && s[xx][i] != f1) zz++;
}
return min(yy, zz);
}
long long sot(long long xx, char f0, char f1) {
long long yy = 0, zz = 0;
for (int i = 0; i < n; i++) {
if (i % 2 && s[i][xx] != f1) yy++;
if (!(i % 2) && s[i][xx] != f0) yy++;
}
for (int i = 0; i < n; i++) {
if (i % 2 && s[i][xx] != f0) zz++;
if (!(i % 2) && s[i][xx] != f1) zz++;
}
return min(yy, zz);
}
void solve(string f, long long r) {
long long xx = 0;
if (r == 0)
for (int i = 0; i < n; i++)
xx += (!(i % 2) ? sat(i, f[0], f[1]) : sat(i, f[2], f[3]));
else
for (int i = 0; i < m; i++)
xx += (!(i % 2) ? sot(i, f[0], f[1]) : sot(i, f[2], f[3]));
if (xx < ans) {
ans = xx;
ans1 = f[0];
ans1 += f[1];
ans2 = f[2];
ans2 += f[3];
t = r;
}
}
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> s[i];
f[i] = s[i];
}
solve("AGCT", 0);
solve("ACGT", 0);
solve("ATCG", 0);
solve("CTAG", 0);
solve("GTAC", 0);
solve("CGAT", 0);
solve("AGCT", 1);
solve("ACGT", 1);
solve("ATCG", 1);
solve("CTAG", 1);
solve("GTAC", 1);
solve("CGAT", 1);
if (t == 0) {
for (int i = 0; i < n; i++) {
long long xx = 0, zz = 0;
for (int j = 0; j < m; j++) {
if ((j % 2 && s[i][j] != (i % 2 ? ans2[1] : ans1[1])) ||
(j % 2 == 0 && s[i][j] != (i % 2 ? ans2[0] : ans1[0])))
xx++;
}
for (int j = 0; j < m; j++) {
if ((j % 2 && s[i][j] != (i % 2 ? ans2[0] : ans1[0])) ||
(j % 2 == 0 && s[i][j] != (i % 2 ? ans2[1] : ans1[1])))
zz++;
}
w = 0;
if (zz < xx) w = 1;
for (int j = 0; j < m; j++)
cout << (i % 2 ? ans2[(w + j % 2) % 2] : ans1[(w + j % 2) % 2]);
cout << endl;
}
} else {
for (int i = 0; i < m; i++) {
long long xx = 0, zz = 0;
for (int j = 0; j < n; j++) {
if ((j % 2 && s[j][i] != (i % 2 ? ans2[1] : ans1[1])) ||
(j % 2 == 0 && s[j][i] != (i % 2 ? ans2[0] : ans1[0])))
xx++;
}
for (int j = 0; j < n; j++) {
if ((j % 2 && s[j][i] != (i % 2 ? ans2[0] : ans1[0])) ||
(j % 2 == 0 && s[j][i] != (i % 2 ? ans2[1] : ans1[1])))
zz++;
}
w = 0;
if (zz < xx) w = 1;
for (int j = 0; j < n; j++)
f[j][i] = (i % 2 ? ans2[(w + j % 2) % 2] : ans1[(w + j % 2) % 2]);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) cout << f[i][j];
cout << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
map<char, int> mp;
vector<int> lc[300010];
char toCout[] = {'A', 'G', 'C', 'T'};
int forCol[300010][4][2], forRow[300010][4][2];
int main() {
mp['A'] = 0;
mp['G'] = 1;
mp['C'] = 2;
mp['T'] = 3;
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
char a;
cin >> a;
lc[i].push_back(mp[a]);
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) forCol[i][lc[j][i]][j % 2]++;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) forRow[i][lc[i][j]][j % 2]++;
}
int row = 0;
vector<int> ele, good;
ele.push_back(0);
ele.push_back(1);
ele.push_back(2);
ele.push_back(3);
int realAns = 0;
do {
int var = ele[0], var1 = ele[1], var2 = ele[2], var3 = ele[3];
int ans1 = forCol[0][var][0] + forCol[0][var2][1] + forCol[1][var1][0] +
forCol[1][var3][1];
int ans2 = forRow[0][var][0] + forRow[0][var1][1] + forRow[1][var2][0] +
forRow[1][var3][1];
for (int i = 2; i < m; i++) {
if (i % 2 == 0)
ans1 += max(forCol[i][var][0] + forCol[i][var2][1],
forCol[i][var][1] + forCol[i][var2][0]);
else
ans1 += max(forCol[i][var1][0] + forCol[i][var3][1],
forCol[i][var1][1] + forCol[i][var3][0]);
}
for (int i = 2; i < n; i++) {
if (i % 2 == 0)
ans2 += max(forRow[i][var][0] + forRow[i][var1][1],
forRow[i][var][1] + forRow[i][var1][0]);
else
ans2 += max(forRow[i][var2][0] + forRow[i][var3][1],
forRow[i][var2][1] + forRow[i][var3][0]);
}
if (max(ans1, ans2) > realAns) {
if (ans1 > ans2)
row = 0;
else
row = 1;
good = vector<int>();
good.push_back(var);
good.push_back(var1);
good.push_back(var2);
good.push_back(var3);
realAns = max(ans1, ans2);
}
} while (next_permutation(ele.begin(), ele.end()));
if (row) {
for (int i = 0; i < m; i++) {
if (i % 2 == 0)
cout << toCout[good[0]];
else
cout << toCout[good[1]];
}
cout << '\n';
for (int i = 0; i < m; i++) {
if (i % 2 == 0)
cout << toCout[good[2]];
else
cout << toCout[good[3]];
}
cout << '\n';
for (int i = 2; i < n; i++) {
int add = 0;
if (i % 2) add = 2;
int var, var1;
if (forRow[i][good[0 + add]][0] + forRow[i][good[1 + add]][1] >
forRow[i][good[0 + add]][1] + forRow[i][good[1 + add]][0])
var = good[0 + add], var1 = good[1 + add];
else
var = good[1 + add], var1 = good[0 + add];
for (int j = 0; j < m; j++) {
if (j % 2 == 0)
cout << toCout[var];
else
cout << toCout[var1];
}
cout << '\n';
}
} else {
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
cout << toCout[good[0]] << toCout[good[1]];
for (int j = 2; j < m; j++) {
if (j % 2 == 0) {
if (forCol[j][good[0]][0] + forCol[j][good[2]][1] >
forCol[j][good[0]][1] + forCol[j][good[2]][0])
cout << toCout[good[0]];
else
cout << toCout[good[2]];
} else {
if (forCol[j][good[1]][0] + forCol[j][good[3]][1] >
forCol[j][good[1]][1] + forCol[j][good[3]][0])
cout << toCout[good[1]];
else
cout << toCout[good[3]];
}
}
cout << '\n';
} else {
cout << toCout[good[2]] << toCout[good[3]];
for (int j = 2; j < m; j++) {
if (j % 2 == 0) {
if (forCol[j][good[0]][0] + forCol[j][good[2]][1] >
forCol[j][good[0]][1] + forCol[j][good[2]][0])
cout << toCout[good[2]];
else
cout << toCout[good[0]];
} else {
if (forCol[j][good[1]][0] + forCol[j][good[3]][1] >
forCol[j][good[1]][1] + forCol[j][good[3]][0])
cout << toCout[good[3]];
else
cout << toCout[good[1]];
}
}
cout << '\n';
}
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
long long m, n, ans, a, b, cnt[2][300100][4], zh[210], dp[300100][4][4];
pair<long long, long long> last[300100][4][4], pp;
char fz[4];
string str[300100], tmp[300100], A, B, C, D;
inline long long get(long long u, long long v) {
long long i, j, p, q, res = 0;
for (p = 0; p < 4; p++)
if (p != u && p != v) break;
for (q = p + 1; q < 4; q++)
if (q != u && q != v) break;
for (i = 0; i < n; i++) {
A[i] = (i & 1) ? fz[p] : fz[q];
B[i] = (i & 1) ? fz[q] : fz[p];
C[i] = (i & 1) ? fz[u] : fz[v];
D[i] = (i & 1) ? fz[v] : fz[u];
}
for (i = 0; i < m; i++) {
long long c1, c2;
c1 = c2 = 0;
if (i & 1) {
for (j = 0; j < n; j++)
c1 += (C[j] == str[i][j]), c2 += (D[j] == str[i][j]);
if (c1 > c2)
tmp[i] = C, res += c1;
else
tmp[i] = D, res += c2;
} else {
for (j = 0; j < n; j++)
c1 += (A[j] == str[i][j]), c2 += (B[j] == str[i][j]);
if (c1 > c2)
tmp[i] = A, res += c1;
else
tmp[i] = B, res += c2;
}
}
return res;
}
inline long long cmp() {
long long i, j, res = 0;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
res += (str[i][j] == tmp[i][j]);
}
}
return res;
}
int main() {
memset(dp, -0x3f, sizeof(dp));
ios::sync_with_stdio(0);
long long i, j, k, p, q, t;
zh['A'] = 0, zh['G'] = 1, zh['T'] = 2, zh['C'] = 3;
fz[0] = 'A', fz[1] = 'G', fz[2] = 'T', fz[3] = 'C';
cin >> m >> n;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++) tmp[i] += '0';
for (i = 0; i < n; i++) A += '0', B += '0', C += '0', D += '0';
for (i = 0; i < m; i++) {
cin >> str[i];
for (j = 0; j < n; j++) {
cnt[i & 1][j][zh[str[i][j]]]++;
}
}
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
if (i != j) dp[0][i][j] = cnt[0][0][i] + cnt[1][0][j];
for (i = 0; i < n - 1; i++) {
for (j = 0; j < 4; j++) {
for (k = 0; k < 4; k++) {
if (j == k) continue;
for (p = 0; p < 4; p++) {
if (p == j || p == k) continue;
for (q = 0; q < 4; q++) {
if (q == p || q == j || q == k) continue;
t = dp[i][j][k] + cnt[0][i + 1][p] + cnt[1][i + 1][q];
if (dp[i + 1][p][q] < t) {
dp[i + 1][p][q] = t;
last[i + 1][p][q] = make_pair(j, k);
}
}
}
}
}
}
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
if (i == j) continue;
if (dp[n - 1][i][j] > ans) {
a = i, b = j;
ans = dp[n - 1][i][j];
}
}
}
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
if (i == j) continue;
t = get(i, j);
if (t > ans) {
ans = t;
pp = make_pair(i, j);
}
}
}
if (pp.first || pp.second) {
get(pp.first, pp.second);
for (i = 0; i < m; i++) cout << tmp[i] << endl;
return 0;
}
for (p = a, q = b, i = n - 1; i >= 0; i--) {
tmp[0][i] = fz[p];
tmp[1][i] = fz[q];
pp = make_pair(p, q);
p = last[i][pp.first][pp.second].first;
q = last[i][pp.first][pp.second].second;
}
for (i = 0; i < m; i++) {
cout << tmp[i & 1] << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
string str = "ACGT";
int n, m, ansmin = 1e9;
vector<string> s, ans;
void work() {
vector<string> tmp;
int thisans = 0;
for (int i = 0; i < n; i++) tmp.push_back(string(m, '.'));
string a[2];
a[0] = str.substr(0, 2), a[1] = str.substr(2);
int cur = 0, nxt = 1;
for (int i = 0; i < n; i++) {
int A = 0, B = 0;
for (int j = 0; j < m; j++) {
if (s[i][j] != a[cur][j & 1]) A++;
if (s[i][j] != a[cur][1 - (j & 1)]) B++;
}
if (A > B) {
thisans += B;
for (int j = 0; j < m; j++) tmp[i][j] = a[cur][1 - (j & 1)];
} else {
thisans += A;
for (int j = 0; j < m; j++) tmp[i][j] = a[cur][j & 1];
}
swap(cur, nxt);
}
if (thisans < ansmin) ansmin = thisans, ans = tmp;
cur = 0;
nxt = 1;
thisans = 0;
for (int i = 0; i < m; i++) {
int A = 0, B = 0;
for (int j = 0; j < n; j++) {
if (s[j][i] != a[cur][j & 1]) A++;
if (s[j][i] != a[cur][1 - (j & 1)]) B++;
}
if (A > B) {
thisans += B;
for (int j = 0; j < n; j++) tmp[j][i] = a[cur][1 - (j & 1)];
} else {
thisans += A;
for (int j = 0; j < n; j++) tmp[j][i] = a[cur][j & 1];
}
swap(cur, nxt);
}
if (thisans < ansmin) ansmin = thisans, ans = tmp;
}
int main() {
ios::sync_with_stdio(0);
cin >> n >> m;
for (int i = 0; i < n; i++) {
string t;
cin >> t;
s.push_back(t);
}
do {
work();
} while (next_permutation(str.begin(), str.end()));
for (int i = 0; i < n; i++) cout << ans[i] << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const char* const TG[] = {"ACGT", "AGCT", "ATCG", "CGAT", "CTAG", "GTAC"};
int topChg = INT_MAX;
vector<string> topGd;
void sv(vector<string> const& gd) {
int N = ((int)(gd).size()), M = ((int)(gd[0]).size());
topChg = INT_MAX;
topGd.clear();
for (int a = 0; a < int(6); a++) {
vector<string> newGd(N);
int ochg = 0;
for (int n = 0; n < int(N); n++) {
int bestChg = INT_MAX;
for (int o = 0; o < int(2); o++) {
int chg = 0;
string tr = gd[n];
for (int m = 0; m < int(M); m++) {
char ec = TG[a][2 * (n % 2) + (m + o) % 2];
if (ec != tr[m]) {
tr[m] = ec;
++chg;
}
}
if (chg < bestChg) {
bestChg = chg;
newGd[n] = move(tr);
}
}
ochg += bestChg;
}
if (ochg < topChg) {
topChg = ochg;
topGd = move(newGd);
}
}
}
vector<string> flipIt(vector<string> const& gd) {
int N = ((int)(gd).size()), M = ((int)(gd[0]).size());
vector<string> newGd(M, string(N, '\0'));
for (int n = 0; n < int(N); n++)
for (int m = 0; m < int(M); m++) newGd[m][n] = gd[n][m];
return newGd;
}
int main() {
int N, M;
scanf("%d%d", &N, &M);
vector<string> gd(N, string(M, '\0'));
char tmp[300001];
for (int n = 0; n < int(N); n++) {
scanf(" %s", tmp);
memcpy(&gd[n][0], tmp, M);
}
sv(gd);
int chg = topChg;
vector<string> ngd = topGd;
sv(flipIt(gd));
if (topChg < chg) {
chg = topChg;
ngd = flipIt(topGd);
}
for (int n = 0; n < int(N); n++) {
puts(ngd[n].c_str());
}
}
|
#include <bits/stdc++.h>
using namespace std;
int mod = 1000000007;
vector<vector<int> > a, b, best;
int res = -1;
int main() {
ios::sync_with_stdio(0);
int n, m;
cin >> n >> m;
a.resize(n);
for (int i = 0; i < (int)(n); ++i) {
string s;
cin >> s;
a[i].resize(m);
for (int j = 0; j < (int)(m); ++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 if (s[j] == 'T') {
a[i][j] = 3;
}
}
}
b = a;
for (int x = 0; x < (int)(4); ++x) {
for (int y = 0; y < (int)(x); ++y) {
for (int z = 0; z < (int)(4); ++z) {
for (int t = 0; t < (int)(z); ++t) {
if (x == z || x == t || y == z || y == t) {
continue;
}
int sum = 0;
for (int i = 0; i < (int)(n); ++i) {
int cur1 = 0, cur2 = 0;
vector<int> bb1(m), bb2(m);
if (i % 2) {
for (int j = 0; j < (int)(m); ++j) {
bb1[j] = j % 2 ? x : y;
bb2[j] = j % 2 ? y : x;
}
} else {
for (int j = 0; j < (int)(m); ++j) {
bb1[j] = j % 2 ? z : t;
bb2[j] = j % 2 ? t : z;
}
}
for (int j = 0; j < (int)(m); ++j) {
if (a[i][j] == bb1[j]) {
++cur1;
}
if (a[i][j] == bb2[j]) {
++cur2;
}
}
if (cur1 > cur2) {
sum += cur1;
b[i] = bb1;
} else {
sum += cur2;
b[i] = bb2;
}
}
if (sum > res) {
res = sum;
best = b;
}
sum = 0;
for (int j = 0; j < (int)(m); ++j) {
int cur1 = 0, cur2 = 0;
vector<int> bb1(n), bb2(n);
if (j % 2) {
for (int i = 0; i < (int)(n); ++i) {
bb1[i] = i % 2 ? x : y;
bb2[i] = i % 2 ? y : x;
}
} else {
for (int i = 0; i < (int)(n); ++i) {
bb1[i] = i % 2 ? z : t;
bb2[i] = i % 2 ? t : z;
}
}
for (int i = 0; i < (int)(n); ++i) {
if (a[i][j] == bb1[i]) {
++cur1;
}
if (a[i][j] == bb2[i]) {
++cur2;
}
}
if (cur1 > cur2) {
sum += cur1;
for (int i = 0; i < (int)(n); ++i) {
b[i][j] = bb1[i];
}
} else {
sum += cur2;
for (int i = 0; i < (int)(n); ++i) {
b[i][j] = bb2[i];
}
}
}
if (sum > res) {
res = sum;
best = b;
}
}
}
}
}
for (int i = 0; i < (int)(n); ++i) {
for (int j = 0; j < (int)(m); ++j) {
if (best[i][j] == 0) {
cout << 'A';
} else if (best[i][j] == 1) {
cout << 'G';
} else if (best[i][j] == 2) {
cout << 'C';
} else if (best[i][j] == 3) {
cout << 'T';
}
}
cout << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void chkmax(T &x, T y) {
x = x > y ? x : y;
}
template <typename T>
void chkmin(T &x, T y) {
x = x > y ? y : x;
}
const int INF = 2139062143;
template <typename T>
void read(T &x) {
x = 0;
bool f = 1;
char ch;
do {
ch = getchar();
if (ch == '-') f = 0;
} while (ch > '9' || ch < '0');
do {
x = x * 10 + ch - '0';
ch = getchar();
} while (ch >= '0' && ch <= '9');
x = f ? x : -x;
}
template <typename T>
void write(T x) {
if (x < 0) x = ~x + 1, putchar('-');
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
const int N = 3e5 + 7;
int n, m, ans;
string mp = "ACGT";
string a[N], cur[N], rev[N];
bool flag[N];
inline void calc(const string *a, int n, int m, bool rev) {
int now = 0;
for (int i = 0; i < n; i++) {
int cnt0 = 0, cnt1 = 0;
for (int j = 0; j < m; j++) {
cnt0 += (mp[((i & 1) * 2) + (j & 1)] != a[i][j]);
cnt1 += (mp[((i & 1) * 2) + !(j & 1)] != a[i][j]);
}
flag[i] = cnt0 < cnt1;
now += min(cnt0, cnt1);
}
if (ans > now) {
ans = now;
if (rev) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cur[j][i] = flag[i] ? mp[((i & 1) * 2) + (j & 1)]
: mp[((i & 1) * 2) + !(j & 1)];
}
}
} else {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cur[i][j] = flag[i] ? mp[((i & 1) * 2) + (j & 1)]
: mp[((i & 1) * 2) + !(j & 1)];
}
}
}
}
}
int main() {
ans = INF;
read(n);
read(m);
for (int i = 0; i < n; i++) cin >> a[i], cur[i] = a[i];
for (int i = 0; i < m; i++) rev[i].resize(n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
rev[j][i] = a[i][j];
}
}
do {
calc(a, n, m, 0);
calc(rev, m, n, 1);
} while (next_permutation(mp.begin(), mp.end()));
for (int i = 0; i < n; i++) cout << cur[i] << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long m, n, ans, a, b, cnt[2][300100][4], zh[210], dp[300100][4][4];
pair<long long, long long> last[300100][4][4], pp;
char fz[210];
string str[300100], tmp[300100], A, B, C, D;
inline long long get(long long u, long long v) {
long long i, j, p, q, res = 0;
for (p = 0; p < 4; p++)
if (p != u && p != v) break;
for (q = p + 1; q < 4; q++)
if (q != u && q != v) break;
for (i = 0; i < n; i++) {
A[i] = (i & 1) ? fz[p] : fz[q];
B[i] = (i & 1) ? fz[q] : fz[p];
C[i] = (i & 1) ? fz[u] : fz[v];
D[i] = (i & 1) ? fz[v] : fz[u];
}
for (i = 0; i < m; i++) {
long long c1, c2;
c1 = c2 = 0;
if (i & 1) {
for (j = 0; j < n; j++)
c1 += (C[j] == str[i][j]), c2 += (D[j] == str[i][j]);
if (c1 > c2)
tmp[i] = C, res += c1;
else
tmp[i] = D, res += c2;
} else {
for (j = 0; j < n; j++)
c1 += (A[j] == str[i][j]), c2 += (B[j] == str[i][j]);
if (c1 > c2)
tmp[i] = A, res += c1;
else
tmp[i] = B, res += c2;
}
}
return res;
}
inline long long cmp() {
long long i, j, res = 0;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
res += (str[i][j] == tmp[i][j]);
}
}
return res;
}
int main() {
memset(dp, -0x3f, sizeof(dp));
ios::sync_with_stdio(0);
long long i, j, k, p, q, t;
zh['A'] = 0, zh['G'] = 1, zh['T'] = 2, zh['C'] = 3;
fz[0] = 'A', fz[1] = 'G', fz[2] = 'T', fz[3] = 'C';
cin >> m >> n;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++) tmp[i] += '0';
for (i = 0; i < n; i++) A += '0', B += '0', C += '0', D += '0';
for (i = 0; i < m; i++) {
cin >> str[i];
for (j = 0; j < n; j++) {
cnt[i & 1][j][zh[str[i][j]]]++;
}
}
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
if (i != j) dp[0][i][j] = cnt[0][0][i] + cnt[1][0][j];
for (i = 0; i < n - 1; i++) {
for (j = 0; j < 4; j++) {
for (k = 0; k < 4; k++) {
if (j == k) continue;
for (p = 0; p < 4; p++) {
if (p == j || p == k) continue;
for (q = 0; q < 4; q++) {
if (q == p || q == j || q == k) continue;
t = dp[i][j][k] + cnt[0][i + 1][p] + cnt[1][i + 1][q];
if (dp[i + 1][p][q] < t) {
dp[i + 1][p][q] = t;
last[i + 1][p][q] = make_pair(j, k);
}
}
}
}
}
}
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
if (i == j) continue;
if (dp[n - 1][i][j] > ans) {
a = i, b = j;
ans = dp[n - 1][i][j];
}
}
}
for (i = 0; i < 4; i++) {
for (j = i + 1; j < 4; j++) {
t = get(i, j);
if (t > ans) {
ans = t;
pp = make_pair(i, j);
}
}
}
if (pp.first + pp.second) {
get(pp.first, pp.second);
for (i = 0; i < m; i++) cout << tmp[i] << endl;
return 0;
}
for (p = a, q = b, i = n - 1; i >= 0; i--) {
tmp[0][i] = fz[p];
tmp[1][i] = fz[q];
pp = make_pair(p, q);
p = last[i][pp.first][pp.second].first;
q = last[i][pp.first][pp.second].second;
}
for (i = 0; i < m; i++) {
cout << tmp[i & 1] << endl;
}
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
using namespace std;
const int arr = 2e5 + 10;
const int ar = 2e3 + 10;
const long double pi = acos(-1);
const long double eps = 1e-10;
const long long md = 1e9 + 7;
int num[ar];
char rnum[ar];
vector<int> cnt1[4][2];
signed dp1[(int)(3e5 + 10)][4][4];
signed from1[(int)(3e5 + 10)][4][4];
vector<int> cnt2[4][2];
signed dp2[(int)(3e5 + 10)][4][4];
signed from2[(int)(3e5 + 10)][4][4];
bool maximize(int& a, int b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
char ans[(int)(3e5 + 10)][2];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
num['A'] = 0;
num['G'] = 1;
num['C'] = 2;
num['T'] = 3;
rnum[0] = 'A';
rnum[1] = 'G';
rnum[2] = 'C';
rnum[3] = 'T';
int n, m;
cin >> n >> m;
vector<string> a(n);
for (auto& i : a) {
cin >> i;
}
for (int i = 0; i < 4; i++) {
cnt1[i][0].assign(m, 0);
cnt1[i][1].assign(m, 0);
cnt2[i][0].assign(n, 0);
cnt2[i][1].assign(n, 0);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cnt1[num[a[i][j]]][i % 2][j]++;
cnt2[num[a[i][j]]][j % 2][i]++;
}
}
for (int i = 0; i < (int)(3e5 + 10); i++) {
for (int j = 0; j < 4; j++) {
for (int k = 0; k < 4; k++) {
dp1[i][j][k] = -1e9;
dp2[i][j][k] = -1e9;
}
}
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (i != j) {
dp1[0][i][j] = cnt1[i][0][0] + cnt1[j][1][0];
}
}
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (i != j) {
dp2[0][i][j] = cnt2[i][0][0] + cnt2[j][1][0];
}
}
}
for (int j = 0; j + 1 < m; j++) {
for (int k1 = 0; k1 < 4; k1++) {
for (int k2 = 0; k2 < 4; k2++) {
if (k1 != k2) {
for (int k3 = 0; k3 < 4; k3++) {
if (k1 != k3 && k2 != k3) {
int k4 = 0 + 1 + 2 + 3 - k1 - k2 - k3;
if (maximize(dp1[j + 1][k3][k4], dp1[j][k1][k2] +
cnt1[k3][0][j + 1] +
cnt1[k4][1][j + 1])) {
from1[j + 1][k3][k4] = k1 * 10 + k2;
}
}
}
}
}
}
}
for (int j = 0; j + 1 < n; j++) {
for (int k1 = 0; k1 < 4; k1++) {
for (int k2 = 0; k2 < 4; k2++) {
if (k1 != k2) {
for (int k3 = 0; k3 < 4; k3++) {
if (k1 != k3 && k2 != k3) {
int k4 = 0 + 1 + 2 + 3 - k1 - k2 - k3;
if (maximize(dp2[j + 1][k3][k4], dp2[j][k1][k2] +
cnt2[k3][0][j + 1] +
cnt2[k4][1][j + 1])) {
from2[j + 1][k3][k4] = k1 * 10 + k2;
}
}
}
}
}
}
}
int best = -1;
int kek1 = -1;
int kek2 = -1;
int type = -1;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (i != j) {
if (dp1[m - 1][i][j] > best) {
best = dp1[m - 1][i][j];
kek1 = i;
kek2 = j;
type = 1;
}
}
}
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (i != j) {
if (dp2[n - 1][i][j] > best) {
best = dp2[n - 1][i][j];
kek1 = i;
kek2 = j;
type = 2;
}
}
}
}
if (type == 1) {
ans[m - 1][0] = rnum[kek1];
ans[m - 1][1] = rnum[kek2];
for (int i = m - 1; i > 0; i--) {
int buf = from1[i][kek1][kek2];
kek1 = buf / 10;
kek2 = buf % 10;
ans[i - 1][0] = rnum[kek1];
ans[i - 1][1] = rnum[kek2];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << ans[j][i % 2];
}
cout << "\n";
}
} else {
ans[n - 1][0] = rnum[kek1];
ans[n - 1][1] = rnum[kek2];
for (int i = n - 1; i > 0; i--) {
int buf = from2[i][kek1][kek2];
kek1 = buf / 10;
kek2 = buf % 10;
ans[i - 1][0] = rnum[kek1];
ans[i - 1][1] = rnum[kek2];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << ans[i][j % 2];
}
cout << "\n";
}
}
}
|
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:256000000")
using namespace std;
const int INF = (int)(1e9 + 1e5);
const long long LINF = (long long)(2e18);
const long long BASE = 1003;
const double EPS = 1e-9;
const int MOD = 1000000007;
int tnum;
struct filling {
bool vert;
string pattern;
};
int n, m;
vector<string> f;
vector<string> ans;
void init() {
cin >> n >> m;
f.resize(n);
for (int i = 0; i < n; ++i) {
cin >> f[i];
}
}
int get_row_fixed(int num, const string& pat) {
int res = 0;
for (int i = 0; i < m; ++i) {
res += (f[num][i] != pat[i & 1]);
ans[num][i] = pat[i & 1];
}
return res;
}
int get_row(int num, const string& pat) {
auto rev_pat = pat;
std::reverse(rev_pat.begin(), rev_pat.end());
auto res = get_row_fixed(num, pat);
auto res_rev = get_row_fixed(num, rev_pat);
if (res < res_rev) {
get_row_fixed(num, pat);
}
return min(res, res_rev);
}
int get_column_fixed(int num, const string& pat) {
int res = 0;
for (int i = 0; i < n; ++i) {
res += (f[i][num] != pat[i & 1]);
ans[i][num] = pat[i & 1];
}
return res;
}
int get_column(int num, const string& pat) {
auto rev_pat = pat;
std::reverse(rev_pat.begin(), rev_pat.end());
auto res = get_column_fixed(num, pat);
auto res_rev = get_column_fixed(num, rev_pat);
if (res < res_rev) {
get_column_fixed(num, pat);
}
return min(res, res_rev);
}
int try_fill(filling fil) {
int res = 0;
auto first = fil.pattern.substr(0, 2);
auto second = fil.pattern.substr(2, 2);
if (fil.vert) {
for (int i = 0; i < m; ++i) {
res += get_column(i, (i & 1) ? second : first);
}
} else {
for (int i = 0; i < n; ++i) {
res += get_row(i, (i & 1) ? second : first);
}
}
return res;
}
void solve() {
ans.resize(n);
for (int i = 0; i < n; ++i) {
ans[i].resize(m);
}
filling cur, best;
int best_res = INF;
for (int vert = 0; vert <= 1; ++vert) {
cur.pattern = "ACGT";
cur.vert = vert;
unordered_set<string> was;
do {
auto str = cur.pattern.substr(0, 2);
if (str[0] > str[1]) {
swap(str[0], str[1]);
}
if (was.count(str)) {
continue;
}
auto cur_res = try_fill(cur);
if (cur_res < best_res) {
best_res = cur_res;
best = cur;
}
was.insert(str);
} while (next_permutation(cur.pattern.begin(), cur.pattern.end()));
}
try_fill(best);
}
void print_answer() {
for (int i = 0; i < n; ++i) {
cout << ans[i] << "\n";
}
}
void precalc() {}
void run() {
precalc();
int t = 1;
for (tnum = 1; tnum <= t; ++tnum) {
init();
solve();
print_answer();
}
}
int main() {
srand(90);
ios_base::sync_with_stdio(false);
cin.tie(0);
run();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 330000;
const int INF = 1e9 + 1e8;
char pat[10] = "AGCT";
string pan[N], ans[N], chk[N];
int frq[10][10][300];
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> pan[i], ans[i].resize(m), chk[i].resize(m);
int tot_diff = INF;
auto solve = [&](char *p) {
int tot_cnt = 0;
for (int i = 0; i < n; i++) {
int cc[2] = {0};
for (int k2 = 0; k2 < 2; k2++) {
for (int j = 0; j < m; j++) {
if (pan[i][j] != p[i % 2 * 2 + (j + k2) % 2]) cc[k2]++;
}
}
int k2 = (cc[0] < cc[1]) ? 0 : 1;
tot_cnt += cc[k2];
for (int j = 0; j < m; j++) chk[i][j] = p[i % 2 * 2 + (j + k2) % 2];
}
if (tot_diff > tot_cnt) {
tot_diff = tot_cnt;
for (int i = 0; i < n; i++) ans[i] = chk[i];
}
tot_cnt = 0;
for (int j = 0; j < m; j++) {
int cc[2] = {0};
for (int k1 = 0; k1 < 2; k1++) {
for (int i = 0; i < n; i++) {
if (pan[i][j] != p[(i + k1) % 2 * 2 + j % 2]) cc[k1]++;
}
}
int k1 = (cc[0] < cc[1]) ? 0 : 1;
tot_cnt += cc[k1];
for (int i = 0; i < n; i++) chk[i][j] = p[(i + k1) % 2 * 2 + j % 2];
}
if (tot_diff > tot_cnt) {
tot_diff = tot_cnt;
for (int i = 0; i < n; i++) ans[i] = chk[i];
}
};
do {
solve(pat);
} while (next_permutation(pat, pat + 4));
for (int i = 0; i < n; i++) cout << ans[i] << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <class x>
vector<typename x::value_type> tovec(const x& a) {
return vector<typename x::value_type>(a.begin(), a.end());
}
template <class u>
vector<typename iterator_traits<u>::value_type> tovec(u a, u b) {
return vector<typename iterator_traits<u>::value_type>(a, b);
}
template <typename x>
ostream& operator<<(ostream& cout, const vector<x>& a) {
cout << "[";
if (((int)a.size())) cout << a[0];
for (int i = 0; i++ < ((int)a.size()) - 1;) cout << ", " << a[i];
return cout << "]";
}
template <typename u, typename v>
ostream& operator<<(ostream& cout, const pair<u, v>& a) {
return cout << "(" << a.first << ", " << a.second << ")";
}
int n, m;
vector<string> a;
pair<int, vector<string>> genWithRow(char u1, char u2, char v1, char v2) {
vector<string> ans(n, string(m + 1, '\n'));
int cost = 0;
for (int i = -1; ++i < n;) {
char x = i & 1 ? u1 : v1;
char y = i & 1 ? u2 : v2;
int c1 = 0, c2 = 0;
for (int f = -1; ++f < m;) {
char t1 = f & 1 ? x : y;
char t2 = x + y - t1;
c1 += t1 != a[i][f];
c2 += t2 != a[i][f];
}
bool s = c1 > c2;
for (int f = -1; ++f < m;) {
ans[i][f] = s ? x : y;
s = !s;
}
cost += min(c1, c2);
}
return {cost, ans};
}
pair<int, vector<string>> genWithCol(char u1, char u2, char v1, char v2) {
vector<string> ans(n, string(m + 1, '\n'));
int cost = 0;
for (int f = -1; ++f < m;) {
char x = f & 1 ? u1 : v1;
char y = f & 1 ? u2 : v2;
int c1 = 0, c2 = 0;
for (int i = -1; ++i < n;) {
char t1 = i & 1 ? x : y;
char t2 = x + y - t1;
c1 += t1 != a[i][f];
c2 += t2 != a[i][f];
}
bool s = c1 > c2;
for (int i = -1; ++i < n;) {
ans[i][f] = s ? x : y;
s = !s;
}
cost += min(c1, c2);
}
return {cost, ans};
}
int main(void) {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
a.resize(n);
for (int i = -1; ++i < n;) cin >> a[i];
string s = "ACGT";
pair<int, vector<string>> ans;
ans.first = INT_MAX;
do {
ans = min(ans, genWithRow(s[0], s[1], s[2], s[3]));
ans = min(ans, genWithCol(s[0], s[1], s[2], s[3]));
} while (next_permutation(s.begin(), s.end()));
for (auto i : ans.second) cout << i;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
bool uin(T &a, T b) {
return a > b ? (a = b, true) : false;
}
template <class T>
bool uax(T &a, T b) {
return a < b ? (a = b, true) : false;
}
const int maxn = 310000;
string f[maxn];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.precision(10);
cout << fixed;
int n, m;
cin >> n >> m;
for (int i = 0; i < (int)(n); ++i) cin >> f[i];
string chars = "ACGT";
string bchars;
bool frows;
vector<int> to_flip;
int bcost = 1e9;
do {
{
vector<int> flip_rows(n);
int cost = 0;
for (int i = 0; i < (int)(n); ++i) {
int dif0 = 0, dif1 = 0;
for (int j = 0; j < (int)(m); ++j) {
if (f[i][j] != chars[2 * (i & 1) + (j & 1)]) ++dif0;
if (f[i][j] != chars[2 * (i & 1) + ((j ^ 1) & 1)]) ++dif1;
}
cost += min(dif0, dif1);
flip_rows[i] = dif0 < dif1 ? 0 : 1;
}
if (uin(bcost, cost)) {
bchars = chars;
frows = 1;
to_flip = flip_rows;
}
}
{
vector<int> flip_cols(m);
int cost = 0;
for (int j = 0; j < (int)(m); ++j) {
int dif0 = 0, dif1 = 0;
for (int i = 0; i < (int)(n); ++i) {
if (f[i][j] != chars[2 * (i & 1) + (j & 1)]) ++dif0;
if (f[i][j] != chars[2 * ((i ^ 1) & 1) + (j & 1)]) ++dif1;
}
cost += min(dif0, dif1);
flip_cols[j] = dif0 < dif1 ? 0 : 1;
}
if (uin(bcost, cost)) {
bchars = chars;
frows = 0;
to_flip = flip_cols;
}
}
} while (next_permutation((chars).begin(), (chars).end()));
if (frows) {
for (int i = 0; i < (int)(n); ++i) {
for (int j = 0; j < (int)(m); ++j)
cout << bchars[2 * (i & 1) + ((j ^ to_flip[i]) & 1)];
cout << '\n';
}
} else {
for (int i = 0; i < (int)(n); ++i) {
for (int j = 0; j < (int)(m); ++j)
cout << bchars[2 * ((i ^ to_flip[j]) & 1) + (j & 1)];
cout << '\n';
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;
long long fexp(long long a, long long b, long long m) {
long long ans = 1;
while (b) {
if (b & 1) ans = ans * a % m;
b /= 2;
a = a * a % m;
}
return ans;
}
long long inverse(long long a, long long p) { return fexp(a, p - 2, p); }
long long fac[300005];
long long ncr(long long n, long long r, long long p) {
if (r == 0) return 1;
return (fac[n] * inverse(fac[r], p) % p * inverse(fac[n - r], p) % p) % p;
}
bool sortby(pair<pair<long long, long long>, long long> &a,
pair<pair<long long, long long>, long long> &b) {
if (a.first.first == b.first.first) return (a.first.second < b.first.second);
return (a.first.first < b.first.first);
if (a.second == b.second) return (a.first < b.first);
return (a.second < b.second);
}
long long n, m, sum = 3000000;
vector<string> ans;
void fun(vector<string> &c, string x, string y) {
vector<string> s;
long long temp = 0;
string t, tt, qt, qtt;
for (long long j = 0; j < m / 2; j++) t.push_back(x[0]), t.push_back(x[1]);
if (m & 1) t.push_back(x[0]);
if (m & 1) tt.push_back(x[1]);
for (long long j = 0; j < m / 2; j++) tt.push_back(x[0]), tt.push_back(x[1]);
reverse((tt).begin(), (tt).end());
for (long long j = 0; j < m / 2; j++) qt.push_back(y[0]), qt.push_back(y[1]);
if (m & 1) qt.push_back(y[0]);
if (m & 1) qtt.push_back(y[1]);
for (long long j = 0; j < m / 2; j++)
qtt.push_back(y[0]), qtt.push_back(y[1]);
reverse((qtt).begin(), (qtt).end());
for (long long i = 0; i < n; i++) {
long long a = 0, b = 0;
for (long long j = 0; j < m; j++) {
if (i & 1 ^ 1) {
if (c[i][j] != t[j]) a++;
if (c[i][j] != tt[j]) b++;
} else {
if (c[i][j] != qt[j]) a++;
if (c[i][j] != qtt[j]) b++;
}
}
if (i & 1 ^ 1) {
if (a < b)
s.push_back(t);
else
s.push_back(tt);
} else {
if (a < b)
s.push_back(qt);
else
s.push_back(qtt);
}
temp += (min(a, b));
}
if (temp < sum) {
sum = temp;
if ((long long)ans.size() == 0)
for (long long i = 0; i < n; i++) ans.push_back(s[i]);
for (long long i = 0; i < n; i++) ans[i] = s[i];
}
s.clear();
temp = 0;
t.clear();
tt.clear();
qt.clear();
qtt.clear();
for (long long j = 0; j < n / 2; j++) t.push_back(x[0]), t.push_back(x[1]);
if (n & 1) t.push_back(x[0]);
if (n & 1) tt.push_back(x[1]);
for (long long j = 0; j < n / 2; j++) tt.push_back(x[0]), tt.push_back(x[1]);
reverse((tt).begin(), (tt).end());
for (long long j = 0; j < n / 2; j++) qt.push_back(y[0]), qt.push_back(y[1]);
if (n & 1) qt.push_back(y[0]);
if (n & 1) qtt.push_back(y[1]);
for (long long j = 0; j < n / 2; j++)
qtt.push_back(y[0]), qtt.push_back(y[1]);
reverse((qtt).begin(), (qtt).end());
for (long long j = 0; j < m; j++) {
long long a = 0, b = 0;
for (long long i = 0; i < n; i++) {
if (j & 1 ^ 1) {
if (c[i][j] != t[i]) a++;
if (c[i][j] != tt[i]) b++;
} else {
if (c[i][j] != qt[i]) a++;
if (c[i][j] != qtt[i]) b++;
}
}
if (j & 1 ^ 1) {
if (a < b)
s.push_back(t);
else
s.push_back(tt);
} else {
if (a < b)
s.push_back(qt);
else
s.push_back(qtt);
}
temp += (min(a, b));
}
if (temp < sum) {
sum = temp;
for (long long i = 0; i < n; i++)
for (long long j = 0; j < m; j++) ans[i][j] = s[j][i];
}
}
long long solve() {
cin >> n >> m;
vector<string> c(n);
for (long long i = 0; i < n; i++) cin >> c[i];
string s = "ACGT";
fun(c, "AC", "GT");
fun(c, "AG", "CT");
fun(c, "AT", "CG");
fun(c, "CG", "AT");
fun(c, "CT", "AG");
fun(c, "GT", "AC");
for (long long i = 0; i < n; i++) cout << ans[i] << "\n";
return 0;
}
signed main() {
auto start = high_resolution_clock::now();
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
long long t = 1;
while (t--) {
solve();
cout << "\n";
}
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
cerr << "Time taken:" << duration.count() / 1000000.0 << "seconds"
<< "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int n, m;
cin >> n >> m;
vector<string> t(n);
for (int i = 0; i < n; ++i) cin >> t[i];
char c[] = "ACGT";
int solc = n * m + 1;
string sol[2];
int dir = 0;
do {
{
string tt[2];
tt[0].resize(m);
tt[1].resize(m);
for (int i = 0; i < m; ++i) tt[0][i] = c[i % 4];
for (int i = 0; i < m; ++i) tt[1][i] = c[(i + 2) % 4];
int cnt = 0;
for (int i = 0; i < m; ++i) {
int res1 = 0, res2 = 0;
for (int j = 0; j < n; ++j) res1 += t[j][i] != tt[j % 2][i];
swap(tt[0][i], tt[1][i]);
for (int j = 0; j < n; ++j) res2 += t[j][i] != tt[j % 2][i];
swap(tt[0][i], tt[1][i]);
cnt += min(res1, res2);
if (res1 > res2) swap(tt[0][i], tt[1][i]);
}
if (cnt < solc) {
dir = 1;
solc = cnt;
sol[0] = tt[0];
sol[1] = tt[1];
}
}
{
string tt[2];
tt[0].resize(n);
tt[1].resize(n);
for (int i = 0; i < n; ++i) tt[0][i] = c[i % 4];
for (int i = 0; i < n; ++i) tt[1][i] = c[(i + 2) % 4];
int cnt = 0;
for (int i = 0; i < n; ++i) {
int res1 = 0, res2 = 0;
for (int j = 0; j < m; ++j) res1 += t[i][j] != tt[j % 2][i];
swap(tt[0][i], tt[1][i]);
for (int j = 0; j < m; ++j) res2 += t[i][j] != tt[j % 2][i];
swap(tt[0][i], tt[1][i]);
cnt += min(res1, res2);
if (res1 > res2) swap(tt[0][i], tt[1][i]);
}
if (cnt < solc) {
dir = 2;
solc = cnt;
sol[0] = tt[0];
sol[1] = tt[1];
}
}
} while (next_permutation(c, c + 4));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (dir == 1) cout << sol[i % 2][j];
if (dir == 2) cout << sol[j % 2][i];
}
cout << '\n';
}
}
|
#include <bits/stdc++.h>
using namespace std;
vector<string> s;
string z = "ACGT";
vector<string> ans;
int cost = 1e8;
void test(vector<string> t) {
int cc = 0;
for (int i = 0; i < s.size(); i++) {
for (int j = 0; j < s[0].size(); j++) {
if (s[i][j] != t[i][j]) {
cc++;
}
}
}
if (cc < cost) {
cost = cc;
ans = t;
}
}
int diff(string a, string b) {
assert(a.size() == b.size());
int x = 0;
for (int i = 0; i < a.size(); i++) {
x += a[i] != b[i];
}
return x;
}
vector<string> transpose(vector<string> b) {
vector<string> a(b[0].size(), string(b.size(), ' '));
for (int i = 0; i < a.size(); i++) {
for (int j = 0; j < a[0].size(); j++) {
a[i][j] = b[j][i];
}
}
return a;
}
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
s.resize(n);
for (int i = 0; i < n; i++) {
cin >> s[i];
}
for (int j = 0; j < 2; j++) {
vector<string> p1 = {"AC", "AG", "AT", "CG", "CT", "GT"};
vector<string> p2 = {"GT", "CT", "CG", "AT", "AG", "AC"};
for (int z = 0; z < 6; z++) {
vector<string> stuff = s;
for (int i = 0; i < s.size(); i++) {
string bank;
if (i % 2 == 0) bank = p1[z];
if (i % 2 == 1) bank = p2[z];
string t1(s[0].size(), ' ');
string t2(s[0].size(), ' ');
for (int j = 0; j < s[0].size(); j++) {
t1[j] = bank[j % 2];
t2[j] = bank[1 - j % 2];
}
if (diff(t1, s[i]) > diff(t2, s[i])) {
swap(t1, t2);
}
stuff[i] = t1;
}
test(stuff);
}
s = transpose(s);
ans = transpose(ans);
}
for (string x : ans) {
cout << x << '\n';
}
}
|
#include <bits/stdc++.h>
#pragma comment(linker, "/stack:225450978")
#pragma GCC optimize("Ofast")
using namespace std;
const long long Mod = 1000000007LL, INF = 1e9, LINF = 1e18;
const long double Pi = 3.141592653589793116, EPS = 1e-9,
Gold = ((1 + sqrt(5)) / 2);
long long keymod[] = {1000000007LL, 1000000009LL, 1000000021LL, 1000000033LL};
long long keyCount = sizeof(keymod) / sizeof(long long);
mt19937 rng32(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng64(chrono::steady_clock::now().time_since_epoch().count());
template <class T>
int getbit(T s, int i) {
return (s >> i) & 1;
}
template <class T>
T onbit(T s, int i) {
return s | (T(1) << i);
}
template <class T>
T offbit(T s, int i) {
return s & (~(T(1) << i));
}
template <class T>
int cntbit(T s) {
return __builtin_popcountll(s);
}
auto TimeStart = chrono::steady_clock::now();
auto TimeEnd = chrono::steady_clock::now();
void ControlIO(int argc, char* argv[]);
void TimerStart();
void TimerStop();
void Exit();
string cppstr_infile = "FILE.IN";
string cppstr_outfile = "FILE.OUT";
long long n, m;
vector<vector<char> > A;
map<char, int> Map;
vector<char> IMap(4);
long long MinDiff = LINF;
vector<vector<char> > OptimalTable;
long long Differs(vector<vector<char> >& Table1,
vector<vector<char> >& Table2) {
long long res = 0;
for (long long i = 0; i < n; i++) {
for (long long j = 0; j < m; j++) {
res += (Table1[i][j] != Table2[i][j]);
}
}
return res;
}
void Input() {
cin >> n >> m;
A.resize(n, vector<char>(m));
for (long long i = 0; i < n; i++) {
for (long long j = 0; j < m; j++) {
cin >> A[i][j];
}
}
Map['A'] = 0;
Map['T'] = 1;
Map['G'] = 2;
Map['C'] = 3;
IMap[0] = 'A';
IMap[1] = 'T';
IMap[2] = 'G';
IMap[3] = 'C';
}
void Solve() {
for (long long i = 0; i < 4; i++) {
for (long long j = 0; j < 4; j++) {
if (j == i) continue;
for (long long k = 0; k < 4; k++) {
if (k == i) continue;
if (k == j) continue;
for (long long l = 0; l < 4; l++) {
if (l == i) continue;
if (l == j) continue;
if (l == k) continue;
vector<string> pattern(2, "");
pattern[0] += IMap[i];
pattern[0] += IMap[j];
pattern[1] += IMap[k];
pattern[1] += IMap[l];
vector<vector<char> > Sample(n, vector<char>(m));
for (long long i = 0; i < n; i++) {
vector<long long> subdiff(2, 0);
for (long long j = 0; j < m; j++) {
subdiff[0] += (pattern[i % 2][j % 2] != A[i][j]);
subdiff[1] += (pattern[i % 2][1 - j % 2] != A[i][j]);
}
if (subdiff[0] <= subdiff[1])
for (long long j = 0; j < m; j++)
Sample[i][j] = pattern[i % 2][j % 2];
else
for (long long j = 0; j < m; j++)
Sample[i][j] = pattern[i % 2][1 - j % 2];
}
long long Diff = Differs(Sample, A);
if (Diff < MinDiff) {
MinDiff = Diff;
OptimalTable = Sample;
}
}
}
}
}
for (long long i = 0; i < 4; i++) {
for (long long j = 0; j < 4; j++) {
if (j == i) continue;
for (long long k = 0; k < 4; k++) {
if (k == i) continue;
if (k == j) continue;
for (long long l = 0; l < 4; l++) {
if (l == i) continue;
if (l == j) continue;
if (l == k) continue;
vector<string> pattern(2, "");
pattern[0] += IMap[i];
pattern[0] += IMap[j];
pattern[1] += IMap[k];
pattern[1] += IMap[l];
vector<vector<char> > Sample(n, vector<char>(m));
for (long long j = 0; j < m; j++) {
vector<long long> subdiff(2, 0);
for (long long i = 0; i < n; i++) {
subdiff[0] += (pattern[j % 2][i % 2] != A[i][j]);
subdiff[1] += (pattern[j % 2][1 - i % 2] != A[i][j]);
}
if (subdiff[0] <= subdiff[1])
for (long long i = 0; i < n; i++)
Sample[i][j] = pattern[j % 2][i % 2];
else
for (long long i = 0; i < n; i++)
Sample[i][j] = pattern[j % 2][1 - i % 2];
}
long long Diff = Differs(Sample, A);
if (Diff < MinDiff) {
MinDiff = Diff;
OptimalTable = Sample;
}
}
}
}
}
for (auto Row : OptimalTable) {
for (auto Cell : Row) cout << Cell;
cout << '\n';
}
}
int main(int argc, char* argv[]) {
ControlIO(argc, argv);
ios_base::sync_with_stdio(0);
cin.tie(NULL);
Input();
TimerStart();
Solve();
TimerStop();
return 0;
}
void ControlIO(int argc, char* argv[]) {
char* infile = new char[cppstr_infile.size() + 1];
char* outfile = new char[cppstr_outfile.size() + 1];
strcpy(infile, cppstr_infile.c_str());
strcpy(outfile, cppstr_outfile.c_str());
}
void TimerStart() {}
void TimerStop() {}
void Exit() {
TimerStop();
exit(0);
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m;
string s[300005];
string tmpS[300005];
int best = 300005;
int rn, rm;
string res[300005];
string curS[300005];
char chars[] = {'A', 'C', 'G', 'T'};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for (int i = 0; i < n; ++i) {
cin >> s[i];
tmpS[i] = s[i];
}
for (int iter = 0; iter < 2; ++iter) {
sort(chars, chars + 4);
do {
int curVal = 0;
for (int i = 0; i < n; ++i) {
string tmp = "";
tmp += chars[2 * (i % 2)];
tmp += chars[2 * (i % 2) + 1];
string bestS;
int bestRes = 300005;
for (int t = 0; t < 2; ++t) {
int cur = 0;
for (int j = 0; j < m; ++j)
if (tmp[j % 2] != s[i][j]) ++cur;
if (cur < bestRes) {
bestRes = cur;
bestS = tmp;
}
swap(tmp[0], tmp[1]);
}
curVal += bestRes;
curS[i] = "";
for (int j = 0; j < m; ++j) curS[i] += bestS[j % 2];
}
if (curVal < best) {
best = curVal;
rn = n;
rm = m;
for (int i = 0; i < n; ++i) res[i] = curS[i];
}
} while (next_permutation(chars, chars + 4));
for (int j = 0; j < m; ++j) {
s[j] = "";
for (int i = 0; i < n; ++i) s[j] += tmpS[i][j];
}
swap(n, m);
}
if (rn == n) {
for (int i = 0; i < n; ++i) cout << res[i] << endl;
} else {
for (int i = 0; i < rm; ++i) {
for (int j = 0; j < rn; ++j) cout << res[j][i];
cout << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int R, C, res = INT_MAX;
bool swapped, c_swapped;
vector<vector<char>> arr, ans;
vector<char> v = {'A', 'C', 'G', 'T'};
inline void rotate(vector<vector<char>> &arr) {
vector<vector<char>> nxt(C, vector<char>(R));
for (int i = 0; i < C; i++)
for (int k = 0; k < R; k++) nxt[i][k] = arr[k][C - i - 1];
swap(arr, nxt);
}
inline vector<vector<char>> make(vector<char> t[2]) {
vector<vector<char>> ans(R, vector<char>(C));
for (int i = 0; i < R; i++) {
int min_cost = INT_MAX, a = 0;
for (int k = 0; k < 2; k++) {
int cost = 0;
for (int a = 0; a < C; a++) cost += arr[i][a] != t[i % 2][a % 2];
if (cost < min_cost) {
min_cost = cost;
a = k;
}
swap(t[i % 2][0], t[i % 2][1]);
}
if (a) swap(t[i % 2][0], t[i % 2][1]);
for (int k = 0; k < C; k++) ans[i][k] = t[i % 2][k % 2];
if (a) swap(t[i % 2][0], t[i % 2][1]);
}
return ans;
}
void solve() {
sort(v.begin(), v.end());
do {
vector<char> t[2];
t[0] = {v[0], v[1]};
t[1] = {v[2], v[3]};
int sum = 0;
for (int i = 0; i < R; i++) {
int min_cost = INT_MAX;
for (int k = 0; k < 2; k++) {
int cost = 0;
for (int a = 0; a < C; a++) cost += arr[i][a] != t[i % 2][a % 2];
min_cost = min(min_cost, cost);
swap(t[i % 2][0], t[i % 2][1]);
}
sum += min_cost;
}
if (sum < res) {
if (c_swapped) swapped = true;
res = sum;
ans = make(t);
}
} while (next_permutation(v.begin(), v.end()));
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> R >> C;
arr.resize(R);
for (int i = 0; i < R; i++) {
arr[i].resize(C);
for (int k = 0; k < C; k++) cin >> arr[i][k];
}
solve();
rotate(arr);
swap(R, C);
c_swapped = true;
solve();
if (swapped) {
rotate(ans);
swap(R, C);
rotate(ans);
swap(R, C);
rotate(ans);
}
swap(R, C);
for (int i = 0; i < R; i++) {
for (int k = 0; k < C; k++) cout << ans[i][k];
cout << '\n';
}
}
|
#include <bits/stdc++.h>
using namespace std;
char t[5] = "ACTG";
char s[300010];
vector<int> a[300010];
vector<int> ans[300010];
vector<int> p;
int wh[300010];
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) {
scanf("%s", s);
a[i].resize(m);
for (int j = 0; j < m; j++) {
for (int k = 0; k < 4; k++) {
if (t[k] == s[j]) a[i][j] = k;
}
}
ans[i].resize(m);
}
for (int i = 0; i < 4; i++) {
p.push_back(i);
}
int be = n * m + 1;
do {
int c = 0;
for (int i = 0; i < n; i++) {
int s = i & 1 ? 2 : 0;
for (int j = 0; j < m; j++) {
if (a[i][j] != p[s]) c++;
s = s == 3 ? 0 : s + 1;
}
}
if (be > c) {
be = c;
for (int i = 0; i < n; i++) {
int s = i & 1 ? 2 : 0;
for (int j = 0; j < m; j++) {
ans[i][j] = p[s];
s = s == 3 ? 0 : s + 1;
}
}
}
c = 0;
for (int i = 0; i < n; i++) {
int c0 = 0, c1 = 0;
for (int j = 0; j < m; j++) {
int s = (i & 1) * 2 + (j & 1);
if (a[i][j] != p[s]) c0++;
if (a[i][j] != p[s ^ 1]) c1++;
}
if (c0 < c1) {
c += c0;
wh[i] = 0;
} else {
c += c1;
wh[i] = 1;
}
}
if (be > c) {
be = c;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int s = (i & 1) * 2 + (j & 1);
ans[i][j] = p[s ^ wh[i]];
}
}
}
c = 0;
for (int i = 0; i < m; i++) {
int c0 = 0, c1 = 0;
for (int j = 0; j < n; j++) {
int s = (i & 1) * 2 + (j & 1);
if (a[j][i] != p[s]) c0++;
if (a[j][i] != p[s ^ 1]) c1++;
}
if (c0 < c1) {
c += c0;
wh[i] = 0;
} else {
c += c1;
wh[i] = 1;
}
}
if (be > c) {
be = c;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
int s = (i & 1) * 2 + (j & 1);
ans[j][i] = p[s ^ wh[i]];
}
}
}
} while (next_permutation(p.begin(), p.end()));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
putchar(t[ans[i][j]]);
}
puts("");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void solve(istream& in, ostream& out) {
int n, m;
in >> n >> m;
vector<string> a(n);
for (int i = 0; i < n; ++i) {
in >> a[i];
}
string S = "AGCT";
int best = numeric_limits<int>::max();
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < i; ++j) {
for (int k = 0; k < 4; ++k) {
for (int p = 0; p < k; ++p) {
set<int> s = {i, j, k, p};
if (s.size() < 4) continue;
int cur = 0;
for (int r = 0; r < n; ++r) {
char ch[2];
if (r % 2 == 0) {
ch[0] = S[i];
ch[1] = S[j];
} else {
ch[0] = S[k];
ch[1] = S[p];
}
int cost1 = 0;
for (int c = 0; c < m; ++c) {
cost1 += a[r][c] != ch[c % 2];
}
int cost2 = 0;
for (int c = 0; c < m; ++c) {
cost2 += a[r][c] != ch[(c + 1) % 2];
}
cur += min(cost1, cost2);
}
best = min(best, cur);
}
}
}
}
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < i; ++j) {
for (int k = 0; k < 4; ++k) {
for (int p = 0; p < k; ++p) {
set<int> s = {i, j, k, p};
if (s.size() < 4) continue;
int cur = 0;
for (int c = 0; c < m; ++c) {
char ch[2];
if (c % 2 == 0) {
ch[0] = S[i];
ch[1] = S[j];
} else {
ch[0] = S[k];
ch[1] = S[p];
}
int cost1 = 0;
for (int r = 0; r < n; ++r) {
cost1 += a[r][c] != ch[r % 2];
}
int cost2 = 0;
for (int r = 0; r < n; ++r) {
cost2 += a[r][c] != ch[(r + 1) % 2];
}
cur += min(cost1, cost2);
}
best = min(best, cur);
}
}
}
}
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < i; ++j) {
for (int k = 0; k < 4; ++k) {
for (int p = 0; p < k; ++p) {
set<int> s = {i, j, k, p};
if (s.size() < 4) continue;
int cur = 0;
for (int r = 0; r < n; ++r) {
char ch[2];
if (r % 2 == 0) {
ch[0] = S[i];
ch[1] = S[j];
} else {
ch[0] = S[k];
ch[1] = S[p];
}
int cost1 = 0;
for (int c = 0; c < m; ++c) {
cost1 += a[r][c] != ch[c % 2];
}
int cost2 = 0;
for (int c = 0; c < m; ++c) {
cost2 += a[r][c] != ch[(c + 1) % 2];
}
cur += min(cost1, cost2);
}
if (cur == best) {
for (int r = 0; r < n; ++r) {
char ch[2];
if (r % 2 == 0) {
ch[0] = S[i];
ch[1] = S[j];
} else {
ch[0] = S[k];
ch[1] = S[p];
}
int cost1 = 0;
for (int c = 0; c < m; ++c) {
cost1 += a[r][c] != ch[c % 2];
}
int cost2 = 0;
for (int c = 0; c < m; ++c) {
cost2 += a[r][c] != ch[(c + 1) % 2];
}
for (int c = 0; c < m; ++c) {
out << ch[(c + (cost2 < cost1)) % 2];
}
out << endl;
}
return;
}
}
}
}
}
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < i; ++j) {
for (int k = 0; k < 4; ++k) {
for (int p = 0; p < k; ++p) {
set<int> s = {i, j, k, p};
if (s.size() < 4) continue;
int cur = 0;
for (int c = 0; c < m; ++c) {
char ch[2];
if (c % 2 == 0) {
ch[0] = S[i];
ch[1] = S[j];
} else {
ch[0] = S[k];
ch[1] = S[p];
}
int cost1 = 0;
for (int r = 0; r < n; ++r) {
cost1 += a[r][c] != ch[r % 2];
}
int cost2 = 0;
for (int r = 0; r < n; ++r) {
cost2 += a[r][c] != ch[(r + 1) % 2];
}
cur += min(cost1, cost2);
}
if (cur == best) {
vector<string> res(m);
for (int c = 0; c < m; ++c) {
char ch[2];
if (c % 2 == 0) {
ch[0] = S[i];
ch[1] = S[j];
} else {
ch[0] = S[k];
ch[1] = S[p];
}
int cost1 = 0;
for (int r = 0; r < n; ++r) {
cost1 += a[r][c] != ch[r % 2];
}
int cost2 = 0;
for (int r = 0; r < n; ++r) {
cost2 += a[r][c] != ch[(r + 1) % 2];
}
for (int r = 0; r < n; ++r) {
res[c] += ch[(r + (cost2 < cost1)) % 2];
}
}
for (int r = 0; r < n; ++r) {
for (int c = 0; c < m; ++c) {
out << res[c][r];
}
out << endl;
}
return;
}
}
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
std::istream& in(std::cin);
std::ostream& out(std::cout);
solve(in, out);
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<string> grid, res;
string ch = "ACGT", bch;
vector<int> DP[2], nxt[2];
int solve(const int idx, const bool turned, const bool t) {
if (DP[turned][idx] != -1) return DP[turned][idx];
if (idx == n) return 0;
int tot = 0;
for (int i = 0; i < m; i++)
tot += ((!t ? grid[idx][i] : grid[i][idx]) ==
ch[((idx & 1) << 1) + ((turned + i) & 1)]);
int ret1 = solve(idx + 1, 0, t);
int ret2 = solve(idx + 1, 1, t);
if (!nxt[0].empty()) nxt[turned][idx] = (ret2 > ret1);
return DP[turned][idx] = tot + max(ret1, ret2);
}
int main() {
ios::sync_with_stdio(0);
int ret, best = 0, st, sw;
cin >> n >> m;
grid.assign(n + 1, ""), res.assign(n + 5, "");
for (int i = 0; i < n; i++) cin >> grid[i];
do {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
DP[0].assign(n + 5, -1), DP[1].assign(n + 5, -1);
if ((ret = solve(0, i, j)) > best) best = ret, bch = ch, st = i, sw = j;
swap(n, m);
}
}
} while (next_permutation(ch.begin(), ch.end()));
if (sw) swap(n, m);
ch = bch, DP[0].assign(n + 5, -1), DP[1].assign(n + 5, -1),
nxt[0].assign(n + 5, -1), nxt[1].assign(n + 5, -1), solve(0, st, sw);
int curr = 0;
while (curr != n) {
for (int i = 0; i < m; i++)
res[!sw ? curr : i].push_back(bch[((curr & 1) << 1) + ((st + i) & 1)]);
st = nxt[st][curr++];
}
for (int i = 0; i < (sw ? m : n); i++) cout << res[i] << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
inline int Read() {
int ret = 0, flag = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') flag = -1;
ch = getchar();
}
while ('0' <= ch && ch <= '9') {
ret = ret * 10 + (ch - '0');
ch = getchar();
}
return ret * flag;
}
const int maxn = 3e5 + 7;
vector<int> row[maxn];
char Str[maxn];
int n, m, ans = maxn;
int rcd[5], sta;
int ChangeChar(char key) {
if (key == 'A')
return 1;
else if (key == 'G')
return 2;
else if (key == 'C')
return 3;
else
return 4;
}
char ChangeNum(int key) {
if (key == 1)
return 'A';
else if (key == 2)
return 'G';
else if (key == 3)
return 'C';
else
return 'T';
}
void Check(int step, int s1, int s2, int s3, int s4, int k) {
if (step >= ans) return;
rcd[1] = s1, rcd[2] = s2;
rcd[3] = s3, rcd[4] = s4;
sta = k, ans = step;
}
void Work(int s1, int s2, int s3, int s4) {
int tmp = 0, tmp1, tmp2;
int k1 = s1, k2 = s2, k3 = s3, k4 = s4;
for (int i = 1; i <= n; i++) {
tmp1 = tmp2 = 0;
for (int j = 1; j <= m; j++) {
if (row[i][j] != s1) tmp1++;
if (row[i][j] != s2) tmp2++;
swap(s1, s2);
}
tmp += min(tmp1, tmp2);
swap(s1, s3);
swap(s2, s4);
}
Check(tmp, k1, k2, k3, k4, 0);
tmp = 0, s1 = k1, s2 = k2, s3 = k3, s4 = k4;
for (int j = 1; j <= m; j++) {
tmp1 = tmp2 = 0;
for (int i = 1; i <= n; i++) {
if (row[i][j] != s1) tmp1++;
if (row[i][j] != s2) tmp2++;
swap(s1, s2);
}
tmp += min(tmp1, tmp2);
swap(s1, s3);
swap(s2, s4);
}
Check(tmp, k1, k2, k3, k4, 1);
}
int main() {
n = Read(), m = Read();
for (int i = 1; i <= n; i++) {
scanf("%s", Str + 1);
row[i].push_back(0);
for (int j = 1; j <= m; j++) row[i].push_back(ChangeChar(Str[j]));
}
Work(1, 2, 3, 4);
Work(1, 3, 2, 4);
Work(1, 4, 2, 3);
Work(2, 3, 1, 4);
Work(2, 4, 1, 3);
Work(3, 4, 1, 2);
if (!sta) {
for (int i = 1; i <= n; i++) {
int tmp1 = 0, tmp2 = 0, f1 = rcd[1], f2 = rcd[2];
for (int j = 1; j <= m; j++) {
if (row[i][j] != rcd[1]) tmp1++;
if (row[i][j] != rcd[2]) tmp2++;
swap(rcd[1], rcd[2]);
}
if (tmp2 < tmp1) swap(f1, f2);
for (int j = 1; j <= m; j++) {
if (j & 1)
row[i][j] = f1;
else
row[i][j] = f2;
}
swap(rcd[1], rcd[3]);
swap(rcd[2], rcd[4]);
}
} else {
for (int j = 1; j <= m; j++) {
int tmp1 = 0, tmp2 = 0, f1 = rcd[1], f2 = rcd[2];
for (int i = 1; i <= n; i++) {
if (row[i][j] != rcd[1]) tmp1++;
if (row[i][j] != rcd[2]) tmp2++;
swap(rcd[1], rcd[2]);
}
if (tmp1 > tmp2) swap(f1, f2);
for (int i = 1; i <= n; i++) {
if (i & 1)
row[i][j] = f1;
else
row[i][j] = f2;
}
swap(rcd[1], rcd[3]);
swap(rcd[2], rcd[4]);
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
putchar(ChangeNum(row[i][j]));
}
putchar('\n');
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m;
map<char, int> char_idx;
vector<char> idx_to_char;
const int INF = 1e8;
int main() {
ios_base::sync_with_stdio(false);
char_idx['A'] = 0;
char_idx['C'] = 1;
char_idx['G'] = 2;
char_idx['T'] = 3;
idx_to_char = {'A', 'C', 'G', 'T'};
cin >> n >> m;
vector<vector<int>> table;
table.assign(n, vector<int>(m));
for (int i = 0; i != n; ++i) {
for (int j = 0; j != m; ++j) {
char c;
cin >> c;
table[i][j] = char_idx[c];
}
}
int L = char_idx.size();
vector<vector<vector<int>>> row_cost;
vector<vector<vector<int>>> column_cost;
row_cost.assign(n, vector<vector<int>>(L, vector<int>(L)));
for (int i = 0; i != n; ++i) {
for (int c1 = 0; c1 != L; ++c1) {
for (int c2 = 0; c2 != L; ++c2) {
if (c1 == c2)
row_cost[i][c1][c2] = INF;
else {
int c[2] = {c1, c2};
row_cost[i][c1][c2] = 0;
for (int j = 0; j != m; ++j)
row_cost[i][c1][c2] += table[i][j] != c[j % 2];
}
}
}
}
column_cost.assign(m, vector<vector<int>>(L, vector<int>(L)));
for (int j = 0; j != m; ++j) {
for (int c1 = 0; c1 != L; ++c1) {
for (int c2 = 0; c2 != L; ++c2) {
if (c1 == c2)
column_cost[j][c1][c2] = INF;
else {
int c[2] = {c1, c2};
column_cost[j][c1][c2] = 0;
for (int i = 0; i != n; ++i)
column_cost[j][c1][c2] += table[i][j] != c[i % 2];
}
}
}
}
int best_cost = INF;
vector<int> best_solution = {0, 0};
bool is_best_rowwise;
for (int c1 = 0; c1 != L; ++c1) {
for (int c2 = c1 + 1; c2 != L; ++c2) {
int c3 = (c1 + 1) % 4;
if (c2 == c3) c3 = (c3 + 1) % 4;
int c4 = (c1 + 3) % 4;
if (c2 == c4) c4 = (c4 + 3) % 4;
int cost = 0;
for (int i = 0; i != n; ++i) {
int cx = (i % 2) == 0 ? c1 : c3;
int cy = (i % 2) == 0 ? c2 : c4;
cost += min(row_cost[i][cx][cy], row_cost[i][cy][cx]);
}
if (cost < best_cost) {
best_cost = cost;
best_solution = {c1, c2, c3, c4};
is_best_rowwise = true;
}
}
}
for (int c1 = 0; c1 != L; ++c1) {
for (int c2 = c1 + 1; c2 != L; ++c2) {
int c3 = (c1 + 1) % 4;
if (c2 == c3) c3 = (c3 + 1) % 4;
int c4 = (c1 + 3) % 4;
if (c2 == c4) c4 = (c4 + 3) % 4;
int cost = 0;
for (int j = 0; j != m; ++j) {
int cx = (j % 2) == 0 ? c1 : c3;
int cy = (j % 2) == 0 ? c2 : c4;
cost += min(column_cost[j][cx][cy], column_cost[j][cy][cx]);
}
if (cost < best_cost) {
best_cost = cost;
best_solution = {c1, c2, c3, c4};
is_best_rowwise = false;
}
}
}
vector<vector<char>> output_table(n, vector<char>(m));
vector<char> r;
for (auto x : best_solution) r.push_back(idx_to_char[x]);
vector<int> c(best_solution.begin(), best_solution.end());
if (is_best_rowwise) {
for (int i = 0; i != n; ++i) {
int cx = c[0 + 2 * (i % 2)];
int cy = c[1 + 2 * (i % 2)];
vector<char> out;
if (row_cost[i][cy][cx] < row_cost[i][cx][cy])
out = {idx_to_char[cy], idx_to_char[cx]};
else
out = {idx_to_char[cx], idx_to_char[cy]};
for (int j = 0; j != m; ++j) output_table[i][j] = out[j % 2];
}
} else {
for (int j = 0; j != m; ++j) {
int cx = c[0 + 2 * (j % 2)];
int cy = c[1 + 2 * (j % 2)];
vector<char> out;
if (column_cost[j][cy][cx] < column_cost[j][cx][cy])
out = {idx_to_char[cy], idx_to_char[cx]};
else
out = {idx_to_char[cx], idx_to_char[cy]};
for (int i = 0; i != n; ++i) output_table[i][j] = out[i % 2];
}
}
for (auto& row : output_table) {
for (auto& c : row) {
cout << c;
}
cout << "\n";
}
}
|
#include <bits/stdc++.h>
int n, m;
int ans;
int p[300100];
int b[300100], c[300100];
int c2i[300];
char i2c[300];
int used[10];
char s[300100];
void Compute(int u, int v) {
int x[3][3];
int cost[2], current, phase;
x[0][0] = u;
x[0][1] = v;
for (x[1][0] = 0; x[1][0] < 4; ++x[1][0])
if (x[1][0] != u && x[1][0] != v) break;
x[1][1] = 6 - u - v - x[1][0];
current = 0;
for (int i = 0; i < n; ++i) {
cost[0] = cost[1] = 0;
for (int j = 0; j < m; ++j) {
if (b[i * m + j] != x[i & 1][0 ^ (j & 1)]) cost[0] += 1;
if (b[i * m + j] != x[i & 1][1 ^ (j & 1)]) cost[1] += 1;
}
phase = 0;
if (cost[1] < cost[0]) phase = 1;
current += cost[phase];
for (int j = 0; j < m; ++j) {
c[i * m + j] = x[i & 1][phase ^ (j & 1)];
}
}
if (current < ans) {
ans = current;
for (int i = 0; i < n * m; ++i) p[i] = c[i];
}
current = 0;
for (int j = 0; j < m; ++j) {
cost[0] = cost[1] = 0;
for (int i = 0; i < n; ++i) {
if (b[i * m + j] != x[j & 1][(i & 1) ^ 0]) cost[0] += 1;
if (b[i * m + j] != x[j & 1][(i & 1) ^ 1]) cost[1] += 1;
}
phase = 0;
if (cost[1] < cost[0]) phase = 1;
current += cost[phase];
for (int i = 0; i < n; ++i) {
c[i * m + j] = x[j & 1][(i & 1) ^ phase];
}
}
if (current < ans) {
ans = current;
for (int i = 0; i < n * m; ++i) p[i] = c[i];
}
}
int main() {
scanf("%d %d", &n, &m);
ans = n * m + 1;
i2c[0] = 'A';
i2c[1] = 'C';
i2c[2] = 'G';
i2c[3] = 'T';
for (int i = 0; i < 4; ++i) c2i[i2c[i]] = i;
for (int i = 0; i < n; ++i) {
scanf("%s", s);
for (int j = 0; j < m; ++j) b[i * m + j] = c2i[s[j]];
}
for (int i = 0; i < 4; ++i)
for (int j = i + 1; j < 4; ++j) Compute(i, j);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) printf("%c", i2c[p[i * m + j]]);
printf("\n");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MX = 300005;
string ans[MX];
string s[MX];
int n, m;
string cur[MX];
int calcRow(int i, string S) {
int res = 0;
for (int j = 0; j < m; j++) {
res += (s[i][j] != S[j % 2]);
}
return res;
}
void paintRow(int i, string S) {
for (int j = 0; j < m; j++) {
cur[i][j] = S[j % 2];
}
}
int calcColumn(int j, string S) {
int res = 0;
for (int i = 0; i < n; i++) {
res += (s[i][j] != S[i % 2]);
}
return res;
}
void paintColumn(int j, string S) {
for (int i = 0; i < n; i++) {
cur[i][j] = S[i % 2];
}
}
int calcVert(string S1, string S2) {
string S1_rev = S1;
reverse(S1_rev.begin(), S1_rev.end());
string S2_rev = S2;
reverse(S2_rev.begin(), S2_rev.end());
int total = 0;
for (int j = 0; j < m; j += 2) {
int s1 = calcColumn(j, S1);
int s2 = calcColumn(j, S1_rev);
if (s1 < s2)
paintColumn(j, S1);
else
paintColumn(j, S1_rev);
total += min(s1, s2);
}
for (int j = 1; j < m; j += 2) {
int s1 = calcColumn(j, S2);
int s2 = calcColumn(j, S2_rev);
if (s1 < s2)
paintColumn(j, S2);
else
paintColumn(j, S2_rev);
total += min(s1, s2);
}
return total;
}
int calcHoriz(string S1, string S2) {
string S1_rev = S1;
reverse(S1_rev.begin(), S1_rev.end());
string S2_rev = S2;
reverse(S2_rev.begin(), S2_rev.end());
int total = 0;
for (int i = 0; i < n; i += 2) {
int s1 = calcRow(i, S1);
int s2 = calcRow(i, S1_rev);
if (s1 < s2)
paintRow(i, S1);
else
paintRow(i, S1_rev);
total += min(s1, s2);
}
for (int i = 1; i < n; i += 2) {
int s1 = calcRow(i, S2);
int s2 = calcRow(i, S2_rev);
if (s1 < s2)
paintRow(i, S2);
else
paintRow(i, S2_rev);
total += min(s1, s2);
}
return total;
}
string SS1[6];
string SS2[6];
int main() {
SS1[0] = "AT";
SS1[1] = "AC";
SS1[2] = "AG";
SS1[3] = "CT";
SS1[4] = "CG";
SS1[5] = "TG";
SS2[0] = "CG";
SS2[1] = "TG";
SS2[2] = "TC";
SS2[3] = "AG";
SS2[4] = "AT";
SS2[5] = "AC";
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> s[i];
cur[i].resize(m);
}
int mn = n * m;
for (int i = 0; i < 6; i++) {
int tmp = calcVert(SS1[i], SS2[i]);
if (tmp <= mn) {
mn = tmp;
for (int j = 0; j < n; j++) {
ans[j] = cur[j];
}
}
}
for (int i = 0; i < 6; i++) {
int tmp = calcHoriz(SS1[i], SS2[i]);
if (tmp <= mn) {
mn = tmp;
for (int j = 0; j < n; j++) {
ans[j] = cur[j];
}
}
}
for (int i = 0; i < n; i++) {
cout << ans[i] << "\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
char arr[4] = {'A', 'C', 'G', 'T'};
int ans = 1000000006LL;
string s[300005];
int n, m;
int isrow;
vector<int> anslist;
int ansa, ansb;
void rowfixed(int a, int b) {
vector<int> collist;
int c, d;
for (int i = 0; i < 4; i++) {
if (i != a && i != b) {
c = i;
d = 6 - a - b - c;
break;
}
}
int xx = 0;
for (int i = 0; i < n; i++) {
int x = 0, y = 0;
if (i & 1) {
for (int j = 0; j < m; j += 2) {
if (s[i][j] != arr[a]) x++;
if (s[i][j] != arr[b]) y++;
}
for (int j = 1; j < m; j += 2) {
if (s[i][j] != arr[a]) y++;
if (s[i][j] != arr[b]) x++;
}
} else {
for (int j = 0; j < m; j += 2) {
if (s[i][j] != arr[c]) x++;
if (s[i][j] != arr[d]) y++;
}
for (int j = 1; j < m; j += 2) {
if (s[i][j] != arr[c]) y++;
if (s[i][j] != arr[d]) x++;
}
}
xx += min(x, y);
if (x < y) {
collist.push_back((i & 1) ? a : c);
} else {
collist.push_back((i & 1) ? b : d);
}
}
if (xx < ans) {
isrow = 1;
anslist = collist;
ansb = collist[1] == a ? b : a;
ansa = collist[0] == c ? d : c;
ans = xx;
}
}
void columnfixed(int a, int b) {
vector<int> rowlist;
int c, d;
for (int i = 0; i < 4; i++) {
if (i != a && i != b) {
c = i;
d = 6 - a - b - c;
break;
}
}
int xx = 0;
for (int j = 0; j < m; j++) {
int x = 0, y = 0;
if (j & 1) {
for (int i = 0; i < n; i += 2) {
if (s[i][j] != arr[a]) x++;
if (s[i][j] != arr[b]) y++;
}
for (int i = 1; i < n; i += 2) {
if (s[i][j] != arr[a]) y++;
if (s[i][j] != arr[b]) x++;
}
} else {
for (int i = 0; i < n; i += 2) {
if (s[i][j] != arr[c]) x++;
if (s[i][j] != arr[d]) y++;
}
for (int i = 1; i < n; i += 2) {
if (s[i][j] != arr[c]) y++;
if (s[i][j] != arr[d]) x++;
}
}
xx += min(x, y);
if (x < y) {
rowlist.push_back((j & 1) ? a : c);
} else {
rowlist.push_back((j & 1) ? b : d);
}
}
if (xx < ans) {
isrow = 0;
anslist = rowlist;
ansb = rowlist[1] == a ? b : a;
ansa = rowlist[0] == c ? d : c;
ans = xx;
}
}
void print() {
if (isrow) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (j & 1) {
if (i & 1)
cout << arr[(anslist[i] == ansb) ? anslist[1] : ansb];
else
cout << arr[(anslist[i] == ansa) ? anslist[0] : ansa];
} else
cout << arr[anslist[i]];
}
cout << endl;
}
} else {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (i & 1) {
if (j & 1)
cout << arr[(anslist[j] == ansb) ? anslist[1] : ansb];
else
cout << arr[(anslist[j] == ansa) ? anslist[0] : ansa];
} else
cout << arr[anslist[j]];
}
cout << endl;
}
}
}
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> s[i];
for (int i = 0; i < 4; i++) {
for (int j = i + 1; j < 4; j++) {
rowfixed(i, j);
columnfixed(i, j);
}
}
print();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
struct problem {
int n, m;
vector<string> a;
vector<vector<int>> b, dp, pre;
int transponovano;
char kodiraj(char x) {
switch (x) {
case 'A':
return 0;
case 'C':
return 1;
case 'G':
return 2;
case 'T':
return 3;
}
return -1;
}
char dekodiraj(char x) {
switch (x) {
case 0:
return 'A';
case 1:
return 'C';
case 2:
return 'G';
case 3:
return 'T';
}
return -1;
}
void ucitaj() {
cin >> n >> m;
a.assign(n, "");
for (int i = 0; i < n; i++) {
cin >> a[i];
for (int j = 0; j < m; j++) {
a[i][j] = kodiraj(a[i][j]);
}
}
transponovano = 0;
}
void transponuj() {
vector<string> b(m, string(n, '.'));
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) b[j][i] = a[i][j];
swap(n, m);
swap(a, b);
transponovano ^= 1;
}
void precompute() {
b.assign(m, vector<int>(8, 0));
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) b[j][((i & 1) << 2) + (int)a[i][j]]++;
}
int column_cost(int i, int x, int y) { return b[i][x] + b[i][4 + y]; }
int __x, __y;
void probaj(int x, int y) {
__x = x;
__y = y;
int p = (x ^ y) % 3 + 1;
int q = p % 3 + 1;
dp.assign(m, vector<int>(4, -1123123123));
pre.assign(m, vector<int>(4, 0));
dp[0][x] = column_cost(0, x, y);
for (int i = 1; i < m; i++) {
for (int j = 0; j < 4; j++) {
int kand;
kand = dp[i - 1][j ^ p] + column_cost(i, j, j ^ x ^ y);
if (kand > dp[i][j]) {
dp[i][j] = kand;
pre[i][j] = j ^ p;
}
kand = dp[i - 1][j ^ q] + column_cost(i, j, j ^ x ^ y);
if (kand > dp[i][j]) {
dp[i][j] = kand;
pre[i][j] = j ^ q;
}
}
}
}
int cena() {
if (dp.size() == 0) return -1;
return *max_element(dp[m - 1].begin(), dp[m - 1].end());
}
void rekonstruisi() {
int x = __x;
int y = __y;
int j = max_element(dp[m - 1].begin(), dp[m - 1].end()) - dp[m - 1].begin();
vector<int> h(m);
for (int i = m - 1; i >= 0; j = pre[i][j], i--) {
h[i] = j;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int v = (i & 1 ? x ^ y : 0) ^ h[j];
a[i][j] = dekodiraj(v);
}
}
}
void stampaj() {
for (int i = 0; i < n; i++) cout << a[i] << '\n';
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cerr.tie(nullptr);
problem pr, best;
pr.ucitaj();
pr.precompute();
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (i == j) continue;
auto tmp = pr;
tmp.probaj(i, j);
if (tmp.cena() > best.cena()) {
best = tmp;
}
}
}
pr.transponuj();
pr.precompute();
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (i == j) continue;
auto tmp = pr;
tmp.probaj(i, j);
if (tmp.cena() > best.cena()) {
best = tmp;
}
}
}
best.rekonstruisi();
if (best.transponovano) best.transponuj();
best.stampaj();
}
|
#include <bits/stdc++.h>
using namespace std;
string s[300009];
int n, m;
int mn = 1e9;
char al[3] = {'C', 'G', 'T'};
int la, aa, oa;
void proc(int l, int aaa, int o) {
int s1 = n, s2 = m;
if (l) swap(s1, s2);
string si1 = "A", si2 = "";
for (int i = 0; i < 3; i++) {
if (i == aaa)
si1 += al[i];
else
si2 += al[i];
}
int mnn = 0;
for (int i = 0; i < s1; i++) {
int cmn;
string a;
if (i % 2 == o)
a = si1;
else
a = si2;
int cc = 0;
for (int j = 0; j < s2; j++) {
int k = j;
if (l) swap(i, j);
if (s[i][j] != a[k % 2]) cc++;
if (l) swap(i, j);
}
swap(a[0], a[1]);
cmn = cc;
cc = 0;
for (int j = 0; j < s2; j++) {
int k = j;
if (l) swap(i, j);
if (s[i][j] != a[k % 2]) cc++;
if (l) swap(i, j);
}
cmn = min(cmn, cc);
mnn += cmn;
}
mn = min(mn, mnn);
if (mn == mnn) {
la = l;
aa = aaa;
oa = o;
}
}
string ans[300009];
void fna(int l, int aaa, int o) {
int s1 = n, s2 = m;
if (l) swap(s1, s2);
string si1 = "A", si2 = "";
for (int i = 0; i < 3; i++) {
if (i == aaa)
si1 += al[i];
else
si2 += al[i];
}
int mnn = 0;
for (int i = 0; i < s1; i++) {
int cmn;
string a;
if (i % 2 == o)
a = si1;
else
a = si2;
int cc = 0;
for (int j = 0; j < s2; j++) {
int k = j;
if (l) swap(i, j);
if (s[i][j] != a[k % 2]) cc++;
if (l) swap(i, j);
}
swap(a[0], a[1]);
cmn = cc;
cc = 0;
for (int j = 0; j < s2; j++) {
int k = j;
if (l) swap(i, j);
if (s[i][j] != a[k % 2]) cc++;
if (l) swap(i, j);
}
cmn = min(cmn, cc);
if (cmn != cc) swap(a[0], a[1]);
for (int j = 0; j < s2; j++) {
int k = j;
if (l) swap(i, j);
ans[i] += a[k % 2];
if (l) swap(i, j);
}
}
mn = min(mn, mnn);
if (mn == mnn) {
la = l;
aa = aaa;
oa = o;
}
}
int main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
ios_base::sync_with_stdio(0);
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> s[i];
int cur = 0;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 2; k++) proc(i, j, k);
}
}
fna(la, aa, oa);
for (int i = 0; i < n; i++) cout << ans[i] << "\n";
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
int const N = 3e5 + 20, inf = 1e9 + 20;
int n, m;
string s[N];
char ch[4] = {'A', 'G', 'C', 'T'};
int per[4];
int res = inf;
int best[4];
bool f;
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> s[i];
iota(per, per + 4, 0);
do {
char a[4];
for (int i = 0; i < 4; i++) a[i] = ch[per[i]];
int cnt = 0;
for (int i = 0; i < n; i++) {
int val = inf;
for (int f = 0; f < 2; f++) {
char b[2];
b[0] = a[(2 * (i & 1)) ^ f], b[1] = a[((2 * (i & 1)) + 1) ^ f];
int cc = 0;
for (int j = 0; j < m; j++) cc += (s[i][j] != b[j & 1]);
val = min(val, cc);
}
cnt += val;
}
if (cnt < res) {
res = cnt;
for (int i = 0; i < 4; i++) best[i] = per[i];
f = 0;
}
cnt = 0;
for (int j = 0; j < m; j++) {
int val = inf;
for (int f = 0; f < 2; f++) {
char b[2];
b[0] = a[(2 * (j & 1)) ^ f], b[1] = a[((2 * (j & 1)) + 1) ^ f];
int cc = 0;
for (int i = 0; i < n; i++) cc += (s[i][j] != b[i & 1]);
val = min(val, cc);
}
cnt += val;
}
if (cnt < res) {
res = cnt;
for (int i = 0; i < 4; i++) best[i] = per[i];
f = 1;
}
} while (next_permutation(per, per + 4));
if (f) {
for (int j = 0; j < m; j++) {
int val = inf, bb = -1;
for (int f = 0; f < 2; f++) {
char b[2];
b[0] = ch[best[(2 * (j & 1)) ^ f]],
b[1] = ch[best[((2 * (j & 1)) + 1) ^ f]];
int cc = 0;
for (int i = 0; i < n; i++) cc += (s[i][j] != b[i & 1]);
if (cc < val) bb = f, val = cc;
}
char b[2];
b[0] = ch[best[(2 * (j & 1)) ^ bb]],
b[1] = ch[best[((2 * (j & 1)) + 1) ^ bb]];
for (int i = 0; i < n; i++) s[i][j] = b[i & 1];
}
} else {
for (int i = 0; i < n; i++) {
int val = inf, bb = -1;
for (int f = 0; f < 2; f++) {
char b[2];
b[0] = ch[best[(2 * (i & 1)) ^ f]],
b[1] = ch[best[((2 * (i & 1)) + 1) ^ f]];
int cc = 0;
for (int j = 0; j < m; j++) cc += (s[i][j] != b[j & 1]);
if (cc < val) bb = f, val = cc;
}
char b[2];
b[0] = ch[best[(2 * (i & 1)) ^ bb]],
b[1] = ch[best[((2 * (i & 1)) + 1) ^ bb]];
for (int j = 0; j < m; j++) s[i][j] = b[j & 1];
}
}
for (int i = 0; i < n; i++) {
cout << s[i] << '\n';
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000, N = 300010;
const string pat[6] = {"AC", "AG", "AT", "CG", "CT", "GT"};
string st[N];
int rpt[N], cpt[N], ansrpt[N], anscpt[N], currpt[N], curcpt[N];
int n, m, ansr[2], ansc[2];
int rcalc(int pt, int odd, int row) {
int cnt = 0;
for (int j = 0; j < m; j++)
if (st[row][j] != pat[pt][(j & 1) ^ odd]) cnt++;
return cnt;
}
int ccalc(int pt, int odd, int col) {
int cnt = 0;
for (int i = 0; i < n; i++)
if (st[i][col] != pat[pt][(i & 1) ^ odd]) cnt++;
return cnt;
}
int main() {
ios::sync_with_stdio(false);
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> st[i];
int rmin = INF, cmin = INF;
for (rpt[0] = 0; rpt[0] < 6; rpt[0]++) {
rpt[1] = 5 - rpt[0];
int cnt = 0;
for (int i = 0; i < n; i++) {
int cnt0 = rcalc(rpt[i & 1], 0, i), cnt1 = rcalc(rpt[i & 1], 1, i);
if (cnt0 <= cnt1) {
currpt[i] = 0;
cnt += cnt0;
} else {
currpt[i] = 1;
cnt += cnt1;
}
}
if (cnt < rmin) {
rmin = cnt;
ansr[0] = rpt[0];
ansr[1] = rpt[1];
for (int i = 0; i < n; i++) ansrpt[i] = currpt[i];
}
}
for (cpt[0] = 0; cpt[0] < 6; cpt[0]++) {
cpt[1] = 5 - cpt[0];
int cnt = 0;
for (int j = 0; j < m; j++) {
int cnt0 = ccalc(cpt[j & 1], 0, j), cnt1 = ccalc(cpt[j & 1], 1, j);
if (cnt0 <= cnt1) {
curcpt[j] = 0;
cnt += cnt0;
} else {
curcpt[j] = 1;
cnt += cnt1;
}
}
if (cnt < cmin) {
cmin = cnt;
ansc[0] = cpt[0];
ansc[1] = cpt[1];
for (int j = 0; j < m; j++) anscpt[j] = curcpt[j];
}
}
if (rmin <= cmin) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) cout << pat[ansr[i & 1]][ansrpt[i] ^ (j & 1)];
cout << "\n";
}
} else {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) cout << pat[ansc[j & 1]][anscpt[j] ^ (i & 1)];
cout << "\n";
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m;
string row[150005], col[150005];
char ch[] = {'A', 'C', 'G', 'T'};
inline void init() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; ++i) cin >> row[i];
for (int i = 1; i <= m; ++i)
for (int j = 1; j <= n; ++j) col[i] += row[j][i - 1];
}
string ans;
int ansv = 1e8;
inline string gen(char a, char b, int n) {
string s = "";
for (int i = 1; i <= n; ++i) s += i & 1 ? a : b;
return s;
}
inline int cmp(const string &a, const string &b) {
int ans = 0;
for (int i = 0; i < a.size(); ++i)
if (a[i] != b[i]) ++ans;
return ans;
}
inline void solve_row(char a, char b, char c, char d) {
static string s[150005];
int sv = 0;
for (int i = 1; i <= n; ++i) {
char x = i & 1 ? a : c, y = i & 1 ? b : d;
string s1 = gen(x, y, m), s2 = gen(y, x, m);
int v1 = cmp(row[i], s1), v2 = cmp(row[i], s2);
if (v1 < v2)
s[i] = s1, sv += v1;
else
s[i] = s2, sv += v2;
if (sv >= ansv) return;
}
ansv = sv;
ans = "";
for (int i = 1; i <= n; ++i) ans += s[i] + '\n';
}
inline void solve_col(char a, char b, char c, char d) {
static string s[150005];
int sv = 0;
for (int i = 1; i <= m; ++i) {
char x = i & 1 ? a : c, y = i & 1 ? b : d;
string s1 = gen(x, y, n), s2 = gen(y, x, n);
int v1 = cmp(col[i], s1), v2 = cmp(col[i], s2);
if (v1 < v2)
s[i] = s1, sv += v1;
else
s[i] = s2, sv += v2;
if (sv >= ansv) return;
}
ansv = sv;
ans = "";
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) ans += s[j][i - 1];
ans += '\n';
}
}
inline void solve() {
for (int i = 0; i < 4; ++i)
for (int j = i + 1; j < 4; ++j)
for (int k = 0; k < 4; ++k)
for (int t = k + 1; t < 4; ++t)
if (k != i && k != j && t != i && t != j) {
solve_row(ch[i], ch[j], ch[k], ch[t]);
solve_col(ch[i], ch[j], ch[k], ch[t]);
}
cout << ans;
}
int main() {
init();
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <class S, class T>
pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) {
return pair<S, T>(s.first + t.first, s.second + t.second);
}
template <class S, class T>
pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) {
return pair<S, T>(s.first - t.first, s.second - t.second);
}
template <class S, class T>
ostream &operator<<(ostream &os, pair<S, T> p) {
os << "(" << p.first << ", " << p.second << ")";
return os;
}
template <class T>
bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
const long long inf = 1000000001;
const long long INF = 1e18;
const long long MOD = 1000000007;
const double pi = 3.14159265358979323846;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int dx2[8] = {1, 1, 0, -1, -1, -1, 0, 1}, dy2[8] = {0, 1, 1, 1, 0, -1, -1, -1};
vector<string> rotate(vector<string> s) {
int h = s.size(), w = s[0].size();
vector<string> t(w, string(h, '?'));
for (int i = 0; i < (h); i++) {
for (int j = 0; j < (w); j++) {
t[j][i] = s[i][j];
}
}
return t;
}
long long best = INF;
int bestm = -1;
int besti = -1;
long long solve(vector<string> s, int m, string p1, string p2) {
int h = s.size(), w = s[0].size();
long long sum = 0;
for (int i = 0; i < (h); i++) {
string p;
if (i % 2 == 0) {
p = p1;
} else {
p = p2;
}
int temp1 = 0, temp2 = 0;
for (int j = 0; j < (w); j++) {
if (j % 2 == 0) {
if (s[i][j] != p[0]) temp1++;
if (s[i][j] != p[1]) temp2++;
} else {
if (s[i][j] != p[1]) temp1++;
if (s[i][j] != p[0]) temp2++;
}
}
sum += min(temp1, temp2);
}
return sum;
}
vector<string> solve2(vector<string> s, int m, string p1, string p2) {
int h = s.size(), w = s[0].size();
long long sum = 0;
for (int i = 0; i < (h); i++) {
string p;
if (i % 2 == 0) {
p = p1;
} else {
p = p2;
}
int temp1 = 0, temp2 = 0;
for (int j = 0; j < (w); j++) {
if (j % 2 == 0) {
if (s[i][j] != p[0]) temp1++;
if (s[i][j] != p[1]) temp2++;
} else {
if (s[i][j] != p[1]) temp1++;
if (s[i][j] != p[0]) temp2++;
}
}
if (temp1 < temp2) {
for (int j = 0; j < (w); j++) {
if (j % 2 == 0) {
s[i][j] = p[0];
} else {
s[i][j] = p[1];
}
}
} else {
for (int j = 0; j < (w); j++) {
if (j % 2 == 0) {
s[i][j] = p[1];
} else {
s[i][j] = p[0];
}
}
}
}
return s;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
;
int h, w;
cin >> h >> w;
vector<string> s(h);
for (int i = 0; i < (h); i++) {
cin >> s[i];
}
vector<string> i2s1 = {"AG", "AC", "AT", "GC", "GT", "CT"};
vector<string> i2s2 = {"CT", "GT", "GC", "AT", "AC", "AG"};
if (min(h, w) == 2) {
bool flag = false;
if (w < h) {
swap(h, w);
s = rotate(s);
flag = true;
}
long long mi = INF;
int idx = -1;
for (int i = 0; i < (6); i++) {
string p1 = i2s1[i], p2 = i2s2[i];
long long sum = 0;
for (int j = 0; j < (w); j++) {
long long temp1 = 0, temp2 = 0;
if (j % 2 == 0) {
if (s[0][j] != p1[0]) temp1++;
if (s[1][j] != p1[1]) temp1++;
if (s[0][j] != p1[1]) temp2++;
if (s[1][j] != p1[0]) temp2++;
} else {
if (s[0][j] != p2[0]) temp1++;
if (s[1][j] != p2[1]) temp1++;
if (s[0][j] != p2[1]) temp2++;
if (s[1][j] != p2[0]) temp2++;
}
sum += min(temp1, temp2);
}
if (sum < mi) {
mi = sum;
idx = i;
}
}
string p1 = i2s1[idx], p2 = i2s2[idx];
long long sum = 0;
for (int j = 0; j < (w); j++) {
long long temp1 = 0, temp2 = 0;
if (j % 2 == 0) {
if (s[0][j] != p1[0]) temp1++;
if (s[1][j] != p1[1]) temp1++;
if (s[0][j] != p1[1]) temp2++;
if (s[1][j] != p1[0]) temp2++;
if (temp1 < temp2) {
s[0][j] = p1[0];
s[1][j] = p1[1];
} else {
s[0][j] = p1[1];
s[1][j] = p1[0];
}
} else {
if (s[0][j] != p2[0]) temp1++;
if (s[1][j] != p2[1]) temp1++;
if (s[0][j] != p2[1]) temp2++;
if (s[1][j] != p2[0]) temp2++;
if (temp1 < temp2) {
s[0][j] = p2[0];
s[1][j] = p2[1];
} else {
s[0][j] = p2[1];
s[1][j] = p2[0];
}
}
}
if (flag) {
s = rotate(s);
swap(h, w);
}
for (int i = 0; i < (h); i++) {
cout << s[i] << endl;
}
return 0;
}
for (int i = 0; i < (6); i++) {
string p1 = i2s1[i], p2 = i2s2[i];
long long res = solve(s, 0, p1, p2);
if (res < best) {
best = res;
bestm = 0;
besti = i;
}
}
s = rotate(s);
for (int i = 0; i < (6); i++) {
string p1 = i2s1[i], p2 = i2s2[i];
long long res = solve(s, 1, p1, p2);
if (res < best) {
best = res;
bestm = 1;
besti = i;
}
}
string p1 = i2s1[besti], p2 = i2s2[besti];
if (bestm == 0) {
s = rotate(s);
}
s = solve2(s, bestm, p1, p2);
if (bestm == 1) {
s = rotate(s);
}
for (int i = 0; i < (s.size()); i++) {
cout << s[i] << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 3;
int n, m;
string s[N];
int ans = N;
string t_ans[N];
string t[N];
void solve(const string &l) {
int cnt;
cnt = 0;
for (int i = 0; i < n; ++i) {
int start = (i & 1) << 1;
int curr1 = 0, curr2 = 0;
string line1, line2;
for (int j = 0; j < m; ++j) {
line1.push_back(l[start + (j & 1)]);
line2.push_back(l[start + 1 - (j & 1)]);
}
for (int j = 0; j < m; ++j) {
curr1 += s[i][j] != line1[j];
curr2 += s[i][j] != line2[j];
}
if (curr1 < curr2)
t[i] = line1;
else
t[i] = line2;
cnt += min(curr1, curr2);
}
if (cnt < ans) {
for (int i = 0; i < n; ++i) t_ans[i] = t[i];
ans = cnt;
}
cnt = 0;
for (int i = 0; i < m; ++i) {
int start = (i & 1) << 1;
int curr1 = 0, curr2 = 0;
string line1, line2;
for (int j = 0; j < n; ++j) {
line1.push_back(l[start + (j & 1)]);
line2.push_back(l[start + 1 - (j & 1)]);
}
for (int j = 0; j < n; ++j) {
curr1 += s[j][i] != line1[j];
curr2 += s[j][i] != line2[j];
}
if (curr1 < curr2) {
for (int j = 0; j < n; ++j) t[j][i] = line1[j];
} else {
for (int j = 0; j < n; ++j) t[j][i] = line2[j];
}
cnt += min(curr1, curr2);
}
if (cnt < ans) {
for (int i = 0; i < n; ++i) t_ans[i] = t[i];
ans = cnt;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for (int i = 0; i < n; ++i) cin >> s[i];
string letters = "ACGT";
sort(letters.begin(), letters.end());
do {
solve(letters);
} while (next_permutation(letters.begin(), letters.end()));
for (int i = 0; i < n; ++i) cout << t_ans[i] << "\n";
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline bool chkmin(T &a, const T &b) {
return b < a ? a = b, 1 : 0;
}
template <typename T>
inline bool chkmax(T &a, const T &b) {
return a < b ? a = b, 1 : 0;
}
const int oo = 0x3f3f3f3f;
inline int c2i(const char &c) {
if (c == 'A') return 0;
if (c == 'G') return 1;
if (c == 'C') return 2;
if (c == 'T') return 3;
return -1;
}
inline char i2c(const int first) {
if (first == 0) return 'A';
if (first == 1) return 'G';
if (first == 2) return 'C';
if (first == 3) return 'T';
return -1;
}
const int maxn = 300100;
int n, m;
vector<int> a[maxn + 5];
vector<int> ans[maxn + 5];
int ans_flip = -1;
int Max = -1;
inline void transpose(vector<int> *b) {
vector<int> c[maxn + 5];
for (int i = (0), i_end_ = (m); i < i_end_; ++i) c[i].resize(n);
for (int i = (0), i_end_ = (n); i < i_end_; ++i)
for (int j = (0), j_end_ = (m); j < j_end_; ++j) c[j][i] = b[i][j];
for (int i = (0), i_end_ = (n); i < i_end_; ++i) b[i].clear();
for (int i = (0), i_end_ = (m); i < i_end_; ++i) b[i] = c[i];
}
int main() {
static char ss[maxn + 5];
scanf("%d%d", &n, &m);
for (int i = (0), i_end_ = (n); i < i_end_; ++i) {
scanf("%s", ss);
for (int j = (0), j_end_ = (m); j < j_end_; ++j) a[i].push_back(c2i(ss[j]));
}
Max = -1;
for (int flip = (0), flip_end_ = (2); flip < flip_end_; ++flip) {
static vector<int> tmp[maxn + 5];
for (int i = (0), i_end_ = (n); i < i_end_; ++i) tmp[i].resize(m);
for (int a00 = (0), a00_end_ = (4); a00 < a00_end_; ++a00)
for (int a01 = (a00 + 1), a01_end_ = (4); a01 < a01_end_; ++a01)
for (int a10 = (0), a10_end_ = (4); a10 < a10_end_; ++a10)
if (a10 != a00 && a10 != a01) {
for (int a11 = (a10 + 1), a11_end_ = (4); a11 < a11_end_; ++a11)
if (a11 != a00 && a11 != a01) {
int t[2][2];
t[0][0] = a00;
t[0][1] = a01;
t[1][0] = a10;
t[1][1] = a11;
int total = 0;
for (int i = (0), i_end_ = (n); i < i_end_; ++i) {
int bestj = -1;
int Maxj = -1;
for (int j = (0), j_end_ = (2); j < j_end_; ++j) {
int cnt = 0;
for (int k = (0), k_end_ = (m); k < k_end_; ++k) {
int first = t[i & 1][(k ^ j) & 1];
if (first == a[i][k]) ++cnt;
}
if (chkmax(Maxj, cnt)) bestj = j;
}
total += Maxj;
for (int k = (0), k_end_ = (m); k < k_end_; ++k)
tmp[i][k] = t[i & 1][(k ^ bestj) & 1];
}
if (chkmax(Max, total)) {
ans_flip = flip;
for (int i = (0), i_end_ = (n); i < i_end_; ++i)
ans[i] = tmp[i];
}
}
}
transpose(a);
swap(n, m);
}
if (ans_flip) swap(n, m), transpose(ans), swap(n, m);
for (int i = (0), i_end_ = (n); i < i_end_; ++i) {
for (int j = (0), j_end_ = (m); j < j_end_; ++j) putchar(i2c(ans[i][j]));
printf("\n");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const long long INFLL = 1e18 + 1;
const int MAX = 200001;
const long long MOD = 1000000007;
const int SZ = 300100;
const double PI = 3.14159265358979323846264338327;
long long inq(long long k, long long q, long long mod) {
if (q == 0) return 1;
long long l = inq(k, q / 2, mod);
if (q % 2 == 0)
return l * l % mod;
else
return l * l % mod * k % mod;
}
long long gcd(long long a, long long b) {
if (a < b) swap(a, b);
if (b == 0) return a;
return gcd(b, a % b);
}
long long cubr(long long a) {
long long l = -1, r = 1e6 + 2;
while (l < r - 1) {
long long mid = (l + r) / 2;
if (mid * mid * mid > a)
r = mid;
else
l = mid;
}
return l;
}
long long max(long long a, long long b) {
if (a > b) return a;
return b;
}
long long min(long long a, long long b) { return -1 * max(-a, -b); }
long long possible(long long q) {
if (q == INF) return -1;
return q;
}
bool correct(int x, int xx) {
if (x < 0) return 0;
if (x >= xx) return 0;
return 1;
}
long long dsumm(long long x, long long k) {
long long y = 1;
long long z = 0;
for (int i = 0; y < 1e18; i++) {
z += x / y % k;
y *= k;
}
return z;
}
long long dcount(long long x) {
long long y = 1;
long long z = 0;
int c[100];
for (int i = 0; i < 10; i++) c[i] = 0;
for (int i = 0; x > 0; i++) {
if (c[x / y % 10] == 0) z++;
c[x / y % 10] = 1;
x /= 10;
}
return z;
}
long long lg10(long long x) {
if (10000 <= x && x < 100000) return 5;
if (x == 0) return 0;
return lg10(x / 10) + 1;
}
long long g(long long x, long long mod) {
if (x == 0) return 0;
return x / mod + g(x / mod, mod);
}
bool is_digit(char c) { return ('0' <= c && c <= '9'); }
long long n, m;
string s[500000];
vector<int> t[500000];
vector<char> fant[500000];
long long ans;
vector<char> answ[500000];
bool check_invertation_rows(int i) {
int now = 0;
for (int j = 0; j < m; j++) {
if (t[i][j] != 5) {
now += (fant[i][j] != s[i][j]);
}
}
int will = 0;
for (int j = 0; j < m; j++) {
if (j % 2 == 0 && t[i][j] != 5) {
will += (fant[i][1] != s[i][j]);
}
if (j % 2 == 1 && t[i][j] != 5) {
will += (fant[i][0] != s[i][j]);
}
}
if (will < now) {
for (int j = 0; j < m; j += 2) {
swap(fant[i][j], fant[i][j + 1]);
}
return 1;
}
return 0;
}
bool check_invertation_coll(int j) {
int now = 0;
for (int i = 0; i < n; i++) {
if (t[i][j] != 5) {
now += (fant[i][j] != s[i][j]);
}
}
int will = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0 && t[i][j] != 5) {
will += (fant[1][j] != s[i][j]);
}
if (i % 2 == 1 && t[i][j] != 5) {
will += (fant[0][j] != s[i][j]);
}
}
if (will < now) {
for (int i = 0; i < n; i += 2) {
swap(fant[i][j], fant[i + 1][j]);
}
return 1;
}
return 0;
}
void checker(vector<char> c) {
for (int i = 0; i < n; i++) {
fant[i].resize(m, 0);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int x = i % 2;
int y = j % 2;
if (x == 0 && y == 0) {
fant[i][j] = c[0];
}
if (x == 0 && y == 1) {
fant[i][j] = c[1];
}
if (x == 1 && y == 0) {
fant[i][j] = c[2];
}
if (x == 1 && y == 1) {
fant[i][j] = c[3];
}
}
}
for (int i = 2; i < n; i++) {
check_invertation_rows(i);
}
int ext = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (t[i][j] != 5) {
ext += (fant[i][j] != s[i][j]);
}
}
}
if (ext < ans) {
ans = ext;
for (int i = 0; i < n; i++) {
answ[i] = fant[i];
}
}
for (int i = 0; i < n; i++) {
fant[i].resize(m, 0);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int x = i % 2;
int y = j % 2;
if (x == 0 && y == 0) {
fant[i][j] = c[0];
}
if (x == 0 && y == 1) {
fant[i][j] = c[1];
}
if (x == 1 && y == 0) {
fant[i][j] = c[2];
}
if (x == 1 && y == 1) {
fant[i][j] = c[3];
}
}
}
for (int i = 2; i < m; i++) {
check_invertation_coll(i);
}
ext = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (t[i][j] != 5) {
ext += (fant[i][j] != s[i][j]);
}
}
}
if (ext < ans) {
ans = ext;
for (int i = 0; i < n; i++) {
answ[i] = fant[i];
}
}
}
signed main() {
ans = INF;
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> s[i];
int e = m % 2;
if (e) {
s[i] = s[i] + 'Q';
}
t[i].resize(m + e);
for (int j = 0; j < m + e; j++) {
if (s[i][j] == 'A') {
t[i][j] = 1;
}
if (s[i][j] == 'C') {
t[i][j] = 2;
}
if (s[i][j] == 'G') {
t[i][j] = 3;
}
if (s[i][j] == 'T') {
t[i][j] = 4;
}
if (s[i][j] == 'Q') {
t[i][j] = 5;
}
}
}
if (m % 2) {
m++;
}
if (n % 2) {
t[n].resize(m);
for (int i = 0; i < m; i++) {
t[n][i] = 5;
}
n++;
}
vector<char> perm;
perm.push_back('A');
perm.push_back('C');
perm.push_back('G');
perm.push_back('T');
for (int i = 0; i < 24; i++) {
checker(perm);
next_permutation((perm).begin(), (perm).end());
}
cerr << ans << endl;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (t[i][j] != 5) {
cout << answ[i][j];
}
}
cout << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
vector<int> print(303456);
vector<int> auxprint;
vector<string> auxtxt;
int maxx[303456];
string txt[303456];
int n, m;
int maxresp;
bool tipox;
void clr() {
for (int i = 0; i < 303456; ++i) {
maxx[i] = -1;
}
}
void calc(vector<string> &act) {
clr();
for (int x = 0; x < m; ++x) {
for (int j = 0; j < 2; j++) {
int c = 0;
for (int y = 0; y < n; y++) {
if (txt[y][x] == act[x % 2][(y + j) % 2]) c++;
}
if (c > maxx[x]) maxx[x] = c, print[x] = j;
}
}
int tot = 0;
for (int x = 0; x < m; ++x) tot += maxx[x];
if (tot > maxresp) {
tipox = 1;
maxresp = tot;
auxtxt = act;
auxprint = print;
}
clr();
for (int y = 0; y < n; y++) {
for (int j = 0; j < 2; j++) {
int c = 0;
for (int x = 0; x < m; x++) {
if (txt[y][x] == act[y % 2][(x + j) % 2]) {
c++;
}
}
if (c > maxx[y]) print[y] = j, maxx[y] = c;
}
}
tot = 0;
for (int y = 0; y < n; y++) tot += maxx[y];
if (tot > maxresp) {
tipox = 0;
maxresp = tot;
auxtxt = act;
auxprint = print;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
maxresp = 0;
cin >> n >> m;
for (int i = 0; i < n; ++i) {
cin >> txt[i];
}
string permutar = "ACGT";
do {
vector<string> auxp;
auxp.push_back(permutar.substr(0, 2));
auxp.push_back(permutar.substr(2));
calc(auxp);
} while (next_permutation(permutar.begin(), permutar.end()));
if (tipox == 1) {
for (int x = 0; x < m; ++x) {
for (int y = 0; y < n; y++) {
txt[y][x] = auxtxt[x % 2][(y + auxprint[x]) % 2];
}
}
} else {
for (int y = 0; y < n; y++) {
for (int x = 0; x < m; x++) {
txt[y][x] = auxtxt[y % 2][(x + auxprint[y]) % 2];
}
}
}
for (int i = 0; i < n; ++i) {
cout << txt[i] << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, best;
char *s[300005], t[300005], base[5], temp[2][2], ans[300005], draw[300005];
int main() {
scanf("%d %d", &n, &m);
best = n * m;
for (int i = (0); i < (n); i++) {
scanf("%s", t + i * m);
s[i] = t + i * m;
}
strcpy(base, "ACGT");
for (int i = (0); i < (4); i++)
for (int j = (i + 1); j < (4); j++) {
temp[0][0] = base[i];
temp[0][1] = base[j];
int ptr = 0;
for (int k = (0); k < (4); k++) {
if (k != i && k != j) {
temp[1][ptr++] = base[k];
}
}
int tot = 0;
for (int ii = (0); ii < (n); ii++) {
int sum = 0, rev = 0;
for (int jj = (0); jj < (m); jj++)
if (s[ii][jj] != temp[ii % 2][jj % 2]) sum++;
for (int jj = (0); jj < (m); jj++)
if (s[ii][jj] != temp[ii % 2][(jj + 1) % 2]) rev++;
if (sum <= rev) {
for (int jj = (0); jj < (m); jj++)
draw[ii * m + jj] = temp[ii % 2][jj % 2];
} else {
for (int jj = (0); jj < (m); jj++)
draw[ii * m + jj] = temp[ii % 2][(jj + 1) % 2];
}
tot += min(sum, rev);
}
if (tot < best) {
strcpy(ans, draw);
best = tot;
}
tot = 0;
for (int jj = (0); jj < (m); jj++) {
int sum = 0, rev = 0;
for (int ii = (0); ii < (n); ii++)
if (s[ii][jj] != temp[jj % 2][ii % 2]) sum++;
for (int ii = (0); ii < (n); ii++)
if (s[ii][jj] != temp[jj % 2][(ii + 1) % 2]) rev++;
if (sum <= rev) {
for (int ii = (0); ii < (n); ii++)
draw[ii * m + jj] = temp[jj % 2][ii % 2];
} else {
for (int ii = (0); ii < (n); ii++)
draw[ii * m + jj] = temp[jj % 2][(ii + 1) % 2];
}
tot += min(sum, rev);
}
if (tot < best) {
strcpy(ans, draw);
best = tot;
}
}
for (int i = (0); i < (n); i++) {
for (int j = (0); j < (m); j++) printf("%c", ans[i * m + j]);
puts("");
}
}
|
#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = 200200;
string a[N];
string ans[N];
string cur[N];
char ALP[] = "AGCT";
int n, m;
int bestScore;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> a[i];
cur[i] = a[i];
}
bestScore = n * m + 1;
for (int xx = 0; xx < 4; xx++)
for (int yy = 0; yy < xx; yy++) {
int score = 0;
int x = xx;
int y = yy;
int u = 0;
while (u == x || u == y) u++;
int w = x ^ y ^ u;
for (int i = 0; i < n; i++) {
swap(x, u);
swap(y, w);
int s1 = 0, s2 = 0;
string p1, p2;
p1.resize(m);
p2.resize(m);
for (int j = 0; j < m; j++) {
swap(x, y);
p1[j] = ALP[x];
p2[j] = ALP[y];
if (p1[j] != a[i][j]) s1++;
if (p2[j] != a[i][j]) s2++;
}
if (s2 < s1) {
swap(s1, s2);
swap(p1, p2);
}
score += s1;
for (int j = 0; j < m; j++) cur[i][j] = p1[j];
}
if (score < bestScore) {
bestScore = score;
for (int i = 0; i < n; i++) ans[i] = cur[i];
}
score = 0;
x = xx;
y = yy;
u = 0;
while (u == x || u == y) u++;
w = x ^ y ^ u;
for (int i = 0; i < m; i++) {
swap(x, u);
swap(y, w);
int s1 = 0, s2 = 0;
string p1, p2;
p1.resize(n);
p2.resize(n);
for (int j = 0; j < n; j++) {
swap(x, y);
p1[j] = ALP[x];
p2[j] = ALP[y];
if (p1[j] != a[j][i]) s1++;
if (p2[j] != a[j][i]) s2++;
}
if (s2 < s1) {
swap(s1, s2);
swap(p1, p2);
}
score += s1;
for (int j = 0; j < n; j++) cur[j][i] = p1[j];
}
if (score < bestScore) {
bestScore = score;
for (int i = 0; i < n; i++) ans[i] = cur[i];
}
}
for (int i = 0; i < n; i++) cout << ans[i] << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, nn[300000][2][4], mm[300000][2][4], ans, sign, a1, a2, pp[300000];
char s[300000];
int trans(char c) {
if (c == 'A')
return 0;
else if (c == 'G')
return 1;
else if (c == 'C')
return 2;
else
return 3;
}
char tran2(int a) {
if (a == 0)
return 'A';
else if (a == 1)
return 'G';
else if (a == 2)
return 'C';
else
return 'T';
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%s", s + 1);
for (int j = 1; j <= m; j++) {
nn[i][j % 2][trans(s[j])]++;
mm[j][i % 2][trans(s[j])]++;
}
}
for (int i = 0; i <= 3; i++) {
for (int j = i + 1; j <= 3; j++) {
if (i == j) continue;
int k, l, sum = 0;
for (k = 0; k <= 3; k++) {
if (k != i && k != j) {
break;
}
}
for (l = k + 1; l <= 3; l++) {
if (l != i && l != j) {
break;
}
}
for (int q = 1; q <= n; q++) {
if (q % 2)
sum += max(nn[q][0][i] + nn[q][1][j], nn[q][1][i] + nn[q][0][j]);
else
sum += max(nn[q][0][k] + nn[q][1][l], nn[q][1][k] + nn[q][0][l]);
}
if (sum > ans) {
ans = sum;
sign = 0;
a1 = i;
a2 = j;
}
}
}
for (int i = 0; i <= 3; i++) {
for (int j = i + 1; j <= 3; j++) {
if (i == j) continue;
int k, l, sum = 0;
for (k = 0; k <= 3; k++) {
if (k != i && k != j) {
break;
}
}
for (l = k + 1; l <= 3; l++) {
if (l != i && l != j) {
break;
}
}
for (int q = 1; q <= m; q++) {
if (q % 2)
sum += max(mm[q][0][i] + mm[q][1][j], mm[q][1][i] + mm[q][0][j]);
else
sum += max(mm[q][0][k] + mm[q][1][l], mm[q][1][k] + mm[q][0][l]);
}
if (sum > ans) {
ans = sum;
sign = 1;
a1 = i;
a2 = j;
}
}
}
if (sign == 0) {
int i = a1, j = a2, k, l;
for (k = 0; k <= 3; k++) {
if (k != i && k != j) {
break;
}
}
for (l = k + 1; l <= 3; l++) {
if (l != i && l != j) {
break;
}
}
for (int q = 1; q <= n; q++) {
if (q % 2) {
if (nn[q][0][i] + nn[q][1][j] > nn[q][1][i] + nn[q][0][j])
a1 = j, a2 = i;
else
a1 = i, a2 = j;
} else {
if (nn[q][0][k] + nn[q][1][l] > nn[q][1][k] + nn[q][0][l])
a1 = l, a2 = k;
else
a1 = k, a2 = l;
}
for (int p = 1; p <= m; p++) {
if (p % 2) {
printf("%c", tran2(a1));
} else
printf("%c", tran2(a2));
}
printf("\n");
}
} else {
int i = a1, j = a2, k, l;
for (k = 0; k <= 3; k++) {
if (k != i && k != j) {
break;
}
}
for (l = k + 1; l <= 3; l++) {
if (l != i && l != j) {
break;
}
}
int rrr[4];
rrr[i] = j;
rrr[j] = i;
rrr[k] = l;
rrr[l] = k;
for (int p = 1; p <= m; p++) {
if (p % 2) {
if (mm[p][0][i] + mm[p][1][j] > mm[p][1][i] + mm[p][0][j])
pp[p] = j;
else
pp[p] = i;
} else {
if (mm[p][0][k] + mm[p][1][l] > mm[p][1][k] + mm[p][0][l])
pp[p] = l;
else
pp[p] = k;
}
}
for (int p = 1; p <= n; p++) {
if (p % 2)
for (int q = 1; q <= m; q++) printf("%c", tran2(pp[q]));
else
for (int q = 1; q <= m; q++) printf("%c", tran2(rrr[pp[q]]));
printf("\n");
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int N, M, ans;
vector<char> A[300009], B[300009];
char S[5] = "ACGT";
void ga(char a, char b, char c, char d) {
int cnt = 0;
vector<bool> chk(N, 0);
for (int i = 0; i < N; i++) {
int s1 = 0, s2 = 0;
for (int j = 0; j < M; j++) {
if (i % 2) {
if (A[i][j] != c && A[i][j] != d && (A[i][j] == a) ^ (j % 2)) ++s1;
if (A[i][j] != c && A[i][j] != d && (A[i][j] == b) ^ (j % 2)) ++s2;
} else {
if (A[i][j] != a && A[i][j] != b && (A[i][j] == c) ^ (j % 2)) ++s1;
if (A[i][j] != a && A[i][j] != b && (A[i][j] == d) ^ (j % 2)) ++s2;
}
}
cnt += max(s1, s2);
if (s1 > s2) chk[i] = 1;
}
if (ans < cnt) {
ans = cnt;
for (int i = 0; i < N; i++) {
if (chk[i]) {
for (int j = 0; j < M; j++) {
if (i % 2)
B[i][j] = j % 2 ? b : a;
else
B[i][j] = j % 2 ? d : c;
}
} else {
for (int j = 0; j < M; j++) {
if (i % 2)
B[i][j] = j % 2 ? a : b;
else
B[i][j] = j % 2 ? c : d;
}
}
}
}
}
void se(char a, char b, char c, char d) {
int cnt = 0;
vector<bool> chk(M, 0);
for (int i = 0; i < M; i++) {
int s1 = 0, s2 = 0;
for (int j = 0; j < N; j++) {
if (i % 2) {
if (A[j][i] != c && A[j][i] != d && (A[j][i] == a) ^ (j % 2)) ++s1;
if (A[j][i] != c && A[j][i] != d && (A[j][i] == b) ^ (j % 2)) ++s2;
} else {
if (A[j][i] != a && A[j][i] != b && (A[j][i] == c) ^ (j % 2)) ++s1;
if (A[j][i] != a && A[j][i] != b && (A[j][i] == d) ^ (j % 2)) ++s2;
}
}
cnt += max(s1, s2);
if (s1 > s2) chk[i] = 1;
}
if (ans < cnt) {
ans = cnt;
for (int i = 0; i < M; i++) {
if (chk[i]) {
for (int j = 0; j < N; j++) {
if (i % 2)
B[j][i] = j % 2 ? b : a;
else
B[j][i] = j % 2 ? d : c;
}
} else {
for (int j = 0; j < N; j++) {
if (i % 2)
B[j][i] = j % 2 ? a : b;
else
B[j][i] = j % 2 ? c : d;
}
}
}
}
}
int main() {
scanf("%d%d", &N, &M);
for (int i = 0; i < N; i++) {
A[i].resize(M);
B[i].resize(M);
for (int j = 0; j < M; j++) {
scanf(" %c", &A[i][j]);
}
}
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> st;
st.insert(a);
st.insert(b);
st.insert(c);
st.insert(d);
if ((int)st.size() == 4) {
ga(S[a], S[b], S[c], S[d]);
se(S[a], S[b], S[c], S[d]);
}
}
}
}
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
printf("%c", B[i][j]);
}
puts("");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<string> tab, rev;
unordered_map<string, string> inv;
inline string inversePattern(string pattern) {
if (inv.count(pattern)) return inv[pattern];
string ans = "ACTG";
ans.erase(ans.find(pattern[0]), 1);
ans.erase(ans.find(pattern[1]), 1);
return inv[pattern] = ans;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
tab.resize(n);
for (auto& row : tab) cin >> row;
rev.resize(m);
for (int i = 0; i < m; ++i) {
rev[i].resize(n);
for (int j = 0; j < n; ++j) rev[i][j] = tab[j][i];
}
int ans = 333333;
vector<bool> R;
string pattern;
bool mode = false;
bool optimal = false;
for (string pat : {"AC", "AG", "AT", "CG", "CT", "GT"}) {
for (auto table : {tab, rev}) {
string tmp = pat;
mode = !mode;
int diff = 0;
vector<bool> r;
for (auto row : table) {
int r1 = 0, r2 = 0;
for (int i = 0; i < (int)row.length(); ++i) {
if (row[i] != tmp[i & 1]) ++r1;
if (row[i] != tmp[~i & 1]) ++r2;
}
diff += min(r1, r2);
r.push_back(r1 > r2);
tmp = inversePattern(tmp);
}
if (diff < ans) {
ans = diff;
R = move(r);
pattern = pat;
optimal = mode;
}
}
}
if (optimal) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
tab[i][j] = pattern[(j & 1) ^ R[i]];
}
pattern = inversePattern(pattern);
}
} else {
for (int j = 0; j < m; ++j) {
for (int i = 0; i < n; ++i) {
tab[i][j] = pattern[(i & 1) ^ R[j]];
}
pattern = inversePattern(pattern);
}
}
for (auto row : tab) {
for (auto cell : row) {
cout << cell;
}
cout << '\n';
}
}
|
#include <bits/stdc++.h>
using namespace std;
int score = 1e9;
vector<string> ans;
bool af = false;
void go(const vector<char>& perm, const vector<string>& a, bool flip) {
int n = a.size();
int m = a[0].length();
vector<string> cand(n);
for (int i = 0; i < n; i++) {
cand[i] = a[i];
}
cand[0][0] = perm[0];
cand[0][1] = perm[1];
cand[1][0] = perm[2];
cand[1][1] = perm[3];
for (int i = 2; i < n; i++) {
cand[i][0] = cand[i - 2][0];
cand[i][1] = cand[i - 2][1];
}
for (int j = 2; j < m; j++) {
int sc1 = 0;
for (int i = 0; i < n; i++) {
if (a[i][j] != cand[i][j - 2]) {
sc1++;
}
}
int sc2 = 0;
for (int i = 0; i < n; i++) {
if (a[i][j] != cand[(i + 1) % 2][j - 2]) {
sc2++;
}
}
if (sc1 < sc2) {
for (int i = 0; i < n; i++) {
cand[i][j] = cand[i][j - 2];
}
} else {
for (int i = 0; i < n; i++) {
cand[i][j] = cand[(i + 1) % 2][j - 2];
}
}
}
int tot = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] != cand[i][j]) tot++;
}
}
if (tot < score) {
af = flip;
score = tot;
ans = cand;
}
}
int main() {
ios_base::sync_with_stdio(false);
cout.precision(12);
cout << fixed;
cin.tie(0);
int n, m;
cin >> n >> m;
vector<string> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<char> v;
v.push_back('A');
v.push_back('C');
v.push_back('G');
v.push_back('T');
do {
go(v, a, false);
} while (next_permutation(v.begin(), v.end()));
vector<string> a2(m);
for (int j = 0; j < m; j++) {
for (int i = 0; i < n; i++) {
a2[j] += a[i][j];
}
}
sort(v.begin(), v.end());
do {
go(v, a2, true);
} while (next_permutation(v.begin(), v.end()));
if (af) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << ans[j][i];
}
cout << "\n";
}
} else {
for (int i = 0; i < n; i++) {
cout << ans[i] << "\n";
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int score_row(vector<char>& row, char c1, char c2) {
int score1 = 0;
int score2 = 0;
for (int i = 0; i < row.size(); i++) {
if (i % 2 == 0) {
if (row[i] != c1) score1++;
if (row[i] != c2) score2++;
} else {
if (row[i] != c2) score1++;
if (row[i] != c1) score2++;
}
}
return min(score1, score2);
}
int score(vector<vector<char> >& grid, char c1, char c2, char c3, char c4) {
int n = grid.size();
int score = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
score += score_row(grid[i], c1, c2);
} else {
score += score_row(grid[i], c3, c4);
}
}
return score;
}
void make_best_row(vector<char>& row, char c1, char c2) {
int score1 = 0;
int score2 = 0;
for (int i = 0; i < row.size(); i++) {
if (i % 2 == 0) {
if (row[i] != c1) score1++;
if (row[i] != c2) score2++;
} else {
if (row[i] != c2) score1++;
if (row[i] != c1) score2++;
}
}
for (int i = 0; i < row.size(); i++) {
if (score1 < score2) {
if (i % 2 == 0)
row[i] = c1;
else
row[i] = c2;
} else {
if (i % 2 == 0)
row[i] = c2;
else
row[i] = c1;
}
}
}
void make_best(vector<vector<char> >& grid, char c1, char c2, char c3,
char c4) {
int n = grid.size();
int score = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
make_best_row(grid[i], c1, c2);
} else {
make_best_row(grid[i], c3, c4);
}
}
}
int score_grid(vector<vector<char> >& grid) {
int best_score = 400000;
string best = "****";
int cur_score;
cur_score = score(grid, 'A', 'C', 'G', 'T');
if (cur_score < best_score) {
best_score = cur_score;
best = "ACGT";
}
cur_score = score(grid, 'A', 'G', 'C', 'T');
if (cur_score < best_score) {
best_score = cur_score;
best = "AGCT";
}
cur_score = score(grid, 'A', 'T', 'C', 'G');
if (cur_score < best_score) {
best_score = cur_score;
best = "ATGC";
}
cur_score = score(grid, 'C', 'G', 'A', 'T');
if (cur_score < best_score) {
best_score = cur_score;
best = "CGAT";
}
cur_score = score(grid, 'C', 'T', 'A', 'G');
if (cur_score < best_score) {
best_score = cur_score;
best = "CTAG";
}
cur_score = score(grid, 'G', 'T', 'A', 'C');
if (cur_score < best_score) {
best_score = cur_score;
best = "GTAC";
}
make_best(grid, best[0], best[1], best[2], best[3]);
return best_score;
}
int main() {
int n, m;
cin >> n >> m;
vector<char> row(m);
vector<vector<char> > grid(n, row);
for (int i = 0; i < n; i++) {
string s;
cin >> s;
for (int j = 0; j < m; j++) grid[i][j] = s[j];
}
vector<char> trow(n);
vector<vector<char> > tgrid(m, trow);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) tgrid[j][i] = grid[i][j];
int score = score_grid(grid);
int tscore = score_grid(tgrid);
if (score < tscore) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) cout << grid[i][j];
cout << endl;
}
} else {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) cout << tgrid[j][i];
cout << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
void print(vector<T> s) {
for (auto x : s) cout << x << " ";
cout << endl;
}
int solve(vector<vector<char> > &s) {
int n = s.size(), m = s[0].size();
vector<char> c = {'A', 'G', 'T', 'C'};
int answer = 0;
for (int i = 0; i < 24; i++) {
int cnt = 0;
if (c[0] > c[1] || c[2] > c[3]) {
next_permutation(c.begin(), c.end());
continue;
}
for (int i = 0; i < n; i++) {
int cnt1 = 0, cnt2 = 0;
for (int j = 0; j < m; j++) {
if (s[i][j] == c[((i & 1) << 1) | (j & 1)]) cnt1++;
if (s[i][j] == c[((i & 1) << 1) | ((j & 1) ^ 1)]) cnt2++;
}
cnt += max(cnt1, cnt2);
}
answer = max(answer, cnt);
next_permutation(c.begin(), c.end());
}
for (int i = 0; i < 24; i++) {
int cnt = 0;
if (c[0] > c[1] || c[2] > c[3]) {
next_permutation(c.begin(), c.end());
continue;
}
for (int i = 0; i < n; i++) {
int cnt1 = 0, cnt2 = 0;
for (int j = 0; j < m; j++) {
if (s[i][j] == c[((i & 1) << 1) | (j & 1)]) cnt1++;
if (s[i][j] == c[((i & 1) << 1) | ((j & 1) ^ 1)]) cnt2++;
}
cnt += max(cnt1, cnt2);
}
if (answer == cnt) {
for (int i = 0; i < n; i++) {
int cnt1 = 0, cnt2 = 0;
for (int j = 0; j < m; j++) {
if (s[i][j] == c[((i & 1) << 1) | (j & 1)]) cnt1++;
if (s[i][j] == c[((i & 1) << 1) | ((j & 1) ^ 1)]) cnt2++;
}
if (cnt1 > cnt2)
for (int j = 0; j < m; j++) s[i][j] = c[((i & 1) << 1) | (j & 1)];
else
for (int j = 0; j < m; j++)
s[i][j] = c[((i & 1) << 1) | ((j & 1) ^ 1)];
}
return answer;
}
next_permutation(c.begin(), c.end());
}
assert(false);
return 0;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<string> s(n);
for (int i = 0; i < n; i++) cin >> s[i];
vector<vector<char> > s1(n, vector<char>(m));
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) s1[i][j] = s[i][j];
vector<vector<char> > s2(m, vector<char>(n));
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) s2[j][i] = s[i][j];
int answer1 = solve(s1);
int answer2 = solve(s2);
if (answer1 >= answer2) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) cout << s1[i][j];
cout << "\n";
}
} else {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) cout << s2[j][i];
cout << "\n";
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long N = 3e5 + 5;
int n, m;
string second[N];
string s[N];
int pas = N + 5;
string ans[N];
char ch[4] = {'A', 'C', 'G', 'T'};
int a[4];
int A[2];
int main() {
ios::sync_with_stdio(false);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> second[i];
second[i] = "#" + second[i];
s[i] = second[i];
ans[i] = second[i];
}
for (a[0] = 0; a[0] < 4; a[0]++)
for (a[1] = a[0] + 1; a[1] < 4; a[1]++)
for (a[2] = 0; a[2] < 4; a[2]++)
if (a[0] != a[2] && a[1] != a[2]) {
a[3] = 6 - a[0] - a[1] - a[2];
if (a[3] < a[2]) continue;
for (int I = 0; I < 4; I += 2) {
for (int i = 1 + I / 2; i <= n; i += 2) {
int x = 0, y = 0;
for (int j = 1; j <= m; j++) {
if (second[i][j] == ch[a[I + j % 2]]) x++;
if (second[i][j] == ch[a[I + 1 - j % 2]]) y++;
}
A[0] = a[I];
A[1] = a[I + 1];
if (x < y) swap(A[0], A[1]);
for (int j = 1; j <= m; j++) s[i][j] = ch[A[j % 2]];
}
}
int c = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (s[i][j] != second[i][j]) c++;
if (c < pas) {
pas = c;
for (int i = 1; i <= n; i++) ans[i] = s[i];
}
}
for (a[0] = 0; a[0] < 4; a[0]++)
for (a[1] = a[0] + 1; a[1] < 4; a[1]++)
for (a[2] = 0; a[2] < 4; a[2]++)
if (a[0] != a[2] && a[1] != a[2]) {
a[3] = 6 - a[0] - a[1] - a[2];
if (a[3] < a[2]) continue;
for (int I = 0; I < 4; I += 2) {
for (int i = 1 + I / 2; i <= m; i += 2) {
int x = 0, y = 0;
for (int j = 1; j <= n; j++) {
if (second[j][i] == ch[a[I + j % 2]]) x++;
if (second[j][i] == ch[a[I + 1 - j % 2]]) y++;
}
A[0] = a[I];
A[1] = a[I + 1];
if (x < y) swap(A[0], A[1]);
for (int j = 1; j <= n; j++) s[j][i] = ch[A[j % 2]];
}
}
int c = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (s[i][j] != second[i][j]) c++;
if (c < pas) {
pas = c;
for (int i = 1; i <= n; i++) ans[i] = s[i];
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) cout << ans[i][j];
cout << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int Maxn = 300005;
const string lets = "AGCT";
int n, m;
string B[Maxn];
char tmp[Maxn];
string R[Maxn];
int best = Maxn;
char ba, bb, bc, bd;
bool column;
void Read(string &s) {
scanf("%s", tmp);
s = tmp;
}
int checkCol(int c, char a, char b) {
int res = 0;
for (int i = 0; i < n; i++) {
res += B[i][c] != a;
swap(a, b);
}
return res;
}
int checkCols(char a, char b, char c, char d) {
int res = 0;
for (int j = 0; j < m; j++) {
res += min(checkCol(j, a, b), checkCol(j, b, a));
swap(a, c);
swap(b, d);
}
return res;
}
void writeCol(int c, char a, char b) {
for (int i = 0; i < n; i++) {
R[i][c] = a;
swap(a, b);
}
}
void writeCols(char a, char b, char c, char d) {
for (int j = 0; j < m; j++) {
if (checkCol(j, a, b) <= checkCol(j, b, a))
writeCol(j, a, b);
else
writeCol(j, b, a);
swap(a, c);
swap(b, d);
}
}
int checkRow(int r, char a, char b) {
int res = 0;
for (int j = 0; j < m; j++) {
res += B[r][j] != a;
swap(a, b);
}
return res;
}
int checkRows(char a, char b, char c, char d) {
int res = 0;
for (int i = 0; i < n; i++) {
res += min(checkRow(i, a, b), checkRow(i, b, a));
swap(a, c);
swap(b, d);
}
return res;
}
void writeRow(int r, char a, char b) {
for (int j = 0; j < m; j++) {
R[r][j] = a;
swap(a, b);
}
}
void writeRows(char a, char b, char c, char d) {
for (int i = 0; i < n; i++) {
if (checkRow(i, a, b) <= checkRow(i, b, a))
writeRow(i, a, b);
else
writeRow(i, b, a);
swap(a, c);
swap(b, d);
}
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++) {
Read(B[i]);
R[i].resize(m);
}
for (int i = 0; i < lets.length(); i++)
for (int j = i + 1; j < lets.length(); j++) {
int rem[2];
int rlen = 0;
for (int l = 0; l < lets.length(); l++)
if (l != i && l != j) rem[rlen++] = l;
char a = lets[i], b = lets[j];
char c = lets[rem[0]], d = lets[rem[1]];
int cand = checkCols(a, b, c, d);
if (cand < best) {
best = cand;
ba = a;
bb = b;
bc = c;
bd = d;
column = true;
}
cand = checkRows(a, b, c, d);
if (cand < best) {
best = cand;
ba = a;
bb = b;
bc = c;
bd = d;
column = false;
}
}
if (column)
writeCols(ba, bb, bc, bd);
else
writeRows(ba, bb, bc, bd);
for (int i = 0; i < n; i++) printf("%s\n", R[i].c_str());
return 0;
}
|
#include <bits/stdc++.h>
#pragma warning(disable : 4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;
int IT_MAX = 1 << 19;
const long long MOD = 1000000007;
const int INF = 0x3f3f3f3f;
const long long LL_INF = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1);
const double ERR = 1e-10;
vector<string> in;
char tus[300050];
vector<string> ans;
vector<string> u;
int mnp[300050];
int main() {
int N, M, i, j;
scanf("%d %d", &N, &M);
for (i = 0; i < N; i++) {
scanf("%s", tus);
in.emplace_back(tus);
}
ans = in;
u = in;
int mn = INF;
char ch[5] = "ACGT";
do {
int tot = 0;
for (int i = 0; i < N; i++) {
char c[2] = {ch[0], ch[1]};
if (i % 2 == 1) c[0] = ch[2], c[1] = ch[3];
int v = INF;
for (int j = 0; j < 2; j++) {
int s = 0;
for (int k = 0; k < M; k++) {
if (c[j ^ (k & 1)] != in[i][k]) s++;
}
if (v > s) {
v = s;
mnp[i] = j;
}
}
tot += v;
}
if (mn > tot) {
for (int i = 0; i < N; i++) {
char c[2] = {ch[0], ch[1]};
if (i % 2 == 1) c[0] = ch[2], c[1] = ch[3];
if (mnp[i]) swap(c[0], c[1]);
for (int j = 0; j < M; j++) ans[i][j] = c[j & 1];
}
mn = tot;
}
} while (next_permutation(ch, ch + 4));
do {
int tot = 0;
for (int i = 0; i < M; i++) {
char c[2] = {ch[0], ch[1]};
if (i % 2 == 1) c[0] = ch[2], c[1] = ch[3];
int v = INF;
for (int j = 0; j < 2; j++) {
int s = 0;
for (int k = 0; k < N; k++) {
if (c[j ^ (k & 1)] != in[k][i]) s++;
}
if (v > s) {
v = s;
mnp[i] = j;
}
}
tot += v;
}
if (mn > tot) {
for (int i = 0; i < M; i++) {
char c[2] = {ch[0], ch[1]};
if (i % 2 == 1) c[0] = ch[2], c[1] = ch[3];
if (mnp[i]) swap(c[0], c[1]);
for (int j = 0; j < N; j++) ans[j][i] = c[j & 1];
}
mn = tot;
}
} while (next_permutation(ch, ch + 4));
for (int i = 0; i < N; i++) printf("%s\n", ans[i].c_str());
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int vx[300000][4][4] = {}, vy[300000][4][4] = {};
int Index(char c) {
if (c == 'A') return 0;
if (c == 'C') return 1;
if (c == 'G') return 2;
return 3;
}
char NumToChar(int ind) {
if (ind == 0) return 'A';
if (ind == 1) return 'C';
if (ind == 2) return 'G';
return 'T';
}
vector<vector<char>> megT;
void Kiir(int n, int j, int a, int b) {
for (int i = 0; i < n; i++) {
if (i % 2 == 0)
megT[j][i] = NumToChar(a);
else
megT[j][i] = NumToChar(b);
}
return;
}
void Kiir2(int n, int j, int a, int b) {
for (int i = 0; i < n; i++) {
if (i % 2 == 0)
megT[i][j] = NumToChar(a);
else
megT[i][j] = NumToChar(b);
}
return;
}
int main() {
cin.sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<vector<char>> table(n, vector<char>(m));
megT.resize(n, vector<char>(m));
vector<int> v = {0, 1, 2, 3};
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) cin >> table[i][j];
for (int x = 0; x < 4; x++) {
for (int y = 0; y < 4; y++) {
if (x == y) continue;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (j % 2 == 0 && Index(table[i][j]) == x)
vx[i][x][y]++;
else if (j % 2 == 1 && Index(table[i][j]) == y)
vx[i][x][y]++;
}
}
}
}
for (int x = 0; x < 4; x++) {
for (int y = 0; y < 4; y++) {
if (x == y) continue;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (j % 2 == 0 && Index(table[j][i]) == x)
vy[i][x][y]++;
else if (j % 2 == 1 && Index(table[j][i]) == y)
vy[i][x][y]++;
}
}
}
}
vector<int> megVX, megVY;
int megX = -1, megY = -1;
do {
int akt = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0)
akt += max(vx[i][v[0]][v[1]], vx[i][v[1]][v[0]]);
else
akt += max(vx[i][v[2]][v[3]], vx[i][v[3]][v[2]]);
}
if (akt > megX) {
megVX = v;
megX = akt;
}
akt = 0;
for (int i = 0; i < m; i++) {
if (i % 2 == 0)
akt += max(vy[i][v[0]][v[1]], vy[i][v[1]][v[0]]);
else
akt += max(vy[i][v[2]][v[3]], vy[i][v[3]][v[2]]);
}
if (akt > megY) {
megVY = v;
megY = akt;
}
} while (next_permutation(v.begin(), v.end()));
if (megX >= megY) {
v = megVX;
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
if (vx[i][v[0]][v[1]] > vx[i][v[1]][v[0]])
Kiir(m, i, v[0], v[1]);
else
Kiir(m, i, v[1], v[0]);
} else {
if (vx[i][v[2]][v[3]] > vx[i][v[3]][v[2]])
Kiir(m, i, v[2], v[3]);
else
Kiir(m, i, v[3], v[2]);
}
}
} else {
v = megVY;
for (int i = 0; i < m; i++) {
if (i % 2 == 0) {
if (vy[i][v[0]][v[1]] > vy[i][v[1]][v[0]])
Kiir2(n, i, v[0], v[1]);
else
Kiir2(n, i, v[1], v[0]);
} else {
if (vy[i][v[2]][v[3]] > vy[i][v[3]][v[2]])
Kiir2(n, i, v[2], v[3]);
else
Kiir2(n, i, v[3], v[2]);
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) cout << megT[i][j];
cout << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const char D[] = "ACGT";
char c[600005], *s[600005];
int n, m, d[300], a[2][600005][4], b[600005][2][4], P[2][2], p[4], ans[4], A, N,
i, j, k;
int main() {
scanf("%d%d", &n, &m);
for (i = 0; i < n; i++) {
s[i] = c + i * (m + 1);
scanf("%s", s[i]);
}
d['A'] = 0;
d['C'] = 1;
d['G'] = 2;
d['T'] = 3;
for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
for (k = 0; k < 4; k++)
if (k != d[s[i][j]]) {
a[i & 1][j][k]++;
b[i][j & 1][k]++;
}
for (i = 0; i < 4; i++) p[i] = i;
N = n * m + 1;
do {
for (i = 0; i < 2; i++)
for (j = 0; j < 2; j++) P[i][j] = p[i << 1 | j];
for (j = k = 0; j < m; j++)
k += min(a[0][j][P[0][j & 1]] + a[1][j][P[1][j & 1]],
a[1][j][P[0][j & 1]] + a[0][j][P[1][j & 1]]);
if (k < N) {
N = k;
A = 0;
for (i = 0; i < 4; i++) ans[i] = p[i];
}
for (i = k = 0; i < n; i++)
k += min(b[i][0][P[i & 1][0]] + b[i][1][P[i & 1][1]],
b[i][1][P[i & 1][0]] + b[i][0][P[i & 1][1]]);
if (k < N) {
N = k;
A = 1;
for (i = 0; i < 4; i++) ans[i] = p[i];
}
} while (next_permutation(p, p + 4));
for (i = 0; i < 2; i++)
for (j = 0; j < 2; j++) P[i][j] = ans[i << 1 | j];
if (!A) {
for (j = 0; j < m; j++)
if (a[0][j][P[0][j & 1]] + a[1][j][P[1][j & 1]] <
a[1][j][P[0][j & 1]] + a[0][j][P[1][j & 1]]) {
s[0][j] = D[P[0][j & 1]];
s[1][j] = D[P[1][j & 1]];
} else {
s[0][j] = D[P[1][j & 1]];
s[1][j] = D[P[0][j & 1]];
}
for (i = 2; i < n; i++)
for (j = 0; j < m; j++) s[i][j] = s[i & 1][j];
} else {
for (i = 0; i < n; i++)
if (b[i][0][P[i & 1][0]] + b[i][1][P[i & 1][1]] <
b[i][1][P[i & 1][0]] + b[i][0][P[i & 1][1]]) {
s[i][0] = D[P[i & 1][0]];
s[i][1] = D[P[i & 1][1]];
} else {
s[i][0] = D[P[i & 1][1]];
s[i][1] = D[P[i & 1][0]];
}
for (i = 0; i < n; i++)
for (j = 2; j < m; j++) s[i][j] = s[i][j & 1];
}
for (i = 0; i < n; i++) puts(s[i]);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n, m;
cin >> n >> m;
vector<string> grid(n);
for (long long i = 0; i < n; ++i) {
cin >> grid[i];
}
string s = "AGCT";
vector<long long> perm = {0, 1, 2, 3};
long long resMinDiff = 1e9;
vector<string> res;
do {
long long sumDiff = 0;
vector<string> guess(n);
for (long long i = 0; i < n; ++i) {
long long add = (i % 2) * 2;
long long diff0 = 0;
long long diff1 = 0;
for (long long j = 0; j < m; ++j) {
if (grid[i][j] != s[perm[add + j % 2]]) {
diff0++;
}
}
for (long long j = 0; j < m; ++j) {
if (grid[i][j] != s[perm[add + (j + 1) % 2]]) {
diff1++;
}
}
sumDiff += min(diff0, diff1);
if (diff0 < diff1) {
string row = "";
for (long long j = 0; j < m; ++j) {
row += s[perm[add + j % 2]];
}
guess[i] = row;
} else {
string row = "";
for (long long j = 0; j < m; ++j) {
row += s[perm[add + (j + 1) % 2]];
}
guess[i] = row;
}
}
if (resMinDiff > sumDiff) {
resMinDiff = sumDiff;
res = guess;
}
sumDiff = 0;
for (long long j = 0; j < m; ++j) {
long long add = (j % 2) * 2;
long long diff0 = 0;
long long diff1 = 0;
for (long long i = 0; i < n; ++i) {
if (grid[i][j] != s[perm[add + i % 2]]) {
diff0++;
}
}
for (long long i = 0; i < n; ++i) {
if (grid[i][j] != s[perm[add + (i + 1) % 2]]) {
diff1++;
}
}
sumDiff += min(diff0, diff1);
if (diff0 < diff1) {
for (long long i = 0; i < n; ++i) {
guess[i][j] = s[perm[add + i % 2]];
}
} else {
for (long long i = 0; i < n; ++i) {
guess[i][j] = s[perm[add + (i + 1) % 2]];
}
}
}
if (resMinDiff > sumDiff) {
resMinDiff = sumDiff;
res = guess;
}
} while (next_permutation(perm.begin(), perm.end()));
for (long long i = 0; i < n; ++i) {
cout << res[i] << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 150005;
int mp[400], a[6], n, m, cnt, ans, f[6], type, t[2][4];
char ch[6], s[maxn];
vector<int> vc[maxn];
bool vis[6];
void calc(int o) {
if (o == 1) {
for (int i = 1; i <= n; i++) {
int u = i & 1, now, temp = 0, v = INT_MAX;
now = 1;
for (int j = 1; j <= m; j++) {
if (vc[i][j] != t[u][now]) temp++;
now++;
if (now > 2) now -= 2;
}
v = min(v, temp);
now = 2;
temp = 0;
for (int j = 1; j <= m; j++) {
if (vc[i][j] != t[u][now]) temp++;
now++;
if (now > 2) now -= 2;
}
v = min(v, temp);
cnt += v;
}
} else {
for (int i = 1; i <= m; i++) {
int u = i & 1, now, temp = 0, v = INT_MAX;
now = 1;
for (int j = 1; j <= n; j++) {
if (vc[j][i] != t[u][now]) temp++;
now++;
if (now > 2) now -= 2;
}
v = min(v, temp);
now = 2;
temp = 0;
for (int j = 1; j <= n; j++) {
if (vc[j][i] != t[u][now]) temp++;
now++;
if (now > 2) now -= 2;
}
v = min(v, temp);
cnt += v;
}
}
}
void dfs(int step) {
if (step == 4) {
t[0][1] = a[1];
t[0][2] = a[2];
t[1][1] = a[3];
t[1][2] = a[4];
cnt = 0;
calc(1);
if (cnt < ans) {
for (int i = 1; i <= 4; i++) f[i] = a[i];
type = 1;
ans = cnt;
}
cnt = 0;
calc(2);
if (cnt < ans) {
for (int i = 1; i <= 4; i++) f[i] = a[i];
type = 2;
ans = cnt;
}
return;
}
for (int i = 1; i <= 4; i++) {
if (vis[i]) continue;
vis[i] = true;
a[step + 1] = i;
dfs(step + 1);
vis[i] = false;
}
}
int main() {
mp['A'] = 1;
mp['G'] = 2;
mp['C'] = 3;
mp['T'] = 4;
ch[1] = 'A';
ch[2] = 'G';
ch[3] = 'C';
ch[4] = 'T';
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++) vc[i].push_back(0);
for (int i = 1; i <= n; i++) {
scanf("%s", s + 1);
for (int j = 1; j <= m; j++) vc[i].push_back(mp[s[j]]);
}
ans = 1e9;
dfs(0);
t[0][1] = f[1];
t[0][2] = f[2];
t[1][1] = f[3];
t[1][2] = f[4];
if (type == 1) {
for (int i = 1; i <= n; i++) {
int u = i & 1, now, ca = 0, cb = 0;
now = 1;
for (int j = 1; j <= m; j++) {
if (vc[i][j] != t[u][now]) ca++;
now++;
if (now > 2) now -= 2;
}
now = 2;
for (int j = 1; j <= m; j++) {
if (vc[i][j] != t[u][now]) cb++;
now++;
if (now > 2) now -= 2;
}
if (ca < cb)
now = 1;
else
now = 2;
for (int j = 1; j <= m; j++) {
vc[i][j] = t[u][now];
now++;
if (now > 2) now -= 2;
}
}
} else {
for (int i = 1; i <= m; i++) {
int u = i & 1, now, ca = 0, cb = 0;
now = 1;
for (int j = 1; j <= n; j++) {
if (vc[j][i] != t[u][now]) ca++;
now++;
if (now > 2) now -= 2;
}
now = 2;
for (int j = 1; j <= n; j++) {
if (vc[j][i] != t[u][now]) cb++;
now++;
if (now > 2) now -= 2;
}
if (ca < cb)
now = 1;
else
now = 2;
for (int j = 1; j <= n; j++) {
vc[j][i] = t[u][now];
now++;
if (now > 2) now -= 2;
}
}
}
for (int i = 1; i <= n; i++, puts(""))
for (int j = 1; j <= m; j++) printf("%c", ch[vc[i][j]]);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int m, n, bestCol[300300], bestRow[300300];
string a[300300];
int solveRow(string pattern) {
int res = 0;
for (int i = 0; i < m; i++) {
int cost[2] = {0};
for (int j = 0; j < n; j++)
for (int k = 0; k < 2; k++) {
char expectedChar = pattern[(i % 2) * 2 + (j % 2 ^ k)];
cost[k] += a[i][j] != expectedChar;
}
bestRow[i] = cost[1] < cost[0];
res += cost[bestRow[i]];
}
return res;
}
int solveCol(string pattern) {
int res = 0;
for (int j = 0; j < n; j++) {
int cost[2] = {0};
for (int i = 0; i < m; i++)
for (int k = 0; k < 2; k++) {
char expectedChar = pattern[(j % 2) * 2 + (i % 2 ^ k)];
cost[k] += a[i][j] != expectedChar;
}
bestCol[j] = cost[1] < cost[0];
res += cost[bestCol[j]];
}
return res;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> m >> n;
for (int i = 0; i < m; i++) cin >> a[i];
int ans = m * n + 1, direction = 0;
string pattern = "ACGT", bestPattern;
do {
int costRow = solveRow(pattern);
if (costRow < ans) {
ans = costRow;
bestPattern = pattern;
direction = 0;
}
int costCol = solveCol(pattern);
if (costCol < ans) {
ans = costCol;
bestPattern = pattern;
direction = 1;
}
} while (next_permutation(pattern.begin(), pattern.end()));
if (!direction) {
solveRow(bestPattern);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++)
cout << bestPattern[(i % 2) * 2 + (j % 2 ^ bestRow[i])];
cout << '\n';
}
} else {
solveCol(bestPattern);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++)
cout << bestPattern[(j % 2) * 2 + (i % 2 ^ bestCol[j])];
cout << '\n';
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
void rotate(vector<vector<char>> &grid, int &n, int &m) {
vector<vector<char>> mat(m, vector<char>(n));
for (int j = 0, a = 0; j < m; j++, a++)
for (int i = n - 1, b = 0; i >= 0; i--, b++) mat[a][b] = grid[i][j];
grid = mat;
swap(n, m);
}
int match(vector<char> &g, vector<char> &s) {
int cnt = 0;
for (int i = 0; i < g.size(); i++) cnt += (g[i] != s[i]);
return cnt;
}
void solve() {
int n, m;
cin >> n >> m;
vector<vector<char>> grid(n, vector<char>(m)), res(n, vector<char>(m));
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) cin >> grid[i][j];
string chs = "ACGT";
int mn = n * m + 1, r = 0;
for (int is_rotate = 0; is_rotate < 2; is_rotate++) {
for (int a = 1; a <= 3; a++) {
swap(chs[1], chs[a]);
vector<string> s(2);
s[0] = chs.substr(0, 2);
s[1] = chs.substr(2, 2);
for (int t = 0; t < 2; t++) {
vector<vector<char>> tmp = grid;
int op = 0;
for (int i = 0; i < n; i++) {
string ss = s[i % 2];
vector<char> c1(m), c2(m);
for (int id = 0; id < m; id++) c1[id] = ss[id % 2];
swap(ss[0], ss[1]);
for (int id = 0; id < m; id++) c2[id] = ss[id % 2];
int cnt1 = match(grid[i], c1);
int cnt2 = match(grid[i], c2);
if (cnt1 <= cnt2)
tmp[i] = c1;
else
tmp[i] = c2;
op += min(cnt1, cnt2);
}
if (op < mn) {
mn = op;
res = tmp;
r = is_rotate;
}
swap(s[0], s[1]);
}
}
rotate(grid, n, m);
}
if (r) {
rotate(grid, n, m);
rotate(res, n, m);
rotate(res, n, m);
rotate(res, n, m);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) cout << res[i][j];
cout << "\n";
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <class T, class T2>
inline int chkmax(T &x, const T2 &y) {
return x < y ? x = y, 1 : 0;
}
template <class T, class T2>
inline int chkmin(T &x, const T2 &y) {
return x > y ? x = y, 1 : 0;
}
const int MAXN = (int)3e5 + 42;
using table = vector<string>;
int n, m;
table s;
void read() {
cin >> n >> m;
s.resize(n);
for (int i = 0; i < n; i++) cin >> s[i];
}
string Sg = {'A', 'G', 'C', 'T'};
pair<int, table> answer;
int dp[MAXN][4][4];
pair<int, int> best[MAXN][4][4];
int rec1(int row, int last1, int last2) {
if (row == n) return 0;
int &memo = dp[row][last1][last2];
if (memo != -1) return memo;
vector<int> li;
for (int i = 0; i < 4; i++)
if (row == 0 || (i != last1 && i != last2)) li.push_back(i);
for (int i = 0; i < ((int)li.size()); i++)
for (int j = 0; j < ((int)li.size()); j++)
if (i != j) {
int cost = 0;
for (int c = 0; c < m; c++) {
if (!(c & 1))
if (Sg[li[i]] == s[row][c]) cost++;
if (c & 1)
if (Sg[li[j]] == s[row][c]) cost++;
}
if (chkmax(memo, cost + rec1(row + 1, li[i], li[j])))
best[row][last1][last2] = {li[i], li[j]};
}
return memo;
}
int rec2(int row, int last1, int last2) {
if (row == m) return 0;
int &memo = dp[row][last1][last2];
if (memo != -1) return memo;
vector<int> li;
for (int i = 0; i < 4; i++)
if (row == 0 || (i != last1 && i != last2)) li.push_back(i);
for (int i = 0; i < ((int)li.size()); i++)
for (int j = 0; j < ((int)li.size()); j++)
if (i != j) {
int cost = 0;
for (int c = 0; c < n; c++) {
if (!(c & 1))
if (Sg[li[i]] == s[c][row]) cost++;
if (c & 1)
if (Sg[li[j]] == s[c][row]) cost++;
}
if (chkmax(memo, cost + rec2(row + 1, li[i], li[j])))
best[row][last1][last2] = {li[i], li[j]};
}
return memo;
}
void solve1() {
memset(dp, -1, sizeof(dp));
int best_ans = rec1(0, 0, 0);
int row = 0, last1 = 0, last2 = 0;
table answer;
answer.assign(n, string(m, ' '));
while (row < n) {
rec1(row, last1, last2);
int nw_last1 = best[row][last1][last2].first,
nw_last2 = best[row][last1][last2].second;
last1 = nw_last1, last2 = nw_last2;
for (int c = 0; c < m; c++)
if (c & 1)
answer[row][c] = Sg[last2];
else
answer[row][c] = Sg[last1];
row++;
}
chkmax(::answer, make_pair(best_ans, answer));
}
void solve2() {
memset(dp, -1, sizeof(dp));
int best_ans = rec2(0, 0, 0);
int row = 0, last1 = 0, last2 = 0;
table answer;
answer.assign(n, string(m, ' '));
while (row < m) {
rec2(row, last1, last2);
int nw_last1 = best[row][last1][last2].first,
nw_last2 = best[row][last1][last2].second;
last1 = nw_last1, last2 = nw_last2;
for (int c = 0; c < n; c++)
if (c & 1)
answer[c][row] = Sg[last2];
else
answer[c][row] = Sg[last1];
row++;
}
chkmax(::answer, make_pair(best_ans, answer));
}
void solve() {
answer.first = -1;
solve1();
solve2();
for (int i = 0; i < n; i++) cout << answer.second[i] << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
read();
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 5;
const char lib[6][2] = {{'A', 'C'}, {'A', 'T'}, {'A', 'G'},
{'C', 'T'}, {'C', 'G'}, {'G', 'T'}};
int n, m;
int order[2][maxn][6], sum[2][maxn];
int op = 0, ch = 0;
string out[maxn];
string s[maxn];
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> s[i];
memset(sum, 0, sizeof(sum));
for (int i = 0; i < n; i++) {
for (int k = 0; k < 6; k++) {
int cnt1 = 0, cnt2 = 0;
for (int j = 0; j < m; j++) {
cnt1 += (s[i][j] != lib[(i & 1) ? 5 - k : k][j & 1]);
cnt2 += (s[i][j] != lib[(i & 1) ? 5 - k : k][(j & 1) ^ 1]);
}
order[0][i][k] = cnt1 < cnt2 ? 0 : 1;
sum[0][k] += min(cnt1, cnt2);
}
}
for (int j = 0; j < m; j++) {
for (int k = 0; k < 6; k++) {
int cnt1 = 0, cnt2 = 0;
for (int i = 0; i < n; i++) {
cnt1 += (s[i][j] != lib[(j & 1) ? 5 - k : k][i & 1]);
cnt2 += (s[i][j] != lib[(j & 1) ? 5 - k : k][(i & 1) ^ 1]);
}
order[1][j][k] = cnt1 < cnt2 ? 0 : 1;
sum[1][k] += min(cnt1, cnt2);
}
}
int plk = 0X3f3f3f3f;
for (int i = 0; i <= 1; i++)
for (int r = 0; r < 6; r++)
if (sum[i][r] < plk) {
plk = sum[i][r], op = i, ch = r;
}
for (int i = 0; i < n; i++) out[i] = "";
if (op == 0) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
out[i] += lib[(i & 1) ? 5 - ch : ch][(j & 1) ^ order[0][i][ch]];
}
} else {
for (int j = 0; j < m; j++) {
for (int i = 0; i < n; i++)
out[i] += lib[(j & 1) ? 5 - ch : ch][(i & 1) ^ order[1][j][ch]];
}
}
for (int i = 0; i < n; i++) cout << out[i] << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<vector<int>> G;
vector<int> row_cost[4][4], col_cost[4][4];
int best_cost, type;
string s1, s2;
int getval(char c) {
if (c == 'A') return 0;
if (c == 'C') return 1;
if (c == 'G') return 2;
return 3;
}
char getsym(int v) {
if (v == 0) return 'A';
if (v == 1) return 'C';
if (v == 2) return 'G';
return 'T';
}
void compute_row_cost(int r, int a, int b) {
for (int i = 0; i < m; i += 2)
if (G[r][i] != a) row_cost[a][b][r]++;
for (int i = 1; i < m; i += 2)
if (G[r][i] != b) row_cost[a][b][r]++;
}
void compute_col_cost(int c, int a, int b) {
for (int i = 0; i < n; i += 2)
if (G[i][c] != a) col_cost[a][b][c]++;
for (int i = 1; i < n; i += 2)
if (G[i][c] != b) col_cost[a][b][c]++;
}
void check_best_row(int a, int b, int c, int d) {
int cost = 0;
string t1, t2;
t1.resize(m);
t2.resize(m);
for (int i = 0; i < m; ++i) {
if (i % 2 == 0) {
cost += min(col_cost[a][b][i], col_cost[b][a][i]);
if (col_cost[a][b][i] <= col_cost[b][a][i]) {
t1[i] = getsym(a);
t2[i] = getsym(b);
} else {
t1[i] = getsym(b);
t2[i] = getsym(a);
}
} else {
cost += min(col_cost[c][d][i], col_cost[d][c][i]);
if (col_cost[c][d][i] <= col_cost[d][c][i]) {
t1[i] = getsym(c);
t2[i] = getsym(d);
} else {
t1[i] = getsym(d);
t2[i] = getsym(c);
}
}
}
if (cost < best_cost) {
s1 = t1;
s2 = t2;
best_cost = cost;
type = 0;
}
}
void check_best_col(int a, int b, int c, int d) {
int cost = 0;
string t1, t2;
t1.resize(n);
t2.resize(n);
for (int i = 0; i < n; ++i) {
if (i % 2 == 0) {
cost += min(row_cost[a][b][i], row_cost[b][a][i]);
if (row_cost[a][b][i] <= row_cost[b][a][i]) {
t1[i] = getsym(a);
t2[i] = getsym(b);
} else {
t1[i] = getsym(b);
t2[i] = getsym(a);
}
} else {
cost += min(row_cost[c][d][i], row_cost[d][c][i]);
if (row_cost[c][d][i] <= row_cost[d][c][i]) {
t1[i] = getsym(c);
t2[i] = getsym(d);
} else {
t1[i] = getsym(d);
t2[i] = getsym(c);
}
}
}
if (cost < best_cost) {
s1 = t1;
s2 = t2;
best_cost = cost;
type = 1;
}
}
int main() {
best_cost = 1e9;
cin >> n >> m;
G.resize(n);
for (int i = 0; i < n; ++i) G[i].resize(m);
for (int a = 0; a < 4; ++a)
for (int b = 0; b < 4; ++b)
if (a != b) row_cost[a][b].resize(n), col_cost[a][b].resize(m);
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j) {
char c;
cin >> c;
G[i][j] = getval(c);
}
for (int i = 0; i < n; ++i)
for (int a = 0; a < 4; ++a)
for (int b = 0; b < 4; ++b)
if (a != b) compute_row_cost(i, a, b);
for (int i = 0; i < m; ++i)
for (int a = 0; a < 4; ++a)
for (int b = 0; b < 4; ++b)
if (a != b) compute_col_cost(i, a, b);
for (int a = 0; a < 4; ++a)
for (int b = a + 1; b < 4; ++b) {
int c, d;
for (int i = 0; i < 4; ++i)
if (i != a and i != b) {
c = i;
break;
}
for (int i = 0; i < 4; ++i)
if (i != a and i != b and i != c) {
d = i;
break;
}
check_best_row(a, b, c, d);
check_best_col(a, b, c, d);
}
if (type == 0) {
for (int i = 0; i < n; ++i)
if (i % 2 == 0)
cout << s1 << "\n";
else
cout << s2 << "\n";
} else {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j)
if (j % 2 == 0)
cout << s1[i];
else
cout << s2[i];
cout << "\n";
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
using ld = long double;
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
vector<vector<char> > mapa;
vector<vector<char> > sol_mapa;
vector<vector<char> > aux_mapa;
vector<vector<char> > tmp_mapa;
vector<char> arr;
const char letras[4] = {'A', 'C', 'G', 'T'};
void init(int n, int m) {
mapa.resize(n + 2, vector<char>(m + 2));
sol_mapa.resize(n + 2, vector<char>(m + 2));
aux_mapa.resize(n + 2, vector<char>(m + 2));
tmp_mapa.resize(n + 2, vector<char>(m + 2));
}
int calcula(int n, int a, int b) {
int v1 = 0, v2 = 0;
for (int i = 1; i <= n; i++) {
if (i & 1) {
if (arr[i] != letras[a]) v1++;
if (arr[i] != letras[b]) v2++;
} else {
if (arr[i] != letras[b]) v1++;
if (arr[i] != letras[a]) v2++;
}
}
if (v2 < v1) swap(a, b);
for (int i = 1; i <= n; i++) {
if (i & 1)
arr[i] = letras[a];
else
arr[i] = letras[b];
}
return min(v1, v2);
}
int resuelve_filas(int n, int m, int a, int b, int c, int d) {
int val = 0;
arr.resize(m + 2);
for (int i = 1; i <= n; i += 2) {
for (int j = 1; j <= m; j++) arr[j] = mapa[i][j];
val += calcula(m, a, b);
for (int j = 1; j <= m; j++) aux_mapa[i][j] = arr[j];
}
for (int i = 2; i <= n; i += 2) {
for (int j = 1; j <= m; j++) arr[j] = mapa[i][j];
val += calcula(m, c, d);
for (int j = 1; j <= m; j++) aux_mapa[i][j] = arr[j];
}
return val;
}
int resuelve_columnas(int n, int m, int a, int b, int c, int d) {
int val = 0;
arr.resize(n + 2);
for (int j = 1; j <= m; j += 2) {
for (int i = 1; i <= n; i++) arr[i] = mapa[i][j];
val += calcula(n, a, b);
for (int i = 1; i <= n; i++) tmp_mapa[i][j] = arr[i];
}
for (int j = 2; j <= m; j += 2) {
for (int i = 1; i <= n; i++) arr[i] = mapa[i][j];
val += calcula(n, c, d);
for (int i = 1; i <= n; i++) tmp_mapa[i][j] = arr[i];
}
return val;
}
int resuelve(int n, int m, int a, int b, int c, int d) {
int v1 = resuelve_filas(n, m, a, b, c, d);
int v2 = resuelve_columnas(n, m, a, b, c, d);
if (v2 < v1) swap(tmp_mapa, aux_mapa);
return min(v1, v2);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
init(n, m);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> mapa[i][j];
}
}
int res = n * m + 1;
vi acomodo(4);
iota(acomodo.begin(), acomodo.end(), 0);
do {
int a = acomodo[0];
int b = acomodo[1];
int c = acomodo[2];
int d = acomodo[3];
int aux = resuelve(n, m, a, b, c, d);
if (aux < res) {
res = aux;
swap(aux_mapa, sol_mapa);
}
} while (next_permutation(acomodo.begin(), acomodo.end()));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cout << sol_mapa[i][j];
}
cout << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
const int INF = 0x3f3f3f3f;
const std::string str = "ATGC";
int main() {
int n, m;
std::vector<std::string> s;
std::vector<std::string> tmp;
std::vector<std::string> ans;
int min_ans = INF, pos;
std::cin >> n >> m;
for (int i = 0; i < n; ++i) {
std::string st;
std::cin >> st;
s.push_back(st);
}
for (int c1 = 0; c1 < 4; ++c1)
for (int c2 = c1 + 1; c2 < 4; ++c2) {
std::string stri;
std::set<int> st;
for (int i = 0; i < 4; ++i) st.insert(i);
st.erase(c1);
st.erase(c2);
auto it = st.begin();
stri.push_back(str[c1]);
stri.push_back(str[c2]);
stri.push_back(str[*(it++)]);
stri.push_back(str[*it]);
pos = 0;
tmp.clear();
for (int i = 0; i < n; ++i) {
tmp.push_back("");
int bit = (i & 1) << 1;
int cnt1 = 0, cnt2 = 0;
for (int j = 0; j < m; ++j) {
if (s[i][j] != stri[bit + (j & 1)]) ++cnt1;
if (s[i][j] != stri[bit + ((j & 1) ^ 1)]) ++cnt2;
}
if (cnt1 < cnt2)
for (int j = 0; j < m; ++j) tmp[i].push_back(stri[bit + (j & 1)]);
else
for (int j = 0; j < m; ++j)
tmp[i].push_back(stri[bit + ((j & 1) ^ 1)]);
pos += std::min(cnt1, cnt2);
}
if (pos < min_ans) {
ans = tmp;
min_ans = pos;
}
pos = 0;
tmp.clear();
for (int i = 0; i < n; ++i) tmp.push_back("");
for (int j = 0; j < m; ++j) {
int bit = (j & 1) << 1;
int cnt1 = 0, cnt2 = 0;
for (int i = 0; i < n; ++i) {
if (s[i][j] != stri[bit + (i & 1)]) ++cnt1;
if (s[i][j] != stri[bit + ((i & 1) ^ 1)]) ++cnt2;
}
if (cnt1 < cnt2)
for (int i = 0; i < n; ++i) tmp[i].push_back(stri[bit + (i & 1)]);
else
for (int i = 0; i < n; ++i)
tmp[i].push_back(stri[bit + ((i & 1) ^ 1)]);
pos += std::min(cnt1, cnt2);
}
if (pos < min_ans) {
ans = tmp;
min_ans = pos;
}
}
for (int i = 0; i < n; ++i) std::cout << ans[i] << std::endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
cin >> n >> m;
vector<vector<int>> table(n, vector<int>(m));
for (int i = 0; i < n; i++) {
string str;
cin >> str;
for (int j = 0; j < m; j++) {
if (str[j] == 'A') {
table[i][j] = 0;
} else if (str[j] == 'G') {
table[i][j] = 1;
} else if (str[j] == 'C') {
table[i][j] = 2;
} else {
table[i][j] = 3;
}
}
}
vector<int> gu(2), ki(2);
long long ecost = 1LL << 60, fcost = 1LL << 60;
vector<int> gug(2), kik(2), ccc(n), cccc(n);
vector<int> gugg(2), kikk(2), ddd(m), dddd(m);
for (int i = 0; i < 4; i++) {
for (int j = i + 1; j < 4; j++) {
if (i == j) continue;
gu[0] = i, gu[1] = j;
int k = 0, l = 0;
for (; k == i || k == j; k++) {
}
for (; l == i || l == j || l == k; l++) {
}
ki[0] = k, ki[1] = l;
long long dcost = 0;
for (int a = 0; a < n; a++) {
long long mincost = 1LL << 60;
for (int c = 0; c < 2; c++) {
long long ccost = 0;
for (int b = 0; b < m; b++)
if (a % 2 == 0) {
if (table[a][b] != gu[(b + c) % 2]) {
ccost++;
}
} else {
if (table[a][b] != ki[(b + c) % 2]) {
ccost++;
}
}
if (mincost > ccost) {
ccc[a] = c;
}
mincost = min(mincost, ccost);
}
dcost += mincost;
}
if (ecost > dcost) {
gug = gu;
kik = ki;
ecost = dcost;
cccc = ccc;
}
long long gcost = 0;
for (int a = 0; a < m; a++) {
long long mincost = 1LL << 60;
for (int c = 0; c < 2; c++) {
long long ccost = 0;
for (int b = 0; b < n; b++)
if (a % 2 == 0) {
if (table[b][a] != gu[(b + c) % 2]) {
ccost++;
}
} else {
if (table[b][a] != ki[(b + c) % 2]) {
ccost++;
}
}
if (mincost > ccost) {
ddd[a] = c;
}
mincost = min(mincost, ccost);
}
gcost += mincost;
}
if (fcost > gcost) {
gugg = gu;
kikk = ki;
fcost = gcost;
dddd = ddd;
}
}
}
vector<char> tai(4);
tai[0] = 'A';
tai[1] = 'G';
tai[2] = 'C';
tai[3] = 'T';
if (ecost < fcost) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (i % 2 == 0) {
cout << tai[gug[(j + cccc[i]) % 2]];
} else {
cout << tai[kik[(j + cccc[i]) % 2]];
}
}
cout << "\n";
}
} else {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (j % 2 == 0) {
cout << tai[gugg[(i + dddd[j]) % 2]];
} else {
cout << tai[kikk[(i + dddd[j]) % 2]];
}
}
cout << "\n";
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int minMatrixDiff = INT_MAX;
vector<string> v, ans;
void Transpose(vector<string> &v) {
int n = v.size(), m = n > 0 ? v[0].size() : 0;
vector<string> u;
for (int y = 0; y < m; ++y) {
string s;
for (int x = 0; x < n; ++x) {
s.push_back(v[x][y]);
}
u.push_back(s);
}
v.clear();
for (string ux : u) {
v.push_back(ux);
}
}
string AlternateString(int n, char a, char b) {
string s;
for (int i = 0; i < n; ++i, swap(a, b)) {
s.push_back(a);
}
return s;
}
int Diff(string &s, string &t) {
int ans = 0;
for (int i = 0; i < s.size(); ++i) {
ans += (s[i] != t[i]);
}
return ans;
}
void CheckAndUpdate(string s, bool transpose) {
int n = v.size(), m = n > 0 ? v[0].size() : 0;
int matrixDiff = 0;
vector<string> u;
for (int x = 0; x < n; ++x) {
char a = s[2 * (x % 2 != 0)];
char b = s[1 + 2 * (x % 2 != 0)];
int minRowDiff = INT_MAX;
string ux;
for (int k = 0; k < 2; ++k, swap(a, b)) {
string t = AlternateString(m, a, b);
int rowDiff = Diff(t, v[x]);
if (rowDiff < minRowDiff) {
minRowDiff = rowDiff;
ux = t;
}
}
matrixDiff += minRowDiff;
u.push_back(ux);
}
if (matrixDiff < minMatrixDiff) {
minMatrixDiff = matrixDiff;
if (transpose) {
Transpose(u);
}
ans = u;
}
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 0; i < n; ++i) {
string s;
cin >> s;
v.push_back(s);
}
for (int k = 0; k < 2; ++k) {
CheckAndUpdate("ACGT", k);
CheckAndUpdate("AGCT", k);
CheckAndUpdate("ATCG", k);
CheckAndUpdate("CGAT", k);
CheckAndUpdate("CTAG", k);
CheckAndUpdate("GTCA", k);
Transpose(v);
}
for (int x = 0; x < n; ++x) {
cout << ans[x] << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
string mp[300005], smp[300005], fin[300005];
int ans, sum, n, m;
string s[5];
int cnt[300005][2][5];
int change[300];
void calc(int a, int b, int c, int d) {
for (int i = 0; i < n; i++) smp[i] = "";
for (int j = 0; j < m; j++) {
if (j % 2 == 0) {
if (cnt[j][0][a] + cnt[j][1][b] > cnt[j][0][b] + cnt[j][1][a])
smp[0] += s[a], smp[1] += s[b];
else
smp[0] += s[b], smp[1] += s[a];
} else {
if (cnt[j][0][c] + cnt[j][1][d] > cnt[j][0][d] + cnt[j][1][c])
smp[0] += s[c], smp[1] += s[d];
else
smp[0] += s[d], smp[1] += s[c];
}
}
for (int i = 2; i < n; i++) {
smp[i] = smp[i - 2];
}
int sum = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (mp[i][j] != smp[i][j]) sum++;
if (sum < ans) {
for (int i = 0; i < n; i++) fin[i] = smp[i];
ans = sum;
}
}
void calc2(int a, int b, int c, int d) {
for (int i = 0; i < n; i++) smp[i] = "";
for (int j = 0; j < n; j++) {
if (j % 2 == 0) {
if (cnt[j][0][a] + cnt[j][1][b] > cnt[j][0][b] + cnt[j][1][a])
smp[j] += s[a], smp[j] += s[b];
else
smp[j] += s[b], smp[j] += s[a];
} else {
if (cnt[j][0][c] + cnt[j][1][d] > cnt[j][0][d] + cnt[j][1][c])
smp[j] += s[c], smp[j] += s[d];
else
smp[j] += s[d], smp[j] += s[c];
}
}
for (int i = 0; i < n; i++) {
for (int j = 2; j < m; j++) smp[i] += smp[i][j - 2];
}
int sum = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (mp[i][j] != smp[i][j]) sum++;
if (sum < ans) {
for (int i = 0; i < n; i++) fin[i] = smp[i];
ans = sum;
}
}
int main() {
change['A'] = 1, change['T'] = 2, change['G'] = 3, change['C'] = 4;
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) {
cin >> mp[i];
for (int j = 0; j < m; j++) cnt[j][i % 2][change[mp[i][j]]]++;
}
s[1] = s[2] = s[3] = s[4] = "";
s[1] += "A", s[2] += "T", s[3] += "G", s[4] += "C";
ans = 1e8;
int a[5] = {1, 2, 3, 4};
do {
calc(a[0], a[1], a[2], a[3]);
} while (next_permutation(a, a + 4));
memset(cnt, 0, sizeof(cnt));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) cnt[i][j % 2][change[mp[i][j]]]++;
}
do {
calc2(a[0], a[1], a[2], a[3]);
} while (next_permutation(a, a + 4));
for (int i = 0; i < n; i++) cout << fin[i] << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
inline long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
int month[15] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int dir[8][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1},
{-1, -1}, {1, 1}, {1, -1}, {-1, 1}};
const int MAXN = 3e5 + 5;
const int INF = 1 << 30;
const long long mod = 1e9 + 7;
const double eps = 1e-8;
const long long inff = 0x3f3f3f3f3f3f3f3f;
int n, m, ans = mod, s[3];
char c, d[5] = {'A', 'G', 'C', 'T'};
vector<char> q[MAXN];
void solve(char s1, char s2, int t) {
char s3 = ' ', s4;
for (int i = 0; i <= 3; i++) {
if (d[i] != s1 && d[i] != s2 && s3 == ' ') s3 = d[i];
if (d[i] != s1 && d[i] != s2 && s3 != ' ') s4 = d[i];
}
int sum = 0;
if (t == 1) {
for (int i = 0; i <= n - 1; i++) {
int cn[3] = {0, 0};
char cc[3];
if (i % 2)
cc[0] = s1, cc[1] = s2;
else
cc[0] = s3, cc[1] = s4;
for (int j = 0; j <= m - 1; j++) {
int k = j % 2, kk = !k;
if (q[i][j] != cc[k]) cn[0]++;
if (q[i][j] != cc[kk]) cn[1]++;
}
sum += min(cn[0], cn[1]);
}
}
if (t == 2) {
for (int i = 0; i <= m - 1; i++) {
int cn[3] = {0, 0};
char cc[3];
if (i % 2)
cc[0] = s1, cc[1] = s2;
else
cc[0] = s3, cc[1] = s4;
for (int j = 0; j <= n - 1; j++) {
int k = j % 2, kk = !k;
if (q[j][i] != cc[k]) cn[0]++;
if (q[j][i] != cc[kk]) cn[1]++;
}
sum += min(cn[0], cn[1]);
}
}
if (t != 0) {
if (sum < ans) ans = sum, s[0] = s1, s[1] = s2, s[2] = t;
} else {
t = s[2];
if (t == 1) {
for (int i = 0; i <= n - 1; i++) {
int cn[3] = {0, 0};
char cc[3];
if (i % 2)
cc[0] = s1, cc[1] = s2;
else
cc[0] = s3, cc[1] = s4;
for (int j = 0; j <= m - 1; j++) {
int k = j % 2, kk = !k;
if (q[i][j] != cc[k]) cn[0]++;
if (q[i][j] != cc[kk]) cn[1]++;
}
if (cn[0] < cn[1]) {
for (int j = 0; j <= m - 1; j++) {
int k = j % 2;
q[i][j] = cc[k];
}
} else {
for (int j = 0; j <= m - 1; j++) {
int k = !(j % 2);
q[i][j] = cc[k];
}
}
}
}
if (t == 2) {
for (int i = 0; i <= m - 1; i++) {
int cn[3] = {0, 0};
char cc[3];
if (i % 2)
cc[0] = s1, cc[1] = s2;
else
cc[0] = s3, cc[1] = s4;
for (int j = 0; j <= n - 1; j++) {
int k = j % 2, kk = !k;
if (q[j][i] != cc[k]) cn[0]++;
if (q[j][i] != cc[kk]) cn[1]++;
}
if (cn[0] < cn[1]) {
for (int j = 0; j <= n - 1; j++) {
int k = j % 2;
q[j][i] = cc[k];
}
} else {
for (int j = 0; j <= n - 1; j++) {
int k = !(j % 2);
q[j][i] = cc[k];
}
}
}
}
for (int i = 0; i <= n - 1; i++) {
for (int j = 0; j <= m - 1; j++) printf("%c", q[i][j]);
printf("\n");
}
}
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i <= n - 1; i++) {
getchar();
for (int j = 1; j <= m; j++) {
scanf("%c", &c);
q[i].push_back(c);
}
}
for (int i = 0; i <= 2; i++)
for (int j = i + 1; j <= 3; j++) solve(d[i], d[j], 1);
for (int i = 0; i <= 2; i++)
for (int j = i + 1; j <= 3; j++) solve(d[i], d[j], 2);
solve(s[0], s[1], 0);
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, arr[1000000 + 5], cek[1000000 + 5], res[1000000 + 5];
char temp;
inline int conv(char x) {
if (x == 'A')
return 0;
else if (x == 'G')
return 1;
else if (x == 'C')
return 2;
else
return 3;
}
inline char rconv(int x) {
if (x == 0)
return 'A';
else if (x == 1)
return 'G';
else if (x == 2)
return 'C';
else
return 'T';
}
inline bool eval(int a, int b, int c, int d) {
int x[4] = {a, b, c, d};
sort(x, x + 4);
for (int i = 0; i < 3; i++) {
if (x[i] == x[i + 1]) return false;
}
return true;
}
void printBoard() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
printf("%c", rconv(res[i * m + j]));
}
printf("\n");
}
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++) {
getchar();
for (int j = 0; j < m; j++) {
scanf("%c", &temp);
arr[i * m + j] = conv(temp);
}
}
int xam = n * m + 5;
for (int i = 0; i < (1 << 8); i++) {
int tempx = i;
int a[4];
for (int j = 0; j < 4; j++) {
a[j] = tempx % 4;
tempx /= 4;
}
if (!eval(a[0], a[1], a[2], a[3])) continue;
int val = 0;
for (int j = 0; j < n; j++) {
for (int k = 0; k < m; k++) {
cek[j * m + k] = a[(j % 2) * 2 + k % 2];
if (cek[j * m + k] != arr[j * m + k]) val++;
}
}
bool flag = true;
for (int j = 0; j < n - 1 && flag; j++) {
for (int k = 0; k < m - 1 && flag; k++) {
flag &= eval(cek[j * m + k], cek[j * m + (k + 1)], cek[(j + 1) * m + k],
cek[(j + 1) * m + (k + 1)]);
}
}
if (flag and xam > val) {
xam = val;
for (int j = 0; j < n; j++) {
for (int k = 0; k < m; k++) {
res[j * m + k] = cek[j * m + k];
}
}
}
val = 0;
for (int j = 0; j < n; j++) {
int v1 = 0;
int v2 = 0;
for (int k = 0; k < m; k++) {
if (a[((k % 2) * 2 + j) % 4] == arr[j * m + k])
v1++;
else if (a[(((k + 1) % 2) * 2 + j) % 4] == arr[j * m + k])
v2++;
}
if (v1 > v2) {
for (int k = 0; k < m; k++) {
cek[j * m + k] = a[((k % 2) * 2 + j) % 4];
if (cek[j * m + k] != arr[j * m + k]) val++;
}
} else {
for (int k = 0; k < m; k++) {
cek[j * m + k] = a[(((k + 1) % 2) * 2 + j) % 4];
if (cek[j * m + k] != arr[j * m + k]) val++;
}
}
}
flag = true;
for (int j = 0; j < n - 1 && flag; j++) {
for (int k = 0; k < m - 1 && flag; k++) {
flag &= eval(cek[j * m + k], cek[j * m + (k + 1)], cek[(j + 1) * m + k],
cek[(j + 1) * m + (k + 1)]);
}
}
if (flag and xam > val) {
xam = val;
for (int j = 0; j < n; j++) {
for (int k = 0; k < m; k++) {
res[j * m + k] = cek[j * m + k];
}
}
}
val = 0;
for (int k = 0; k < m; k++) {
int v1 = 0;
int v2 = 0;
for (int j = 0; j < n; j++) {
if (a[((j % 2) * 2 + k) % 4] == arr[j * m + k])
v1++;
else if (a[(((j + 1) % 2) * 2 + k) % 4] == arr[j * m + k])
v2++;
}
if (v1 > v2) {
for (int j = 0; j < n; j++) {
cek[j * m + k] = a[((j % 2) * 2 + k) % 4];
if (cek[j * m + k] != arr[j * m + k]) val++;
}
} else {
for (int j = 0; j < n; j++) {
cek[j * m + k] = a[(((j + 1) % 2) * 2 + k) % 4];
if (cek[j * m + k] != arr[j * m + k]) val++;
}
}
}
flag = true;
for (int j = 0; j < n - 1 && flag; j++) {
for (int k = 0; k < m - 1 && flag; k++) {
flag &= eval(cek[j * m + k], cek[j * m + (k + 1)], cek[(j + 1) * m + k],
cek[(j + 1) * m + (k + 1)]);
}
}
if (flag and xam > val) {
xam = val;
for (int j = 0; j < n; j++) {
for (int k = 0; k < m; k++) {
res[j * m + k] = cek[j * m + k];
}
}
}
}
printBoard();
}
|
#include <bits/stdc++.h>
const long long MAX_N = 3e5 + 10;
const long long MOD = 1e9 + 7;
using namespace std;
vector<int> t[MAX_N];
vector<int> table[MAX_N];
vector<int> temp[MAX_N];
int a[10];
int n, m;
void out(int x) {
if (x == 1)
cout << 'A';
else if (x == 2)
cout << 'T';
else if (x == 3)
cout << 'C';
else
cout << 'G';
}
void update() {
for (int i = 1; i <= n; ++i) {
table[i].clear();
for (auto x : temp[i]) table[i].push_back(x);
}
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
string s;
cin >> s;
for (int j = 0; j < m; ++j) {
if (s[j] == 'A')
t[i].push_back(1);
else if (s[j] == 'T')
t[i].push_back(2);
else if (s[j] == 'C')
t[i].push_back(3);
else
t[i].push_back(4);
}
}
cout << "\n";
for (int i = 1; i <= 4; ++i) a[i - 1] = i;
long long ans = 1e18;
do {
long long sum = 0;
for (int i = 1; i <= n; ++i) temp[i].clear();
for (int i = 1; i <= n; ++i) {
int now1, now2;
int sm;
int c1 = 0, c2 = 0;
if (i % 2) {
sm = a[0] + a[1];
now1 = a[0];
now2 = a[1];
} else {
sm = a[2] + a[3];
now1 = a[2];
now2 = a[3];
}
for (int j = 0; j < m; ++j) {
c1 += (now1 != t[i][j]);
c2 += (now2 != t[i][j]);
now1 = sm - now1;
now2 = sm - now2;
}
if (c1 < c2) {
if (i % 2)
now1 = a[0];
else
now1 = a[2];
for (int j = 0; j < m; ++j) {
temp[i].push_back(now1);
now1 = sm - now1;
}
} else {
if (i % 2)
now1 = a[1];
else
now1 = a[3];
for (int j = 0; j < m; ++j) {
temp[i].push_back(now1);
now1 = sm - now1;
}
}
sum += min(c1, c2);
}
if (sum < ans) {
ans = sum;
update();
}
for (int i = 1; i <= n; ++i) temp[i].clear();
sum = 0;
for (int i = 0; i < m; ++i) {
int now1, now2;
int sm;
int c1 = 0, c2 = 0;
if (i % 2) {
sm = a[0] + a[1];
now1 = a[0];
now2 = a[1];
} else {
sm = a[2] + a[3];
now1 = a[2];
now2 = a[3];
}
for (int j = 1; j <= n; ++j) {
c1 += (now1 != t[j][i]);
c2 += (now2 != t[j][i]);
now1 = sm - now1;
now2 = sm - now2;
}
if (c1 < c2) {
if (i % 2)
now1 = a[0];
else
now1 = a[2];
for (int j = 1; j <= n; ++j) {
temp[j].push_back(now1);
now1 = sm - now1;
}
} else {
if (i % 2)
now1 = a[1];
else
now1 = a[3];
for (int j = 1; j <= n; ++j) {
temp[j].push_back(now1);
now1 = sm - now1;
}
}
sum += min(c1, c2);
}
if (sum < ans) {
ans = sum;
update();
}
} while (next_permutation(a, a + 4));
for (int i = 1; i <= n; ++i) {
for (auto x : table[i]) out(x);
cout << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m;
int a[300005], save[300005], ans[300005];
string s;
void Input() {
cin >> n >> m;
for (int i = 0; i < (n); ++i) {
cin >> s;
for (int j = 0; j < (m); ++j)
if (s[j] == 'G')
a[i * m + j] = 1;
else if (s[j] == 'C')
a[i * m + j] = 2;
else if (s[j] == 'T')
a[i * m + j] = 3;
}
}
int CalcRow(int i, int A, int B) {
int res = 0;
for (int j = 0; j < (m); ++j)
res += (a[i * m + j] != (save[i * m + j] = (j & 1 ? A : B)));
return res;
}
int CalcCol(int j, int A, int B) {
int res = 0;
for (int i = 0; i < (n); ++i)
res += (a[i * m + j] != (save[i * m + j] = (i & 1 ? A : B)));
return res;
}
void Save() {
for (int i = 0; i < (n * m); ++i) ans[i] = save[i];
}
void Solve() {
int res = n * m + 1;
for (int A = 0; A < (4); ++A)
for (int B = 0; B < (A); ++B) {
int C, D;
for (int x = 0; x < (4); ++x)
if (x != A && x != B) C = x;
D = 6 - A - B - C;
int cur = 0;
for (int i = 0; i < (n); ++i) {
if (i & 1) {
int tmp = CalcRow(i, A, B);
int tmp2 = CalcRow(i, B, A);
if (tmp >= tmp2)
cur += tmp2;
else
cur += CalcRow(i, A, B);
} else {
int tmp = CalcRow(i, C, D);
int tmp2 = CalcRow(i, D, C);
if (tmp >= tmp2)
cur += tmp2;
else
cur += CalcRow(i, C, D);
}
}
if (cur < res) res = cur, Save();
cur = 0;
for (int i = 0; i < (m); ++i) {
if (i & 1) {
int tmp = CalcCol(i, A, B);
int tmp2 = CalcCol(i, B, A);
if (tmp >= tmp2)
cur += tmp2;
else
cur += CalcCol(i, A, B);
} else {
int tmp = CalcCol(i, C, D);
int tmp2 = CalcCol(i, D, C);
if (tmp >= tmp2)
cur += tmp2;
else
cur += CalcCol(i, C, D);
}
}
if (cur < res) res = cur, Save();
}
for (int i = 0; i < (n); ++i) {
for (int j = 0; j < (m); ++j)
cout << (ans[i * m + j] ? ans[i * m + j] == 1 ? 'G'
: ans[i * m + j] == 2 ? 'C'
: 'T'
: 'A');
cout << '\n';
}
cerr << res << '\n';
}
int main() {
iostream::sync_with_stdio(false);
cin.tie(0);
Input();
Solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 6;
string aux[N], s[N], sol[N], auxs[N];
int n, m;
bool f[4];
int tr[130], dp[N][4][4][3];
char inv[4];
int solve() {
for (int i = 0; i < n; ++i) {
for (int l = 0; l < 4; ++l) {
for (int k = l + 1; k < 4; ++k) {
f[l] = 1;
f[k] = 1;
int fs(-1), ss(-1);
for (int j = 0; j < 4; ++j) {
if (!f[j]) {
if (fs == -1)
fs = j;
else
ss = j;
}
}
assert(fs >= 0);
assert(ss >= 0);
f[l] = 0;
f[k] = 0;
int a(0), b(0);
for (int j = 0; j < m; ++j) {
if (tr[s[i][j] - 'A'] != l) {
if (j & 1)
++b;
else
++a;
}
if (tr[s[i][j] - 'A'] != k) {
if (j & 1)
++a;
else
++b;
}
}
if (i) {
dp[i][l][k][1] = a + dp[i - 1][fs][ss][0];
dp[i][l][k][2] = b + dp[i - 1][fs][ss][0];
} else {
dp[i][l][k][1] = a;
dp[i][l][k][2] = b;
}
dp[i][l][k][0] = min(dp[i][l][k][1], dp[i][l][k][2]);
}
}
}
int cur(1e9), a(-1), b(-1);
for (int l = 0; l < 4; ++l) {
for (int k = l + 1; k < 4; ++k) {
if (dp[n - 1][l][k][0] < cur) {
cur = dp[n - 1][l][k][0];
a = l;
b = k;
}
}
}
for (int i = 0; i < n; ++i) sol[i].resize(m);
if (dp[n - 1][a][b][0] == dp[n - 1][a][b][1]) {
for (int j = 0; j < m; ++j) {
if (j & 1)
sol[n - 1][j] = inv[b];
else
sol[n - 1][j] = inv[a];
}
} else {
for (int j = 0; j < m; ++j) {
if (j & 1)
sol[n - 1][j] = inv[a];
else
sol[n - 1][j] = inv[b];
}
}
for (int i = n - 2; i >= 0; --i) {
f[a] = 1;
f[b] = 1;
int fs(-1), ss(-1);
for (int j = 0; j < 4; ++j) {
if (!f[j]) {
if (fs == -1)
fs = j;
else
ss = j;
}
}
assert(fs >= 0);
assert(ss >= 0);
f[a] = 0;
f[b] = 0;
if (dp[i][fs][ss][0] == dp[i][fs][ss][1]) {
for (int j = 0; j < m; ++j) {
if (j & 1)
sol[i][j] = inv[ss];
else
sol[i][j] = inv[fs];
}
} else {
for (int j = 0; j < m; ++j) {
if (j & 1)
sol[i][j] = inv[fs];
else
sol[i][j] = inv[ss];
}
}
a = fs;
b = ss;
}
return cur;
}
void spin() {
for (int i = 0; i < m; ++i) aux[i].resize(n);
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j) aux[j][i] = s[i][j];
swap(n, m);
for (int i = 0; i < n; ++i) s[i] = aux[i];
}
int main() {
tr['G' - 'A'] = 1;
tr['C' - 'A'] = 2;
tr['T' - 'A'] = 3;
inv[0] = 'A';
inv[1] = 'G';
inv[2] = 'C';
inv[3] = 'T';
cin >> n >> m;
for (int i = 0; i < n; ++i) cin >> s[i];
int ans = solve();
for (int i = 0; i < n; ++i) auxs[i] = sol[i];
spin();
int ans2 = solve();
if (ans < ans2) {
swap(n, m);
for (int i = 0; i < n; ++i) s[i] = auxs[i];
} else {
for (int i = 0; i < n; ++i) s[i] = sol[i];
spin();
}
for (int i = 0; i < n; ++i) cout << s[i] << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
string str[200000 + 1];
int N, M;
char c[2][2];
char alpha[4] = {'A', 'C', 'G', 'T'};
int t[4] = {0, 1, 2, 3};
string ans[200000 + 1], now[200000 + 1];
int acnt = -1, ncnt = 0, cnt1 = 0, cnt2;
int main() {
scanf("%d%d", &N, &M);
for (int i = 0; i < N; i++) {
cin >> str[i];
ans[i] = now[i] = str[i];
}
for (int k = 1; k <= 24; k++) {
for (int ii = 0; ii < 2; ii++) {
for (int jj = 0; jj < 2; jj++) {
c[ii][jj] = alpha[t[ii * 2 + jj]];
}
}
ncnt = 0;
for (int i = 0; i < N; i++) {
cnt1 = 0;
cnt2 = 0;
for (int j = 0; j < M; j++) {
if (str[i][j] == c[i % 2][j % 2]) {
cnt1++;
}
if (str[i][j] == c[i % 2][(j + 1) % 2]) {
cnt2++;
}
}
if (cnt1 > cnt2) {
for (int j = 0; j < M; j++) {
now[i][j] = c[i % 2][j % 2];
}
ncnt += cnt1;
} else {
for (int j = 0; j < M; j++) {
now[i][j] = c[i % 2][(j + 1) % 2];
}
ncnt += cnt2;
}
}
if (ncnt > acnt) {
acnt = ncnt;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
ans[i][j] = now[i][j];
}
}
}
ncnt = 0;
for (int j = 0; j < M; j++) {
cnt1 = 0;
cnt2 = 0;
for (int i = 0; i < N; i++) {
if (str[i][j] == c[i % 2][j % 2]) {
cnt1++;
}
if (str[i][j] == c[(i + 1) % 2][j % 2]) {
cnt2++;
}
}
if (cnt1 > cnt2) {
for (int i = 0; i < N; i++) {
now[i][j] = c[i % 2][j % 2];
}
ncnt += cnt1;
} else {
for (int i = 0; i < N; i++) {
now[i][j] = c[(i + 1) % 2][j % 2];
}
ncnt += cnt2;
}
}
if (ncnt > acnt) {
acnt = ncnt;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
ans[i][j] = now[i][j];
}
}
}
next_permutation(t, t + 4);
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
printf("%c", ans[i][j]);
}
printf("\n");
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000;
vector<pair<char, char> > fv, v;
bool is_r;
int num[26][2];
void f(char a, char b, char &c, char &d) {
c = 0;
char x[] = {'A', 'T', 'C', 'G'};
for (int i = 0; i < 4; i++)
if (x[i] != a && x[i] != b) {
if (c)
d = x[i];
else
c = x[i];
}
}
int main() {
int n, m;
char *s;
is_r = 1;
scanf("%d%d", &n, &m);
s = (char *)malloc(sizeof(char) * m * n);
getchar();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) scanf("%c", s + i * m + j);
getchar();
}
char x[] = {'A', 'T', 'C', 'G'};
int res = 0, tmp;
for (int i = 0; i < 3; i++)
for (int j = i + 1; j < 4; j++) {
tmp = 0;
for (int r = 0; r < n; r++) {
for (int c = 0; c < m; c++) {
char now = *(s + r * m + c);
num[now - 'A'][c & 1]++;
}
if (r & 1) {
char c1, c2;
f(x[i], x[j], c1, c2);
if (num[c1 - 'A'][0] + num[c2 - 'A'][1] >
num[c1 - 'A'][1] + num[c2 - 'A'][0]) {
tmp += num[c1 - 'A'][0] + num[c2 - 'A'][1];
v.push_back(pair<char, char>(c1, c2));
} else {
tmp += num[c1 - 'A'][1] + num[c2 - 'A'][0];
v.push_back(pair<char, char>(c2, c1));
}
} else {
if (num[x[i] - 'A'][0] + num[x[j] - 'A'][1] >
num[x[i] - 'A'][1] + num[x[j] - 'A'][0]) {
tmp += num[x[i] - 'A'][0] + num[x[j] - 'A'][1];
v.push_back(pair<char, char>(x[i], x[j]));
} else {
tmp += num[x[i] - 'A'][1] + num[x[j] - 'A'][0];
v.push_back(pair<char, char>(x[j], x[i]));
}
}
memset(num, 0, sizeof(num));
}
if (res < tmp) {
is_r = 1;
res = tmp;
fv = v;
}
v.clear();
tmp = 0;
for (int c = 0; c < m; c++) {
for (int r = 0; r < n; r++) {
char now = *(s + r * m + c);
num[now - 'A'][r & 1]++;
}
if (c & 1) {
char c1, c2;
f(x[i], x[j], c1, c2);
if (num[c1 - 'A'][0] + num[c2 - 'A'][1] >
num[c1 - 'A'][1] + num[c2 - 'A'][0]) {
tmp += num[c1 - 'A'][0] + num[c2 - 'A'][1];
v.push_back(pair<char, char>(c1, c2));
} else {
tmp += num[c1 - 'A'][1] + num[c2 - 'A'][0];
v.push_back(pair<char, char>(c2, c1));
}
} else {
if (num[x[i] - 'A'][0] + num[x[j] - 'A'][1] >
num[x[i] - 'A'][1] + num[x[j] - 'A'][0]) {
tmp += num[x[i] - 'A'][0] + num[x[j] - 'A'][1];
v.push_back(pair<char, char>(x[i], x[j]));
} else {
tmp += num[x[i] - 'A'][1] + num[x[j] - 'A'][0];
v.push_back(pair<char, char>(x[j], x[i]));
}
}
memset(num, 0, sizeof(num));
}
if (res < tmp) {
is_r = 0;
res = tmp;
fv = v;
}
v.clear();
res = max(res, tmp);
}
if (is_r) {
for (int r = 0; r < n; r++) {
for (int j = 0; j < m / 2; j++) {
putchar(fv[r].first);
putchar(fv[r].second);
}
if (m & 1) putchar(fv[r].first);
putchar('\n');
}
} else {
for (int c = 0; c < m; c++) {
for (int r = 0; r < n / 2; r++) {
int now = 2 * r * m + c;
*(s + now) = fv[c].first;
*(s + now + m) = fv[c].second;
}
if (n & 1) *(s + (n - 1) * m + c) = fv[c].first;
}
for (int r = 0; r < n; r++) {
for (int c = 0; c < m; c++) putchar(*(s + r * m + c));
putchar('\n');
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long int N = 1e5 + 5;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int n, m;
cin >> n >> m;
string a;
string s[n];
for (long long int i = 0; i < n; ++i) {
cin >> s[i];
}
a += 'A';
a += 'C';
a += 'G';
a += 'T';
long long int mn = N * N;
string f[n];
for (long long int i = 0; i < 24; ++i) {
long long int ans = 0;
string tp[n];
for (long long int j = 0; j < n; ++j) {
for (long long int k = 0; k < m; ++k) {
tp[j] += 'a';
}
}
for (long long int j = 0; j < m; ++j) {
long long int c1 = 0, c2 = 0;
string tp1, tp2;
for (long long int k = 0; k < n; ++k) {
if (j % 2 == 0) {
if (k % 2 == 0) {
tp1 += a[0];
tp2 += a[2];
if (s[k][j] != a[0]) c1++;
if (s[k][j] != a[2]) c2++;
} else {
tp1 += a[2];
tp2 += a[0];
if (s[k][j] != a[2]) c1++;
if (s[k][j] != a[0]) c2++;
}
} else {
if (k % 2 == 0) {
tp1 += a[1];
tp2 += a[3];
if (s[k][j] != a[1]) c1++;
if (s[k][j] != a[3]) c2++;
} else {
tp1 += a[3];
tp2 += a[1];
if (s[k][j] != a[3]) c1++;
if (s[k][j] != a[1]) c2++;
}
}
}
if (c1 < c2) {
for (long long int k = 0; k < n; ++k) {
tp[k][j] = tp1[k];
}
} else {
for (long long int k = 0; k < n; ++k) {
tp[k][j] = tp2[k];
}
}
ans += min(c1, c2);
}
if (ans < mn) {
for (long long int j = 0; j < n; ++j) f[j] = tp[j];
}
mn = min(ans, mn);
next_permutation(a.begin(), a.end());
}
for (long long int i = 0; i < 24; ++i) {
long long int ans = 0;
string tp[n];
for (long long int j = 0; j < n; ++j) {
long long int c1 = 0, c2 = 0;
string tp1, tp2;
for (long long int k = 0; k < m; ++k) {
if (j % 2 == 0) {
if (k % 2 == 0) {
tp1 += a[0];
tp2 += a[1];
if (s[j][k] != a[0]) c1++;
if (s[j][k] != a[1]) c2++;
} else {
tp1 += a[1];
tp2 += a[0];
if (s[j][k] != a[1]) c1++;
if (s[j][k] != a[0]) c2++;
}
} else {
if (k % 2 == 0) {
tp1 += a[2];
tp2 += a[3];
if (s[j][k] != a[2]) c1++;
if (s[j][k] != a[3]) c2++;
} else {
tp1 += a[3];
tp2 += a[2];
if (s[j][k] != a[3]) c1++;
if (s[j][k] != a[2]) c2++;
}
}
}
if (c1 < c2) {
tp[j] = tp1;
} else {
tp[j] = tp2;
}
ans += min(c1, c2);
}
if (ans < mn) {
for (long long int j = 0; j < n; ++j) {
f[j] = tp[j];
}
}
mn = min(ans, mn);
next_permutation(a.begin(), a.end());
}
for (long long int i = 0; i < n; ++i) {
cout << f[i] << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read_to_vector(size_t N, std::vector<T> &v) {
for (size_t i = 0; i < N; i++) {
T tmp;
std::cin >> tmp;
v.push_back(tmp);
}
}
template <typename T>
void print_vector(const std::vector<T> &v) {
for (auto it = v.begin(); it < v.end(); it++) {
if (it != v.begin()) std::cout << " ";
std::cout << (*it);
}
}
long long n, m;
vector<string> t;
string xx[12] = {"AG", "AC", "AT", "GC", "GT", "CT",
"GA", "CA", "TA", "CG", "TG", "TC"};
vector<vector<long long> > row_xx(12, vector<long long>(1 << 20));
vector<vector<long long> > col_xx(12, vector<long long>(1 << 20));
vector<string> getbest_col(int x) {
int xs[2] = {x, 5 - x};
vector<string> res(n, string(m, '0'));
for (int i = 0; i < m; i++) {
string tothis;
int xt = xs[i & 1];
if (col_xx[xt][i] < col_xx[xt + 6][i]) {
tothis = xx[xt];
} else {
tothis = xx[xt + 6];
}
for (int j = 0; j < n; j++) {
res[j][i] = tothis[j & 1];
}
}
return res;
}
vector<string> getbest_row(int x) {
int xs[2] = {x, 5 - x};
vector<string> res(n, string(m, '0'));
for (int i = 0; i < n; i++) {
string tothis;
int xt = xs[i & 1];
if (row_xx[xt][i] < row_xx[xt + 6][i]) {
tothis = xx[xt];
} else {
tothis = xx[xt + 6];
}
for (int j = 0; j < m; j++) {
res[i][j] = tothis[j & 1];
}
}
return res;
}
long long eval(vector<string> &tt) {
long long res = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
res += (tt[i][j] != t[i][j]);
}
}
return res;
}
void solve() {
cin >> n >> m;
read_to_vector(n, t);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
for (int x = 0; x < 12; x++) {
row_xx[x][i] += (t[i][j] != xx[x][j & 1]);
col_xx[x][j] += (t[i][j] != xx[x][i & 1]);
}
}
}
vector<string> bestt = getbest_row(0);
long long bestval = eval(bestt);
for (int x = 0; x < 6; x++) {
auto t0 = getbest_row(x);
auto val0 = eval(t0);
if (val0 < bestval) {
bestt = t0;
bestval = val0;
}
}
for (int x = 0; x < 6; x++) {
auto t0 = getbest_col(x);
auto val0 = eval(t0);
if (val0 < bestval) {
bestt = t0;
bestval = val0;
}
}
for (auto s : bestt) {
cout << s << "\n";
}
return;
}
void pre_compute() {}
int main() {
std::cout << std::setprecision(15);
std::ios::sync_with_stdio(false);
std::cin.tie(0);
pre_compute();
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<string> vec_splitter(string s) {
for (char &c : s) c = c == ',' ? ' ' : c;
stringstream ss;
ss << s;
vector<string> res;
for (string z; ss >> z; res.push_back(z))
;
return res;
}
void debug_out(vector<string> args, int idx) { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, Head H, Tail... T) {
if (idx > 0) cerr << ", ";
stringstream ss;
ss << H;
cerr << args[idx] << " = " << ss.str();
debug_out(args, idx + 1, T...);
}
void localTest() {}
const long long inf = 1e9;
long long n, m;
vector<string> A;
string fin;
void rowSolve(string a, string b, long long &ans) {
string curr = "-" + a + b;
long long cost = 0;
for (long long i = 0; i < n; ++i) {
long long c1 = 0, c2 = 0;
for (long long j = 0; j < m; ++j) {
c1 += (A[i][j] != a[j & 1]);
c2 += (A[i][j] != a[(j + 1) & 1]);
}
cost += min(c1, c2);
swap(a, b);
}
if (cost < ans) {
ans = cost;
fin = curr;
}
return;
}
void colSolve(string a, string b, long long &ans) {
string curr = "|" + a + b;
long long cost = 0;
for (long long i = 0; i < m; ++i) {
long long c1 = 0, c2 = 0;
for (long long j = 0; j < n; ++j) {
c1 += (A[j][i] != a[j & 1]);
c2 += (A[j][i] != a[(j + 1) & 1]);
}
cost += min(c1, c2);
swap(a, b);
}
if (cost < ans) {
ans = cost;
fin = curr;
}
return;
}
void solve() {
string a = fin.substr(1, 2), b = fin.substr(3, 2);
if (fin[0] == '-') {
for (long long i = 0; i < n; ++i) {
long long c1 = 0, c2 = 0;
for (long long j = 0; j < m; ++j) {
c1 += (A[i][j] != a[j & 1]);
c2 += (A[i][j] != a[(j + 1) & 1]);
}
if (c2 < c1) {
swap(a[0], a[1]);
}
for (long long j = 0; j < m; ++j) {
A[i][j] = a[j & 1];
}
if (c2 < c1) {
swap(a[0], a[1]);
}
swap(a, b);
}
} else {
for (long long i = 0; i < m; ++i) {
long long c1 = 0, c2 = 0;
for (long long j = 0; j < n; ++j) {
c1 += (A[j][i] != a[j & 1]);
c2 += (A[j][i] != a[(j + 1) & 1]);
}
if (c2 < c1) {
swap(a[0], a[1]);
}
for (long long j = 0; j < n; ++j) {
A[j][i] = a[j & 1];
}
if (c2 < c1) {
swap(a[0], a[1]);
}
swap(a, b);
}
}
}
int main() {
localTest();
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
long long ans = inf;
for (long long i = 0; i < n; ++i) {
string s;
cin >> s;
A.push_back(s);
}
string perm = "ACGT";
do {
string a = perm.substr(0, 2), b = perm.substr(2, 2);
rowSolve(a, b, ans);
colSolve(a, b, ans);
} while (next_permutation((perm).begin(), (perm).end()));
solve();
for (long long i = 0; i < n; ++i) {
cout << A[i] << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000000 + 7;
int n, m;
string s[300000];
char c[4] = {'T', 'G', 'C', 'A'};
string Ans[300000];
int typ[300000];
bool type = 0;
string t[300000];
int main() {
ios_base::sync_with_stdio(0);
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> s[i];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) t[i] += s[j][i];
}
int ans = 0;
for (int mask = 0; mask < (1 << 4); mask++) {
vector<int> bity0, bity1;
if (__builtin_popcount(mask) != 2) continue;
for (int i = 0; i < 4; i++)
if (mask & (1 << i))
bity0.push_back(i);
else
bity1.push_back(i);
int i00 = bity0[0], i01 = bity0[1], i10 = bity1[0], i11 = bity1[1];
string frstType, scndType, thrdType, frthType;
for (int j = 0; j < m; j++) {
if (j & 1) {
frstType += c[i00];
scndType += c[i01];
thrdType += c[i10];
frthType += c[i11];
} else {
frstType += c[i01];
scndType += c[i00];
thrdType += c[i11];
frthType += c[i10];
}
}
int curAns = 0;
for (int i = 0; i < n; i++) {
int cnt1 = 0, cnt2 = 0;
if (i & 1) {
for (int j = 0; j < m; j++) {
cnt1 += frstType[j] == s[i][j];
cnt2 += scndType[j] == s[i][j];
}
curAns += max(cnt1, cnt2);
if (cnt1 >= cnt2)
typ[i] = 0;
else
typ[i] = 1;
} else {
for (int j = 0; j < m; j++) {
cnt1 += thrdType[j] == s[i][j];
cnt2 += frthType[j] == s[i][j];
}
curAns += max(cnt1, cnt2);
if (cnt1 >= cnt2)
typ[i] = 2;
else
typ[i] = 3;
}
}
if (curAns > ans) {
ans = curAns;
for (int i = 0; i < n; i++) {
if (typ[i] == 0) Ans[i] = frstType;
if (typ[i] == 1) Ans[i] = scndType;
if (typ[i] == 2) Ans[i] = thrdType;
if (typ[i] == 3) Ans[i] = frthType;
}
}
}
swap(n, m);
for (int mask = 0; mask < (1 << 4); mask++) {
vector<int> bity0, bity1;
if (__builtin_popcount(mask) != 2) continue;
for (int i = 0; i < 4; i++)
if (mask & (1 << i))
bity0.push_back(i);
else
bity1.push_back(i);
int i00 = bity0[0], i01 = bity0[1], i10 = bity1[0], i11 = bity1[1];
string frstType, scndType, thrdType, frthType;
for (int j = 0; j < m; j++) {
if (j & 1) {
frstType += c[i00];
scndType += c[i01];
thrdType += c[i10];
frthType += c[i11];
} else {
frstType += c[i01];
scndType += c[i00];
thrdType += c[i11];
frthType += c[i10];
}
}
int curAns = 0;
for (int i = 0; i < n; i++) {
int cnt1 = 0, cnt2 = 0;
if (i & 1) {
for (int j = 0; j < m; j++) {
cnt1 += frstType[j] == t[i][j];
cnt2 += scndType[j] == t[i][j];
}
curAns += max(cnt1, cnt2);
if (cnt1 >= cnt2)
typ[i] = 0;
else
typ[i] = 1;
} else {
for (int j = 0; j < m; j++) {
cnt1 += thrdType[j] == t[i][j];
cnt2 += frthType[j] == t[i][j];
}
curAns += max(cnt1, cnt2);
if (cnt1 >= cnt2)
typ[i] = 2;
else
typ[i] = 3;
}
}
if (curAns > ans) {
type = 1;
ans = curAns;
for (int i = 0; i < n; i++) {
if (typ[i] == 0) Ans[i] = frstType;
if (typ[i] == 1) Ans[i] = scndType;
if (typ[i] == 2) Ans[i] = thrdType;
if (typ[i] == 3) Ans[i] = frthType;
}
}
}
swap(n, m);
if (type == 0) {
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;
static inline void canhazfast() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
}
template <typename T>
T gcd(T a, T b) {
return b ? gcd(b, a % b) : a;
}
template <typename T>
T extgcd(T a, T b, T &x, T &y) {
T x0 = 1, y0 = 0, x1 = 0, y1 = 1;
while (b) {
T q = a / b;
a %= b;
swap(a, b);
x0 -= q * x1;
swap(x0, x1);
y0 -= q * y1;
swap(y0, y1);
}
x = x0;
y = y0;
return a;
}
int ctz(unsigned x) { return __builtin_ctz(x); }
int ctzll(unsigned long long x) { return __builtin_ctzll(x); }
int clz(unsigned x) { return __builtin_clz(x); }
int clzll(unsigned long long x) { return __builtin_clzll(x); }
int popcnt(unsigned x) { return __builtin_popcount(x); }
int popcntll(unsigned long long x) { return __builtin_popcountll(x); }
int bsr(unsigned x) { return 31 ^ clz(x); }
int bsrll(unsigned long long x) { return 63 ^ clzll(x); }
vector<string> a, b, ans;
int calc_rows(int n, int m, string s) {
int res = 0;
for (int i = 0; i < n; ++i) {
int c[2] = {};
int p = i % 2 * 2;
int w;
for (int j = 0; j < m; ++j) {
for (int k : {0, 1}) c[k] += (a[i][j] != s[p ^ (j % 2) ^ k]);
}
if (c[0] < c[1])
w = 0;
else
w = 1;
res += c[w];
for (int j = 0; j < m; ++j) b[i][j] = s[p ^ (j % 2) ^ w];
}
return res;
}
int calc_cols(int n, int m, string s) {
int res = 0;
for (int j = 0; j < m; ++j) {
int c[2] = {};
int p = j % 2 * 2;
int w;
for (int i = 0; i < n; ++i) {
for (int k : {0, 1}) c[k] += (a[i][j] != s[p ^ (i % 2) ^ k]);
}
if (c[0] < c[1])
w = 0;
else
w = 1;
res += c[w];
for (int i = 0; i < n; ++i) b[i][j] = s[p ^ (i % 2) ^ w];
}
return res;
}
int main() {
canhazfast();
int n, m, tmp, lo;
string s = "ACGT";
cin >> n >> m;
a.resize(n);
ans.resize(n);
lo = n * m + 1;
for (int i = 0; i < n; ++i) cin >> a[i];
b = a;
do {
tmp = calc_rows(n, m, s);
if (lo > tmp) lo = tmp, ans = b;
tmp = calc_cols(n, m, s);
if (lo > tmp) lo = tmp, ans = b;
} while (next_permutation((s).begin(), (s).end()));
for (int i = 0; i < n; ++i) cout << ans[i] << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
const int INF = 0x3f3f3f3f;
const int MAXN = 300000 + 5;
const int MAXM = 400000 + 5;
int dr[MAXN][12];
int dc[MAXN][12];
int co[12][2] = {{1, 2}, {2, 1}, {1, 3}, {3, 1}, {1, 4}, {4, 1},
{2, 3}, {3, 2}, {2, 4}, {4, 2}, {3, 4}, {4, 3}};
int main() {
ios_base::sync_with_stdio(0);
int n, m;
cin >> n >> m;
int **a = new int *[n];
int **b = new int *[n];
for (int i = 0; i < n; i++) {
a[i] = new int[m];
b[i] = new int[m];
string s;
cin >> s;
for (int j = 0; j < m; j++) {
switch (s[j]) {
case 'A':
a[i][j] = 1;
break;
case 'G':
a[i][j] = 2;
break;
case 'C':
a[i][j] = 3;
break;
case 'T':
a[i][j] = 4;
break;
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
for (int k = 0; k < 12; k++) {
if (a[i][j] != co[k][j & 1]) {
dr[i][k]++;
}
if (a[i][j] != co[k][i & 1]) {
dc[j][k]++;
}
}
}
}
int ans = INF;
int ansrc = 0;
int ansco = 0;
for (int k = 0; k < 12; k++) {
int cnt = 0;
for (int i = 0; i < n; i++) {
if (i & 1) {
cnt += min(dr[i][11 - k], dr[i][(11 - k) ^ 1]);
} else {
cnt += min(dr[i][k], dr[i][k ^ 1]);
}
}
if (cnt < ans) {
ans = cnt;
ansrc = 0;
ansco = k;
}
cnt = 0;
for (int j = 0; j < m; j++) {
if (j & 1) {
cnt += min(dc[j][11 - k], dc[j][(11 - k) ^ 1]);
} else {
cnt += min(dc[j][k], dc[j][k ^ 1]);
}
}
if (cnt < ans) {
ans = cnt;
ansrc = 1;
ansco = k;
}
}
int k = ansco;
if (ansrc == 0) {
for (int i = 0; i < n; i++) {
int _k;
if (i & 1) {
_k = (11 - k) ^ (dr[i][11 - k] >= dr[i][(11 - k) ^ 1]);
} else {
_k = k ^ (dr[i][k] >= dr[i][k ^ 1]);
}
for (int j = 0; j < m; j++) {
b[i][j] = co[_k][j & 1];
}
}
} else {
for (int j = 0; j < m; j++) {
int _k;
if (j & 1) {
_k = (11 - k) ^ (dc[j][11 - k] >= dc[j][(11 - k) ^ 1]);
} else {
_k = k ^ (dc[j][k] >= dc[j][k ^ 1]);
}
for (int i = 0; i < n; i++) {
b[i][j] = co[_k][i & 1];
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
switch (b[i][j]) {
case 1:
putchar('A');
break;
case 2:
putchar('G');
break;
case 3:
putchar('C');
break;
case 4:
putchar('T');
break;
}
}
putchar('\n');
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100001;
const long long mod = 1e9 + 7;
int main() {
int N, M;
scanf("%d %d", &N, &M);
char fin[N][M], ids[5] = "ACTG";
int arr[N][M];
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
scanf(" %c", &fin[i][j]);
if (fin[i][j] == 'A') {
arr[i][j] = 0;
} else if (fin[i][j] == 'C') {
arr[i][j] = 1;
} else if (fin[i][j] == 'T') {
arr[i][j] = 2;
} else {
arr[i][j] = 3;
}
}
}
int assign[N / 2 + 1][4];
int ans[N / 2 + 1][4];
int ans1 = 2e9;
for (int a = 0; a < 2; a++) {
for (int c = 0; c < 2; c++) {
for (int t = 0; t < 2; t++) {
for (int g = 0; g < 2; g++) {
if (a + c + t + g != 2) continue;
vector<int> top, bot;
if (a)
top.push_back(0);
else
bot.push_back(0);
if (c)
top.push_back(1);
else
bot.push_back(1);
if (t)
top.push_back(2);
else
bot.push_back(2);
if (g)
top.push_back(3);
else
bot.push_back(3);
int tot = 0;
for (int i = 0; i < N / 2; i++) {
int res = 2e9, cnt;
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
cnt = 0;
for (int l = 0; l < M; l++) {
if (arr[i * 2][l] != top[(l & 1) == j]) cnt++;
if (arr[i * 2 + 1][l] != bot[(l & 1) == k]) cnt++;
}
if (cnt < res) {
res = cnt;
assign[i][0] = top[j ^ 1];
assign[i][1] = top[j];
assign[i][2] = bot[k ^ 1];
assign[i][3] = bot[k];
}
}
}
tot += res;
}
if (N % 2) {
int res = 2e9, cnt;
for (int j = 0; j < 2; j++) {
cnt = 0;
for (int k = 0; k < M; k++) {
if (arr[N - 1][k] != top[(k & 1) == j]) cnt++;
}
if (cnt < res) {
res = cnt;
assign[N / 2][0] = top[j ^ 1];
assign[N / 2][1] = top[j];
}
}
tot += res;
}
if (tot < ans1) {
ans1 = tot;
for (int j = 0; j < N / 2 + 1; j++) {
for (int k = 0; k < 4; k++) {
ans[j][k] = assign[j][k];
}
}
}
}
}
}
}
int assign2[M / 2 + 1][4];
int ans2[M / 2 + 1][4];
int ans3 = 2e9;
for (int a = 0; a < 2; a++) {
for (int c = 0; c < 2; c++) {
for (int t = 0; t < 2; t++) {
for (int g = 0; g < 2; g++) {
if (a + c + t + g != 2) continue;
vector<int> top, bot;
if (a)
top.push_back(0);
else
bot.push_back(0);
if (c)
top.push_back(1);
else
bot.push_back(1);
if (t)
top.push_back(2);
else
bot.push_back(2);
if (g)
top.push_back(3);
else
bot.push_back(3);
int tot = 0;
for (int i = 0; i < M / 2; i++) {
int res = 2e9, cnt;
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
cnt = 0;
for (int l = 0; l < N; l++) {
if (arr[l][i * 2] != top[(l & 1) == j]) cnt++;
if (arr[l][i * 2 + 1] != bot[(l & 1) == k]) cnt++;
}
if (cnt < res) {
res = cnt;
assign2[i][0] = top[j ^ 1];
assign2[i][1] = top[j];
assign2[i][2] = bot[k ^ 1];
assign2[i][3] = bot[k];
}
}
}
tot += res;
}
if (M % 2) {
int res = 2e9, cnt;
for (int j = 0; j < 2; j++) {
cnt = 0;
for (int k = 0; k < N; k++) {
if (arr[k][M - 1] != top[(k & 1) == j]) cnt++;
}
if (cnt < res) {
res = cnt;
assign2[M / 2][0] = top[j ^ 1];
assign2[M / 2][1] = top[j];
}
}
tot += res;
}
if (tot < ans3) {
ans3 = tot;
for (int j = 0; j < M / 2 + 1; j++) {
for (int k = 0; k < 4; k++) {
ans2[j][k] = assign2[j][k];
}
}
}
}
}
}
}
if (ans1 < ans3) {
for (int i = 0; i < N / 2; i++) {
for (int j = 0; j < M; j++) {
fin[i * 2][j] = ids[ans[i][j & 1]];
fin[i * 2 + 1][j] = ids[ans[i][(j & 1) + 2]];
}
}
if (N % 2) {
for (int j = 0; j < M; j++) {
fin[N - 1][j] = ids[ans[N / 2][j & 1]];
}
}
} else {
for (int i = 0; i < M / 2; i++) {
for (int j = 0; j < N; j++) {
fin[j][i * 2] = ids[ans2[i][(j & 1)]];
fin[j][i * 2 + 1] = ids[ans2[i][(j & 1) + 2]];
}
}
if (M % 2) {
for (int j = 0; j < N; j++) {
fin[j][M - 1] = ids[ans2[M / 2][j & 1]];
}
}
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
printf("%c", fin[i][j]);
}
printf("\n");
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 300005;
int n, m, ANS;
char c[4];
string s[MAXN];
string t[MAXN];
string ans[MAXN];
void upd() {
int cnt = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) cnt += (s[i][j] != t[i][j]);
if (cnt < ANS) {
ANS = cnt;
for (int i = 0; i < n; i++) ans[i] = t[i];
}
}
int main() {
ios::sync_with_stdio(false);
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> s[i];
t[i] = s[i];
}
c[0] = 'A';
c[1] = 'T';
c[2] = 'G';
c[3] = 'C';
ANS = n * m + 1;
do {
for (int l = 0; l < 2; l++) {
for (int i = 0; i < n; i++) {
int dif[2] = {0, 0};
for (int j = 0; j < 2; j++)
for (int k = 0; k < m; k++)
if (c[(((i ^ l) & 1) << 1) | ((k ^ j) & 1)] != s[i][k]) dif[j]++;
int j = (dif[0] > dif[1]);
for (int k = 0; k < m; k++)
t[i][k] = c[(((i ^ l) & 1) << 1) | ((k ^ j) & 1)];
}
}
upd();
for (int l = 0; l < 2; l++) {
for (int i = 0; i < m; i++) {
int dif[2] = {0, 0};
for (int j = 0; j < 2; j++)
for (int k = 0; k < n; k++)
if (c[(((i ^ l) & 1) << 1) | ((k ^ j) & 1)] != s[k][i]) dif[j]++;
int j = (dif[0] > dif[1]);
for (int k = 0; k < n; k++)
t[k][i] = c[(((i ^ l) & 1) << 1) | ((k ^ j) & 1)];
}
}
upd();
} while (next_permutation(c, c + 4));
for (int i = 0; i < n; i++, cout << endl)
for (int j = 0; j < m; j++) cout << ans[i][j];
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
double PI = 4 * atan(1);
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
int grid[300005];
map<char, int> con;
con['A'] = 0;
con['G'] = 1;
con['C'] = 2;
con['T'] = 3;
map<int, char> noc;
noc[0] = 'A';
noc[1] = 'G';
noc[2] = 'C';
noc[3] = 'T';
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
char c;
cin >> c;
grid[i * m + j] = con[c];
}
}
int minchange = MOD;
int dirchange = 0;
int bestgrid[300005];
for (int a = 0; a < 4; a++) {
for (int b = a + 1; b < 4; b++) {
dirchange = 0;
int tempgrid[300005] = {};
for (int i = 0; i < n; i++) {
char l1;
char l2;
if (i % 2 == 0) {
l1 = a;
l2 = b;
} else {
for (int k = 0; k < 4; k++) {
if (a != k && b != k) {
l1 = k;
}
}
for (int k = 0; k < 4; k++) {
if (a != k && b != k && l1 != k) {
l2 = k;
}
}
}
int rowchange1 = 0;
for (int j = 0; j < m; j++) {
if (j % 2 == 0) {
if (grid[i * m + j] != l1) {
rowchange1 += 1;
}
} else {
if (grid[i * m + j] != l2) {
rowchange1 += 1;
}
}
}
int rowchange2 = 0;
swap(l1, l2);
for (int j = 0; j < m; j++) {
if (j % 2 == 0) {
if (grid[i * m + j] != l1) {
rowchange2 += 1;
}
} else {
if (grid[i * m + j] != l2) {
rowchange2 += 1;
}
}
}
dirchange += min(rowchange1, rowchange2);
if (rowchange1 < rowchange2) {
swap(l1, l2);
}
for (int j = 0; j < m; j++) {
if (j % 2 == 0) {
tempgrid[i * m + j] = l1;
} else {
tempgrid[i * m + j] = l2;
}
}
}
if (dirchange < minchange) {
minchange = dirchange;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
bestgrid[i * m + j] = tempgrid[i * m + j];
}
}
}
}
}
dirchange = 0;
for (int a = 0; a < 4; a++) {
for (int b = a + 1; b < 4; b++) {
dirchange = 0;
int tempgrid[300005] = {};
for (int i = 0; i < m; i++) {
char l1;
char l2;
if (i % 2 == 0) {
l1 = a;
l2 = b;
} else {
for (int k = 0; k < 4; k++) {
if (a != k && b != k) {
l1 = k;
}
}
for (int k = 0; k < 4; k++) {
if (a != k && b != k && l1 != k) {
l2 = k;
}
}
}
int rowchange1 = 0;
for (int j = 0; j < n; j++) {
if (j % 2 == 0) {
if (grid[j * m + i] != l1) {
rowchange1 += 1;
}
} else {
if (grid[j * m + i] != l2) {
rowchange1 += 1;
}
}
}
int rowchange2 = 0;
swap(l1, l2);
for (int j = 0; j < n; j++) {
if (j % 2 == 0) {
if (grid[j * m + i] != l1) {
rowchange2 += 1;
}
} else {
if (grid[j * m + i] != l2) {
rowchange2 += 1;
}
}
}
dirchange += min(rowchange1, rowchange2);
if (rowchange1 < rowchange2) {
swap(l1, l2);
}
for (int j = 0; j < n; j++) {
if (j % 2 == 0) {
tempgrid[j * m + i] = l1;
} else {
tempgrid[j * m + i] = l2;
}
}
}
if (dirchange < minchange) {
minchange = dirchange;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
bestgrid[i * m + j] = tempgrid[i * m + j];
}
}
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << noc[bestgrid[i * m + j]];
}
cout << '\n';
;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int kMaxN = 300000;
string M1[kMaxN + 1];
string M2[kMaxN + 1];
string M3[kMaxN + 1];
int check_lines(string *M, int n, int m, char *s) {
int ans = 0;
for (int i = 0; i < n; ++i) {
int v1 = 0;
for (int j = 0; j < m; ++j) {
if (j % 2 == 0) {
if (M[i][j] != s[(i % 2) * 2]) ++v1;
} else {
if (M[i][j] != s[(i % 2) * 2 + 1]) ++v1;
}
}
int v2 = 0;
for (int j = 0; j < m; ++j) {
if (j % 2 == 0) {
if (M[i][j] != s[(i % 2) * 2 + 1]) ++v2;
} else {
if (M[i][j] != s[(i % 2) * 2]) ++v2;
}
}
ans += min(v1, v2);
}
return ans;
}
void print(string *M, int n, int m, char *s) {
for (int i = 0; i < n; ++i) {
int v1 = 0;
for (int j = 0; j < m; ++j) {
if (j % 2 == 0) {
if (M[i][j] != s[(i % 2) * 2]) ++v1;
} else {
if (M[i][j] != s[(i % 2) * 2 + 1]) ++v1;
}
}
int v2 = 0;
for (int j = 0; j < m; ++j) {
if (j % 2 == 0) {
if (M[i][j] != s[(i % 2) * 2 + 1]) ++v2;
} else {
if (M[i][j] != s[(i % 2) * 2]) ++v2;
}
}
if (v1 < v2) {
for (int j = 0; j < m; ++j) {
if (j % 2 == 0) {
M3[i].push_back(s[(i % 2) * 2]);
} else
M3[i].push_back(s[(i % 2) * 2 + 1]);
}
} else {
for (int j = 0; j < m; ++j) {
if (j % 2 == 0) {
M3[i].push_back(s[(i % 2) * 2 + 1]);
} else
M3[i].push_back(s[(i % 2) * 2]);
}
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = 0; i < n; ++i) {
cin >> M1[i];
for (int j = 0; j < m; ++j) M2[j].push_back(M1[i][j]);
}
char s[] = "AGCT";
int ans = numeric_limits<int>::max() / 2;
char ans_s[] = "AGCT";
int k = 1;
do {
int tmp = check_lines(M1, n, m, s);
if (tmp < ans) {
k = 1;
for (int j = 0; j < 4; ++j) ans_s[j] = s[j];
ans = tmp;
}
tmp = check_lines(M2, m, n, s);
if (tmp < ans) {
k = 2;
for (int j = 0; j < 4; ++j) ans_s[j] = s[j];
ans = tmp;
}
} while (next_permutation(s, s + 4));
if (k == 1) {
print(M1, n, m, ans_s);
for (int i = 0; i < n; ++i) {
cout << M3[i] << '\n';
}
} else {
print(M2, m, n, ans_s);
for (int j = 0; j < n; ++j) {
for (int i = 0; i < m; ++i) cout << M3[i][j];
cout << '\n';
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3e5 + 5;
int N, M;
string row[MAXN], col[MAXN], ans[MAXN];
int sol = MAXN;
string opt;
bool flag;
void load() {
scanf("%d%d", &N, &M);
for (int i = 0; i < N; i++) cin >> row[i];
}
int diff(string s, string per) {
int res = 0;
for (int i = 0; i < s.size(); i++) res += s[i] != per[i % 2];
return res;
}
int min_diff(string s, string per) {
return min(diff(s, {per[0], per[1]}), diff(s, {per[1], per[0]}));
}
int solve_rows(string perm) {
int res = 0;
for (int i = 0; i < N; i++)
res += min_diff(row[i], perm.substr(i % 2 * 2, 2));
return res;
}
int solve_cols(string perm) {
int res = 0;
for (int i = 0; i < M; i++)
res += min_diff(col[i], perm.substr(i % 2 * 2, 2));
return res;
}
void update(int val, string cand, bool f) {
if (val >= sol) return;
sol = val;
opt = cand;
flag = f;
}
void construct_rows(string perm) {
for (int i = 0; i < N; i++) {
string curr = perm.substr(i % 2 * 2, 2);
if (diff(row[i], {curr[1], curr[0]}) < diff(row[i], {curr[0], curr[1]}))
swap(curr[0], curr[1]);
for (int j = 0; j < M; j++) ans[i][j] = curr[j % 2];
}
}
void construct_cols(string perm) {
for (int j = 0; j < M; j++) {
string curr = perm.substr(j % 2 * 2, 2);
if (diff(col[j], {curr[1], curr[0]}) < diff(col[j], {curr[0], curr[1]}))
swap(curr[0], curr[1]);
for (int i = 0; i < N; i++) ans[i][j] = curr[i % 2];
}
}
void solve() {
for (int i = 0; i < N; i++) {
ans[i].resize(M);
for (int j = 0; j < M; j++) col[j] += row[i][j];
}
string ltr = "ACGT";
do {
update(solve_rows(ltr), ltr, false);
update(solve_cols(ltr), ltr, true);
} while (next_permutation(ltr.begin(), ltr.end()));
if (flag)
construct_cols(opt);
else
construct_rows(opt);
for (int i = 0; i < N; i++) puts(ans[i].c_str());
}
int main() {
load();
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, ma;
vector<string> en;
vector<string> res;
vector<string> can;
int che(int i, char c1, char c2) {
int c = 0;
int j;
for (j = 0; j < m; j += 2)
if (en[i][j] == c1) c++;
for (j = 1; j < m; j += 2)
if (en[i][j] == c2) c++;
return c;
}
int che2(int j, char c1, char c2) {
int c = 0;
int i;
for (i = 0; i < n; i += 2)
if (en[i][j] == c1) c++;
for (i = 1; i < n; i += 2)
if (en[i][j] == c2) c++;
return c;
}
void p1(int i, char c1, char c2) {
int j;
for (j = 0; j < m; j += 2) can[i][j] = c1;
for (j = 1; j < m; j += 2) can[i][j] = c2;
}
void p2(int j, char c1, char c2) {
int i;
for (i = 0; i < n; i += 2) can[i][j] = c1;
for (i = 1; i < n; i += 2) can[i][j] = c2;
}
void fu(char c1, char c2, char c3, char c4) {
int i, mc = 0, ca1, ca2;
for (i = 0; i < n; i += 2) {
ca1 = che(i, c1, c2);
ca2 = che(i, c2, c1);
if (ca1 > ca2) {
mc += ca1;
p1(i, c1, c2);
} else {
mc += ca2;
p1(i, c2, c1);
}
}
for (i = 1; i < n; i += 2) {
ca1 = che(i, c3, c4);
ca2 = che(i, c4, c3);
if (ca1 > ca2) {
mc += ca1;
p1(i, c3, c4);
} else {
mc += ca2;
p1(i, c4, c3);
}
}
if (mc > ma) {
ma = mc;
res = can;
}
mc = 0;
for (i = 0; i < m; i += 2) {
ca1 = che2(i, c1, c2);
ca2 = che2(i, c2, c1);
if (ca1 > ca2) {
mc += ca1;
p2(i, c1, c2);
} else {
mc += ca2;
p2(i, c2, c1);
}
}
for (i = 1; i < m; i += 2) {
ca1 = che2(i, c3, c4);
ca2 = che2(i, c4, c3);
if (ca1 > ca2) {
mc += ca1;
p2(i, c3, c4);
} else {
mc += ca2;
p2(i, c4, c3);
}
}
if (mc > ma) {
ma = mc;
res = can;
}
}
int main() {
cin >> n >> m;
en.resize(n);
can.resize(n);
int i, ma = 0;
for (i = 0; i < n; i++) {
cin >> en[i];
can[i] = en[i];
}
fu('G', 'C', 'T', 'A');
fu('G', 'T', 'C', 'A');
fu('G', 'A', 'T', 'C');
fu('A', 'C', 'G', 'T');
fu('T', 'C', 'G', 'A');
fu('T', 'A', 'G', 'C');
for (i = 0; i < n; i++) {
cout << res[i] << endl;
}
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>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
int n, m;
vector<vector<char>> s;
map<vector<char>, vector<vector<char>>> help1, help2;
void read() {
cin >> n >> m;
s.assign(n, vector<char>(m));
vector<char> lett = {'A', 'T', 'G', 'C'};
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> s[i][j];
}
}
}
void gen() {
vector<char> lett = {'A', 'T', 'G', 'C'};
sort(lett.begin(), lett.end());
do {
vector<char> h1;
for (int i = 0; i < m; i++) {
if (i % 2 == 0)
h1.push_back(lett[0]);
else
h1.push_back(lett[1]);
}
vector<char> h2;
for (int i = 0; i < m; i++) {
if (i % 2 == 0)
h2.push_back(lett[2]);
else
h2.push_back(lett[3]);
}
help1[h1].push_back(h2);
h1.clear();
for (int i = 0; i < n; i++) {
if (i % 2 == 0)
h1.push_back(lett[0]);
else
h1.push_back(lett[1]);
}
h2.clear();
for (int i = 0; i < n; i++) {
if (i % 2 == 0)
h2.push_back(lett[2]);
else
h2.push_back(lett[3]);
}
help2[h1].push_back(h2);
} while (next_permutation(lett.begin(), lett.end()));
}
int dist(vector<vector<char>>& a, vector<vector<char>>& b) {
int ans = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) ans += a[i][j] != b[i][j];
return ans;
}
int dist(vector<char>& a, vector<char>& b) {
int sz = a.size();
int ans = 0;
for (int i = 0; i < sz; i++) {
ans += a[i] != b[i];
}
return ans;
}
int fans = 1e9;
vector<vector<char>> ans;
void run() {
gen();
for (auto i : help1) {
vector<vector<char>> for_ans(n, vector<char>(m));
auto start = i.first;
for (int k = 0; k < m; k++) for_ans[0][k] = start[k];
for (int j = 1; j < n; j++) {
int now = 1e9;
vector<char> fcheck;
for (auto k : help1[start]) {
if (dist(k, s[j]) < now) {
now = dist(k, s[j]);
fcheck = k;
}
}
start = fcheck;
for (int k = 0; k < m; k++) for_ans[j][k] = start[k];
}
if (fans > dist(s, for_ans)) {
fans = dist(s, for_ans);
ans = for_ans;
}
}
for (auto i : help2) {
vector<vector<char>> for_ans(n, vector<char>(m));
auto start = i.first;
for (int j = 0; j < n; j++) for_ans[j][0] = start[j];
for (int j = 1; j < m; j++) {
int now = 1e9;
vector<char> fcheck;
vector<char> from_s;
for (int k = 0; k < n; k++) from_s.push_back(s[k][j]);
for (auto k : help2[start]) {
if (dist(k, from_s) < now) {
now = dist(k, from_s);
fcheck = k;
}
}
start = fcheck;
for (int k = 0; k < n; k++) for_ans[k][j] = start[k];
}
if (fans > dist(s, for_ans)) {
fans = dist(s, for_ans);
ans = for_ans;
}
}
}
void write() {
for (auto i : ans) {
for (auto j : i) cout << j;
cout << '\n';
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
read();
run();
write();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
const long long inf = 1e9 + 10;
const long long mod = 1e9 + 7;
const string str = "ATGC";
vector<string> s, tmp, ans;
int main() {
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) {
string st;
cin >> st;
s.push_back(st);
}
int ansmin = inf, now;
for (int c1 = 0; c1 < 4; c1++) {
for (int c2 = c1 + 1; c2 < 4; c2++) {
string t;
t.push_back(str[c1]);
t.push_back(str[c2]);
for (int i = 0; i < 4; i++)
if (i != c1 && i != c2) t.push_back(str[i]);
now = 0;
tmp.clear();
for (int i = 0; i < n; i++) {
tmp.push_back("");
int b = (i & 1) << 1;
int cnt1 = 0, cnt2 = 0;
for (int j = 0; j < m; j++) {
if (s[i][j] != t[b + (j & 1)]) ++cnt1;
if (s[i][j] != t[b + (j & 1) ^ 1]) ++cnt2;
}
if (cnt1 < cnt2)
for (int j = 0; j < m; j++) tmp[i].push_back(t[b + (j & 1)]);
else
for (int j = 0; j < m; j++) tmp[i].push_back(t[b + ((j & 1) ^ 1)]);
now += min(cnt1, cnt2);
}
if (now < ansmin) {
ans = tmp;
ansmin = now;
}
now = 0;
tmp.clear();
for (int i = 0; i < n; i++) tmp.push_back("");
for (int j = 0; j < m; j++) {
int b = (j & 1) << 1;
int cnt1 = 0, cnt2 = 0;
for (int i = 0; i < n; i++) {
if (s[i][j] != t[b + (i & 1)]) ++cnt1;
if (s[i][j] != t[b + (i & 1) ^ 1]) ++cnt2;
}
if (cnt1 < cnt2)
for (int i = 0; i < n; i++) tmp[i].push_back(t[b + (i & 1)]);
else
for (int i = 0; i < n; i++) tmp[i].push_back(t[b + ((i & 1) ^ 1)]);
now += min(cnt1, cnt2);
}
if (now < ansmin) {
ans = tmp;
ansmin = now;
}
}
}
for (int i = 0; i < n; i++) cout << ans[i] << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> mat;
vector<vector<int>> transpose(vector<vector<int>> v) {
int r = v.size(), c = v[0].size();
vector<vector<int>> nw(c);
int temp, temp2;
for (temp = 0; temp < r; temp++)
for (temp2 = 0; temp2 < c; temp2++) nw[temp2].push_back(v[temp][temp2]);
return nw;
}
string s = "ACGT";
void printmat(vector<vector<int>> v) {
for (auto lel : v) {
for (auto lol : lel) cout << s[lol];
cout << "\n";
}
}
int getdiff(vector<int> &v1, vector<int> &v2) {
int cnt = 0;
for (int temp = 0; temp < v1.size(); temp++) cnt += (v1[temp] != v2[temp]);
return cnt;
}
int main() {
int n, m, temp, temp2;
cin >> n >> m;
int hsh[200];
hsh['A'] = 0;
hsh['C'] = 1;
hsh['G'] = 2;
hsh['T'] = 3;
vector<vector<int>> a(n);
for (temp = 0; temp < n; temp++) {
for (temp2 = 0; temp2 < m; temp2++) {
char c;
cin >> c;
a[temp].push_back(hsh[c]);
}
}
bool istranspose = false;
if (n > m) {
istranspose = true;
a = transpose(a);
swap(n, m);
}
int cost[n][2][4];
for (temp = 0; temp < n; temp++) {
for (temp2 = 0; temp2 < 2; temp2++) {
for (int temp3 = 0; temp3 < 4; temp3++) {
cost[temp][temp2][temp3] = 0;
for (int temp4 = temp2; temp4 < m; temp4 += 2) {
cost[temp][temp2][temp3] += (a[temp][temp4] != temp3);
}
}
}
}
vector<int> v = {0, 1, 2, 3};
int ans = 10e8;
vector<vector<int>> jwb;
do {
vector<int> vv[2][2];
for (temp = 0; temp < m; temp++) {
vv[0][0].push_back(v[temp % 2]);
vv[0][1].push_back(v[1 - temp % 2]);
vv[1][0].push_back(v[temp % 2 + 2]);
vv[1][1].push_back(v[3 - temp % 2]);
}
int mn = 0;
vector<vector<int>> opt;
for (temp = 0; temp < n; temp++) {
int lamin = 10e8;
vector<int> optin;
for (int tmp = 0; tmp < 2; tmp++) {
int dst = getdiff(vv[temp % 2][tmp], a[temp]);
if (dst < lamin) {
optin = vv[temp % 2][tmp];
lamin = dst;
}
}
opt.push_back(optin);
mn += lamin;
}
if (mn < ans) {
ans = mn;
jwb = opt;
}
mn = 0;
opt.clear();
opt.resize(n);
for (int tmp = 0; tmp < 2; tmp++) {
for (int tmp2 = 0; tmp2 < 2; tmp2++) {
vv[tmp][tmp2].clear();
}
}
for (temp = 0; temp < n; temp++) {
int a = v[0], b = v[2], c = v[1], d = v[3];
if (temp % 2 == 1) {
swap(a, b);
swap(c, d);
}
vv[0][0].push_back(a);
vv[0][1].push_back(b);
vv[1][0].push_back(c);
vv[1][1].push_back(d);
}
for (temp = 0; temp < m; temp++) {
int lamin = 10e8;
vector<int> curbar, optin;
for (temp2 = 0; temp2 < n; temp2++) curbar.push_back(a[temp2][temp]);
for (int tmp = 0; tmp < 2; tmp++) {
int dst = getdiff(vv[temp % 2][tmp], curbar);
if (dst < lamin) {
lamin = dst;
optin = vv[temp % 2][tmp];
}
}
for (temp2 = 0; temp2 < n; temp2++) {
opt[temp2].push_back(optin[temp2]);
}
mn += lamin;
}
if (mn < ans) {
ans = mn;
jwb = opt;
}
} while (next_permutation(v.begin(), v.end()));
if (istranspose) jwb = transpose(jwb);
printmat(jwb);
}
|
#include <bits/stdc++.h>
using namespace std;
const int lmt = 3e5;
vector<string> s, ans, fek;
int n, m, res = 0, cnt[2][26][lmt];
char ch[4];
void chek1(char a, char b, char c, char d) {
int tot = 0;
for (int i = 0; i < n; i++) {
if (i % 2) {
int mx1 = 0, mx2 = 0;
for (int j = 0; j < m; j++) {
if (j % 2) {
if (s[i][j] == a)
mx1++;
else if (s[i][j] == b)
mx2++;
} else {
if (s[i][j] == b)
mx1++;
else if (s[i][j] == a)
mx2++;
}
}
tot += max(mx2, mx1);
if (mx1 > mx2) {
for (int j = 0; j < m; j++) {
if (j % 2)
fek[i][j] = a;
else
fek[i][j] = b;
}
} else {
for (int j = 0; j < m; j++) {
if (j % 2)
fek[i][j] = b;
else
fek[i][j] = a;
}
}
} else {
int mx1 = 0, mx2 = 0;
for (int j = 0; j < m; j++) {
if (j % 2) {
if (s[i][j] == c)
mx1++;
else if (s[i][j] == d)
mx2++;
} else {
if (s[i][j] == d)
mx1++;
else if (s[i][j] == c)
mx2++;
}
}
tot += max(mx2, mx1);
if (mx1 > mx2) {
for (int j = 0; j < m; j++) {
if (j % 2)
fek[i][j] = c;
else
fek[i][j] = d;
}
} else {
for (int j = 0; j < m; j++) {
if (j % 2)
fek[i][j] = d;
else
fek[i][j] = c;
}
}
}
}
if (tot > res) {
res = tot;
ans = fek;
}
return;
}
void chek2(char a, char b, char c, char d) {
int tot = 0;
for (int j = 0; j < m; j++) {
if (j % 2) {
int mx1 = 0, mx2 = 0;
mx1 += cnt[0][a - 'A'][j] + cnt[1][b - 'A'][j];
mx2 += cnt[0][b - 'A'][j] + cnt[1][a - 'A'][j];
tot += max(mx2, mx1);
if (mx1 > mx2) {
fek[0][j] = a;
} else {
fek[0][j] = b;
}
} else {
int mx1 = 0, mx2 = 0;
mx1 += cnt[0][c - 'A'][j] + cnt[1][d - 'A'][j];
mx2 += cnt[0][d - 'A'][j] + cnt[1][c - 'A'][j];
tot += max(mx2, mx1);
if (mx1 > mx2) {
fek[0][j] = c;
} else {
fek[0][j] = d;
}
}
}
if (tot > res) {
res = tot;
for (int j = 0; j < m; j++) ans[0][j] = fek[0][j];
for (int i = 1; i < n; i++) {
for (int j = 0; j < m; j++) {
if (ans[i - 1][j] == a)
ans[i][j] = b;
else if (ans[i - 1][j] == b)
ans[i][j] = a;
else if (ans[i - 1][j] == c)
ans[i][j] = d;
else
ans[i][j] = c;
}
}
}
return;
}
int main() {
ios_base::sync_with_stdio(false);
ch[0] = 'A', ch[1] = 'C', ch[2] = 'G', ch[3] = 'T';
cin >> n >> m;
for (int i = 0; i < n; i++) {
string t;
cin >> t;
ans.push_back(t);
fek.push_back(t);
s.push_back(t);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) cnt[i % 2][s[i][j] - 'A'][j]++;
}
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 (i == j || i == k || i == l || j == k || j == l || k == l)
continue;
chek1(ch[i], ch[j], ch[k], ch[l]);
chek2(ch[i], ch[j], ch[k], ch[l]);
}
}
}
}
cout << "\n";
for (int i = 0; i < n; i++) {
cout << ans[i] << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void amin(T &x, const T &y) {
if (y < x) x = y;
}
template <class T>
inline void amax(T &x, const T &y) {
if (x < y) x = y;
}
template <class Iter>
void rprintf(const char *fmt, Iter begin, Iter end) {
for (bool sp = 0; begin != end; ++begin) {
if (sp)
putchar(' ');
else
sp = true;
printf(fmt, *begin);
}
putchar('\n');
}
const string P[6][2] = {
{"AG", "CT"}, {"AC", "GT"}, {"AT", "GC"},
{"GC", "AT"}, {"GT", "AC"}, {"CT", "AG"},
};
int ord(char c) {
if (c == 'A') return 0;
if (c == 'G') return 1;
if (c == 'C') return 2;
if (c == 'T') return 3;
return 4;
}
void solve(vector<string> &F, int &ans) {
int N = F.size();
int M = F[0].size();
int best_t = -1;
ans = 1 << 29;
for (int t = 0, t_len = (6); t < t_len; ++t) {
int cost = 0;
for (int i = 0, i_len = (N); i < i_len; ++i) {
const string &pat = P[t][i & 1];
int line_cost = 1 << 29;
for (int a = 0, a_len = (2); a < a_len; ++a) {
int tmp = 0;
for (int j = 0, j_len = (M); j < j_len; ++j) {
tmp += (int)(F[i][j] != pat[(j + a) & 1]);
}
amin(line_cost, tmp);
}
cost += line_cost;
}
if (ans > cost) {
ans = cost;
best_t = t;
}
}
{
for (int i = 0, i_len = (N); i < i_len; ++i) {
const string &pat = P[best_t][i & 1];
int tmp[2] = {};
for (int a = 0, a_len = (2); a < a_len; ++a) {
for (int j = 0, j_len = (M); j < j_len; ++j) {
tmp[a] += (int)(F[i][j] != pat[(j + a) & 1]);
}
}
int a = (tmp[0] > tmp[1] ? 1 : 0);
for (int j = 0, j_len = (M); j < j_len; ++j) F[i][j] = pat[(j + a) & 1];
}
}
}
int N, M;
vector<string> F, G;
char buf[300111];
void MAIN() {
scanf("%d%d", &N, &M);
F.resize(N);
for (int i = 0, i_len = (N); i < i_len; ++i) {
scanf("%s", buf);
F[i] = buf;
}
G.assign(M, string(N, '.'));
for (int i = 0, i_len = (N); i < i_len; ++i)
for (int j = 0, j_len = (M); j < j_len; ++j) G[j][i] = F[i][j];
int f_cost, g_cost;
solve(F, f_cost);
solve(G, g_cost);
if (g_cost < f_cost) {
for (int i = 0, i_len = (N); i < i_len; ++i)
for (int j = 0, j_len = (M); j < j_len; ++j) F[i][j] = G[j][i];
}
for (int i = 0, i_len = (N); i < i_len; ++i) puts(F[i].c_str());
}
int main() {
int TC = 1;
for (int tc = 0, tc_len = (TC); tc < tc_len; ++tc) MAIN();
return 0;
}
|
#include <bits/stdc++.h>
int n, m, i, j, ans = 0x7fffffff, l;
struct table {
int mem[300005];
inline char get(int i, int j) { return mem[i * m + j]; }
inline void cg(int i, int j, char k) { mem[i * m + j] = k; }
} num, ppt;
inline int min(int a, int b) { return a < b ? a : b; }
inline void MIN(int b, int p) {
if (ans > b) ans = b, l = p;
}
int chk1(const char* c1, const char* c2) {
int ret = 0, mn1, mn2;
for (i = 0; i < m; i++) {
for (mn1 = mn2 = j = 0; j < n; j++) {
if (num.get(j, i) != (i & 1 ? c1 : c2)[j & 1]) mn1++;
if (num.get(j, i) != (i & 1 ? c1 : c2)[(j & 1) ^ 1]) mn2++;
}
ret += min(mn1, mn2);
}
return ret;
}
int chk2(const char* c1, const char* c2) {
int ret = 0, mn1, mn2;
for (i = 0; i < n; i++) {
for (mn1 = mn2 = j = 0; j < m; j++) {
if (num.get(i, j) != (i & 1 ? c1 : c2)[j & 1]) mn1++;
if (num.get(i, j) != (i & 1 ? c1 : c2)[(j & 1) ^ 1]) mn2++;
}
ret += min(mn1, mn2);
}
return ret;
}
inline void print1(const char* c1, const char* c2) {
int mn1, mn2;
for (i = 0; i < m; i++) {
for (mn1 = mn2 = j = 0; j < n; j++) {
if (num.get(j, i) != (i & 1 ? c1 : c2)[j & 1]) mn1++;
if (num.get(j, i) != (i & 1 ? c1 : c2)[(j & 1) ^ 1]) mn2++;
}
for (j = 0; j < n; j++)
ppt.cg(j, i, (i & 1 ? c1 : c2)[(j & 1) ^ (mn1 > mn2 ? 1 : 0)]);
}
}
inline void print2(const char* c1, const char* c2) {
int mn1, mn2;
for (i = 0; i < n; i++) {
for (mn1 = mn2 = j = 0; j < m; j++) {
if (num.get(i, j) != (i & 1 ? c1 : c2)[j & 1]) mn1++;
if (num.get(i, j) != (i & 1 ? c1 : c2)[(j & 1) ^ 1]) mn2++;
}
for (j = 0; j < m; j++)
ppt.cg(i, j, (i & 1 ? c1 : c2)[(j & 1) ^ (mn1 > mn2 ? 1 : 0)]);
}
}
int main() {
for (scanf("%d%d", &n, &m), i = 0; i < n; i++)
for (j = 0; j < m; j++)
while (num.get(i, j) > 'Z' || num.get(i, j) < 'A')
num.cg(i, j, getchar());
MIN(chk1("AG", "CT"), 0);
MIN(chk1("AC", "GT"), 1);
MIN(chk1("AT", "CG"), 2);
MIN(chk1("GC", "AT"), 3);
MIN(chk1("GT", "AC"), 4);
MIN(chk1("CT", "AG"), 5);
MIN(chk2("AG", "CT"), 6);
MIN(chk2("AC", "GT"), 7);
MIN(chk2("AT", "CG"), 8);
MIN(chk2("GC", "AT"), 9);
MIN(chk2("GT", "AC"), 10);
MIN(chk2("CT", "AG"), 11);
switch (l) {
case 0: {
print1("AG", "CT");
break;
}
case 1: {
print1("AC", "GT");
break;
}
case 2: {
print1("AT", "CG");
break;
}
case 3: {
print1("GC", "AT");
break;
}
case 4: {
print1("GT", "AC");
break;
}
case 5: {
print1("CT", "AG");
break;
}
case 6: {
print2("AG", "CT");
break;
}
case 7: {
print2("AC", "GT");
break;
}
case 8: {
print2("AT", "CG");
break;
}
case 9: {
print2("GC", "AT");
break;
}
case 10: {
print2("GT", "AC");
break;
}
case 11: {
print2("CT", "AG");
break;
}
}
for (i = 0; i < n; i++, putchar('\n'))
for (j = 0; j < m; j++) putchar(ppt.get(i, j));
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m;
string s[300030];
char t[5] = "ACGT";
int min_score = 2e9;
string ans[300030];
string f[300030];
void update() {
int cnt = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
if (f[i][j] != s[i][j]) cnt++;
}
if (cnt < min_score) {
min_score = cnt;
for (int i = 0; i < n; i++) ans[i] = f[i];
}
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) cin >> s[i];
int p[4];
for (int i = 0; i < 4; i++) p[i] = i;
for (int i = 0; i < n; i++) f[i].resize(m);
while (1) {
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) f[i][j] = t[p[2 * (i & 1) + (j & 1)]];
update();
for (int j = 0; j < m; j++) {
int q = 2 * (j & 1);
int odd = 0, even = 0;
for (int i = 0; i < n; i++) {
if (s[i][j] != t[p[q + (i & 1)]]) odd++;
if (s[i][j] != t[p[q + (1 ^ (i & 1))]]) even++;
}
if (odd < even) {
for (int i = 0; i < n; i++) f[i][j] = t[p[q + (i & 1)]];
} else {
for (int i = 0; i < n; i++) f[i][j] = t[p[q + (1 ^ (i & 1))]];
}
}
update();
for (int i = 0; i < n; i++) {
int q = 2 * (i & 1);
int odd = 0, even = 0;
for (int j = 0; j < m; j++) {
if (s[i][j] != t[p[q + (j & 1)]]) odd++;
if (s[i][j] != t[p[q + (1 ^ (j & 1))]]) even++;
}
if (odd < even) {
for (int j = 0; j < m; j++) f[i][j] = t[p[q + (j & 1)]];
} else {
for (int j = 0; j < m; j++) f[i][j] = t[p[q + (1 ^ (j & 1))]];
}
}
update();
if (!next_permutation(p, p + 4)) break;
}
for (int i = 0; i < n; i++) cout << ans[i] << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int dx[8] = {0, 0, -1, 1, 1, 1, -1, -1};
int dy[8] = {-1, 1, 0, 0, 1, -1, 1, -1};
const int SIZE = 100050;
int bitcount32(int dw32) {
dw32 = ((dw32 & 0xAAAAAAAA) >> 1) + (dw32 & 0x55555555);
dw32 = ((dw32 & 0xCCCCCCCC) >> 2) + (dw32 & 0x33333333);
dw32 = ((dw32 & 0xF0F0F0F0) >> 4) + (dw32 & 0x0F0F0F0F);
dw32 = ((dw32 & 0xFF00FF00) >> 8) + (dw32 & 0x00FF00FF);
dw32 = ((dw32 & 0xFFFF0000) >> 16) + (dw32 & 0x0000FFFF);
return dw32;
}
pair<int, string> mincos(string S, string T) {
int a = 0;
for (int i = 0; i < S.length(); i++) {
if (S[i] != T[i % 2]) a++;
}
reverse(T.begin(), T.end());
int b = 0;
for (int i = 0; i < S.length(); i++) {
if (S[i] != T[i % 2]) b++;
}
string U;
if (a < b) {
reverse(T.begin(), T.end());
}
for (int i = 0; i < S.length(); i++) U += T[i % 2];
return make_pair(min(a, b), U);
}
int main() {
int N, M;
cin >> N >> M;
vector<string> S;
int msty = 0, mstt = 0;
for (int i = 0; i < N; i++) {
string T;
cin >> T;
S.push_back(T);
}
string T = "AGCT";
int yoko = 0x3f3f3f3f, tate = 0x3f3f3f3f;
for (int state = 0; state < (1 << 4); state++) {
if (bitcount32(state) != 2) continue;
int temp = 0;
string U, V;
for (int i = 0; i < 4; i++) {
if (1 & (state >> i))
U += T[i];
else
V += T[i];
}
for (int i = 0; i < N; i++) {
pair<int, string> is;
if (i % 2)
is = mincos(S[i], U);
else
is = mincos(S[i], V);
temp += is.first;
}
if (temp < yoko) {
yoko = temp;
msty = state;
}
}
for (int state = 0; state < (1 << 4); state++) {
if (bitcount32(state) != 2) continue;
int temp = 0;
string U, V;
for (int i = 0; i < 4; i++) {
if (1 & (state >> i))
U += T[i];
else
V += T[i];
}
for (int i = 0; i < M; i++) {
string W;
for (int j = 0; j < N; j++) W += S[j][i];
pair<int, string> is;
if (i % 2)
is = mincos(W, U);
else
is = mincos(W, V);
temp += is.first;
}
if (temp < tate) {
tate = temp;
mstt = state;
}
}
if (yoko < tate) {
string U, V;
for (int i = 0; i < 4; i++) {
if (1 & (msty >> i))
U += T[i];
else
V += T[i];
}
for (int i = 0; i < N; i++) {
pair<int, string> is;
if (i % 2)
is = mincos(S[i], U);
else
is = mincos(S[i], V);
cout << is.second << endl;
}
} else {
vector<string> Y;
string U, V;
for (int i = 0; i < 4; i++) {
if (1 & (mstt >> i))
U += T[i];
else
V += T[i];
}
for (int i = 0; i < M; i++) {
string W;
for (int j = 0; j < N; j++) W += S[j][i];
pair<int, string> is;
if (i % 2)
is = mincos(W, U);
else
is = mincos(W, V);
Y.push_back(is.second);
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) cout << Y[j][i];
cout << endl;
}
}
return 0;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.