【例题 7-5 UVA - 129】Krypton Factor

【链接】 我是链接,点我呀:)
【题意】


在这里输入题意

【题解】


每次枚举增加一个字符;
然后看看新生成的字符的后缀里面有没有出现连续子串就好,前面已经确认过的没必要重复确认
(枚举长度为偶数的一个后缀就好)
是输出完第64组且保证有第65组才要输出一个换行.

【代码】

import java.io.*;
import java.util.*;
public class Main{
    static int n,l,cnt;
    static int a[] = new int[1000];
    static ArrayList<Integer> list = new ArrayList<Integer>();

    static int dfs(int now){
        if (now > 1){
            cnt++;
            if (cnt==n){
                for (int i = 1;i < now;i++) list.add(a[i]);
                return 0;
            }
        }
        for (int i = 1;i <= l;i++){
            a[now] = i;
            boolean ok = true;
            for (int j = 1; now-2*j+1>=1;j++){
                boolean ju = true;
                for (int k = now-j+1;k<=now;k++){
                    if (a[k]!=a[k-j]){
                        ju = false;
                        break;
                    }
                }
                if (ju==true){
                    ok = false;
                    break;
                }
            }
            if (!ok) continue;
            if (dfs(now+1)==0) return 0;
        }
        return 1;
    }

    public static void main(String args[]){
        Scanner cin = new Scanner(System.in);
        while (cin.hasNext()){
            n = cin.nextInt();l = cin.nextInt();
            if (n==0 && l==0) break;
            cnt = 0;
            list.clear();
            dfs(1);
            int tot = list.size();
            for (int i = 0;i < tot;i++){
                if (i%4==0 && i >0) {
                    if (i % 64 == 0) {
                        System.out.println("");
                    } else {
                        System.out.print(' ');
                    }
                }
                System.out.print((char)(list.get(i)+'A'-1));
            }
            System.out.println("");
            System.out.println(tot);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值