1. 程式人生 > >【AGC026】E - Synchronized Subsequence

【AGC026】E - Synchronized Subsequence

tor 最大 直接 ++ fir sign enter || long long

題解

這個E為啥這麽水
不過這個數據範圍不用寫SA或者SAM直接暴力即可,好評(當然其實寫起來也不難。。。)
如果b在a前就一直選,選到某個最大的a的位置會更改成一段a在b前
如果這個a在b前是最後一部分就取到所有的abababab...前,否則就跳過這一段
具體就用string維護一下後綴能取到的max字符串就好了

代碼

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <ctime>
#include <map>
#include <set>
#define fi first
#define se second
#define pii pair<int,int>
//#define ivorysi
#define mp make_pair
#define pb push_back
#define enter putchar(‘\n‘)
#define space putchar(‘ ‘)
#define MAXN 6005
using namespace std;
typedef long long int64;
typedef double db;
typedef unsigned int u32;
template<class T>
void read(T &res) {
    res = 0;T f = 1;char c = getchar();
    while(c < ‘0‘ || c > ‘9‘) {
        if(c == ‘-‘) f = -1;
        c = getchar();
    }
    while(c >= ‘0‘ && c <= ‘9‘ ) {
        res = res * 10 - ‘0‘ + c;
        c = getchar();
    }
    res *= f;
}
template<class T>
void out(T x) {
    if(x < 0) {x = -x;putchar(‘-‘);}
    if(x >= 10) {
        out(x / 10);
    }
    putchar(‘0‘ + x % 10);
}
namespace task {
    char s[MAXN];
    int N,a[MAXN],b[MAXN],tota,totb,pos[MAXN];
    string str[MAXN];
    bool vis[MAXN];
    void Init() {
        read(N);
        scanf("%s",s + 1);
        tota = 0;totb = 0;
        for(int i = 1 ; i <= 2 * N ; ++i) {
            if(s[i] == ‘a‘) {a[++tota] = i;pos[i] = tota;}
            else {b[++totb] = i;pos[i] = totb;}
        }
    }
    void Solve() {
        str[2 * N + 1] = "";
        for(int i = 2 * N ; i >= 1 ; --i) {
            str[i] = max(str[i],str[i + 1]);
            string t = "";
            int p = pos[i];
            if(s[i] == ‘a‘ && a[p] < b[p]) {
                str[i] = max(str[i],"ab" + str[b[p] + 1]);
            }
            else if(s[i] == ‘b‘ && b[p] < a[p]) {
                memset(vis,0,sizeof(vis));
                int maxv = a[p];
                for(int j = i ; j <= maxv ; ++j) {
                    if(s[j] == ‘b‘) {
                        p = pos[j];
                        vis[b[p]] = 1;vis[a[p]] = 1;
                        maxv = max(a[p],maxv);
                    }
                    if(vis[j]) t += s[j];
                }
                t += str[maxv + 1];
                str[i] = max(str[i],t);
            }
        }
        cout<<str[1]<<endl;
    }
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    task::Init();
    task::Solve();
    return 0;
}

【AGC026】E - Synchronized Subsequence