Codeforces Round# Good Bye 2013 New Year Letter

题目描述:戳这里

题解:

这题数据这么小,显然是一道暴力题。
我们枚举一下两个字符串的首和尾是哪个字符,再枚举一下AC的个数,然后DP推一下第k项,判断一下是不是AC总数等于k就好了。
但是细节特别多,代码比较乱。。。。。
代码如下:

#include
using namespace std;
const int maxn=55;
int n,m,k,num,s[maxn],e[maxn];
long long f[maxn];
void prin(char s1,char e1,char s2,char e2,int x1,int x2){
    if (n==1) printf("%c\n",s1); else if (n==2) printf("%c%c\n",s1,e1);
    else {
        printf("%c",s1);
        if (n/2==x1&&n%2==1&&s1=='A') printf("C");
        for (int i=1;iprintf("AC"); if ((n/2!=x1||n%2!=1)&&x1>0) printf("AC");
        for (int i=1;i<=n-2-2*x1;i++) printf("B");
        if (n/2==x1&&n%2==1&&e1=='C') printf("A");
        if (n>1&&(x1!=n/2||(x1==n/2&&n%2==1))) printf("%c\n",e1); else printf("\n");
    }
    if (m==1) printf("%c\n",s2); else if (m==2) printf("%c%c\n",s2,e2);
    else {
        printf("%c",s2);
        if (m/2==x2&&m%2==1&&s2=='A') printf("C");
        for (int i=1;iprintf("AC"); if ((m/2!=x2||m%2!=1)&&x2>0) printf("AC");
        for (int i=1;i<=m-2-2*x2;i++) printf("B");
        if (m/2==x2&&m%2==1&&e2=='C') printf("A");
        if (m>1&&(x2!=m/2||(x2==m/2&&m%2==1))) printf("%c\n",e2);   else printf("\n");
    }
}
bool get(){
    for (int i=3;i<=k;i++){
        s[i]=s[i-2],e[i]=e[i-1];
        f[i]=f[i-2]+f[i-1]+(e[i-2]=='A'&&s[i-1]=='C');
        if (f[i]>num) return 0;
    }
    if (f[k]!=num) return 0; else return 1;
}
int main(){
    scanf("%d %d %d %d",&k,&num,&n,&m);
    for (char x='A';x<='C';x++)
    for (char y='A';y<='C';y++)
    for (char x1='A';x1<='C';x1++)
    for (char y1='A';y1<='C';y1++)
    for (int i=0;i<=n/2;i++)
    for (int j=0;j<=m/2;j++){
        f[1]=i,f[2]=j,s[1]=x,e[1]=y,s[2]=x1,e[2]=y1;
        if (n%2==0&&i==n/2&&(x!='A'||y!='C')) continue;
        if (n%2==1&&i==n/2&&(x!='A'&&y!='C')) continue;
        if (m%2==0&&j==m/2&&(x1!='A'||y1!='C')) continue;
        if (m%2==1&&j==m/2&&(x1!='A'&&y1!='C')) continue;
        if (n==1) e[1]=x; if (m==1) e[2]=x1;
        if (get()) {prin(x,y,x1,y1,i,j); return 0;}
    }
    printf("Happy new year!\n");
    return 0;
}

你可能感兴趣的:(题解,CodeForces题解)