AtCoder Beginner Contest 122 解题报告

手速选手成功混进rated only里面的前30名,但是总排名就到110+了...

A - Double Helix

#include <bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define il inline namespace io { #define in(a) a = read()
#define out(a) write(a)
#define outn(a) out(a), putchar('\n') #define I_int ll
inline I_int read() {
I_int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x * f;
}
char F[200];
inline void write(I_int x) {
if (x == 0) return (void) (putchar('0'));
I_int tmp = x > 0 ? x : -x;
if (x < 0) putchar('-');
int cnt = 0;
while (tmp > 0) {
F[cnt++] = tmp % 10 + '0';
tmp /= 10;
}
while (cnt > 0) putchar(F[--cnt]);
}
#undef I_int }
using namespace io; using namespace std; #define N 100010 char s[N]; int main() {
scanf("%s",s);
if(s[0] == 'A') putchar('T');
if(s[0] == 'T') putchar('A');
if(s[0] == 'G') putchar('C');
if(s[0] == 'C') putchar('G');
}

B - ATCoder

#include <bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define il inline namespace io { #define in(a) a = read()
#define out(a) write(a)
#define outn(a) out(a), putchar('\n') #define I_int ll
inline I_int read() {
I_int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x * f;
}
char F[200];
inline void write(I_int x) {
if (x == 0) return (void) (putchar('0'));
I_int tmp = x > 0 ? x : -x;
if (x < 0) putchar('-');
int cnt = 0;
while (tmp > 0) {
F[cnt++] = tmp % 10 + '0';
tmp /= 10;
}
while (cnt > 0) putchar(F[--cnt]);
}
#undef I_int }
using namespace io; using namespace std; #define N 100010 char s[N]; bool check(char c) {
if(c == 'A') return 1;
if(c == 'G') return 1;
if(c == 'T') return 1;
if(c == 'C') return 1;
return 0;
} int main() {
scanf("%s",s); int cnt = 0, ans = 0, n = strlen(s);
for(int i = 0; i < n; ++i) {
if(check(s[i])) ++cnt;
else ans = max(ans, cnt), cnt = 0;
}
ans = max(ans, cnt);
outn(ans);
}

C - GeT AC

这个C怎么比B还简单...

#include <bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define il inline namespace io { #define in(a) a = read()
#define out(a) write(a)
#define outn(a) out(a), putchar('\n') #define I_int ll
inline I_int read() {
I_int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x * f;
}
char F[200];
inline void write(I_int x) {
if (x == 0) return (void) (putchar('0'));
I_int tmp = x > 0 ? x : -x;
if (x < 0) putchar('-');
int cnt = 0;
while (tmp > 0) {
F[cnt++] = tmp % 10 + '0';
tmp /= 10;
}
while (cnt > 0) putchar(F[--cnt]);
}
#undef I_int }
using namespace io; using namespace std; #define N 100010 int n = read(), q = read();
int sum[N];
char s[N]; int main() {
scanf("%s",s+1);
for(int i = 1; i <= n; ++i) {
sum[i] = sum[i - 1];
if(s[i]=='C'&&s[i-1]=='A') {
++sum[i];
}
}
while(q--){
int l = read(), r = read();
outn(sum[r]-sum[l]);
}
}

D - We Like AGC

全场唯一有点意思的题了...直接考虑太麻烦了,dp一下。

\(f[i][j][a][b]\)表示位置i,i字符j,i-1字符a,i-2字符b。

利用map来简化判断过程: mp[1]='A'; mp[2]='C';mp[3]='G';mp[4]='T'

利用string重载了加法这个特性,也可以简化判断过程。

然后思考一下就会发现不合法的判定情况就6种(虽然我想+调了要半小时...),如下(a,b,c,d的位置是递增的)

map<int,string>mp;
bool check(int a,int b,int c, int d) { if(mp[a]+mp[c]+mp[b]=="AGC") return 0;
if(mp[a]+mp[b]+mp[d]=="AGC") return 0;
if(mp[a]+mp[c]+mp[d]=="AGC") return 0; if(mp[b]+mp[c]+mp[d]=="AGC") return 0;
if(mp[b]+mp[d]+mp[c]=="AGC") return 0; if(mp[c]+mp[b]+mp[d]=="AGC") return 0; return 1;
}

所以转移的时候再多枚举一个第i-3位的字符,就可以\(O(5^4n)\)解决这题了。注意要特判\(n\leq 2\)的情况。

#include <bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define il inline namespace io { #define in(a) a = read()
#define out(a) write(a)
#define outn(a) out(a), putchar('\n') #define I_int ll
inline I_int read() {
I_int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x * f;
}
char F[200];
inline void write(I_int x) {
if (x == 0) return (void) (putchar('0'));
I_int tmp = x > 0 ? x : -x;
if (x < 0) putchar('-');
int cnt = 0;
while (tmp > 0) {
F[cnt++] = tmp % 10 + '0';
tmp /= 10;
}
while (cnt > 0) putchar(F[--cnt]);
}
#undef I_int }
using namespace io; using namespace std; #define N 100010
const ll mod = 1e9 + 7;
int n = read();
ll f[N][5][5][5];
/*
f[i][j][a][b]位置i,字符j,i-1字符a,i-2字符b */
map<int,string>mp;
bool check(int a,int b,int c, int d) { if(mp[a]+mp[c]+mp[b]=="AGC") return 0;
if(mp[a]+mp[b]+mp[d]=="AGC") return 0;
if(mp[a]+mp[c]+mp[d]=="AGC") return 0; if(mp[b]+mp[c]+mp[d]=="AGC") return 0;
if(mp[b]+mp[d]+mp[c]=="AGC") return 0; if(mp[c]+mp[b]+mp[d]=="AGC") return 0; return 1;
} int main() {
if(n<=2) return outn(pow(4,n)), 0;
mp[0]='Q'; mp[1]='A'; mp[2]='C';mp[3]='G';mp[4]='T';
f[0][0][0][0]=1;
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= 4; ++j)
for(int a = 0; a <= 4; ++a)
for(int b = 0; b <= 4; ++b)
for(int l = 0; l <= 4; ++l)
if(check(l,b,a,j)) (f[i][j][a][b] += f[i-1][a][b][l]) %= mod;
ll ans = 0;
for(int i = 1; i <= 4; ++i) {
for(int j = 1; j <= 4; ++j) {
for(int k = 1; k <= 4; ++k) {
(ans+=f[n][i][j][k])%=mod;
}
}
}
outn(ans);
}
上一篇:H3C路由器Telnet服务配置命令


下一篇:通过Canvas及File API缩放并上传图片