1. 程式人生 > >BZOJ4044: [Cerc2014] Virus synthesis(迴文樹+DP)

BZOJ4044: [Cerc2014] Virus synthesis(迴文樹+DP)

Description

Viruses are usually bad for your health. How about fighting them with... other viruses? In  this problem, you need to find out how to synthesize such good viruses.  We have prepared for you a set of strings of the letters A, G, T and C. They correspond to the  DNA nucleotide sequences of viruses that we want to svnthesize, using the following operations:  * Adding a nucleotide either to the beginning or the end of the existing sequence  * Replicating the sequence, reversing the copied piece, and gluing it either to the beginmng or  to the end of the original (so that e.g., AGTC can become AGTCCTGA or CTGAAGTC).  We're concerned about efficiency, since we have very many such sequences, some of them verv  long. Find a wav to svnthesize them in a mmimum number of operations.  你要用ATGC四個字母用兩種操作拼出給定的串:  1.將其中一個字元放在已有串開頭或者結尾  2.將已有串複製,然後reverse,再接在已有串的頭部或者尾部  一開始已有串為空。求最少操作次數。  len<=100000 

Input

The first line of input contains the number of test cases T. The descriptions of the test cases  follow:  Each test case consists of a single line containing a non-empty string. The string uses only  the capital letters A, C, G and T and is not longer than 100 000 characters. 

Output

For each test case, output a single line containing the minimum total number of operations  necessary to construct the given sequence.

Sample Input

4
AAAA
AGCTTGCA
AAGGGGAAGGGGAA
AAACAGTCCTGACAAAAAAAAAAAAC

Sample Output

3
8
6
18

 

解題思路:

有這樣的性質:

1.只能形成1個大回文串。

2.只能生成偶迴文串。 很有動歸的思想嘛。 迴文樹上一個節點代表一個迴文串,dp[i]表示在i節點的迴文串最小生成代價:

其中mid為長度小於len[i]/2的最長字尾迴文串所在節點,可以倍增跳也可以從fa的mid開始跳。

那麼答案就是:

就愉快地結束了。

程式碼:

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<algorithm>
  4 struct pant{
  5     int tranc[4];
  6     int len;
  7     int pre;
  8     int dp;
  9     int mid;
 10 }h[1000000],stpant;
 11 int a[100001];
 12 char tmp[100001];
 13 int siz;
 14 int fin;
 15 int len;
 16 int trans(char t)
 17 {
 18     if(t=='A')
 19         return 0;
 20     if(t=='G')
 21         return 1;
 22     if(t=='C')
 23         return 2;
 24     return 3;
 25 }
 26 void Res(void)
 27 {
 28     memset(a,0x3f,sizeof(a));
 29     a[0]=-1;
 30     h[1]=h[0]=stpant;
 31     h[1].pre=h[0].pre=1;
 32     h[1].len=-1;
 33     h[0].dp=1;
 34     siz=1;
 35     fin=0;
 36     return ;
 37 }
 38 bool mis(int i,int lsp)
 39 {
 40     return a[i]!=a[i-h[lsp].len-1];
 41 }
 42 void Insert(int i)
 43 {
 44     int nwp,lsp,mac;
 45     lsp=fin;
 46     int c=a[i];
 47     while(mis(i,lsp))
 48         lsp=h[lsp].pre;
 49     if(!h[lsp].tranc[c])
 50     {
 51         nwp=++siz;
 52         mac=h[lsp].pre;
 53         h[nwp]=stpant;
 54         h[nwp].len=h[nwp].dp=h[lsp].len+2;
 55         while(mis(i,mac))
 56             mac=h[mac].pre;
 57         h[nwp].pre=h[mac].tranc[c];
 58         h[lsp].tranc[c]=nwp;
 59         
 60         
 61         if(h[nwp].len<=2)
 62             h[nwp].mid=h[nwp].pre;
 63         else{
 64             mac=h[lsp].mid;
 65             while(mis(i,mac)||h[mac].len*2+4>h[nwp].len)
 66                 mac=h[mac].pre;
 67             h[nwp].mid=h[mac].tranc[c];
 68         }
 69         if(h[nwp].len%2==0)
 70         {
 71             h[nwp].dp=std::min(h[lsp].dp+1,h[nwp].len/2-h[h[nwp].mid].len+h[h[nwp].mid].dp+1);
 72         }
 73     }
 74     fin=h[lsp].tranc[c];
 75     return ;
 76 }
 77 int main()
 78 {
 79     int T=0;
 80     scanf("%d",&T);
 81     while(T--)
 82     {
 83         Res();
 84         scanf("%s",tmp+1);
 85         int ans;
 86         len=strlen(tmp+1);
 87         ans=len;
 88         for(int i=1;i<=len;i++)
 89             a[i]=trans(tmp[i]);
 90         for(int i=1;i<=len;i++)
 91             Insert(i);
 92         for(int i=2;i<=siz;i++)
 93         {
 94             if(h[i].len&1^1)
 95             {
 96                 ans=std::min(ans,len-h[i].len+h[i].dp);
 97                 
 98             }//printf("%d\n",h[i].dp);
 99         }
100         //return 0;
101         printf("%d\n",ans);
102     }
103     return 0;
104 }