BZOJ3864: Hero meet devil(dp套dp)

来源:https://www.cnblogs.com/zwfymqz/archive/2018/05/02/8978527.html
-Advertisement-
Play Games

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 ...


Time Limit: 8 Sec  Memory Limit: 128 MB
Submit: 397  Solved: 206
[Submit][Status][Discuss]

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

HINT

 

Source

首先想一下LCS的轉移方程

$$lcs[i][j]=max \begin{cases} lcs[i-1][j-1]+1 & \text{if t[i]=s[j]} \\ lcs[i-1][j] \\ lcs[i][j-1] \end{cases}$$

這樣的話,當$i$確定是,$lcs[i][j]$和$lcs[i][j-1]$最多相差$1$

且題目中說$|S|<= 15$,因此我們考慮把差分後的lcs數組狀壓起來

那麼如何統計答案呢?

設$f[i][sta]$表示在第$i$個位置,此時lcs的狀態為$sta$的方案數,

然後我們枚舉一下這個位置選ACGT中的哪個

設$trans[sta'][A/C/G/T]$為在$sta$狀態表示的lcs後加了ACGT中的一個後的狀態,這個很顯然可以預處理得到

那麼轉移方程為

$$f[i][ trans[sta][k] ] += f[i - 1][sta] $$

$$f[0][0] = 1$$

 

 

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int  MAXN = 1001, mod = 1e9 + 7;
char S[16], SS[] = {"ACGT"};
int a[16], f[MAXN][(1 << 15) + 2], trans[(1 << 15) + 2][5], N, Len, limit, ans[111];
int tmp[2][16];
int solve(int sta, int ch) {
    int ret = 0;
    memset(tmp, 0, sizeof(tmp));
    for(int i = 0; i < N; i++) tmp[0][i + 1] = tmp[0][i] + ((sta >> i) & 1 );    
    for(int i = 1; i <= N; i++) {
        int mx = 0;
        if(a[i] == ch) mx = tmp[0][i - 1] + 1;
        mx = max( max(mx, tmp[0][i]), tmp[1][i-1]);
        tmp[1][i] = mx;
    }
    for(int i = 0; i < N; i++) ret += (1 << i) * (tmp[1][i + 1] - tmp[1][i]);
    return ret;
}
int main() {
    #ifdef WIN32
    freopen("a.in", "r", stdin);
    #endif
    int QWQ;scanf("%d", &QWQ);
    while(QWQ--) { 
        memset(f, 0, sizeof(f));memset(ans, 0, sizeof(ans));
        scanf("%s", S + 1);
        N = strlen(S + 1); limit = (1 << N) - 1;    
        for(int i = 1; i <= N; i++)
            for(int j = 0; j < 4; j++)
                if(S[i] == SS[j]){a[i] = j + 1;break;}
        scanf("%d", &Len);
        f[0][0] = 1;
        for(int sta = 0; sta <= limit; sta++)
            for(int j = 1; j <= 4; j++)
                trans[sta][j] = solve(sta, j);    
        for(int i = 1; i <= Len; i++)
            for(int sta = 0; sta <= limit; sta++)
                for(int k = 1; k <= 4; k++)
                    f[i][ trans[sta][k] ] = (f[i][ trans[sta][k] ] + f[i - 1][sta]) % mod; 
        for(int sta = 0; sta <= limit; sta++)
            ans[__builtin_popcount(sta)] = (ans[__builtin_popcount(sta)] + f[Len][sta]) % mod;
            //這個函數是算出sta中1的個數 
        for(int i = 0; i <= N; i++)
            printf("%d\n", ans[i] % mod);
    }
    return 0;
}

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 面向對象第八次作業 代碼分析 第五次作業 多線程電梯 UML圖和協作圖 代碼複雜度分析 這次作業的主要難點在於對於多線程的理解和實踐,一方面由於老師上課講的著急,內容也更多的偏向JVM的介紹,因此對於多線程編程的一些思路和方法沒有多少瞭解,另一方面由於時間不足也沒有時間去更詳細的自學多線程,因此基本 ...
  • 前言 在最近一個月的面向對象編程學習中,我們進入了編寫多線程程式的階段。線程的創建、調度和信息傳遞,共用對象的處理,線程安全類的編寫,各種有關於線程的操作在一定程度上增加了近三次作業的複雜度與難度,帶來了不小的考驗。本文通過分析總結近三次作業的完成情況,分享我對與多線程編程的一些見解與體會。 作業總 ...
  • 今天5月1號了,回鄭州,在家待了三天。打了幾天手游,花了不少錢。家裡準備再蓋座房子,我也應該為家裡做些貢獻。真他娘累,30多了,單身,現在已有些恐婚了。近期有空要做個記賬軟體,否則錢花的太快了。這個月,20多號還要軟考,我這應該是考不過了,基本放棄了。老闆原說漲3K,又變成2K,坑,套路。 近期我寫 ...
  • 更友好的閱讀體驗,請轉至 OAuth 深入介紹 。 1. 前言 2. OAuth2 角色 2.1 資源所有者(Resource Owner) 2.2 資源/授權伺服器(Resource/Authorization Server) 2.3 客戶端(Client) 3. OAuth 2 的授權流程 4. ...
  • 之前跟大家已經講了有關函數的一部分知識,但是忘了講一個很重要的點,就是變數的作用域,這塊知識不只是適用於函數,它試用域所有的Python程式 在正式寫程式之前,必須要清楚這一塊,否則就很容易犯錯誤 首先理清一個概念,什麼是變數 變數可以我們可以將它看為指向值的名稱,就像我們之前講的字典一樣的,只是這 ...
  • 編程不是盜墓,不是請客吃飯,不是描畫繡花,不能那樣儒雅,那樣閑庭信步,那樣從容不迫。Java編程是一門技術,一門進行創造的技術。絕大多數電腦專業的學生是零基礎,其中不乏被調劑的。等到畢業之際,有的成了大神,進入BAT或者google微軟,有的還是零基礎……但零基礎並不可怕,只有找對方向,就一定會成 ...
  • 2018-3 問題描述: 近來,跳一跳這款小游戲風靡全國,受到不少玩家的喜愛。 簡化後的跳一跳規則如下:玩家每次從當前方塊跳到下一個方塊,如果沒有跳到下一個方塊上則游戲結束。 如果跳到了方塊上,但沒有跳到方塊的中心則獲得1分;跳到方塊中心時,若上一次的得分為1分或這是本局游戲的第一次跳躍則此次得分為 ...
  • 1.Spring-eureka(註冊中心) Eureka基礎架構包括 註冊中心,服務提供者,服務消費者 1.創建服務註冊中心 import org.springframework.boot.SpringApplication; import org.springframework.boot.auto ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...