0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

最長AB列を求める

Last updated at Posted at 2019-07-14

#最長AB列を求める
文字'A'と文字'B'からなる文字列Sがあります。
Sに含まれる連続した区間で'A'と'B'の数が等しいものの最長の長さを答えよ。

#ソースコード

<?php
//入力値
$nyu = "ABBBAAAAA";
//評価する値(それぞれの区切った値を配列に格納)
$hyo1 = "A";
$hyo2 = "B";
$arrray1=explode($hyo1, $nyu);
$arrray2=explode($hyo2, $nyu);

//A、B配列の数分だけ、レングスを格納する
for($i = 0; $i < count($arrray1); $i++){
  $arr1_len[$i] = strlen($arrray1[$i]);
}
for($i = 0; $i < count($arrray2); $i++){
  $arr2_len[$i] = strlen($arrray2[$i]);
}


//取得した配列のmax値を取得
$arr1_max = max($arr1_len);
$arr2_max = max($arr2_len);
//A,B双方で取得したmax値の低いほうを取得
$arr_min = min($arr1_max,$arr2_max);

//結果値
$kekka = $arr_min * 2;
print($kekka);

?>
0
0
1

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?