[Explanations] AGC007E Shik and Copying String (greedy)

[Explanations] AGC007E Shik and Copying String (greedy)

At first reading the title thought it was a rat hole model, but found here also require different kinds of bananas can not match, but the price is not certain ...

Tracking resulting string \ (T \) each character \ (T_i \) source, may find that it is directed \ (p <i \) of the fold line, this line can be found covering \ (X \) axis a length as small as possible, the final answer is the number of inflection points such as all forms of fold line matching up to the inflection point of the polyline. Queue maintains the current match with one pair of end point of inflection of the \ (X \) coordinates, from the viewpoint of the forward \ (T \) matching each character.

To enumerate the current set \ (T_i \) , and \ (T_ {i + 1} \) matched to \ (S_p \) , the deque maintains all the coordinates of the inflection point. Consider the largest \ (P '<P, P of S_ {'} = T_i \) .

  • If \ (P = P '\) , then the \ (T_ {i + 1} \) directly from the \ (T_i \) inheritance where the past, then the \ (T_i \) matches the inflection point is a point i + a polyline all coordinates \ (<i \) inflection point.
  • If \ (the p-\ the p-not = '\) , then due to ensure \ (x \) span as small as possible, the best new polyline should be the last of all the broken line (coordinates + 1) plus (as may be required in order to match this time \ (the p-'\) ) cornering.

Code

//@winlere
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>

using namespace std;  typedef long long ll;
int n,ans;
const int maxn=1e6+5;
char S[maxn],T[maxn];
deque<int> q;
int main(){
    scanf("%d%s%s",&n,S+1,T+1);
    q.push_back(n+1);
    for(int t=n,p=n+1,offset=0;t;--t){
        int sav=p;
        while(p&&(p>t||S[p]!=T[t])) --p;
        if(p<1) puts("-1"),exit(0);
        if(sav==p){
            while(q.size()&&q.back()-offset>=t) q.pop_back();
            q.push_back(t+offset);
        }else{
            ++offset;
            if(t!=p) ans=max(ans,(int)q.size()),q.push_front(p+offset);
        }
    }
    cout<<ans<<endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/winlere/p/12315458.html