POJ 3691:DNA repair(AC自动机+DP)


DNA repair
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 3353   Accepted: 1487

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

这道题用AC自动机+dp做与其他匹配多模式串的AC自动机最大的不同就是:一个结点j的KIND个孩子不会有空,就是说当从结点通过一个不能往下连接的字符时,孩子要通过j的fail结点来找到,或者回到根。

如图:箭头就是next[]的指向

 

所以在Trie树中安全结点中遍历时,就能得到到某个结点的修改值了。

母串的前i个前缀在Trie树中遍历,到达某个安全结点j需要修改的值为dp[i][j]

dp[i][son]=min{dp[i][son],dp[i-1][j]+(str[i-1]!=char(j->son))} ;//son是j的孩子str是从0开始的

初始状态为:dp[0][0]=0,其他为无穷      //0是根结点


源代码:(47MS)

#include
#include
#include
using namespace std;

const int KIND=4;
const int MAX=1005;
const int INF=10000000;

struct TrieNode
{
	int index;
	bool unsafe;
	TrieNode *fail;
	TrieNode *next[KIND];
};

TrieNode memory[MAX];
int allocp;
int verID[100];//verID['A']=0,verID['C']=1....
int n,dp[MAX][MAX];
TrieNode *q[MAX];
char word[25],str[MAX];

TrieNode *CreateTrieNode()
{
	TrieNode *p=&memory[allocp];
	p->index=allocp;
	allocp++;
	p->fail=NULL;
	p->unsafe=false;
	memset(p->next,0,sizeof(p->next));
	return p;
}

void InsertTrieNode(TrieNode *pRoot,char s[])
{
	TrieNode *p=pRoot;
	int i=0;
	while(s[i])
	{
		int k=verID[s[i]];
		if(p->next[k]==NULL)
			p->next[k]=CreateTrieNode();
		i++;
		p=p->next[k];
	}
	p->unsafe=true;
}

void Build_AC_Automation(TrieNode *pRoot)
{
	int head=0,tail=0;
	TrieNode *p,*tmp;
	q[tail++]=pRoot;
	pRoot->fail=NULL;
	while(head!=tail)
	{
		p=q[head++];
		for(int i=0;inext[i]!=NULL)
			{
				if(p==pRoot)
					p->next[i]->fail=pRoot;
				else
				{
					p->next[i]->fail=p->fail->next[i]; 
					//p->fail->next[i]已经指到适当的位置,不会为空,见下个else
					if(p->next[i]->fail->unsafe)//如果一个节点的fail节点不安全
						p->next[i]->unsafe=true;//则该节点也不安全
				}
				q[tail++]=p->next[i];//p非空的孩子入队
			}
			else
			{
				if(p==pRoot)
					p->next[i]=pRoot;
				else
				    p->next[i]=p->fail->next[i];//转移到失败指针的next[i]去找
			}
		}
	}
}

int DP()
{
	int i,j,k,son;
	int len=strlen(str);
	TrieNode *p;
	for(i=0;i<=len;i++)
		for(j=0;jnext[k]->unsafe==0)//在安全节点上找
				    {
					    son=p->next[k]->index; //孩子节点位置
					    dp[i][son]=min(dp[i][son],dp[i-1][j]+(verID[str[i-1]]!=k));//DP转移
				    }
			}

	int ans=INF;
	for(i=0;i>word;
			InsertTrieNode(pRoot,word);
		}
		Build_AC_Automation(pRoot);
		cin>>str;
		printf("Case %d: %d\n",test++,DP());
	}
	system("pause");
	return 0;
}


你可能感兴趣的:(字符串)