代码改变世界

随笔分类 -  POJ

POJ 2653 Pick-up sticks

2012-03-28 10:19 by 咆哮的马甲, 213 阅读, 收藏,
摘要: 考察点在于如何判断两线段相交(快速排斥+跨立),之前使用链表结构超时了,后来用vector实现。判断线段相交的办法,这篇文章讲的很明白。http://324012406.iteye.com/blog/738318#include <iostream>#include <vector>using namespace std;typedef struct Coordinate{ Coordinate(double _x, double _y) :x(_x),y(_y) { } double x; double y;} Coordinate;typedef... 阅读全文

POJ 2255 Tree Recovery

2012-03-09 14:35 by 咆哮的马甲, 215 阅读, 收藏,
摘要: 根据二叉树的先序和中序遍历的结果,构造出这棵树后续遍历的结果。思路是根据先序遍历的节点将中序遍历的结果连续二分,递归即可还原树的本来面目。将找到的子树根节点压栈再退栈即可后序输出二叉树。#include <iostream>#include <string>#include <stack>using namespace std;stack<char> s;void RecreateTree(string& pre, string& in){ if(!pre.empty() && !in.empty()) { cha 阅读全文

POJ 3461 Oulipo

2012-03-05 16:33 by 咆哮的马甲, 169 阅读, 收藏,
摘要: 利用KMP算法查找字符串中字串出现的次数#include <iostream>#include <vector>#include <string>using namespace std;int* computeOverlap(string& s){ int length = s.length(); if(length == 0) return NULL; int* overlayValue = new int[length]; overlayValue[0] = -1; for(int i=1; i<length; i++) { ... 阅读全文

POJ 2388 Who's in the Middle

2012-03-01 14:13 by 咆哮的马甲, 433 阅读, 收藏,
摘要: 今天恰好在总结各种排序算法,在POJ上找到这样一道水题找到给定乱序数组的中间值。直接插入排序解法#include <iostream>using namespace std;int cow[1000000];void swap(int &a, int &b){ if(a == b) return; a = a^b; b = b^a; a = a^b;}int main(){ int n = 0; cin>>n; for(int i=0; i<n; i++) { cin>>cow[i]; } for(int ... 阅读全文

POJ 3750 小孩报数问题

2012-02-23 12:30 by 咆哮的马甲, 223 阅读, 收藏,
摘要: 基本的双向链表即可实现。最后一个element的判定很重要。#include <iostream>#include <string>using namespace std;typedef struct Child{ string name; Child* pre; Child* next;} Child;int main(){ // Get input N int n; cin>>n; Child* head = NULL; Child* tail = NULL; Child* cur = NULL; // Create dlink ... 阅读全文

POJ 3658 Artificial Lake

2012-02-15 19:31 by 咆哮的马甲, 253 阅读, 收藏,
摘要: 这道题做了整整一天,一直遇到Time Limit Exceeded,估计是链表的创建以及修改指针指向比较费时。不断的修改之后才勉强达到要求。解题思路:模拟水流由高到低的过程,单纯的模拟会超时,所以要做一些优化。#include "stdio.h"#include <iostream>#define max 1000010using namespace std;typedef struct Level{ Level() { this->width = 0; this->height = 0; this->index = 0; ... 阅读全文