算法
zem_nezer
这个作者很懒,什么都没留下…
展开
-
POJ3050(DFS)
一定要根据数据规模想问题,这题数据规模就不大,时间复杂度大概是O(R*C*dir^step) = O(5*5*4^6)。规模不大,可以无脑DFS,还是这句话,先将简单的写出来再说。//#define LOCAL#include #include #include #include #define SIZE 5using namespace std;int map[SIZ原创 2018-02-01 06:12:44 · 162 阅读 · 0 评论 -
AOJ0525(bitset, 穷举)
这题有3点要注意: 1. the flip order is not related to result. 2. why we can simply count to maximum of number each column? Imagine only manipulate the row, it is easy to understand that it is unnece原创 2018-02-01 06:29:36 · 153 阅读 · 0 评论 -
POJ3617(贪心)
没啥好说的,书上原题,比较坑就是输出需要每80个换行。//#define LOCAL#include #include #include #define SIZE 2000using namespace std;int main(void){#ifdef LOCAL freopen("data.in", "r", stdin);#endif char原创 2018-02-01 12:24:31 · 106 阅读 · 0 评论 -
POJ3253(贪心)
1.要考虑到规模为20,000 累加起来肯定会超的,要用long long 2. 思想就是先从正着推,一定是先切掉最长的那块,这样之后都不会受影响;再反着来想,就是先合并最小的//#define LOCAL#include <cstdio>#include <cstring>#include <algorithm>#include <que...原创 2018-02-09 06:50:50 · 163 阅读 · 0 评论 -
POJ1852(Ant)
很久没写了,第一个程序。 注意每个case前都要初始化//#define LOCAL#include #include #include using namespace std;int main(){#ifdef LOCAL freopen("data.in", "r", stdin); //freopen("datd.out", "w", stdout)原创 2018-01-29 02:12:55 · 158 阅读 · 0 评论 -
POJ1979(Red and Black, DFS)
注意char处理, when need to manipluate inputs, take care of previous chars, such as ‘\n’ #define LOCAL#include #include #include #define SIZE 30using namespace std;char map[SIZE][SIZE];int dir原创 2018-01-29 03:22:45 · 136 阅读 · 0 评论 -
POJ3009(Curling 2.0, DFS)
while的使用,when aware of next step status, use “while” to deal with it. 转载地址#include #include const int dh[4]={-1, 0, 1, 0}; const int dw[4]={0, -1, 0, 1}; char map[24][24]; int w, h, _转载 2018-01-29 03:26:10 · 147 阅读 · 0 评论 -
AOJ0558(cheese, BFS)
typical BFS usage: first use 2 array, one for record map, the other for shortest path/flag visit.BFS feature: the shortest path from one to the other, just one point!!!!(单源路)//#define LOCAL#原创 2018-01-29 03:29:32 · 207 阅读 · 0 评论 -
POJ3669(meteor shower, BFS)TLE
when initial, try to convenient for later operation, e.g: if step[][] = -1, will hard to compare mini with other. However, my code still TLE for unknown reason.#define LOCAl#include #include转载 2018-01-29 03:35:27 · 154 阅读 · 0 评论 -
AOJ0121(Seven Puzzle, BFS+Cantor+逆向思维)
参考 康托展开自己真的一点头绪都没有,根据前面的大神博客自己几乎100%复制了一个,但是还是WA,觉得还是出在选方向时的判断上面。Cantor感觉这个可以作为模板,状态压缩的一个思路。还有就是BFS特点: 1.从一个起点到一个终点 2.起点和终点可以互相到达,双向的//#define LOCAL#include #include #include #include原创 2018-01-29 06:45:02 · 233 阅读 · 0 评论 -
POJ2718(穷举,贪心)
参考地址一开始连题意都没搞懂就开始直接做,too young。应该静下来用5分钟分析,by the way, may be the typical usage of brute-force is “void” function instead of function with return value.//#define LOCAL#include #include #incl原创 2018-01-29 13:18:32 · 215 阅读 · 0 评论 -
POJ3187(next_permutation)
一开始先用最简单的试试,实在是TLE再想其他办法。不要一直觉得总有公式可以写//#define LOCAL#include #include #include #define SIZE 10using namespace std;int main(){#ifdef LOCAL freopen("data.in", "r", stdin);#endif原创 2018-01-30 11:37:48 · 213 阅读 · 0 评论 -
POJ2376(贪心)
参考:http://blog.csdn.net/v5zsq/article/details/46828461还是之前的技巧没有弄熟,在遇到这种直到满足某一条件时再进行改变时,就应该要想到用while, 方便而且思路清晰。从这里可以看出逻辑还是不够清楚,边界条件还是拿捏的不好。//#define LOCAL#include <cstdio>#include <cs...原创 2018-02-15 03:01:31 · 259 阅读 · 0 评论 -
POJ3069(贪心)
贪心,自己太想当然了,直接 left + 2 * R, 这当然是不对的。。你咋知道left+R 一定有点呢。。//#define LOCAL#include <cstdio>#include <cstring>#include <algorithm>#define N 1000using namespace std;int main(voi...原创 2018-02-08 12:24:12 · 147 阅读 · 0 评论 -
POJ2393(贪心)
水题,compare current storing value and the price of that day, if cheaper then assign, if not then update the store value.//#define LOCAL#include <cstdio>#include <cstring>#include <...原创 2018-02-28 00:15:13 · 207 阅读 · 0 评论 -
POJ31900(贪心)
想对了一半,还是不扎实原本想将初始化和之后处理一起放到for里面的(i.e. 将push, ans = 1等放到for里面),发现比较麻烦,然后死磕这个,要建函数什么的,看了人家的代码之后发现没有必要,当然是美观了一点。其实能不能将初始化和处理一起写最重要的是看你的思路 is clear or not, sometimes it can unified. But in this case, ...原创 2018-02-17 07:29:49 · 163 阅读 · 0 评论 -
leetcode-1-two sum(brute force, hash table)
We can use brute force to get it, use two for loop i and j, which i = 1:n and j = i:n. However, the time complexity is O(n^2), which is not efficient.Use hash table, the first thing is first store e...原创 2018-12-14 06:30:54 · 85 阅读 · 0 评论 -
leetcode-2-Add Two Numbers
The algorithm is straight-forward: Because the list represent reversely, we just need to add two elements then assign the residual to next element.However, I miss some edge case, which do not handle ...原创 2018-12-14 06:31:23 · 95 阅读 · 0 评论 -
leetcode-3-Longest Substring Without Repeating Characters
Use sliding windows to solve the problem.First, we use the hash map to store how many character and which character we store currently. Then judge our current hash map is valid or not. If valid, let ...原创 2018-12-14 06:31:28 · 85 阅读 · 0 评论 -
leetcode-4-Median of Two Sorted Arrays
弃疗,不大懂原创 2018-12-14 06:31:32 · 64 阅读 · 0 评论 -
leetcode-5-Longest Palindromic Substring
I am so stupid!!! Spent long time on that and cannot fix the bugs in short time.It has two solutions: the first is treat each element as the center, then expand base on the center and check the valid...原创 2018-12-14 06:31:54 · 63 阅读 · 0 评论 -
leetcode-6-ZigZag Conversion
Use a trick, actually we just follow the zigzag move our current position, but we can use string to make a trick in the implement: every time we append the element to each string which corresponding t...原创 2018-12-14 06:31:37 · 97 阅读 · 0 评论 -
leetcode-7-Reverse Integer
straight forward, just keep in mind that we need to deal with overflow problem.原创 2018-12-14 06:31:41 · 61 阅读 · 0 评论 -
leetcode-8-String to Integer (atoi)
Follow the problem then we can get the answer. But from here I can see my coding ability is not well than before, many small detail did not handle so well.原创 2018-12-14 06:31:45 · 84 阅读 · 0 评论 -
leetcode-9-Palindrome Number
Easy problem, first is convert number to string, then compare the string pairwise.原创 2018-12-14 06:31:49 · 83 阅读 · 0 评论 -
leetcode-11-Container With Most Water
At first I thought I use the greedy: the move left pointer if the new one has longer height which can counteract the width, and use the same for right pointer. But it works not well, because maybe I s...原创 2018-12-14 06:32:02 · 77 阅读 · 0 评论 -
leetcode-13-Roman to Integer
Though leetcode mark is as easy, but I think it is thicker than I though: we need to traverse it inversely, and when current roman is small than previous one, mins current roman, otherwise add it.原创 2018-12-14 06:51:48 · 80 阅读 · 0 评论 -
leetcode-20-Valid Parentheses
Do it one hundred times, when meet left part of bracket, push it, right part check it. And finally check the stack is empty or not.原创 2018-12-14 07:11:27 · 68 阅读 · 0 评论 -
leetcode-17-Letter Combinations of a Phone Number
It is like my previous problem, I do it many times, but still a stupid, cannot find a job. Use recursive, every store its corresponding elements, remember, 7 and 9 has 4 characters.原创 2018-12-14 07:59:16 · 73 阅读 · 0 评论 -
leetcode-19-Remove Nth Node From End of List
Use a common trick call slow, fast pointer. Because the last n means the distance between last n and the end is n, so we create two pointer, the first is slow, and second is fast, and make fast to n s...原创 2018-12-14 08:32:54 · 83 阅读 · 0 评论 -
leetcode-15-3Sum
Block me too much time, it is use two pointer to do. First is fix one numbers, then use two pointer to scan the remain sorted list and get the result. The time complexity is O(n^2). Just remember to d...原创 2018-12-14 11:15:46 · 55 阅读 · 0 评论 -
leetcode-51-N-Queens
Use backtracking, since we need to return a array, it is better to use void to handle that rather than return type function.Check current situation is valid or not, check previous index is volatile ...原创 2018-12-27 04:54:21 · 80 阅读 · 0 评论 -
leetcode-675-Cut Off Trees for Golf Event
The problem is very classical, first is how do we come up with a BFS? Next time since we meet a minimum step or other minimum problem, we can think of such algorithm. In this case, we will think of si...原创 2018-12-27 07:20:26 · 120 阅读 · 0 评论 -
leetcode-200-Number of Islands
Nothing special, I used two method to do it, BFS and DFS. first I used a loop to traverse the map, and when we meet the 1, we go to the map and set all the connection as 0.Error:In BFS, I use the x,...原创 2018-12-27 08:18:58 · 198 阅读 · 0 评论 -
leetcode-547-Friend Circles
The same idea as island, first, we look up if current student is used or not, then assign it as used and look up its neighbor, then do some modify. While in the island, we do the same thing: look up t...原创 2018-12-27 11:38:43 · 107 阅读 · 0 评论 -
leetcode-127-Word Ladder
A good problem for me to analysis the time complexity. At first I want to create a graph for each word, I mean check each word is a available for the other. However, when I look up the solution I thin...原创 2018-12-27 12:55:22 · 147 阅读 · 0 评论 -
leetcode-21-Merge Two Sorted Lists
Compare the current value of each list, the assign the smaller node to merged list, then return it.It is straight forward, just need to know that it use to merge a new list, not create a new list.原创 2018-12-17 05:00:52 · 67 阅读 · 0 评论 -
leetcode-22-Generate Parentheses
We can use recursive to solve, every time we can check if current situation can append ‘(’ or ‘)’. Then append it recursive. If meet left == n and right == n, store the string.Not too many point to t...原创 2018-12-17 07:13:23 · 62 阅读 · 0 评论 -
leetcode-23-Merge k Sorted Lists
First, we can use priority_queue to deal with that, the time complexity is O(Nlog^k). Since priority_queue is basically a heap, every time we only need to compare the value in O(logk) time, instead of...原创 2018-12-17 08:23:59 · 68 阅读 · 0 评论 -
leetcode-24-Swap Nodes in Pairs
The problem I did is very simple and clearly, which is seems stupid. I just point out its next point, next’s next point, and next’s next’s next point. Then rearrange the next point.The tricky point i...原创 2018-12-17 11:34:25 · 73 阅读 · 0 评论