CF1520E.Arranging The Sheep

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are playing the game "Arranging The Sheep". The goal of this game is to make the sheep line up. The level in the game is described by a string of length nn, consisting of the characters '.' (empty space) and '*' (sheep). In one move, you can move any sheep one square to the left or one square to the right, if the corresponding square exists and is empty. The game ends as soon as the sheep are lined up, that is, there should be no empty cells between any sheep.

For example, if n=6n=6 and the level is described by the string "**.*..", then the following game scenario is possible:

  • the sheep at the 44 position moves to the right, the state of the level: "**..*.";
  • the sheep at the 22 position moves to the right, the state of the level: "*.*.*.";
  • the sheep at the 11 position moves to the right, the state of the level: ".**.*.";
  • the sheep at the 33 position moves to the right, the state of the level: ".*.**.";
  • the sheep at the 22 position moves to the right, the state of the level: "..***.";
  • the sheep are lined up and the game ends.

For a given level, determine the minimum number of moves you need to make to complete the level.

你正在玩一款名为“排列绵羊”的游戏。该游戏的目标是让所有绵羊排成一列。游戏关卡由一个长度为 nn 的字符串描述,字符串中只包含字符 '.'(空格)和 '*'(绵羊)。在一次操作中,你可以将任意一只绵羊向左或向右移动一格,前提是目标位置存在且为空。

当所有绵羊排成一列(即任意两只绵羊之间不存在空格)时,游戏结束。

例如,若 n=6n=6,关卡字符串为 "**.*..",则可能发生如下游戏过程:

  • 位于第 44 位的绵羊向右移动,关卡状态变为 "**..*.";
  • 位于第 22 位的绵羊向右移动,关卡状态变为 "*.*.*.";
  • 位于第 11 位的绵羊向右移动,关卡状态变为 ".**.*.";
  • 位于第 33 位的绵羊向右移动,关卡状态变为 ".*.**.";
  • 位于第 22 位的绵羊向右移动,关卡状态变为 "..***.";
  • 绵羊已排成一列,游戏结束。

对于给定的关卡,求完成该关卡所需的最少操作次数。

输入格式

The first line contains one integer tt (1≤t≤1041 \le t \le 10^4). Then tt test cases follow.

The first line of each test case contains one integer nn (1≤n≤1061 \le n \le 10^6).

The second line of each test case contains a string of length nn, consisting of the characters '.' (empty space) and '*' (sheep) — the description of the level.

It is guaranteed that the sum of nn over all test cases does not exceed 10610^6.

第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)。随后是 tt 组测试数据。

每组测试数据的第一行包含一个整数 nn(1≤n≤1061 \le n \le 10^6)。

每组测试数据的第二行包含一个长度为 nn 的字符串,由字符 '.'(空格)和 '*'(绵羊)组成——表示关卡的布局。

保证所有测试数据的 nn 之和不超过 10610^6。

输出格式

For each test case output the minimum number of moves you need to make to complete the level.

对于每个测试用例,输出完成该关卡所需的最少移动次数。

输入输出样例

  • 输入#1

    5
    6
    **.*..
    5
    *****
    3
    .*.
    3
    ...
    10
    *.*...*.**

    输出#1

    1
    0
    0
    0
    9

输入解题思路,AI测评打分。不知道怎么写?

首页