gym 101194 F Mr. Panda and Fantastic Beasts(SAM+BFS)

題意

給定n個字符串,從第一個字符串中找到一個字典序最小的字串使其只出現在第一個串中

題解

對於這n個串建立廣義後綴自動機,並對第一個串的np節點打上標記0,對於其他串的np節點打上標記1,之後我們對於每個np節點更新其後綴鏈。之後我們再以root爲起點跑BFS直到找到第一個標記爲0的節點爲止,若找不到則不存在,找到了則回溯得出答案。

代碼

/**
 *     author:     TelmaZzzz
 *     create:     2019-10-04-16.19.46
**/
#include <map>
#include <set>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <ctime>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
//#include <random>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
void _R(int &x) { scanf("%d", &x); }
void _R(ll &x) { scanf("%lld", &x); }
void _R(db &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &head, U &... tail) { _R(head); R(tail...); }
void _W(const int &x) { printf("%d", x); }
void _W(const ll &x) { printf("%lld", x); }
void _W(const db &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); }
#define rep(x,y,z) for(int x=y;x<=z;x++)
#define erp(x,y,z) for(int x=y;x>=z;x--)
#define PB push_back
#define MP make_pair
#define INF 1073741824
#define inf 1152921504606846976
#define pi 3.14159265358979323846
#define Fi first
#define Se second
//#pragma comment(linker,"/STACK:10240000,10240000")
//mt19937 rand_(time(0));
const int N=3e5+7,M=2e6;
const long long mod=1e9+7;
inline int read(){int ret=0;char ch=getchar();bool f=1;for(;!isdigit(ch);ch=getchar()) f^=!(ch^'-');for(;isdigit(ch);ch=getchar()) ret=(ret<<1)+(ret<<3)+ch-48;return f?ret:-ret;}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}//逆元
//int head[N],NEXT[M],ver[M],tot;void link(int u,int v){ver[++tot]=v;NEXT[tot]=head[u];head[u]=tot;}
void TelmaZzzz(){
#ifndef ONLINE_JUDGE
    freopen("1.txt","r",stdin);
#endif
}
const int S=26;
int tot=1,last=1,pre[N<<1],son[N<<1][S],ml[N<<1],cnt[N<<1],firstpos[N<<1];
int newnod(){
    tot++;
    for(int i=0;i<S;i++) son[tot][i]=0;
    ml[tot]=0,cnt[tot]=0,pre[tot]=0;
    return tot;
}
void extend(int w,int mark){
    int p=newnod(),x=last,r,q;
    cnt[tot]=mark;
    for(ml[last=p]=ml[x]+1;x&&!son[x][w];x=pre[x]) son[x][w]=p;
    firstpos[tot]=ml[tot]-1;
    if(!x) pre[p]=1;
    else if(ml[x]+1==ml[q=son[x][w]]) pre[p]=q;
    else {
        pre[r=newnod()]=pre[q];
       //cnt[tot]=mark;
        memcpy(son[r],son[q],sizeof son[r]);
        ml[r]=ml[x]+1;pre[p]=pre[q]=r;
        //cnt[r]=cnt[q];
        for(;x&&son[x][w]==q;x=pre[x]) son[x][w]=r;
        firstpos[r]=firstpos[q];
    }
}
int c[N<<1],a[N<<1];
void RadixSort(){
    for(int i=1;i<=tot;i++) c[ml[i]]++;
    for(int i=1;i<=tot;i++) c[i]+=c[i-1];
    for(int i=1;i<=tot;i++) a[c[ml[i]]--]=i;
}
void Count(){
    RadixSort(); //基數排序(相當於拓撲序)
    erp(i,tot,1){
        int u=a[i];
        cnt[pre[u]]+=cnt[u];
    }
}
char str[N],tmp[N];
bool vis[N<<1];
int pos[N<<1],PRE[N<<1];
int bfs(int x){
    queue<int>q;
    q.push(x);
    vis[x]=true;
    while(q.size()){
        int x=q.front();
        q.pop();
        for(int i=0;i<26;i++){
            if(son[x][i]&&!vis[son[x][i]]){
                vis[son[x][i]]=true;
                PRE[son[x][i]]=x;
                pos[son[x][i]]=i;
                q.push(son[x][i]);
                if(cnt[son[x][i]]==0) return son[x][i];
            }
        }
    }
    return -1;
}
int main(){
    TelmaZzzz();
    //ios::sync_with_stdio(false);
    int t;
    int ti=0;
    R(t);
    int n;
    while(t--){
        R(n);
        R(str);
        tot=0;
        last=1;
        newnod();
        int m=strlen(str);
        rep(i,0,m-1){
            extend(str[i]-'a',0);
        }
        rep(i,2,n){
            last=1;
            R(tmp);
            int m2=strlen(tmp);
            rep(j,0,m2-1){
                extend(tmp[j]-'a',1);
            }
        }
        Count();
        int res=bfs(1);
        if(res==-1){
            printf("Case #%d: Impossible\n",++ti);
        }
        else {
            string st="";
            while(res!=1){
                st+=(char)(pos[res]+'a');
                res=PRE[res];
            }
            reverse(st.begin(),st.end());
            printf("Case #%d: %s\n",++ti,st.c_str());
        }
        rep(i,0,tot) c[i]=0,a[i]=0,vis[i]=false,pos[i]=0,PRE[i]=0;
    }
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章