LoginSignup
1
0

More than 1 year has passed since last update.

AtCoderログ:0113 - ABC 122 B

Last updated at Posted at 2021-09-15

問題

問題文

英大文字からなる文字列 $S$ が与えられます。$S$ の部分文字列 (注記を参照) であるような最も長い ACGT 文字列 の長さを求めてください。
ここで、ACGT 文字列とは A, C, G, T 以外の文字を含まない文字列です。

制約

・$S$ は長さ $1$ 以上 $10$ 以下の文字列である。
・$S$ の各文字は英大文字である

収録されている問題セット

回答

回答1 (AC)

文字列を1文字目から順番にチェックし、A,C,G,Tが続いていればその文字数を記録していけば良いでしょう。コードは以下のようになりました。

abc122b-1.cpp
#include <bits/stdc++.h>
using namespace std;

int main() {
  string s;
  cin >> s;

  int series = 0, smax = 0;
  for ( int i=0; i<(int)s.size(); i++ ) {
    if ( s.at(i)=='A' || s.at(i)=='C' || s.at(i)=='G' || s.at(i)=='T' ) {
      series += 1;
      smax = max(smax,series);
    } else {
      series = 0;
    }
  }

  cout << smax << endl;
}

調べたこと

AtCoder の解説ユーザ解説

回答1と同じ方針でした。

AtCoder の解説コンテスト全体の解説

文字列の全ての部分文字列を生成し、それがACGT文字列かをチェックしていました。

リンク

前後の記事

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0