2020 Winter Holiday [gmoj2194] [censor] [Stack Simulation]

Title description

Farmer John has ordered Good Hooves keeping magazines for his cows so that they have enough materials to read. Unfortunately, the latest issue contains articles on how to cook the perfect steak, and FJ does not want his cows to see this rather indecent article (obviously, this magazine needs better editorial supervision).
FJ took all the text from the magazine and created a character string s with a length less than or equal to 10 ^ 6. From now on, he wants to delete a substring T to review inappropriate content. To do this, farmer John finds the T that appears in S and deletes it. Then, he repeats the process again, deleting the appearing T, until the S is not present. Note that after deleting once, a new T may be created. This T did not exist before.
Please output the final content of S determined by FJ after the review.

Input

The first line will contain S and the
second line will contain T. The length of T is at most the length of S. All characters of S and T are lowercase letters (range a… z)

Output

S after complete deletion. Ensure that S will not become empty during the deletion process.

Sample input

whatthemomooofun
moo

Sample output

whatthefun

analysis

Let's blatantly give a bit of cxy here %%.
I really can't think of a stack simulation ... I made it after listening to the explanation.
Press one character at a time, and then determine whether the length (t) characters are equal to t (pattern string), and then pop the stack.
Insert picture description here

Code on

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
string s,t,s1;
int l,len;
int p;
char c[1000001];
int main(){
	freopen("censor.in","r",stdin);
    freopen("censor.out","w",stdout);
	cin>>s;
	cin>>t;
    l=-1;
    len=s.size();
	for(int i=0;i<len;i++)
	{
        l++; 
        if((s[l]==t[t.size()-1])&&(l>=t.size()-1)) 
		{
        	p=1;
            for(int j=l-t.size()+1;j<=l;j++) 
            {
            	if(s[j]!=t[j-(l-t.size()+1)])
				{
                    p=0;
                    break;
                }
			}
            if(p!=0)
            {
            	s.erase(l-t.size()+1,t.size());
				l=l-t.size();
			}
        }
    }
    cout<<s;
    fclose(stdin);
    fclose(stdout);
    return 0;
}

Published 110 original articles · 100 praises · views 8029

Guess you like

Origin blog.csdn.net/dglyr/article/details/104826975