uva 129 - Krypton Factor

本文介绍了一个使用C++解决特定序列问题的程序实现。通过定义一系列辅助函数和算法,作者成功解决了硬序列问题,并展示了如何通过递归和回溯方法找到解决方案。程序在输出时进行了优化,确保了输出格式的清晰性和效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

此题并不是什么难题;主要思想是确定一下这个当前位置的值,此时就是一个hard sequenece;最后在输出上多想一下,就可以了。(输出让我wa了好多次)
//
//  main.cpp
//  uva 129 - Krypton Factor
//
//  Created by XD on 15/7/27.
//  Copyright (c) 2015年 XD. All rights reserved.
//

#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include<vector>
#include <string.h>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cstdio>
using namespace std ;

int result[100] ;

int n ;
int l ;

bool isAcceptable(int cur)
{
    if(!cur)
    {
        return true ;
    }
    int flag = 0 ;
    for (int i = 1; 2* i  <=cur+1; i++) {
        flag = 0 ;
        for (int j = cur - i+1 ; j <=cur ; j++) {
            if (result[j] != result[j - i]) {
                flag = 1  ;
            }
            
        }
        if (flag == 0 ) {
                return false ;
            }
    }
    return true ;
    
}
int t  ;

int solved ;
int  fillCache(int cur )
{
    
    if (t++ == n+1 ) {
        solved = 1 ;
        return cur ;
    }
    for (int i = 1; i <= l ; i++) {
        
        result[cur] = i ;
        if (isAcceptable(cur)) {
            
           int k  =  fillCache(cur+1)  ;
            if (solved ==1) {
                return  k ;
            }
        }
    }
    return  0 ;
}


int main() {
    while (scanf("%d%d" ,&n,&l)==2) {
        solved = 0 ;
        if (n + l == 0 ) {
            return  0 ;
        }
        else{
            t = 1 ;
            int len = fillCache(0) ;
            int j = 0 ;
            int temp = 0 ;
            for (int i = 0; i < len; i++) {
                printf("%c" , result[i] + 'A' - 1) ;
                temp++ ;
                if (temp == 4) {
                    j++ ;
                    if (j == 16) {
                        printf("\n") ;
                        j= 0 ;
                    }
                    else{
                        if (i==len-1) {
                            continue ;
                        }
                        printf(" ") ;
                    }
                    temp = 0 ;
                }
            }
            if (j !=  0 || (j == 0&& t != 0)) {
                printf("\n") ;
            }
            printf("%d\n" , len) ;
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值