【BZOJ1195】【HNOI2006】最短母串(AC自动机,状压,BFS)

Description

click me

Solution

考虑建出所有串的AC自动机,然后直接在AC自动机上进行广搜,队列的每个元素存下已经包含了哪几个串(状压)、队列中的前驱元素位置,BFS可以保证字典序最小。

菜得KJ的我居然调了一个晚上?!果然太菜了。。。

Source

/************************************************
 * Au: Hany01
 * Date: Mar 6th, 2018
 * Prob: [BZOJ1195][HNOI2006]最短母串
 * Email: [email protected]
************************************************/

#include

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define fir first
#define sec second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1000000007)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia

template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }

inline int read()
{
    register int _, __; register char c_;
    for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
    for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

inline void File()
{
#ifdef hany01
    freopen("bzoj1195.in", "r", stdin);
    freopen("bzoj1195.out", "w", stdout);
#endif
}

int ch[12 * 601][26], fail[12 * 601], stt[12 * 601], n, cnt;
bool vis[12 * 601][1 << 12];
char s[601];

inline void add(char* s, int id)
{
    register int u = 0;
    for (register int i = 0, len = strlen(s); i < len; ++ i) {
        int& nex = ch[u][s[i] - 65];
        if (!nex) nex = ++ cnt;
        u = nex;
    }
    stt[u] |= 1 << id;
}

int qu[12 * 601];
inline void getfail()
{
    register int u, v, w, tail, head;
    head = 1, tail = 0;
    rep(i, 26) if (u = ch[0][i]) qu[++ tail] = u, fail[u] = 0;

    while (head <= tail) {
        u = qu[head], ++ head;
        rep(i, 26) if (v = ch[u][i]) {
            qu[++ tail] = v, w = fail[u];
            while (!ch[w][i] && w) w = fail[w];
            fail[v] = ch[w][i];
            stt[v] |= stt[ch[w][i]];
        }
    }
}

struct Item
{
    int id, lastpos;
    short lastch;
    int stt;
    Item(int id = 0, int lastpos = 0, int lastch = 0, int stt = 0): id(id), lastpos(lastpos), lastch(lastch), stt(stt) {}

};

Item q[12 * 50 * (1 << 12)], u;
int head, tail;

inline void Print(Item u)
{
    register short List[12 * 51];
    register int cnt;
    cnt = 0;
    while (u.id) List[++ cnt] = u.lastch, u = q[u.lastpos];
    Fordown(i, cnt, 1) putchar(List[i] + 65);
    putchar('\n');
}

inline void Ins(Item u, int id, int cha, int las) {
    if (!vis[ch[id][cha]][u.stt | stt[ch[id][cha]]])
        vis[ch[id][cha]][u.stt | stt[ch[id][cha]]] = 1, q[++ tail] = Item(ch[id][cha], las, cha, u.stt | stt[ch[id][cha]]);
}

inline void Solve()
{
    q[head = tail = 0] = Item(0, 0, 0, 0);
    while (head <= tail) {
        u = q[head];
        if (u.stt == (1 << n) - 1) { Print(u); return ; }
        rep(i, 26) {
            register int v = u.id;
            if (ch[v][i]) Ins(u, v, i, head);
            else {
                v = fail[v];
                while (v) {
                    if (ch[v][i]) { Ins(u, v, i, head); break; }
                    v = fail[v];
                }
                if (ch[0][i]) Ins(u, 0, i, head);
            }
        }
        ++ head;
    }
}

int main()
{
    File();

    n = read();
    rep(i, n) scanf("%s", s), add(s, i);
    getfail();

    Solve();

    return 0;
}
//人间万事,毫发常重泰山轻。
//    -- 辛弃疾《水调歌头·壬子三山被召陈端仁给事饮饯席上作》

你可能感兴趣的:(BZOJ,省选,AC自动机,状压,BFS)