hdu2457 DNA repair AC自动机+DP

DNA repair

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1476    Accepted Submission(s): 802


Problem Description
Biologists finally invent techniques of repairing DNA that contains segments causing kinds of inherited diseases. For the sake of simplicity, a DNA is represented as a string containing characters 'A', 'G' , 'C' and 'T'. The repairing techniques are simply to change some characters to eliminate all segments causing diseases. For example, we can repair a DNA "AAGCAG" to "AGGCAC" to eliminate the initial causing disease segments "AAG", "AGC" and "CAG" by changing two characters. Note that the repaired DNA can still contain only characters 'A', 'G', 'C' and 'T'.

You are to help the biologists to repair a DNA by changing least number of characters.
 

Input
The input consists of multiple test cases. Each test case starts with a line containing one integers N (1 ≤ N ≤ 50), which is the number of DNA segments causing inherited diseases.
The following N lines gives N non-empty strings of length not greater than 20 containing only characters in "AGCT", which are the DNA segments causing inherited disease.
The last line of the test case is a non-empty string of length not greater than 1000 containing only characters in "AGCT", which is the DNA to be repaired.

The last test case is followed by a line containing one zeros.
 

Output
For each test case, print a line containing the test case number( beginning with 1) followed by the
number of characters which need to be changed. If it's impossible to repair the given DNA, print -1.
 

Sample Input
   
   
   
   
2 AAA AAG AAAG 2 A TG TGAATG 4 A G C T AGT 0
 

Sample Output
   
   
   
   
Case 1: 1 Case 2: 4 Case 3: -1

  给你一个串,只包含ATCG,最少修改几个位置能让这个串不包含上面给的串,如果不能输出-1。

  dp[i][u]表示这个串的前i个字母走到节点u的最少修改次数,有标记的节点不能走,很简单的DP了。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<iostream>
#include<queue>
using namespace std;

typedef unsigned long long ULL;

const int MAXN=55;
const int MAXM=25;
const int MAXL=1010;
const int MAXNODE=MAXN*MAXM;
const int LOGMAXN=50;
const int INF=0x3f3f3f3f;
const int SIGMA_SIZE=4;
const int MOD=20090717;

int T,N;
char str[MAXM];
char P[MAXL];
int dp[MAXL][MAXNODE];

struct AC{
    int ch[MAXNODE][SIGMA_SIZE];
    int val[MAXNODE];
    int f[MAXNODE];
    int sz;

    void clear(){
        memset(ch[0],0,sizeof(ch[0]));
        val[0]=0;
        sz=1;
    }
    int idx(char c){
        switch(c){
        case 'A':return 0;
        case 'G':return 1;
        case 'C':return 2;
        case 'T':return 3;
        }
    }
    void insert(char* s,int v){
        int u=0;
        for(int i=0;s[i];i++){
            int c=idx(s[i]);
            if(!ch[u][c]){
                memset(ch[sz],0,sizeof(ch[sz]));
                val[sz]=0;
                ch[u][c]=sz++;
            }
            u=ch[u][c];
        }
        val[u]=v;
    }
    void get_fail(){
        queue<int> q;
        f[0]=0;
        for(int c=0;c<SIGMA_SIZE;c++){
            int u=ch[0][c];
            if(u){
                f[u]=0;
                q.push(u);
            }
        }
        while(!q.empty()){
            int r=q.front();
            q.pop();
            for(int c=0;c<SIGMA_SIZE;c++){
                int u=ch[r][c];
                if(!u){
                    ch[r][c]=ch[f[r]][c];
                    continue;
                }
                q.push(u);
                f[u]=ch[f[r]][c];
                val[u]|=val[f[u]];
            }
        }
    }
}ac;

void DP(){
    int len=strlen(P);
    memset(dp,INF,sizeof(dp));
    dp[0][0]=0;
    for(int i=0;i<len;i++)
        for(int u=0;u<ac.sz;u++) if(dp[i][u]!=INF){
            for(int c=0;c<SIGMA_SIZE;c++) if(!ac.val[ac.ch[u][c]]){
                if(ac.idx(P[i])==c) dp[i+1][ac.ch[u][c]]=min(dp[i+1][ac.ch[u][c]],dp[i][u]);
                else dp[i+1][ac.ch[u][c]]=min(dp[i+1][ac.ch[u][c]],dp[i][u]+1);
            }
        }
    int ans=INF;
    for(int i=0;i<ac.sz;i++) ans=min(ans,dp[len][i]);
    if(ans==INF) printf("-1\n");
    else printf("%d\n",ans);
}

int main(){
    freopen("in.txt","r",stdin);
    int cas=0;
    while(scanf("%d",&N)!=EOF&&N){
        ac.clear();
        for(int i=1;i<=N;i++){
            scanf("%s",str);
            ac.insert(str,1);
        }
        ac.get_fail();
        scanf("%s",P);
        printf("Case %d: ",++cas);
        DP();
    }
    return 0;
}



你可能感兴趣的:(hdu2457 DNA repair AC自动机+DP)