UVA-129 - 判断没有相邻的(相同子串)

题目链接:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=65
在这里插入图片描述
在这里插入图片描述
思路比较简单,但是在判断有没有相邻的子串这里的思路值的我们借鉴。

我们只考虑增加当前字符,对整个字符串的影响。枚举可能发生相同子串的长度,再判断就可以了。

#include <bits/stdc++.h>
#define LL long long
using namespace std;

int cut=0;
int s[205];
int dfs(int tot, int n, int L){

    if(cut++==n){
        for(int i=1; i<=tot; i++){
            printf("%c", s[i]+'A');
            if(i%64==0&&i!=tot){
                printf("\n");
            }
            else if(i%4==0&&i!=tot){
                printf(" ");
            }
        }
        printf("\n%d\n", tot);
        return 0;
    }

    for(int i=0; i<L; i++){
        s[++tot]=i;
        int ok=1;
        for(int Len=1; Len<=tot/2; Len++){//可能发生相同子串的长度
            int ky=1;
            for(int k=1; k<=Len; k++){
                if(s[tot-Len+k]!=s[tot-2*Len+k]){
                    ky=0;
                    break;
                }
            }
            if(ky){
                ok=0;
            }
        }
        if(ok!=0){
            if(!dfs(tot, n, L)){
                return 0;
            }
        }
        tot--;
    }
    return 1;
}

int main()
{
    int n, L;
    while(scanf("%d%d", &n, &L), n*L){
        cut=0;
        dfs(0, n, L);
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值