题解:P10625 [ICPC 2013 WF] Up a Tree

· · 题解

首先这题的数据明显和原题有出入,原题中不同方案间会多一个换行,但不知道为啥洛谷这个版本没有。

做法不是特别困难。也考虑枚举所有的 Pre In Post 情况,总计也就 90 种。

然后思考怎么 check,一种比较合理的方式是区间 dp。具体来说,我们以它的 Pre 遍历作为参考。每次等价于说每次裂开成一个左区间,一个单点,一个右区间。而且得知第一棵树上的区间后后面两棵的区间也是容易得到的。

之后设计状态,发现我们其实只在意三个遍历分别遍历到了哪个函数,于是我们设计 dp_{l, r, s1, s2, s3} 表示做到了第一个遍历的 [l, r] 区间,当前三个遍历分别遍历到了哪个函数。转移是平凡的。

时间复杂度不想算,我说因为全是常数当它是 O(1) 就行。

注意一下为了方便找到字典序最小的,dp 状态直接就是答案。

#include <bits/stdc++.h>

#define sz(a) (int)((a).size())
#define all(a) (a).begin(), (a).end()
#define folr(i, a, n) for (auto i = (a); i <= (n); ++i)
#define forl(i, n, a) for (auto i = (n); i >= (a); --i)
using namespace std;
using ll = long long;
using ld = double;
string to_string(const string& v) {
    return '"' + v + '"';
}
template<class A> string to_string(const A& v) {
    string s = "{";
    for (const auto& x : v) s += (sz(s) > 1 ? ", " : "") + to_string(x);
    return s += "}";
}
void debug_out() { cerr << '\n'; }
template<class U, class... T> void debug_out(const U& x, const T&... args) {
    cerr << ' ' << to_string(x);
    debug_out(args...);
}
int wcnm = 0; 
#define sc(x) cerr << "\033[" << x << "m"
#define debug(...) //if(wcnm) sc(96), cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__), sc(39)
#define int long long
struct node
{
    bool t; 
    string s[3]; 
};
inline void init(node &x) 
{
    x.t = 0; 
    folr(i, 0, 2) x.s[i] = ""; 
}
inline node Merge(node x, node y) 
{
    node z; z.t = 0; 
    if(x.t or y.t) {z.t = 1; return z;}
    folr(i, 0, 2) z.s[i] = x.s[i] + y.s[i]; 
    return z;  
}
inline void chkmin(node &x, node y) 
{
    if(y.t) return ; 
    if(x.t) {x = y; return ;}
    folr(i, 0, 2) 
    {
        if(x.s[i] < y.s[i]) return ; 
        if(x.s[i] > y.s[i]) {x = y; return ;}
    }
    return ;
}
node dp[27][27][3][3][3]; 
inline void solve(const int &ic)
{
    vector<vector<int>> a(3); 
    folr(i, 0, 2) 
    {
        string tmp; 
        cin >> tmp; 
        folr(j, 0, sz(tmp) - 1) a[i].push_back(tmp[j] - 'A'); 
    } 
    int n = sz(a[0]); 
    folr(i, 0, 2) debug(a[i]); 
    vector<int> p; 
    folr(i, 0, 2) p.push_back(i), p.push_back(i); 
    auto work = [&]() 
    {
        folr(i, 0, n - 1) 
            folr(j, 0, n - 1) 
                folr(s1, 0, 2)
                    folr(s2, 0, 2) 
                        folr(s3, 0, 2) dp[i][j][s1][s2][s3].t = 1;
        folr(len, 1, n) 
            folr(l, 0, n - len) 
            {
                int r = l + len - 1; 
                vector<int> s(3);
                vector<int> le(3, -1), ri(3, -1);  
                le[0] = l, ri[0] = r; 
                auto get = [&](int idx, int xl, int xr, int idy, int yl, int yr) 
                {
                    vector<int> w(26, 0); 
                    assert(xr - xl + 1 == yr - yl + 1); 
                    if(xr - xl + 1 != yr - yl + 1) 
                        return 0; 
                    folr(i, xl, xr) w[a[idx][i]] = 1; 
                    folr(i, yl, yr) 
                        if(!w[a[idy][i]]) return 0; 
                    return 1;  
                };
                folr(xl, 0, n - len) 
                    if(get(0, l, r, 1, xl, xl + len - 1)) le[1] = xl, ri[1] = xl + len - 1; 
                folr(xl, 0, n - len) 
                    if(get(0, l, r, 2, xl, xl + len - 1)) le[2] = xl, ri[2] = xl + len - 1; 
                if(le[1] == -1 or le[2] == -1) continue; 
                //debug(le, ri); 
                auto check = [&](int rt, int xl, int xr, int yl, int yr) 
                {
                //  debug(rt, xl, xr, yl, yr); 
                    node tmp; 
                    tmp.t = 0;
                    node fx; init(fx);   
                    if(xl <= xr) fx = dp[xl][xr][p[s[0] * 2]][p[s[1] * 2]][p[s[2] * 2]];
                    node fy; init(fy); 
                    if(yl <= yr) fy = dp[yl][yr][p[s[0] * 2 + 1]][p[s[1] * 2 + 1]][p[s[2] * 2 + 1]];
                    if(fx.t or fy.t) return ; 
                    char c = a[0][rt] + 'A'; 
                    tmp.s[0] = c + fx.s[0] + fy.s[0]; 
                    tmp.s[1] = fx.s[1] + c + fy.s[1]; 
                    tmp.s[2] = fx.s[2] + fy.s[2] + c;
                    int xlen = xr - xl + 1; 
                    folr(id, 1, 2) 
                    {
                        if(s[id] == 0) 
                        {
                            if(a[id][le[id]] != a[0][rt]) return ; 
                            if(!get(0, xl, xr, id, le[id] + 1, le[id] + xlen)) return ;
                            if(!get(0, yl, yr, id, le[id] + xlen + 1, ri[id])) return ; 
                        }
                        else if(s[id] == 1) 
                        {
                            if(a[id][le[id] + xlen] != a[0][rt]) return ; 
                            if(!get(0, xl, xr, id, le[id], le[id] + xlen - 1)) return ;
                            if(!get(0, yl, yr, id, le[id] + xlen + 1, ri[id])) return ; 
                        } 
                        else 
                        {
                            if(a[id][ri[id]] != a[0][rt]) return ; 
                            if(!get(0, xl, xr, id, le[id], le[id] + xlen - 1)) return ;
                            if(!get(0, yl, yr, id, le[id] + xlen, ri[id] - 1)) return ; 
                        }

                    }       
                    if(!tmp.t) debug(l, r, s, tmp.s[0], tmp.s[1], tmp.s[2]); 
                    chkmin(dp[l][r][s[0]][s[1]][s[2]], tmp); 
                    return ;
                }; 
                folr(s1, 0, 2) 
                    folr(s2, 0, 2) 
                        folr(s3, 0, 2)
                        {
                            s[0] = s1, s[1] = s2, s[2] = s3; 
                            folr(i, l, r) 
                            {
                                if(s1 == 0) check(l, l + 1, i, i + 1, r); 
                                else if(s1 == 1) check(i, l, i - 1, i + 1, r); 
                                else check(r, l, i - 1, i, r - 1); 
                            }
                        } 
            }
        if(dp[0][n - 1][0][1][2].t == 0) 
        {
            folr(i, 0, 5) 
            {
                if(p[i] == 0) cout << "Pre ";
                else if(p[i] == 1) cout << "In ";
                else cout << "Post "; 
            }
            cout << "\n"; 
            folr(i, 0, 2) cout << dp[0][n - 1][0][1][2].s[i] << "\n";  
        //  cout << "\n";  
        } 
    };
    do
    {
    //  if(p[0] == 0 && p[1] == 2 && p[2] == 1 && p[3] == 2 && p[4] == 2 && p[5] == 0) wcnm = 1; 
        work();
    //  if(wcnm) return ; 
    }
    while(next_permutation(all(p))); 
    return ; 
}

signed main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int tt = 1; 
    folr(ic, 1, tt) solve(ic);
    return 0; 
}
/*
4
3 5 1
1 2 3
5 5 1
0 2 1 0 3
5 100 5
1 2 3 4 5
8 3 1
5 1 2 2 0 2 5 1
*/