A - Double Helix
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
typedef long long LL;
int main(){
char c;
cin >> c;
if (c == 'A') cout << 'T' << endl;
if (c == 'T') cout << 'A' << endl;
if (c == 'C') cout << 'G' << endl;
if (c == 'G') cout << 'C' << endl;
return 0;
}
B - ATCoder
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
typedef long long LL;
string s;
int res, num;
int main() {
cin >> s;
for (int i = 0; i < s.size(); i++) {
if (s[i] != 'A' && s[i] != 'T' && s[i] != 'C' && s[i] != 'G') {
res = max(res, num);
num = 0;
} else
num++;
}
res = max(res, num);
cout << res << endl;
return 0;
}
C - GeT AC
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
typedef long long LL;
int n, m;
string s;
int sum[N];
int main() {
cin >> n >> m;
cin >> s;
s = " " + s;
for (int i = 1; i < s.size(); i++) {
sum[i] += (s[i] == 'C' && s[i-1] == 'A')+sum[i-1];
}
while(m--){
int x, y;
cin >> x >> y;
cout << sum[y] - sum[x - 1] -(s[x-1]=='A'&&s[x]=='C')<< endl;
}
return 0;
}
D - We Like AGC
给出一个数n,问长度为n的、满足下列条件的字符串有多少个:
字符均由A G C T组成
不包含AGC的子串
交换一次相邻的字符后,也不包含AGC的子串
dp求解,(dp[len][i][j][k])代表长度为len,以i j k为结尾的字符串的数量,然后就很好dp了
#include <bits/stdc++.h>
using namespace std;
const int N = 1e2 + 5;
typedef long long LL;
LL const mod = 1e9 + 7;
LL dp[N][5][5][5];
bool check(int q, int j, int k, int l) {
// AGCT
if (q == 0 && j == 1 && k == 2) return 0;
if (q == 0 && k == 1 && l == 2) return 0;
if (q == 0 && j == 1 && l == 2) return 0;
if (j == 0 && k == 1 && l == 2) return 0;
if (j == 0 && q == 1 && k == 2) return 0;
if (j == 0 && l == 1 && k == 2) return 0;
if (q == 0 && k == 1 && j == 2) return 0;
if (k == 0 && j == 1 && l == 2) return 0;
return 1;
}
bool check2(int i, int j, int k) {
// AGCT
if (i == 0 && j == 1 && k == 2) return 0;
if (i == 0 && k == 1 && j == 2) return 0;
if (j == 0 && i == 1 && k == 2) return 0;
return 1;
}
int main() {
int n;
cin >> n;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
for (int k = 0; k < 4; k++) {
if (check2(i,j,k))
dp[3][i][j][k] = 1;
}
}
}
for (int i = 4; i <= n; i++) {
for (int j = 0; j < 4; j++) {
for (int k = 0; k < 4; k++) {
for (int l = 0; l < 4; l++) {
for (int q = 0; q < 4; q++) {
if (check(q, j, k, l))
dp[i][j][k][l] =
(dp[i][j][k][l] + dp[i - 1][q][j][k]) % mod;
}
}
}
}
}
LL res = 0;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
for (int k = 0; k < 4; k++) {
res = (res + dp[n][i][j][k]) % mod;
}
}
}
cout << res << endl;
return 0;
}