zoukankan      html  css  js  c++  java
  • 算法竞赛进阶指南--模拟机器实现,把组合型枚举改为非递归

    vector<int> chosen;
    int stack[100010], top = 0, address = 0;
    
    void call(int x, int ret_addr) { // 模拟计算机汇编指令call
    	int old_top = top;
    	stack[++top] = x; // 参数x
    	stack[++top] = ret_addr; // 返回地址标号
    	stack[++top] = old_top; // 在栈顶记录以前的top值
    }
    
    int ret() { // 模拟计算机汇编指令ret
    	int ret_addr = stack[top - 1];
    	top = stack[top]; // 恢复以前的top值
    	return ret_addr;
    }
    
    int main() {
    	int n, m;
    	cin >> n >> m;
    	call(1, 0); // calc(1)
    	while (top) {
    		int x = stack[top - 2]; // 获取参数
    		switch (address) {
    		case 0:
    			if (chosen.size() > m || chosen.size() + (n - x + 1) < m) {
    				address = ret(); // return
    				continue;
    			}
    			if (x == n + 1) {
    				for (int i = 0; i < chosen.size(); i++)
    					printf("%d ", chosen[i]);
    				puts("");
    				address = ret(); // return
    				continue;
    			}
    			call(x + 1, 1); // 相当于calc(x + 1),返回后会从case 1继续执行
    			address = 0;
    			continue; // 回到while循环开头,相当于开始新的递归
    		case 1:
    			chosen.push_back(x);
    			call(x + 1, 2); // 相当于calc(x + 1),返回后会从case 2继续执行
    			address = 0;
    			continue; // 回到while循环开头,相当于开始新的递归
    		case 2:
    			chosen.pop_back();
    			address = ret(); // 相当于原calc函数结尾,执行return
    		}
    	}
    }
    
  • 相关阅读:
    Docker安装MySQL&Redis
    使用VirtualBox+Vagrant快速搭建Linux虚拟机环境
    Java集合工具类的一些坑,Arrays.asList()、Collection.toArray()...
    1.docker常用命令
    4. 带有延迟时间的Queue(DelayQueue)
    3. 基于优先级的Queue(PriorityBlockingQueue)
    2. 常见的Queue
    1. 模拟Queue
    14. 线程调度
    13. 线程池
  • 原文地址:https://www.cnblogs.com/lunatic-talent/p/12798275.html
Copyright © 2011-2022 走看看