hdu2457 DNA repair ac机+dp

DNA repair

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


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
 

Source
 

Recommend
teddy   |   We have carefully selected several similar problems for you:   2458  2459  2456  2461  2462 

题意:给定n个仅由A、G、C、T构成的非法串,又给定一个仅由A、G、C、T构成的字符串s,问最少需要多少步使得s不包含非法串,如若不能输出-1。

思路:先将A、C、G、T编号,并将n个非法串建ac自动机,设dp[i][j]表示长度为i的串以ac上j结点为末尾所需修改的最少步数,设p=trie[j].next[k]->id, 那么dp[i+1][p]=min(dp[i][j]+(k!=idx(str[i])),dp[i+1][p]),转移的前提是p,j结点必须均为合法结点,即该结点的cnt不为1。详见代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int sigma_size=4;
const int MAXN=1000+100;
const int inf=0x3fffffff;
int n,head,tail,sz;
int dp[MAXN][MAXN];
char s[MAXN];
struct node
{
    int cnt,id;
    node *next[sigma_size],*fail;
}trie[MAXN],*root,*que[MAXN];
struct AC
{
    node *createnode()
    {
        for(int k=0;k<sigma_size;k++)
            trie[sz].next[k]=NULL;
        trie[sz].fail=NULL;
        trie[sz].cnt=0,trie[sz].id=sz;
        return &trie[sz++];
    }
    void init()
    {
        sz=0;
        head=tail=0;
        root=createnode();
    }
    int idx(char c)
    {
        if(c=='A') return 0;
        if(c=='G') return 1;
        if(c=='C') return 2;
        return 3;
    }
    void insert(char *str)
    {
        node *p=root;
        int len=strlen(str);
        for(int i=0;i<len;i++)
        {
            int k=idx(str[i]);
            if(p->next[k]==NULL)
                p->next[k]=createnode();
            p=p->next[k];
        }
        p->cnt=1;
    }
    void get_fail()
    {
        que[tail++]=root;
        while(head<tail)
        {
            node *p=que[head++];
            for(int k=0;k<sigma_size;k++)
                if(p->next[k])
                {
                    if(p==root)
                        p->next[k]->fail=root;
                    else
                        p->next[k]->fail=p->fail->next[k];
                    if(p->next[k]->fail->cnt)
                        p->next[k]->cnt=1;
                    que[tail++]=p->next[k];
                }
                else
                {
                    if(p==root)
                        p->next[k]=root;
                    else
                        p->next[k]=p->fail->next[k];
                }
        }
    }
    int query(char *str)
    {
        int len=strlen(str);
        for(int i=0;i<=len;i++)
            for(int j=0;j<sz;j++)
                dp[i][j]=inf;
        dp[0][0]=0;
        for(int i=0;i<len;i++)
            for(int j=0;j<sz;j++)
            {
                if(trie[j].cnt)
                    continue;
                for(int k=0;k<sigma_size;k++)
                {
                    int p=trie[j].next[k]->id;
                    if(trie[p].cnt)
                        continue;
                    dp[i+1][p]=min(dp[i+1][p],dp[i][j]+(k!=idx(str[i])));
                }
            }
        int ans=inf;
        for(int i=0;i<sz;i++)
            ans=min(ans,dp[len][i]);
        if(ans==inf)
            ans=-1;
        return ans;
    }
}ac;
int main()
{
    freopen("text.txt","r",stdin);
    int kase=0;
    while(~scanf("%d",&n)&& n)
    {
        kase++;
        ac.init();
        for(int i=0;i<n;i++)
        {
            scanf("%s",s);
            ac.insert(s);
        }
        ac.get_fail();
        scanf("%s",s);
        printf("Case %d: %d\n",kase,ac.query(s));
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值