[CERC2014]Virus synthesis【回文自动机+DP】

[CERC2014]Virus synthesis

🔗
初始有一个空串,利用下面的操作构造给定串 SS 。

1、串开头或末尾加一个字符

2、串开头或末尾加一个该串的逆串

求最小化操作数, \(|S| \le 10^5\)

可以发现最终的答案必然是先构造出一个偶数的回文串,然后再在回文串的两端把剩下的用操作\(1\)补全
而这个偶数长度的回文串可以通过操作\(2\)得到,最终答案就是这个偶数长度的回文串的构造花费加上剩下来的字符的数量
我们构建回文自动机
我们只关心偶数长度回文串的构造花费,假设自动机上的节点\(x\)的构造花费为\(cost_x\)
存在两种转移:

1.从上一个偶数回文串的两端各加一个字符得到,这个可以在上一个回文串没翻倍之前先在末尾加上一个字符再翻倍,设上一个回文串的构造耗费为\(cost_y\),则\(cost_x=min(cost_x,cost_y+1)\)

2.从上一个长度小于等于当前长度一半的最长回文转移过来,先在上一个回文串的两端补全成当前回文串的一半再翻倍\(cost_x=min(cost_x,cost_y+1+len_x-len_y)\)

//#pragma GCC optimize("O3")
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<bits/stdc++.h>
using namespace std;
function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
const int MAXN = 1e5+7;
const int INF = 0x3f3f3f3f;
class PAM{
private:
    char s[MAXN];
    int len[MAXN],ch[MAXN][4],fail[MAXN][20],last,tot,n,cost[MAXN],f[MAXN];
    int calfail(int x, int pos){ while(s[pos]!=s[pos-len[x]-1]) x = fail[x][0]; return x; }
public:
    void init(){
        memset(ch[0],0,sizeof(ch[0])); memset(ch[1],0,sizeof(ch[1]));
        tot = 1; last = 0;
        len[0] = 0; len[1] = -1;
        fail[0][0] = 1; fail[1][0] = 0;
        scanf("%s",s+1); n = strlen(s+1);
        for(int i = 1; i <= n; i++){
            if(s[i]=='G') s[i] = 'B';
            if(s[i]=='T') s[i] = 'D';
        }
    }
    void newnode(){
        tot++; cost[tot] = INF;
        memset(ch[tot],0,sizeof(ch[tot]));
        memset(fail[tot],0,sizeof(fail[tot]));
    }
    void insert(int pos){
        int c = s[pos] - 'A';
        int u = calfail(last,pos);
        if(!ch[u][c]){
            newnode();
            len[tot] = len[u] + 2;
            fail[tot][0] = ch[calfail(fail[u][0],pos)][c];
            for(int i = 1; fail[tot][i-1] > 1; i++) fail[tot][i] = fail[fail[tot][i-1]][i-1];
            ch[u][c] = tot;
        }
        last = ch[u][c];
    }
    void bfs(){
        queue<int> que;
        for(int i = 0; i < 4; i++) if(ch[0][i]){
            que.push(ch[0][i]);
            cost[ch[0][0]] = 2;
        }
        for(int i = 1; i <= tot; i++) cost[i] = len[i];
        while(!que.empty()){
            int u = que.front();
            que.pop();
            int now = u;
            for(int i = 19; i >= 0; i--) if(len[fail[now][i]]>len[u]/2) now = fail[now][i];
            int half = fail[now][0];
            cost[u] = min(cost[u],cost[half]+1+len[u]/2-len[half]);
            for(int i = 0; i < 4; i++) if(ch[u][i]){
                cost[ch[u][i]] = min(cost[ch[u][i]],cost[u]+1);
                que.push(ch[u][i]);
            }
        }
    }
    void solve(){
        for(int i = 1; i <= n; i++) insert(i);
        bfs();
        int ret = INF;
        for(int i = 2; i <= tot; i++) ret = min(ret,n-len[i]+cost[i]);
        printf("%d\n",ret);
    }
}pam;

void solve(){
    pam.init();
    pam.solve();
}
int main(){
    int T;
    for(scanf("%d",&T); T; T--) solve();
    return 0;
}
posted @ 2020-05-05 10:45  _kiko  阅读(190)  评论(0编辑  收藏  举报