07 2013 档案
POJ2503 Babelfish
摘要:题目链接。分析:应当用字典树,但stl的map做很简单.#include #include #include #include #include #include #include #include #include using namespace std;const int maxn = 20;map m;int main() { char s[maxn], s1[maxn], s2[maxn]; while(gets(s), s[0] != 0) { sscanf(s, "%s%s", s1, s2); m[s2] = s1; } while(g... 阅读全文
posted @ 2013-07-31 23:49 Still_Raining 阅读(137) 评论(0) 推荐(0)
POJ3687 Labeling Balls(拓扑)
摘要:题目链接。题目大意:N个球,从1~N编号,质量不同,范围1~N,无重复。给出小球间的质量关系(#include #include #include #include #include #include #include #include using namespace std;const int maxn = 200+10;bool G[maxn][maxn];int n, m, ind[maxn], a[maxn];bool topsort() { for(int i=n; i>=1; i--) { int k; for(k=n; k>=1; k--) ... 阅读全文
posted @ 2013-07-31 20:00 Still_Raining 阅读(522) 评论(0) 推荐(0)
POJ2251 Dungeon Master(bfs)
摘要:题目链接。题目大意:三维迷宫,搜索从s到e的最小步骤数。分析:#include #include #include #include #include #include #include #include #include using namespace std;const int maxn = 35;int dx[] = {0, 0, -1, 1, 0, 0};int dy[] = {0, 0, 0, 0, -1, 1};int dz[] = {-1, 1, 0, 0, 0, 0};char G[maxn][maxn][maxn];bool vis[maxn][maxn][maxn];int 阅读全文
posted @ 2013-07-31 16:53 Still_Raining 阅读(168) 评论(0) 推荐(0)
POJ1321 棋盘问题(dfs)
摘要:题目链接。分析:用 dfs 一行一行的搜索,col记录当前列是否已经放置。AC代码如下:#include #include #include #include #include #include #include #include #include using namespace std;const int maxn = 10;int n, k, cnt;bool col[maxn];char G[maxn][maxn];void dfs(int row, int num) { if(num == k) { cnt++; return ; } if(row+1 > n) retur... 阅读全文
posted @ 2013-07-31 15:50 Still_Raining 阅读(213) 评论(0) 推荐(0)
POJ3009 Curling 2.0(DFS)
摘要:题目链接。分析:本题BFS A不了。001000000101020000000001000010000100001000030对于这样的数据,本来应当是 5 步,但bfs却 4 步。具体原因可以仔细看一下上面的样例。应当dfs穷举所有的可能,找出最短的。#include #include #include using namespace std;const int maxn = 23;const int INF = (1= 10) return ; for(int d=0; d= h || ny >= w) continue ; if(G[nx][ny] == 1) con... 阅读全文
posted @ 2013-07-30 18:53 Still_Raining 阅读(873) 评论(0) 推荐(0)
POJ2248 A Knight's Journey(DFS)
摘要:题目链接。题目大意:给定一个矩阵,马的初始位置在(0,0),要求给出一个方案,使马走遍所有的点。列为数字,行为字母,搜索按字典序。分析:用 vis[x][y] 标记是否已经访问。因为要搜索所有的可能,所以没搜索完一次要把vis[x][y]标记为未访问。详情见代码。用 p[x][y] 表示 (x,y)点的祖先,以便输出路径。dfs搜索即可。#include #include #include #include #include using namespace std;const int maxn = 30;const int VAL = 10000;int dx[] = {-1, 1, -2, 阅读全文
posted @ 2013-07-29 19:06 Still_Raining 阅读(244) 评论(0) 推荐(0)
POJ3080 Blue Jeans
摘要:题目链接。题目大意:给定n个字符串,找出最长相同且长度大于3的子串,如果存在多个,找出字典序最小的。分析:直接枚举(暴搜)。对于s[0]的每一个子串,判断是否在其它n-1个字符串中都存在。#include #include #include #include using namespace std;#define N 60string s[30], str, str_max;int main(){ int T, n, max_len; scanf("%d", &T); while(T--) { scanf("%d", &n); max_l 阅读全文
posted @ 2013-07-29 15:02 Still_Raining 阅读(194) 评论(0) 推荐(0)
POJ1260 Pearls(dp,矩阵链乘法)
摘要:题目链接。题目大意:给定一个n,和两个序列a[i], p[i]. a[i] 表示需要购买 i品质 的数量,p[i] i 等级的价格。1.每个品质都会有不同的价格,价格依据品质上升而上升2.买一次 i 品质,都要加上10个 i 品质 价格的手续费。3.可一用高品质的代替低品质.求最少的花费.分析:这题就简单地矩阵链乘法(《算法导论》第15章,动态规划)。用 dp[i][j] 表示购买 i 品质到 j 的最少的花费.dp[i][j] = min{dp[i][k]+dp[k+1][j]), i#include using namespace std;const int maxn = 1500;int 阅读全文
posted @ 2013-07-29 09:19 Still_Raining 阅读(367) 评论(0) 推荐(0)
POJ3349 Snowflake Snow Snowflakes(哈希)
摘要:题目链接。分析:哈希竟然能这么用。检查两片雪花是否相同不难,但如果是直接暴力,定会超时。所以要求哈希值相同时再检查。AC代码:#include #include #include #include #include #include #include #include #include using namespace std;const int maxn = 100000+10;const int MOD_VAL = 97777;int sn[maxn][6];vectorhash[MOD_VAL];bool check(int a, int b) { //检查两片是否相等 for(i... 阅读全文
posted @ 2013-07-24 12:41 Still_Raining 阅读(228) 评论(0) 推荐(0)
POJ2479 Maximum sum(dp)
摘要:题目链接。分析:用 d1[i] 表示左向右从0到i的最大连续和,d2[i] 表示从右向左, 即从n-1到i 的最大连续和。ans = max(ans, d1[i]+d2[i+1]), i=0,1, 2,...,n-2直接枚举会TLE, 优化下就可AC。#include #include #include #include #include #include #include #include using namespace std;const int maxn = 50000+10;const int INF = (1= 0) d1[i] = a[i] + d1[i-1]; ... 阅读全文
posted @ 2013-07-13 18:13 Still_Raining 阅读(177) 评论(0) 推荐(0)
POJ1159 Palindrome(dp)
摘要:题目链接。分析:感叹算法的力量。方法一:设 dp[i][j] 为字符串 s, 从 i 到 j 需要添加的最少字符数。那么如果 s[i] == s[j], dp[i][j] = dp[i+1][j-1]. 如果 s[i] != s[j], dp[i][j] = min(dp[i+1][j], dp[i][j-1]) + 1.#include #include #include #include #include using namespace std;const int maxn = 5000;short dp[maxn][maxn];char s[maxn];int d(int i, int 阅读全文
posted @ 2013-07-11 19:48 Still_Raining 阅读(255) 评论(0) 推荐(0)
POJ1080 Human Gene Functions(LCS)
摘要:题目链接。分析:和 LCS 差不多。#include #include #include #include #include using namespace std;const int maxn = 200;int G[][5] = { {5, -1, -2, -1, -3}, {-1, 5, -3, -2, -4}, {-2, -3, 5, -2, -2}, {-1, -2, -2, 5, -1}, {-3, -4, -2, -1, 0}};int dp[maxn][maxn];void trans(char *s, int n) { for(int i=... 阅读全文
posted @ 2013-07-11 18:31 Still_Raining 阅读(206) 评论(0) 推荐(0)
UVA253 Cube painting(数学)
摘要:题目链接。分析:用的《训练指南》上的方法。详见P17.从6个面中选一个做顶面,再从剩下的4个面中选1个做正面,则此正方体唯一确定。需要枚举共6*4=24种。#include #include #include #include #include #include #include #include using namespace std;const int maxn = 1000;int _left[6] = {4, 0, 2, 3, 5, 1};int up[6] = {2, 1, 5, 0, 4, 3};char s1[maxn], s2[maxn], s[maxn];void rot(i 阅读全文
posted @ 2013-07-09 13:21 Still_Raining 阅读(1243) 评论(1) 推荐(0)
POJ2115 C Looooops(数论)
摘要:题目链接。分析:数论了解的还不算太多,解的时候,碰到了不小的麻烦。设答案为x,n = (1#include #include #include #include #include #include #include #include #include using namespace std;typedef long long LL;void gcd(LL a, LL b, LL &d, LL &x, LL &y) { if(!b) { d = a; x = 1; y = 0; } else { gcd(b, a%b, d, y, x); y-= x*(a/b); }}i 阅读全文
posted @ 2013-07-06 15:37 Still_Raining 阅读(763) 评论(0) 推荐(0)
【转】每个程序员都应读的书
摘要:原文链接:http://news.cnblogs.com/n/125826/ 编者按:2008年 8 月 4 日,StackOverflow 网友Bert F发帖提问:哪本最具影响力的书,是每个程序员都应该读? “如果能时光倒流,回到过去,作为一个开发人员,你可以告诉自己在职业生涯初期应该读一本,你会选择哪本书呢?我希望这个书单列表内容丰富,可以涵盖很多东西。” 很多程序员响应,他们在推荐时也写下自己的评语。以前就有国内网友介绍这个程序员书单,不过都是推荐数 Top 10 的书。其实除了前 10 本之外,推荐数前 30 左右的书籍都算经典,伯乐在线整理编译这个问答贴,同时摘译部分推荐人的... 阅读全文
posted @ 2013-07-04 20:33 Still_Raining 阅读(271) 评论(0) 推荐(0)
POJ3041 Asteroids(二分图最大匹配)
摘要:题目链接。分析:暂略。AC代码:#include #include #include #include #include #include #include #include #include using namespace std;const int maxn = 510;int n;bool G[maxn][maxn];int linker[maxn];bool used[maxn];bool dfs(int u) { for(int v=0; v<n; v++) if(G[u][v] && !used[v]) { used[v] = true; i... 阅读全文
posted @ 2013-07-04 20:24 Still_Raining 阅读(230) 评论(0) 推荐(0)
POJ2485 Highways(最小生成树)
摘要:题目链接。分析:比POJ2253要简单些。AC代码:#include #include #include #include #include #include #include #include #include using namespace std;const int maxn = 550;const int INF = (1= d[y]) m = d[x=y]; vis[x] = true; for(int y=0; y#include #include #include #include #include #include #include #include... 阅读全文
posted @ 2013-07-03 22:34 Still_Raining 阅读(215) 评论(0) 推荐(0)
POJ2253 Frogger(最短路)
摘要:题目链接。题意:从0号点,到1号点,找一条能通过的路,使得这条路中的最大的边,比其它所有可能的路中的边都小。分析:这题就是按着dijkstra写,写着写着觉得像是prim了。其中d[n]表示从0出发到达该点的某条路中的最大边,且比其它可能的路中的都小。从d[i]到d[j], 就是要用 max(d[i], G[i][j]) 去更新 d[j] 。注意:输出时要用 %.3f 不能用 %.3lf.#include #include #include #include #include #include #include #include #include #include using namespac 阅读全文
posted @ 2013-07-03 17:03 Still_Raining 阅读(3610) 评论(2) 推荐(0)
POJ2240 Arbitrage(最短路)
摘要:题目链接。题意:根据汇率可以将一种金币换成其他的金币,求最后能否赚到比原来更多的金币。分析:最短路的求法,用floyd.#include #include #include #include #include #include #include #include #include using namespace std;const int maxn = 100;map a;double G[maxn][maxn];int cn ;int main() { int n, m, u, v, cnt = 0; char s[1000], s1[1000], s2[1000]; do... 阅读全文
posted @ 2013-07-03 13:37 Still_Raining 阅读(209) 评论(0) 推荐(0)