POJ-1472(循环复杂度,递归分治 + 多项式)
来源:互联网 发布:三菱3uplc编程手册 编辑:程序博客网 时间:2024/04/29 04:45
题目:http://poj.org/problem?id=1472
思想是分而治之,核心是递归算法,重点是多项式乘法和加法
#include <cstdlib>#include <cstring>#include <iostream>#include <string>#include <vector>using namespace std;#define MAX 10class Polynomial{private: struct Term{ int co, exp; Term(int c, int e): co(c), exp(e){} }; vector<Term> formula;//reverse of the normalized expression, each co > 0private: void reset(int arr[MAX + 1]){ formula.clear(); for(int i = 0, n = MAX; i <= n; ++i){ if(arr[i]) formula.push_back(Term(arr[i], i)); } }public: Polynomial(){ formula.clear(); } Polynomial(const string& s){ formula.clear(); if(s[0] == 'n') formula.push_back(Term(1, 1)); else if(int n = atoi(s.c_str())) formula.push_back(Term(n, 0)); } Polynomial& operator *= (const Polynomial& other){ if(formula.empty()) return *this; if(other.formula.empty()){ formula.clear(); return *this; } int arr[MAX + 1]; memset(arr, 0, sizeof(arr)); const vector<Term>& v = other.formula; int i, n = v.size(), j, m = formula.size(), e, c; for(i = 0; i < n; ++i){ e = v[i].exp; c = v[i].co; for(j = 0; j < m; ++j) arr[e + formula[j].exp] += c * formula[j].co; } reset(arr); return *this; } Polynomial& operator += (const Polynomial& other){ if(other.formula.empty()) return *this; int arr[MAX + 1]; memset(arr, 0, sizeof(arr)); const vector<Term>& v = other.formula; int i, n; for(i = 0, n = v.size(); i < n; ++i) arr[v[i].exp] += v[i].co; for(i = 0, n = formula.size(); i < n; ++i) arr[formula[i].exp] += formula[i].co; reset(arr); return *this; } friend ostream& operator << (ostream& out, const Polynomial& poly){ const vector<Term>& formula = poly.formula; if(formula.empty()){ out<< "0"; return out; } bool first = true; for(int i = formula.size() - 1; i >= 0; --i){ if(first) first = false; else out << "+"; if(formula[i].exp){ if(formula[i].co != 1) out << formula[i].co << "*"; out << "n"; if(formula[i].exp > 1) out << '^' << formula[i].exp; } else out << formula[i].co; } return out; }};Polynomial analyseLoop(istream& in){ string s; in >> s; Polynomial poly(s), move; while(in >> s, s != "END"){ if(s == "OP"){ in >> s; move += Polynomial(s); } else move += analyseLoop(in); } return poly *= move;}Polynomial analyseProgram(istream& in){ string s; Polynomial poly; in >> s; while(in >> s, s != "END"){ if(s == "LOOP") poly += analyseLoop(in); else{ in >> s; poly += Polynomial(s); } } return poly;}int main(){ ios::sync_with_stdio(false); int t, test; cin >> test; for(t = 1; t <= test; ++t){ cout << "Program #" << t << "\n"; cout << "Runtime = " << analyseProgram(cin) << "\n\n"; } return 0;}A了之后,感觉C++的函数重载特性简直酷毙了……
0 0
- POJ-1472(循环复杂度,递归分治 + 多项式)
- POJ-1240(分治,递归降解)
- 算法时间复杂度----分治与递归
- 算法复杂度的分析【递归分治法】
- POJ 2083 Fractal 分治+递归
- 分治法的递归算法时间复杂度分析
- 多项式求值算法(递归)
- 多项式求值算法(递归)
- 高效算法设计_递归与分治(棋盘覆盖问题,循环日程表,巨人与鬼)
- 【C语言】求取第n个斐波那契数的时间复杂度、空间复杂度分析,用三种方式实现(普通递归,循环,优化递归)
- hdu 4643 (分治+递归)
- 二分搜索(分治递归)
- 合并排序(分治递归)
- HDU 1007 (分治递归)
- 众数问题(递归、分治)
- 分治:循环赛日程表(递归+非递归)
- 伪多项式复杂度
- 多项式的时间复杂度
- Linux 设备驱动之字符设备(一)
- 微信怎么编辑照片浅谈我们该如何把握微信?立足营销 做出未来
- 关于什么是句柄(经典)
- C.7
- 历届试题 剪格子
- POJ-1472(循环复杂度,递归分治 + 多项式)
- socket通信概要
- 函数指针 指针函数
- hdu 2602 Bone Collector
- Linux Namespaces机制
- jQuery_review之textarea滚动条往上往下的实现
- AC自动机浅析
- POJ 2236 Wireless Network 简单并查集
- C#中堆和栈的区别分析