You are given a necklace consists of N beads linked as a circle. Each bead is either crystal or jade.

Now, your task is:

1. Choose an arbitrary position to cut it into a chain.

2. Choose either direction to collect it.

3. Collect all the beads in the chosen direction under the constraint that the number of crystal beads in your hand is not less than the jade at any time.

Calculate the number of ways to cut meeting the constraint

Now, your task is:

1. Choose an arbitrary position to cut it into a chain.

2. Choose either direction to collect it.

3. Collect all the beads in the chosen direction under the constraint that the number of crystal beads in your hand is not less than the jade at any time.

Calculate the number of ways to cut meeting the constraint

In the first line there is an integer T, indicates the number of test cases. (T<=50)

Then T lines follow, each line describes a necklace. ‘C’ stands for a crystal bead and ‘J’ stands for a jade bead. The length of necklace is between 2 and 10^6.

Then T lines follow, each line describes a necklace. ‘C’ stands for a crystal bead and ‘J’ stands for a jade bead. The length of necklace is between 2 and 10^6.

提交代码