跳到正文

题解

P8853 [NOI1997] 文件匹配

sdqe约 23 分钟
原文
文章信息与版本

文章信息

发布
2022年11月17日
收录
2025年12月12日
更新
2026年11月11日
篇幅
约 23 分钟
题目
P8853

版本

  1. v1当前

思路

这题一开始用搜索剪枝匹配用bitset优化过了,爆搜的复杂度保证不了,可以用某种方法构造卡满整棵搜索树,虽然节点数不多,但是操作太慢,用bitset优化还是不够,下面写一个枚举整颗搜索树的。

每个串的长度不超过 88,观察每个串最多有多少不同的匹配串,我们枚举一个串的子集 SS,然后用这个子集把串分割成了 ∣S∣+1|S| + 1 份,每一份里面有可能是 ′∗′'*' 拼上若干个 ′?′'?' 或者只有若干个 ′?′'?'。写一发爆搜发现每个串的匹配串不会超过 3804838048。最多只有 250250 个串,即答案只可能在 38048×25038048 \times 250 个串里,串压成一个 int128int128 然后暴力检查即可。

如果答案串是允许超过 88,那下面这个case应该要输出,很多通过的代码限制了长度。

C++
Input:
11
EBBBBBBB +
BBBBBBBD +
E -
D -
ED -
EBD -
EBBD -
EBBBD -
EBBBBD -
EBBBBBD -
EBBBBBBD -

Output:
2
*BBBBBBB*

这个是枚举所有情况的代码。

C++
#include <bits/stdc++.h>
#include <type_traits>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define allr(x) (x).rbegin(),(x).rend()
#define x first
#define y second
#define next nxt
using namespace std;

typedef long long ll;
typedef long long LL;
typedef unsigned long long ull;
typedef __int128 xl;

typedef pair<int,int> pii;
typedef pair<int,pii> piii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef vector<ll> vl;

template<typename Ta, typename Tb>
std::ostream & operator << (std::ostream & o, const std::pair<Ta, Tb> &p)
{
    o << p.first << " " << p.second << ";";
    return o;
}

std::ostream & operator << (std::ostream & o, xl v)
{
    if(v < 0)o << '-', v *= -1;
    if(v >= 10)o << v / 10;
    o << (int)(v % 10);
    return o;
}

template<typename T>
struct is_string : public std::false_type { };

template <>
struct is_string<std::string> : public std::true_type {};

template<typename T, template<class, class...> typename C, class... Args>
typename std::enable_if<!is_string<C<T, Args...>>::value, std::ostream&>::type
        operator << (std::ostream & o, const C<T, Args...> &obj)
{
    for(const auto & x : obj)
    {
        o << x << " ";
    }
    return o;
}

#ifdef local
#include "mydebug.h"
#define de(...) pretty_dbg(cerr,__LINE__,__func__,##__VA_ARGS__);
#else
#define de(...) void(0);
#endif


mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T>
T rnd(T a, T b)
{
    return uniform_int_distribution<T>(a, b)(rng);
}
template<class T>
T rnd()
{
    return uniform_int_distribution<T>()(rng);
}

const int N = 2e5 + 8;
const int M = 1e6 + 8;


const int inf = 1e9 + 1;
const ll INF = 1ll<<61;
const int mod = 1e9 + 7;
const int B = 20;
const double eps = 1e-8;

using fb =  bitset<9 * 250>;
fb match[64];

fb good, bad;

int c2i(char c)
{
    if(c >= 'a' && c <= 'z') return c - 'a';
    else if(c >= 'A' && c <= 'Z') return c - 'A' + 26;
    else if(c >= '0' && c <= '9') return c - '0' + 52;
    else if(c == '?')return 62;
    else if(c == '*')return 63;
}

char i2c(int i)
{
    if(i < 26) return i + 'a';
    else if(i < 52) return i - 26 + 'A';
    else if(i < 62) return i - 52 + '0';
    else if(i == 62) return '?';
    else if(i == 63) return '*';

}

xl bg[20];
auto che(const vi &b, int type, const string &s)
{
    vector<pair<xl,int>> tmp;
    tmp.reserve(200);
    auto se = [&](auto & self, int st, int len, xl v)
    {
        if(st == b.size())
        {
            tmp.emplace_back(v, type * 64 + len);
            return;
        }
        xl tmpv = v;
        for(int df = 0; df <= b[st] - b[st - 1] - 1; df++)
        {
            int can0 = (df != b[st] - b[st - 1] - 1);
            for(int g = 1; g >= can0; g--)
            {
                xl tmppv = tmpv;
                if(g == 1) tmppv = tmpv + 63 * bg[len + df + g - 1];
                if(b[st] < s.size()) tmppv = tmppv + c2i(s[b[st]]) * bg[len + df + g];
                self(self, st + 1, len + g + df + (b[st] < s.size()), tmppv);
            }
            tmpv = tmpv + 62 * bg[len + df];
        }
    };
    se(se, 1, 0, 0);
    return tmp;
}
int cnt[251];
auto bs(const auto &g)
{
    bg[0] = 1;
    for(int i = 1; i < 10; i++) bg[i] = bg[i - 1] * 64;
    vector<pair<xl,int>> st;
    st.reserve(1e7);
    int gp = -1;
    for(int i = 0; i < g.size(); i++)
    {
        const auto& [s, b] = g[i];
        int n = s.size();
        for(int j = 0; j < (1 << n); j++)
        {
            vi tp{-1};
            for(int k = 0; k < n; k++)
            {
                if(j >> k & 1)tp.pb(k);
            }
            tp.pb(n);
            auto sta = che(tp, i, s);
            st.insert(st.end(), all(sta));
        }
        if(b) gp = i;
    }
    auto ans = make_pair(0, string());
    sort(all(st));
    for(int i = 0, j; i < st.size(); i = j)
    {
        j = i;
        bool f = 0;
        int mt = 0;
        int len = st[i].y % 64;
        while(j < st.size() && st[j].x == st[i].x)
        {
            int id = st[j].y / 64;
            if(id > gp)f = 1;
            if(cnt[id] != i + 1 && id <= gp)
            {
                mt++;
                cnt[id] = i + 1;
            }
            ++j;
        }
        if(f)continue;
        if(ans.x < mt || (ans.x == mt && ans.y.size() > len))
        {
            ans.x = mt;
            xl v = st[i].x;
            ans.y = "";
            for(int k = 0; k < len; k++)
            {
                ans.y += i2c(v % 64);
                v /= 64;
            }
        }
    }
    return ans;
}

void solve()
{

    int n;
    cin >> n;
    vector<pair<string, bool>> g(n);
    for(auto &[s, b] : g)
    {
        string op;
        cin >> s >> op;
        b = op[0] == '+';
    }
    sort(all(g), [](const auto &a, const auto &b)
    {
        return a.y > b.y;
    });
    auto ans = bs(g);
    cout << ans.x << endl << ans.y << endl;
    return;
}
int main()
{
#ifdef local
    freopen("in.txt", "r", stdin);
#endif // local
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int T = 1;

#define mulcases

#ifdef mulcase
    cin >> T;
#endif // mulcase
    while(T--)
    {
        solve();
    }

    return 0;
}

这个是搜索剪枝的代码。

C++
#include <bits/stdc++.h>
#include <type_traits>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define allr(x) (x).rbegin(),(x).rend()
#define x first
#define y second
#define next nxt
using namespace std;

typedef long long ll;
typedef long long LL;
typedef unsigned long long ull;
typedef __int128 xl;

typedef pair<int,int> pii;
typedef pair<int,pii> piii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef vector<ll> vl;

template<typename Ta, typename Tb>
std::ostream & operator << (std::ostream & o, const std::pair<Ta, Tb> &p)
{
    o << p.first << " " << p.second << ";";
    return o;
}

std::ostream & operator << (std::ostream & o, xl v)
{
    if(v < 0)o << '-', v *= -1;
    if(v >= 10)o << v / 10;
    o << (int)(v % 10);
    return o;
}

template<typename T>
struct is_string : public std::false_type { };

template <>
struct is_string<std::string> : public std::true_type {};

template<typename T, template<class, class...> typename C, class... Args>
typename std::enable_if<!is_string<C<T, Args...>>::value, std::ostream&>::type
        operator << (std::ostream & o, const C<T, Args...> &obj)
{
    for(const auto & x : obj)
    {
        o << x << " ";
    }
    return o;
}

#ifdef local
#include "mydebug.h"
#define de(...) pretty_dbg(cerr,__LINE__,__func__,##__VA_ARGS__);
#else
#define de(...) void(0);
#endif


mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T>
T rnd(T a, T b)
{
    return uniform_int_distribution<T>(a, b)(rng);
}
template<class T>
T rnd()
{
    return uniform_int_distribution<T>()(rng);
}

const int N = 2e5 + 8;
const int M = 1e6 + 8;


const int inf = 1e9 + 1;
const ll INF = 1ll<<61;
const int mod = 1e9 + 7;
const int B = 20;
const double eps = 1e-8;

using fb =  bitset<9 * 250>;
fb match[64];

fb good, bad;

int c2i(char c)
{
    if(c >= 'a' && c <= 'z') return c - 'a';
    else if(c >= 'A' && c <= 'Z') return c - 'A' + 26;
    else if(c >= '0' && c <= '9') return c - '0' + 52;
    else if(c == '?')return 62;
    else if(c == '*')return 63;
}

char i2c(int i)
{
    if(i < 26) return i + 'a';
    else if(i < 52) return i - 26 + 'A';
    else if(i < 62) return i - 52 + '0';
    else if(i == 62) return '?';
    else if(i == 63) return '*';

}

fb gao(fb now, int v)
{
    fb ans;
    if(v == 63)
    {
        if(now[0])
        {
            ans.set();
            return ans;
        }
        else
        {
            ans = move(now);
            for(int i = 0; i < 8; i++)
            {
                ans |= ans << 1;
                ans &= match[62];
            }
        }
    }
    else
    {
        ans = now << 1 & match[v];
    }
    return ans;
}

int goodcnt = 0;
int badcnt = 0;
int possible_match(const fb &now)
{
    if(now[0] == 1)
    {
        return goodcnt;
    }
    int cnt = 0;
    for(int i = now._Find_next(0) / 9; i < goodcnt; i = now._Find_next((i + 1) * 9) / 9)
    {
        cnt++;
    }
    return cnt;
}

fb cur;

int maxans = 0;
int minlen = 0;
int ans[20];
int p[20];

string debug()
{
    string s;
    for(int i = 1; i <= 19; i++)
    {
        s += i2c(p[i]);
    }
    return s;
}

void dfs(int len)
{
    if(len)
    {
        cur = gao(cur, p[len]);
        auto pm = possible_match(cur);
        if(pm < maxans || (pm == maxans && len >= minlen)) return;

        int goodp = (cur & good).count();
        if(maxans < goodp || (maxans == goodp && minlen > len))
        {
            if(pm == 0 || 0 == (cur & bad).count())
            {
                maxans = goodp;
                minlen = len;
                memcpy(ans, p, sizeof(ans));
            }
        }

        if(pm < maxans || (pm == maxans && len + 1 >= minlen)) return;
        if(p[len] == 63 && (goodp < maxans || (goodp == maxans && len + 1 >= minlen))) return;
    }
    fb tmp = cur;
    for(int i = 63; i >= 0; i--)
    {
        if(len && (p[len] == 63 || p[len] == 62) && i == 63) continue;
        p[len + 1] = i;
        dfs(len + 1);
        cur = tmp;
    }
}


void solve()
{
    int n;
    cin >> n;
    vector<pair<string, bool>> g(n);
    for(auto &[s, b] : g)
    {
        string op;
        cin >> s >> op;
        b = op[0] == '+';
        goodcnt += (int)b;
    }
    sort(all(g), [](const auto &a, const auto &b)
    {
        return a.y > b.y;
    });
    n = g.size();
    badcnt = n - goodcnt;
    for(int j = 0; j < n; j++)
    {
        for(int k = 0; k < (int)g[j].x.size(); k++)
        {
            match[c2i(g[j].x[k])][j * 9 + k + 1] = 1;
        }
    }
    match[62].set();
    for(int j = 0; j < n; j++)
    {
        match[62][9 * j] = 0;
        cur[9 * j] = 1;
    }

    for(int j = 0; j < n; j++)
    {
        if(g[j].y)
        {
            good[9 * j + g[j].x.size()] = 1;
        }
        else
        {
            bad[9 * j + g[j].x.size()] = 1;
        }
    }
    dfs(0);
    cout << maxans << endl;
    for(int i = 1; i <= minlen; i++)
    {
        cout << i2c(ans[i]);
    }
}
int main()
{
#ifdef local
    freopen("in.txt", "r", stdin);
#endif // local
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int T = 1;

#define mulcases

#ifdef mulcase
    cin >> T;
#endif // mulcase
    while(T--)
    {
        solve();
    }

    return 0;
}
原文