## Iron, Magnet and Wall Codechef November Long challenge 2020 solution with code explanation -

Chef loves to play with iron (Fe) and magnets (Ma). He took a row of cells (numbered through ) and placed some objects in some of these cells. You are given a string with length describing them; for each valid , the -th character of is one of the following:

- 'I' if the -th cell contains a piece of iron
- 'M' if the -th cell contains a magnet
- '_' if the -th cell is empty
- ':' if the -th cell contains a conducting sheet
- 'X' if the -th cell is blocked

If there is a magnet in a cell and iron in a cell , the *attraction power* between these cells is , where is the number of cells containing sheets between cells and . This magnet can only *attract* this iron if and there are no blocked cells between the cells and .

Chef wants to choose some magnets (possibly none) and to each of these magnets, assign a piece of iron which this magnet should attract. Each piece of iron may only be attracted by at most one magnet and only if the attraction power between them is positive and there are no blocked cells between them. Find the maximum number of magnets Chef can choose.

### Input

- The first line of the input contains a single integer denoting the number of test cases. The description of test cases follows.
- The first line of each test case contains two space-separated integers and .
- The second line contains a single string with length .

### Output

For each test case, print a single line containing one integer ― the maximum number of magnets that can attract iron.

### Constraints

- contains only characters 'I', 'M', '_', ':' and 'X'
- the sum of over all test cases does not exceed

### Subtasks

Subtask #1 (30 points): there are no sheets, i.e. does not contain the character ':'

Subtask #2 (70 points): original constraints

### Example Input

```
2
4 5
I::M
9 10
MIM_XII:M
```

### Example Output

```
1
2
```

### Explanation

Example case 1: The attraction power between the only magnet and the only piece of iron is . Note that it decreases with distance and the number of sheets.

Example case 2: The magnets in cells and can attract the piece of iron in cell , since the attraction power is in both cases. They cannot attract iron in cells or because there is a wall between them.

The magnet in cell can attract the pieces of iron in cells and ; the attraction power is and respectively.

link-https://www.codechef.com/NOV20B/problems/FEMA2

### Solution-

#### Read also -https://www.technicalkeeda.in/2020/11/magical-candy-problem-solution-codechef.html

**Must Join Telegram channel for Editorial -**

## Comments

## Post a Comment