Poj 3691 DNA repair (AC automation + dp)

Source: Internet
Author: User

Poj 3691 DNA repair (AC automation + dp)

DNA repair
Time Limit:2000 MS   Memory Limit:65536 K
Total Submissions:5877   Accepted:2760

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 retried red 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 integersN(1 ≤N≤ 50), which is the number of DNA segments causing inherited diseases.
The followingNLines givesNNon-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 retried red.

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
Number of characters which need to be changed. If it's impossible to repair the given DNA, print-1.

 

Sample Input

2AAAAAGAAAG    2ATGTGAATG4AGCTAGT0

Sample Output

Case 1: 1Case 2: 4Case 3: -1

Source

2008 Asia Hefei Regional Contest Online by USTC

 

Question:

Given N mode strings (1 ≤ N ≤ 50), the maximum length is 20. A master string (up to 1000 long) can contain 4 {'A' characters ', 'T', 'G', 'C'}. Modify at least a few characters so that the master string does not contain all mode strings.

 

Ideas:

Establish an AC automatic machine for the mode string and use the AC automatic machine as the dp.

Dp [I] [j] indicates the minimum number of characters to be modified when the I character of the Main string reaches the j node of the automatic machine.

The next enumerative character is one of AGCT to be transferred. Note that the transfer cannot contain nodes in the mode string. Therefore, an array is used to record whether words are included at the end of the node.

 

Code:

 

# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
Using namespace std; const int INF = 0x3f3f3f3f; const int maxn = 10010; const int bsz = 26; typedef long ll; char txt [maxn], cc [] = "AGCT"; bool vis [maxn]; int ans; struct Trie {bool have [maxn]; // whether the end of the node contains the words int ch [maxn] [bsz], val [maxn], sz, cnt [maxn]; // ch stores the word cnt corresponding to the Trie val-node. The number of words ending with the node. int f [maxn], last [maxn]; // f-Mismatch pointer last-suffix link int newnode () {val [sz] = 0; cnt [sz] = 0; have [sz] = 0; memset (ch [sz],-1, sizeof ch [sz]); return sz ++;} void init () {sz = 0; newnode ();} int idx (char c) // obtain the c label. For details, refer to the {return c-'A';} void Insert (char * st, int id) {int u = 0, n = strlen (st), c, I; for (I = 0; I
     
      
Q; f [0] = 0; for (I = 0; I
      
        = INF) continue; for (k = 0; k <4; k ++) {id = ac. idx (cc [k]); next = ac. ch [j] [id]; if (ac. have [next]) continue; if (cc [k] = txt [I + 1]) {dp [I + 1] [next] = min (dp [I + 1] [next], dp [I] [j]);} else {dp [I + 1] [next] = min (dp [I + 1] [next], dp [I] [j] + 1 );}}}} ans = INF; for (j = 0; j = INF) ans =-1 ;}int main () {int I, j, n, ca = 0; while (~ Scanf ("% d", & n) {if (n = 0) break; ac. init (); for (I = 1; I <= n; I ++) {scanf ("% s", txt); ac. insert (txt, I);} ac. build (); scanf ("% s", txt + 1); solve (); printf ("Case % d: % d \ n", ++ ca, ans);} return 0 ;}
      
     
    
   
  
 


 

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.