随笔分类 -  POJ

1 2 下一页

MST pku 1258 Agri-Net
摘要:# include <cstdio># include <algorithm>using namespace std;# define N 100 + 5# define M 5000 + 5int n, m;int p[N];int u[M], v[M], w[M], r[M];void read_graph(void){ int tmp; m = 0; for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) { scanf("%d", &tmp); if (i < 阅读全文

posted @ 2012-07-23 16:50 getgoing 阅读(196) 评论(0) 推荐(0)

PKU 3278 Catch That Cow
摘要:bfs,需要注意0和N是可以到达的,除此之外也可以到达,但实际上不可能更优,因为超过这个范围表明需要回退,回退的步数超过2时,完全可以多走几步再翻倍,结果更优。# include <cstdio># include <queue># include <cstring>using namespace std;# define MAXN 100000 + 5# define N 100000 + 1int n, k;char vis[MAXN];int d[MAXN];int bfs(void){ queue <int> Q; memset(vis, 阅读全文

posted @ 2012-07-22 22:32 getgoing 阅读(304) 评论(0) 推荐(0)

POJ 2488 A Knight's Journey
摘要:DFS,要求输出字典序最小的,注意扩展方向。# include <cstdio># include <cstring># define N 26 + 5const int dx[] = {-1, 1,-2, 2,-2, 2,-1, 1};const int dy[] = {-2,-2,-1,-1, 1, 1, 2, 2};int p, q, cnt;bool finished;char solu[N][2];char vis[N][N];void dfs(int x, int y){ solu[cnt][0] = x, solu[cnt][1] = y; if (cnt 阅读全文

posted @ 2012-07-22 22:03 getgoing 阅读(220) 评论(0) 推荐(0)

POJ 2243 Knight Moves
摘要:bfs,使用C++的queue,300ms。# include <cstdio># include <cstring># include <queue>using namespace std;const int dx[] = {1,1,-1,-1,2,2,-2,-2};const int dy[] = {2,-2,2,-2,1,-1,1,-1};struct Pos{int x, y, d;};char s[5], g[5];int sx, sy, gx, gy;int bfs(void){ queue <Pos> Q; Pos cur, nst 阅读全文

posted @ 2012-07-22 20:09 getgoing 阅读(212) 评论(0) 推荐(0)

POJ 3620 Avoid The Lakes
摘要:dfs,统计最多多少个相邻(有公共边)方块。# include <cstdio># include <cstring># define N 100 + 15int n, m, k;char lake[N][N];int ans, cur;const int dx[] = {0,1,0,-1};const int dy[] = {1,0,-1,0};void dfs(int x, int y){ lake[x][y] = 0, ++cur; for (int i = 0; i < 4; ++i) { int nx = x + dx[i]; int n... 阅读全文

posted @ 2012-07-22 19:31 getgoing 阅读(222) 评论(0) 推荐(0)

POJ 2528 Mayor's posters
摘要:线段树,离散化;WA了三次,前两次主要是查询时只要遇到标记了颜色就要返回,而不是标记颜色并且没记录过就返回,后一次是空间开的小,因为离散化可能使数据量增加一倍,因此空间至少要开到区间数目的 4 倍;-----------------------------------------------------------2012/4/15View Code # include <cstdio># include <cstring># include <algorithm>using namespace std;# define N 10000 + 5int n, 阅读全文

posted @ 2012-07-13 10:55 getgoing 阅读(511) 评论(0) 推荐(0)

POJ 2503 Babelfish
摘要:字典树,WA;----------------------------------------------------# include <stdio.h># include <string.h># define LEN 12char buf[2 * LEN], str[LEN];struct word{ char a[LEN];} ;struct tree{ tree *node[26]; char bj; word *pw; tree() { for (int i = 0; i < 26; ++i) node[i] = NU... 阅读全文

posted @ 2012-07-12 17:35 getgoing 阅读(235) 评论(0) 推荐(0)

POJ 3468 A Simple Problem with Integers
摘要:线段树,维护区间的和,支持区间范围修改,注意由于增加的标记传递时是累加起来,结果会超出 int ,要用 long long;--------------------------------------------------------------DescriptionYou have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given int 阅读全文

posted @ 2012-07-12 15:32 getgoing 阅读(184) 评论(0) 推荐(0)

POJ 2352 Stars
摘要:因为输入是按y坐标升序的,对每个点统计在它左边的个数就是它的level(线段树);DescriptionAstronomers often examine star maps where stars are represented by points on a plane and each star has Cartesian coordinates. Let the level of a star be an amount of the stars that are not higher and not to the right of the given star. Astronomers 阅读全文

posted @ 2012-07-12 09:48 getgoing 阅读(963) 评论(0) 推荐(0)

POJ 3630 Trie树 TLE
摘要:动态 Trie 会超时,DISCUSS 中有人提醒要用静态数组的 Trie,还没学;这个题要注意除了判断当前串是否有和字典中的串相同的前缀外,还要判断当前串是否是字典中串的前缀;-----------------------------------------------------------------DescriptionGiven a list of phone numbers, determine if it is consistent in the sense that no number is the prefix of another. Let's say the ph 阅读全文

posted @ 2012-07-11 01:34 getgoing 阅读(225) 评论(0) 推荐(0)

POJ 2001 Trie
摘要:给出的单词不会重复出现,可以建立字典树,设一个域为当前结点重复次数(或者是否重复),对每个单词输出到第一个不重复的字母或者这个单词的结尾。-----------------------------------------------------------------------------------------DescriptionA prefix of a string is a substring starting at the beginning of the given string. The prefixes of "carbon" are: "c& 阅读全文

posted @ 2012-07-11 00:45 getgoing 阅读(275) 评论(0) 推荐(0)

POJ 3660 Cow Contest
摘要:对每个结点 BFS 一遍,找出能被它打败和可以打败它的结点的总和 s ,如果 s == n-1 ,它的排名可以确定;C++ vector 邻接表;# include <cstdio># include <iostream># include <cstring># include <vector># define N 105using namespace std;int n, m;char vis[N];vector <int> pre[N];vector <int> aft[N];int bfs(int u, int d) 阅读全文

posted @ 2012-07-09 08:41 getgoing 阅读(255) 评论(0) 推荐(0)

POJ 1562 Oil Deposits
摘要:简单DFS,搜索八个方向。# include <stdio.h>const int dir[][2] = {{-1,0}, {0,-1}, {0,1}, {1,0}, {1,1}, {1,-1}, {-1,1}, {-1,-1}};int n, m;char f[105][105];char read_data(void){ int i, j; scanf("%d", &m); scanf("%d", &n); if (m == 0) return 0; for (i = 1; i <= m; ++i) { scanf( 阅读全文

posted @ 2012-06-04 12:06 getgoing 阅读(209) 评论(0) 推荐(0)

POJ 2406 Power Strings
摘要:白书上看过这道题,枚举即可,530MS左右,这道题分类是 KMP ,可能是用 next 数组。# include <stdio.h># include <string.h>char s[1000005];int check(int i, int t);int solve(int len);int main(){ int len; while (1) { scanf("%s", s); len = strlen(s); if (len == 1 && s[0] == '.') break; else... 阅读全文

posted @ 2012-05-11 11:13 getgoing 阅读(183) 评论(0) 推荐(0)

字符串模式匹配:POJ 3461 Oulipo
摘要:这道题是字符串的模式匹配,要求计算出模式串在文本串中出现的次数,比如:"AZA" 在 "AZAZAZA" 中出现了 3 次;这道题使用 KMP 过的,但是 horspool 却不能过,尝试了一下各种方法后,还是回到了麻烦的 KMP,留下了以下几个模版:1. shift-or,据说比 KMP 快两倍,但只适用于模式串在 32 位以内,64位以内的也可以,需要把 b[] 改为 long long int 型,超出范围的可以尝试自己构造长类型……这道题不适用# include <string.h>int b[128]; /* int型共32位,模式 阅读全文

posted @ 2012-05-11 10:43 getgoing 阅读(557) 评论(0) 推荐(0)

POJ 1401 Factorial
摘要:求 n! 末尾零的个数,在编程之美上看过,其实也不难:求因子2和5的个数取最小值,因为5出现的频率远低于2。# include <stdio.h>int solve(int n);int main(){ int T, n; scanf("%d", &T); while (T--) { scanf("%d", &n); printf("%d\n", solve(n)); } return 0;}int solve(int n){ int t; t = 0; while (n>0) {n /=... 阅读全文

posted @ 2012-05-06 18:25 getgoing 阅读(172) 评论(0) 推荐(0)

POJ 2407 Relatives
摘要:欧拉函数的应用,素数定理;看了DISCUSS中那段超短的代码后才明白欧拉函数直接解决,我之前想的是集合中的容斥定理,实际上这里欧拉公式也实质上就是容斥定理;另外用了刚学的优化的筛法计算素数表。# include <stdio.h># define N 1000005int m;char pt[N];int p[N/10];void build_ptable(void);int solve(int n);int main(){ int n; build_ptable(); while (~scanf("%d", &n) && n) prin 阅读全文

posted @ 2012-05-05 14:48 getgoing 阅读(249) 评论(0) 推荐(0)

[TLE] POJ 1730 Perfect Pth Powers
摘要:给出一个32位整型数 n,形如 n = x^p 的最大的 p;质因数分解;TLE,DISCUSS 中有的用枚举做的(要考虑浮点数精度问题),但是质因数分解应该也可以啊,为什么老是TLE?求大牛指点。# include <stdio.h># include <math.h>int gcd(int a, int b){return !b ? a:gcd(b, a%b);}int m;char t[60005];int p[10005];void solve(long long int n);void build_ptable(void);int main(){ long l 阅读全文

posted @ 2012-05-02 22:02 getgoing 阅读(257) 评论(0) 推荐(0)

POJ 2262 Goldbach's Conjecture
摘要:在 1000000 以内验证哥德巴赫猜想:筛法求素数;为了减少空间,可能要用素数定理,但这道题空间足够了;1WA,太着急了没注意到输入为 0 时结束。# include <stdio.h># define MAXN 1000000int m;int ptable[MAXN+1], p[MAXN/4];void build_ptable(void);void solve(int n);int main(){ int n; build_ptable(); while (~scanf("%d", &n) && n) solve(n); r... 阅读全文

posted @ 2012-05-02 15:29 getgoing 阅读(290) 评论(0) 推荐(0)

POJ 2551 Ones
摘要:题目描述:Given any integer 0 <= n <= 10000 not divisible by 2 or 5, some multiple of n is a number which in decimal notation is a sequence of 1's. How many digits are in the smallest such a multiple of n?a multiple of n 的意思是某一个数与 n 的积(吧?)开始我愣是没读懂题,一搜都说是水题,没看别人的题解,自己想了两种方法:1. 试因子法:每次选取一个0-9的数字使 阅读全文

posted @ 2012-05-02 13:04 getgoing 阅读(419) 评论(0) 推荐(0)

1 2 下一页

导航