Krypton Factor(UVA 129)

网址如下:

Krypton Factor - UVA 129 - Virtual Judge (vjudge.net)

(第三方网站)

一道枚举题,用回溯法枚举

因为添加新字符之前的字串已经判断过了,所以只需要判断包含新字符的字串是否有相邻的重复字串就可以了

代码如下:

#include<cstdio>
const int maxn = 81;
char A[maxn];
int cnt, n, L;

bool judge(int b, int e){
    bool is_h = false;
    for(int i = b, j = (b + e) / 2; j < e; i++, j++)
        if(A[i] != A[j]){is_h = true; break;}
    return is_h;
}
void print_ans(int cur){
    if(cnt > n) return;
    if(cnt == n){
        for(int i = 0, j = 0, k = 0; i < cur; i++, j++){
            if(j == 4){j = 0; if(++k == 16) putchar('\n'); else putchar(' ');}
            printf("%c", A[i]);
        }
        putchar('\n');
        printf("%d\n", cur);
        cnt++;
    }
    else{
        for(int i = 0; i < L; i++){
            A[cur] = 'A' + i;
            //判断是否为hard串
            bool is_h = true;
            for(int i = cur - 1; i >= 0; i -= 2)
                if(!judge(i, cur + 1)){is_h = false; break;}
            if(is_h){cnt++; print_ans(cur + 1);}
        }
    }
}

int main(void)
{
    while(scanf("%d%d", &n, &L) == 2 && n){
        cnt = 0;
        print_ans(0);
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值