【例题 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);
        }
    }
}
posted @ 2017-12-05 13:35  AWCXV  阅读(137)  评论(0编辑  收藏  举报