[Cattle off network] [thinking staining problems, simulation]

Judge Online : cattle off network NOIP2018 before training camp - improve group (Session 8) T1

The Label : thinking problems, simulation

ps: this title was changed due to the off-line simulation game, so the following topics describe the adaptation, the subject is still the same.


Title Description

As we all know, Sheldon is a strong sense of territory people.

Leonard and Sheldon recently bought a new sofa together, this sofa is very long, on top of a total of nn a cushion, meaning that only nn locations, and each location can only do one person. After careful selection in Sheldon, he found a position to submit to long-term occupy this position Leonard.

Finally, Leonard Sheldon can no longer tolerate the unreasonable demands of the sofa seat, and Sheldon to fight for sofa seat! !

Leonard will select several consecutive position (may be zero) and occupied them. Sheldon certainly will not back down, he, like several consecutive selection as Sheldon position (may be zero) and occupied them. Then Leonard is not happy, so he occupied some positions. Sheldon and upset. . . . . .

Finally, both tired, because just too excited, so forget what position he once occupied, just remember what his last occupied the position (a position that may be occupied by angry Sheldon Leonard then occupied, and any time a position can only be occupied by at most one person). Coincidentally, all the final position are occupied. Since the middle of Leonard and Sheldon could go to the bathroom, so the two are not alternately occupied the position, but some people may occupy the position twice.

Penny was at the side of silence to remember the chronological order of two people occupied Penny doubt due to his memory, it got old X to determine whether what is written in the order of their own right. Simply put, is to let the old X ascertain whether there is some kind of occupation scheme makes the occupation to last with Leonard and Sheldon's "territory" in line.

Entry

The first row contains an integer T, T represents a total data set of the present set of test points.

The first row of each group is a test point by the R and B component color string s represents the final grid. R represents Sheldon territory, B represents Leonard territory, while the length of the string is n.

The second line is the F and L string composed t m th operation F represents a particular operation is Sheldon, L represents a Leonard operation, while the length of the string is m.

Export

The test point in each line of output, if the conditions are satisfied or output No. outputs Yes

Sample

Input

3
R
FL
RRRBR
FFFF
BRRBBRB
LFL

Output

Yes
No
Yes

Hint

100% of the data satisfies \ (n, m≤100000 \)

All data satisfies \ (t ≦ 20 \) .

answer

Being the time axis to simulate the situation will be very complicated, trying to find the law to see sample found nothing, not quite do.

But I found that the color must be the last to leave that section of the final sum (nonsense). We try to reverse simulation operations against the timeline, appropriate, TianTu color becomes eliminate color , why do these two equivalent. See the examples below:

When the reverse: In the first t seconds, we remove the middle of this period of R :

T-s: \ ([] [] [] [] BBBRRBBB [] [] [] \)

The first t-1 seconds: \ ([] [] [] [] the BBB [] [] the BBB [] [] [] \)

When the positive sequence: the first t-1 second, we will cover in some R B on

The first t-1 seconds: \ ([] [] [] [] BBBBBBBB [] [] [] \)

T-s: \ ([] [] [] [] BBBRRBBB [] [] [] \)

In contrast, the case where t-1 seconds, but the time to do the reverse: the middle empty section, to make positive sequence: the middle is connected to. But there is actually no difference. Because reverse operation, the middle section can no longer be filled with color , so we can ends B to merge , and this also corresponds to a period of time that the positive sequence.

Practice came out, as long as the simulation of the process just fine. Of course, the simulation does not really need to use something like a doubly linked list to maintain, we began recording a \ (cntb, cntr \) to indicate when the initial L number of blocks , the number of block R . Then from the back to the subject according to the sequence of operations to remove a piece.

Note that, when the final judgment is \ (CNTB <= 0 \) && \ (CNTR <= 0 \) , taking ≤ because coloring can choose not to intermediate operations.

code show as below:

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int main(){
    int T;scanf("%d",&T);
    while(T--){
        char a[N],op[N];
        scanf("%s",a+1);scanf("%s",op+1);
        int len=strlen(a+1),round=strlen(op+1);
        int cntr=0,cntb=0;
        for(int i=1;i<=len;i++)if(a[i]!=a[i-1]){
            if(a[i]=='R')cntr++;
            else cntb++;
        }
        for(int i=round;i>=1;i--){
            if(op[i]=='L'){
                cntb--;if(cntr>=2)cntr--;
            }
            else{
                cntr--;if(cntb>=2)cntb--;
            }
        }
        if(cntb<=0&&cntr<=0)puts("Yes");
        else puts("No");
    }
}

Guess you like

Origin www.cnblogs.com/Tieechal/p/11487846.html