设计密码-修复DNA-文本生成器

· · 个人记录

设计密码 - 题目简述:

  1. 给定一个模式串 T,要求构造一个长度为 n 的字符串 S,使得字符串 S 不包含子串 T,问满足要求的字符串 S 的方案数

  2. 数据范围:1 \le n \le 50 ,1 \le \left |T\right| \le 50

思路分析:

  1. 状态设计:dp[i][j] 表示对于字符串 S 第 i 个字符,kmp 匹配到第 j 个位置的方案数,即 f[i]=j

  2. 考虑状态转移,对于已知的 dp[i][j],枚举第 i+1 位填字符 c,并对字符 c 继续进行 kmp 匹配得到 t=f[i+1],则有:dp[i+1][t]+=dp[i][j]

  3. 答案统计:\sum_{i=0}^{m-1} dp[n][i]

Code:

#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
const int N=60;
int n;
char str[N];
int nxt[N],f[N][N]; //写到密码串第 i 个字符,且当前 kmp 跳到 的第 j 状态(即 f[i]=j )的方案数
int main() {
    scanf("%d%s",&n,str+1);
    int len=strlen(str+1); 
    for(int i=2,j=0; i<=len; i++) {
        while(j>0 && str[i]!=str[j+1]) j=nxt[j];
        if(str[i]==str[j+1]) j++;
        nxt[i]=j;
    }
    f[0][0]=1;
    for(int i=0; i<n; i++)
        for(int j=0; j<len; j++) 
            for(char k='a'; k<='z'; k++) {
                int t=j;
                while(t>0 && k!=str[t+1]) t=nxt[t];
                if(k==str[t+1]) t++;
                if(t<len) f[i+1][t]=(f[i+1][t]+f[i][j])%mod;
            }
    int ans=0;
    for(int i=0; i<len; i++) ans=(ans+f[n][i])%mod;
    printf("%d",ans);
    return 0;
}

修复DNA - 题目简述:

  1. 给定 n 个模式串 T,以及一个字符串 S,要求进行若干次修改使得字符串 S 不包含任意子串 T,问最小化修改次数

  2. 数据范围:1 \le n \le 50,1 \le \left|T\right| \le 50,1 \le \left|S\right| \le 1000

思路分析:

  1. 状态设计:dp[i][j] 表示对于字符串 S 第 i 个字符,在 trie 树上匹配到第 j 个节点且从未匹配到匹配点的最小修改次数

  2. 状态转移:dp[i+1][k]=\min(dp[i+1][k],dp[i][j]+s[i+1] \ne c)

  3. 答案统计:\min_{i=0}^{tot} dp[n][i]

Code:

#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f 
const int N=1010;
int n,t,f[N][N*4];
char str[N]; 
map<char,int> mp;
struct node {
    int son[4];
    int fail;
}trie[N*4];
bool ed[N*4];
int tot;
void Insert(char str[]) {
    int len=strlen(str+1),p=0;
    for(int i=1; i<=len; i++) {
        int ch=mp[str[i]];
        if(!trie[p].son[ch]) trie[p].son[ch]=++tot;
        p=trie[p].son[ch];
    }
    ed[p]=1;
}
queue<int> q;
void make_fail() {
    for(int i=0; i<4; i++) if(trie[0].son[i]) q.push(trie[0].son[i]);
    while(!q.empty()) {
        int x=q.front(); q.pop();
        ed[x]|=ed[trie[x].fail];
        for(int i=0; i<4; i++) {
            int y=trie[x].son[i];
            int Fail=trie[x].fail;
            if(!y) trie[x].son[i]=trie[Fail].son[i];
            else trie[y].fail=trie[Fail].son[i],q.push(y);
        }
    }
}
void init() {
    memset(trie,0,sizeof(trie));
    tot=0;
    memset(ed,0,sizeof(ed));
    memset(f,inf,sizeof(f));
}
int main() {
    mp['A']=0,mp['C']=1,mp['G']=2,mp['T']=3;
    while(scanf("%d",&n) && n!=0) {
        init();
        for(int i=1; i<=n; i++) {
            scanf("%s",str+1);
            Insert(str);
        }
        make_fail(); 
        scanf("%s",str+1);
        int len=strlen(str+1);
        f[0][0]=0;
        for(int i=0; i<len; i++) 
            for(int j=0; j<=tot; j++) {
                if(ed[j] || f[i][j]==inf) continue;
                for(int k=0; k<4; k++) {
                    int p=trie[j].son[k];
                    if(ed[p]) continue;
                    f[i+1][p]=min(f[i+1][p],f[i][j]+(mp[str[i+1]]!=k));
                }
            }
        int ans=inf;
        for(int i=0; i<=tot; i++)
            ans=min(ans,f[len][i]);
        printf("Case %d: %d\n",++t,ans==inf?-1:ans);
    }
    return 0;
}

练习题:文本生成器

思维拓展: