P2679 子串

來源:互聯網
上載者:User

標籤:stream   cli   using   href   amp   spl   nbsp   ons   color   

P2679 子串

設$f[i][j][k][p]$表示匹配到A串第$i$個位置,B串第$j$個位置,已經匹配了$k$段,$p=0 or 1$表示A串的該位有沒取

$p==1$時

$f[i][j][k][1]=f[i-1][j-1][k][1]+f[i-1][j-1][k-1][0]+f[i-1][j-1][k-1][1]$

$f[i][j][k][0]=f[i-1][j][k][0]+f[i-1][j][k][1]$

$p==0$時

$f[i][j][k][1]=0$(不合法)

$f[i][j][k][0]=f[i-1][j][k][0]+f[i-1][j][k][1]$

藍後我們發現第一維可以滾動數組最佳化掉

那麼時間$O(nmk)$空間$O(4m^{2})$就能過了

 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #define re register 5 using namespace std; 6 const int p=1e9+7; 7 int n,m,kk,f[2][202][202][2]; 8 char a[1003],b[203]; 9 int main(){10     scanf("%d%d%d",&n,&m,&kk);11     scanf("%s%s",a+1,b+1);12     f[0][0][0][0]=f[1][0][0][0]=1;13     for(re int i=1,t=1;i<=n;++i,t^=1)14         for(re int j=1;j<=m;++j)15             for(re int k=1;k<=kk;++k){16                 if(a[i]==b[j])//字元相同可以取17                     f[t][j][k][1]=((f[t^1][j-1][k-1][0]+f[t^1][j-1][k-1][1])%p+f[t^1][j-1][k][1])%p;18                 else f[t][j][k][1]=0;19                 f[t][j][k][0]=(f[t^1][j][k][0]+f[t^1][j][k][1])%p;20             }21     printf("%d",(f[n&1][m][kk][0]+f[n&1][m][kk][1])%p);22     return 0;23 }
View Code

 

P2679 子串

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.