hdu DNA repair(AC自动机+DP)
2016-08-03 14:23
441 查看
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
Sample Output
先将DNA片段建立AC自动机,然后在AC自动机上进行dp,dp[i][j]表示长度为i移动到j节点
修改了最少的步数。每次走到边如果和字符串不同,权值即为1;相同则为0。单词节点不能向后转移。
dp[i+1][u]=min(dp[i+1][u],dp[i][j]+c!=idx(s[i]));
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int maxnode=55*22;
const int maxn=1010;
const int kind=4;
const int inf=0x7f7f7f7f;
struct node
{
int ch[maxnode][kind],sz;
int val[maxnode],fail[maxnode],last[maxnode];
int dp[maxn][maxnode];
void init()
{
sz=1;
memset(ch[0],0,sizeof(ch[0]));
memset(dp,inf,sizeof(dp));
}
int idx(char c)
{
if(c=='A') return 0;
if(c=='C') return 1;
if(c=='G') return 2;
if(c=='T') return 3;
}
void insert(char *s,int v)
{
int u=0,n=strlen(s);
for(int i=0;i<n;i++)
{
int id=idx(s[i]);
if(!ch[u][id])
{
memset(ch[sz],0,sizeof(ch[sz]));
val[sz]=0;
ch[u][id]=sz++;
}
u=ch[u][id];
}
val[u]=v;
}
void getFail()
{
queue<int>q;
fail[0]=0;
for(int i=0;i<kind;i++)
{
int u=ch[0][i];
if(u)
{
fail[u]=0;
q.push(u);
last[u]=0;
}
}
while(!q.empty())
{
int r=q.front();
q.pop();
for(int i=0;i<kind;i++)
{
int u=ch[r][i];
if(!u)
{
ch[r][i]=ch[fail[r]][i];
continue;
}
q.push(u);
int v=fail[r];
while(v&&!ch[v][i]) v=fail[v];
fail[u]=ch[v][i];
last[u]=val[fail[u]]?fail[u]:last[fail[u]];
}
}
}
void print(int x)
{
if(x)
{
printf("%d: %d\n",x,val[x]);
print(last[x]);
}
}
void find(char *s)
{
int j=0,n=strlen(s);
for(int i=0;i<n;i++)
{
int id=idx(s[i]);
while(j&&!ch[j][id]) j=fail[j];
j=ch[j][id];
if(val[j]) print(j);
else if(last[j]) print(last[j]);
}
}
int DP(char *s)
{
int n=strlen(s);
dp[0][0]=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<sz;j++)
{
if(dp[i][j]==inf||val[j]||last[j])
continue;
for(int k=0;k<kind;k++)
{
int u=j;
while(u&&!ch[u][k]) u=fail[u];
u=ch[u][k];
int d=idx(s[i])==k?0:1;
dp[i+1][u]=min(dp[i+1][u],dp[i][j]+d);
}
}
}
int ans=inf;
for(int i=0;i<sz;i++)
{
if(val[i]||last[i])
continue;
ans=min(ans,dp
[i]);
}
return ans==inf?-1:ans;
}
};
node ac;
char P[maxn];
int main()
{
int n,Case=0;
while(scanf("%d",&n)==1&&n!=0)
{
ac.init();
for(int i=1;i<=n;i++)
{
scanf("%s",P);
ac.insert(P,i);
}
ac.getFail();
scanf("%s",P);
int ans=ac.DP(P);
printf("Case %d: %d\n",++Case,ans);
}
return 0;
}
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
先将DNA片段建立AC自动机,然后在AC自动机上进行dp,dp[i][j]表示长度为i移动到j节点
修改了最少的步数。每次走到边如果和字符串不同,权值即为1;相同则为0。单词节点不能向后转移。
dp[i+1][u]=min(dp[i+1][u],dp[i][j]+c!=idx(s[i]));
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int maxnode=55*22;
const int maxn=1010;
const int kind=4;
const int inf=0x7f7f7f7f;
struct node
{
int ch[maxnode][kind],sz;
int val[maxnode],fail[maxnode],last[maxnode];
int dp[maxn][maxnode];
void init()
{
sz=1;
memset(ch[0],0,sizeof(ch[0]));
memset(dp,inf,sizeof(dp));
}
int idx(char c)
{
if(c=='A') return 0;
if(c=='C') return 1;
if(c=='G') return 2;
if(c=='T') return 3;
}
void insert(char *s,int v)
{
int u=0,n=strlen(s);
for(int i=0;i<n;i++)
{
int id=idx(s[i]);
if(!ch[u][id])
{
memset(ch[sz],0,sizeof(ch[sz]));
val[sz]=0;
ch[u][id]=sz++;
}
u=ch[u][id];
}
val[u]=v;
}
void getFail()
{
queue<int>q;
fail[0]=0;
for(int i=0;i<kind;i++)
{
int u=ch[0][i];
if(u)
{
fail[u]=0;
q.push(u);
last[u]=0;
}
}
while(!q.empty())
{
int r=q.front();
q.pop();
for(int i=0;i<kind;i++)
{
int u=ch[r][i];
if(!u)
{
ch[r][i]=ch[fail[r]][i];
continue;
}
q.push(u);
int v=fail[r];
while(v&&!ch[v][i]) v=fail[v];
fail[u]=ch[v][i];
last[u]=val[fail[u]]?fail[u]:last[fail[u]];
}
}
}
void print(int x)
{
if(x)
{
printf("%d: %d\n",x,val[x]);
print(last[x]);
}
}
void find(char *s)
{
int j=0,n=strlen(s);
for(int i=0;i<n;i++)
{
int id=idx(s[i]);
while(j&&!ch[j][id]) j=fail[j];
j=ch[j][id];
if(val[j]) print(j);
else if(last[j]) print(last[j]);
}
}
int DP(char *s)
{
int n=strlen(s);
dp[0][0]=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<sz;j++)
{
if(dp[i][j]==inf||val[j]||last[j])
continue;
for(int k=0;k<kind;k++)
{
int u=j;
while(u&&!ch[u][k]) u=fail[u];
u=ch[u][k];
int d=idx(s[i])==k?0:1;
dp[i+1][u]=min(dp[i+1][u],dp[i][j]+d);
}
}
}
int ans=inf;
for(int i=0;i<sz;i++)
{
if(val[i]||last[i])
continue;
ans=min(ans,dp
[i]);
}
return ans==inf?-1:ans;
}
};
node ac;
char P[maxn];
int main()
{
int n,Case=0;
while(scanf("%d",&n)==1&&n!=0)
{
ac.init();
for(int i=1;i<=n;i++)
{
scanf("%s",P);
ac.insert(P,i);
}
ac.getFail();
scanf("%s",P);
int ans=ac.DP(P);
printf("Case %d: %d\n",++Case,ans);
}
return 0;
}
相关文章推荐
- HDU DNA repair (AC自动机+DP)
- HDOJ 2457 DNA repair (AC自动机+DP)
- DNA repair - HDU 2457(自动机+dp)
- POJ3691:DNA repair(AC自动机+DP)
- AC自动机专题——J - DNA repair HDU - 2457 DP+AC自动机
- POJ 3691:DNA repair(AC自动机+DP)
- HDU 4057 Rescue the Rabbit(11年大连,AC自动机+状态压缩DP)
- hdu 4057(ac自动机+状态压缩dp)
- HDU 3341 Lost's revenge(AC自动机+DP)
- HDU 2825 Wireless Password(AC自动机+状压DP)
- Hdu 2457 DNA repair (字符串_AC自…
- HDU 4534 郑厂长系列故事——新闻净化(AC自动机+DP)
- HDU 2296 Ring(AC自动机+DP)
- hdu 2825 Wireless Password(AC自动机+压缩DP,5级)
- HDU 2825 Wireless Password(AC自动机+状压DP)
- hdu 2457(ac自动机+dp)
- HDU 4534 郑厂长系列故事——新闻净化(AC自动机+DP)
- Hdu 4057 Rescue the Rabbit (AC自动机+状态压缩dp) - 2011 ACM-ICPC Dalian Regional Contest Problem G
- HDU 3247 Resource Archiver(AC自动机+BFS+状态DP)
- POJ 2778 DNA Sequence(AC自动机+DP)