QOJ.ac

QOJ

시간 제한: 2 s 메모리 제한: 1024 MB 총점: 100 해킹 가능 ✓

#18675. 동전 쌍 뒤집기

통계

$N$ 個のコインが一列に並んでいる。各コインは表または裏が上になるように置かれている。

あなたは次の操作を好きなだけ繰り返すことができる。

  • 隣り合っていて同じ面が上になっている 2 枚のコインを選び、両方をひっくり返す。

表が上になっているコインをひっくり返すと裏になり、裏が上になっているコインをひっくり返すと表になる。

各コインの状態が与えられたとき、すべてのコインを表が上になるようにできるか判定し、可能ならばそのために必要な操作の最小回数を求めよ。

入力

最初の行にテストケースの個数 $T$ が与えられる。($1 \le T \le 10\,000$)

テストケースの最初の行にはコインの枚数 $N$ が与えられる。($1 \le N \le 1\,000\,000$)

テストケースの 2 行目には、各コインの状態を表す長さ $N$ の文字列 $S$ が与えられる。すべての $1 \leq i \leq N$ について、$S$ の $i$ 番目の文字は、$i$ 番目のコインが表の場合は H、裏の場合は T である。

すべてのテストケースに対する $N$ の合計は $1\,000\,000$ を超えない。

出力

各テストケースについて、与えられた状態からすべてのコインを表が上になるようにするために必要な操作の最小回数を一行に出力する。不可能な場合は $-1$ を代わりに出力する。

入出力例

入力 1

2
5
HTHHT
2
HT

出力 1

3
-1

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.