Editorial for VOSHANDL


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Lưu ý: Các code mẫu dưới đây chỉ mang tính tham khảo và có thể không AC được bài tập này

Code mẫu của ladpro98

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
const int N = 100050;
using namespace std;
char S[N], S1[N], S2[N];
int nch, n;
int prev[N][75], ch[1000], last[1000], chr[1000];

bool bad(char *a, int len) {
    int pos = n;
    for(int i = len - 1; i >= 0; i--) {
        //cout << a[i] << endl;
        if (ch[a[i]] == 0) return true;
        if (prev[pos][ch[a[i]]] < 0) return true;
        pos = prev[pos][ch[a[i]]];
    }
    return false;
}

int main() {
    ios :: sync_with_stdio(0); cin.tie(0);
    cin >> S; n = strlen(S);
    nch = 0;
    for(int i = 0; i < n; i++) {
        if (last[S[i]] == 0) {
            nch++;
            ch[S[i]] = nch;
            chr[nch] = S[i];
        }
        last[S[i]] = i;
    }
    for(int j = 1; j <= nch; j++)
        prev[0][j] = -1;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= nch; j++) 
            if (S[i - 1] == chr[j]) prev[i][j] = i - 1;
            else prev[i][j] = prev[i - 1][j];
    int ntest;
    cin >> ntest;
    while (ntest--) {
        cin >> S1 >> S2;
        int p = strlen(S1), q = strlen(S2);
        if (bad(S1, p) || bad(S2, q)) {
            cout << -4 << '\n'; continue;
        }
        int x = last[S1[p - 1]], y = last[S2[q - 1]];
        if (x == y) cout << -3 << '\n'; else 
        if (x < y) cout << -1 << '\n'; else
        cout << -2 << '\n';
        //cout << x << ' ' << y << endl;
    }
    return 0;
}

Code mẫu của skyvn97

#include<bits/stdc++.h>
#define MAX   100100
#define MAXC   260
#define NMOD   3
#define FOR(i,a,b) for (int i=(a),_b=(b);i<=_b;i=i+1)
#define FORD(i,b,a) for (int i=(b),_a=(a);i>=_a;i=i-1)
#define REP(i,n) for (int i=0,_n=(n);i<_n;i=i+1)
using namespace std;
int prev[MAX][MAXC];
vector<int> chPos[MAXC];
string a;
int n,q;
void init(void) {
    cin>>a;
    n=a.size();
    FOR(i,1,n) chPos[a[i-1]].push_back(i);
    FOR(i,2,n) {
        REP(j,MAXC) prev[i][j]=prev[i-1][j];
        prev[i][a[i-2]]=i-1;
    }
}
bool canJump(const string &s,int pos) {
    if (s.size()<=1) return (true);
    FORD(i,(int)s.size()-2,0) {
        if (prev[pos][s[i]]<1) return (false);
        pos=prev[pos][s[i]];
    }
    return (true);
}
int lastPos(const string &s) {
    vector<int> &cur=chPos[s[s.size()-1]];
    if (cur.empty()) return (-1);
    return (canJump(s,cur.back())?cur.back():-1);
}
int answer(const string &s,const string &t) {
    int ls=lastPos(s);
    int lt=lastPos(t);
    if (ls<0 || lt<0) return (-4);
    if (ls<lt) return (-1);
    if (ls>lt) return (-2);
    return (-3);
}
void process(void) {
    cin>>q;
    REP(zz,q) {
        string s,t;
        cin>>s>>t;
        printf("%d\n",answer(s,t));
    }
}
int main(void) {
    ios::sync_with_stdio(false);
    init();
    process();
    return 0;
}

Comments

Please read the guidelines before commenting.


There are no comments at the moment.