bzoj 4606: [Apio2008]DNA (DP)

6900 단어 동적 기획
제목 설명
전송문
문제풀이
코드
#include
#include
#include
#include
#include
#define N 50003
#define LL long long 
using namespace std;
int n,m,a[N],a1[N],b[N];
LL f[N][5][13],g[N][13],R;
char s[N];
char opt(int x)
{
    if (x==1) return 'A';
    if (x==2) return 'C';
    if (x==3) return 'G';
    if (x==4) return 'T';
}
int main()
{
    freopen("a.in","r",stdin);
    scanf("%d%d%lld",&n,&m,&R);
    scanf("%s",s+1);
    for (int i=1;i<=n;i++) {
        if (s[i]=='A') a[i]=1;
        if (s[i]=='C') a[i]=2;
        if (s[i]=='G') a[i]=3;
        if (s[i]=='T') a[i]=4;
    }
    for (int i=1;i<=n;i++) a1[n-i+1]=a[i];
    memset(f,0,sizeof(f));
    if (!a1[1]) for (int i=1;i<=4;i++) f[1][i][1]=1;
    else f[1][a1[1]][1]=1;
    for (int i=2;i<=n;i++)
      for (int k=1;k<=m;k++) {
       if (!a1[i]){
        for (int j=1;j<=4;j++){
           for (int l=1;l<=4;l++)
             if (j<=l) f[i][j][k]+=f[i-1][l][k];
             else f[i][j][k]+=f[i-1][l][k-1];
         }
       }
       else {
            int x=a1[i];
            for (int l=1;l1][l][k-1];
            for (int l=x;l<=4;l++) 
              f[i][x][k]+=f[i-1][l][k];
       }
    }
    for (int i=1;i<=n;i++)
     for (int j=1;j<=4;j++)
      for (int k=1;k<=m;k++) f[i][j][k]+=f[i][j][k-1];
    for (int i=1;i<=n;i++) {
        int t=n-i+1;
        if (a[i]) {
            printf("%c",opt(a[i]));
            b[i]=a[i];
        }
        else {
            for (int j=1;j<=4;j++){
             int K=m;
             if (j1]) K--;
             if (R>f[t][j][K]) R-=f[t][j][K];
             else { 
                b[i]=j;
                printf("%c",opt(j));
                break;
             }
            }
        }
        if (b[i]1]) m--;
    }
    printf("
"
); }

좋은 웹페이지 즐겨찾기