[hdu 4899]14年多校第四場C Hero meet devil 狀壓DP

來源:互聯網
上載者:User

標籤:dp

Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 122    Accepted Submission(s): 49

Problem Description
There is an old country and the king fell in love with a devil. The devil always asks the king to do some crazy things. Although the king used to be wise and beloved by his people. Now he is just like a boy in love and can’t refuse any request from the devil. Also, this devil is looking like a very cute Loli.

After the ring has been destroyed, the devil doesn‘t feel angry, and she is attracted by z*p‘s wisdom and handsomeness. So she wants to find z*p out.

But what she only knows is one part of z*p‘s DNA sequence S leaving on the broken ring.

Let us denote one man‘s DNA sequence as a string consist of letters from ACGT. The similarity of two string S and T is the maximum common subsequence of them, denote by LCS(S,T).

After some days, the devil finds that. The kingdom‘s people‘s DNA sequence is pairwise different, and each is of length m. And there are 4^m people in the kingdom.
Then the devil wants to know, for each 0 <= i <= |S|, how many people in this kingdom having DNA sequence T such that LCS(S,T) = i.
You only to tell her the result modulo 10^9+7.
 
Input
The first line contains an integer T, denoting the number of the test cases.
For each test case, the first line contains a string S. the second line contains an integer m.


T<=5
|S|<=15. m<= 1000.
 
Output
For each case, output the results for i=0,1,...,|S|, each on a single line.
 
Sample Input
1
GTC
10
 
Sample Output
1
22783
528340
497452

Author
WJMZBMR
 
Source

2014 Multi-University Training Contest 4


題目大意

給定DNA序列長度m和一個DNA(每單位DNA有AGCT 4種可能)片段,求所有和所給序列最長公用子串長度為0~len的DNA數量


解題思路

在開題的時候以為是數論+組合數學,思路越想越偏……

後來CLJ給出超簡要的題解……

聽別人的一種按位壓縮的思路,就是枚舉到該位置之時LCS所對應的位置,若一一對應則該位為1,否則為0

而當我們要處理新的單位DNA時就有一個變換LCS對應的會改變。

則我們枚舉所有可能的匹配位置並枚舉下一位,算出下一個狀態對應的LCS所在位置,按位壓縮。


當我們處理長度為m的DNA是就可以直接帶入了。

因為m<=1000dp狀態採用滾動數組儲存


code:

#include <cstdio>#include <iostream>#include <algorithm>#include <ctime>#include <cctype>#include <cmath>#include <string>#include <cstring>#include <stack>#include <queue>#include <list>#include <vector>#include <map>#include <set>#define sqr(x) ((x)*(x))#define LL long long #define INF 0x3f3f3f3f#define PI acos(-1.0)#define eps 1e-10#define mod 1000000007using namespace std;int bitc(int x)//計算LCS長度{    int ans=0;    while (x)    {        ans+=x%2;        x/=2;    }    return ans;}int dp[2][(1<<16)+10];int to[(1<<16)+10][4];int bt[20];int go[20];int ma[20];int t[20];char l[5]="ACGT";char s[100];int main(){    int T,n,m;    scanf("%d",&T);    while (T--)    {        scanf("%s",s);        n=strlen(s);        scanf("%d",&m);        for (int i=0;i<(1<<(n+1));i++)        {            bt[0]=0;            // printf("%d   ",i );            for (int j = 1; j <= n; ++j)            {                if ((1<<(j-1))&i) bt[j]=bt[j-1]+1;                else bt[j]=bt[j-1];            }            for (int p = 0; p < 4; ++p)            {                go[0]=0;                for (int j = 1; j <= n; ++j)                {                    if (l[p]==s[j-1]) go[j]=bt[j-1]+1;                    else go[j]=max(go[j-1],bt[j]);                }                int tmp=0;                for (int j = 1; j <= n; ++j)                if (go[j]-go[j-1])                {                    tmp|=1<<(j-1);                }                to[i][p]=tmp;                // printf("%d|",tmp );            }            // puts("");        }        memset(dp[0],0,sizeof dp[0]);        dp[0][0]=1;        for (int i=1;i<=m;i++)        {            memset(dp[i&1],0,sizeof dp[i&1]);            for (int j=0;j<(1<<(n+1));j++)            {                if (dp[i&1^1][j]==0) continue;                for (int p=0;p<4;p++)                {                    dp[i&1][to[j][p]]+=dp[i&1^1][j];                    dp[i&1][to[j][p]]%=mod;                }            }            // for (int j=0;j<(1<<(n+1));j++)            //     printf("%d ",dp[i&1][j]);            // puts("");        }        memset(t,0,sizeof t);        for (int j=0;j<(1<<(n+1));j++)            {                t[bitc(j)]+=dp[m&1][j];                t[bitc(j)]%=mod;            }        for (int i=0;i<=n;i++)            printf("%d\n",t[i]);    }    return 0;}/*1GTC10 122783528340497452*/


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.