UVA 10066 - The Twin Towers
摘要:#include<stdio.h>#include<string.h>#define MAXD 105int a[MAXD], b[MAXD];int d[MAXD][MAXD];int N1, N2;int max( int a, int b){ return a > b ? a : b;}void init(){ for( int i = 1; i <= N1; i ++) scanf( "%d", &a[i]); for( int i = 1; i <= N2; i ++) scanf( "%d",
阅读全文
posted @
2011-11-30 18:43
找回失去的
阅读(249)
推荐(0)
UVA 111 - History Grading
摘要:这道题的输入比较奇葩,先输入N,再输入事件号,事件对应的时间是输入序列中的编号。然后再求序列c与序列r的最长公共子序列。#include<stdio.h>#include<string.h>#define MAXD 25int d[MAXD][MAXD], c[MAXD], r[MAXD];int N;int max( int a, int b){ return a > b ? a : b;}void dp(){ memset( d, 0, sizeof d); for( int i = 1; i <= N; i ++) for( int j = 1; j
阅读全文
posted @
2011-11-30 17:50
找回失去的
阅读(243)
推荐(0)
UVA 10340 - All in All
摘要:判断s串中的字符是不是依次出现在t串中,如果s的长度大于t,显然不行,其他的依次比较即可。但是字符串要定义得长一些,开始定义10000,RE了。#include<stdio.h>#include<string.h>char s[100000], t[100000];int main(){ while( scanf( "%s%s", s, t) == 2) { int len1 = strlen(s); int len2 = strlen(t); if( len1 > len2) { printf( "No\n"); ...
阅读全文
posted @
2011-11-30 12:18
找回失去的
阅读(214)
推荐(0)
UVA 10763 - Foreign Exchange
摘要:判断u->v 和 v -> u是否成对出现。#include<stdio.h>#include<string.h>#define MAXD 1005int G[MAXD][MAXD];int N, u, v;bool check(){ for( int i = 1; i <= 1000; i ++) for( int j = 1; j <= 1000; j ++) if( G[i][j] != 0) return false; return true;}void init(){ memset(G, 0, sizeof G); for(...
阅读全文
posted @
2011-11-30 11:33
找回失去的
阅读(236)
推荐(0)
UVA 10034 - Freckles
摘要:这道题和连接校园一样都是求最小生成树,用kruskal算法做,点的坐标要用double型#include<stdio.h>#include<stdlib.h>#include<math.h>#include<string.h>#define MAXD 150double x[MAXD], y[MAXD], w[MAXD * MAXD], ans;int r[MAXD * MAXD], p[MAXD], u[MAXD * MAXD], v[MAXD * MAXD];int cas, N, n, nx, ny;double dist( int i,
阅读全文
posted @
2011-11-30 11:06
找回失去的
阅读(200)
推荐(0)
UVA 10986 - Sending email
摘要:这道题就是在求最短路,而且是单源最短路吗,所以我先想到了dijkstra算法,结果TLE了,dijkstra的代码:#include<stdio.h>#include<string.h>#define INF 200000005#define MAXD 20005int u, v, w, S, T, n, m, N;int d[MAXD][MAXD], f[MAXD];bool vis[MAXD];int min( int a, int b){ return a < b ? a : b;}void init(){ for( int i = 0; i < n;
阅读全文
posted @
2011-11-29 23:34
找回失去的
阅读(460)
推荐(1)
UVA 10048 - Audiophobia
摘要:这道题和刚才那道题不同,求的是最大边的最小值。同样用了floyd算法d[i][j]=min{d[i][j],max{d[i][k],d[k][j]}}Initilization: d[1..n][1..n]= -1,d[i][i]=0;#include<stdio.h>#define MAXD 105int u, v, w, C, S, Q, q[10005];int d[MAXD][MAXD];int max( int a, int b){ return a > b ? a : b;}void init(){ for( int i = 1; i <= C; i ++)
阅读全文
posted @
2011-11-28 22:26
找回失去的
阅读(230)
推荐(1)
UVA 10099 - The Tourist Guide
摘要:这道题要使游客从一个点到另一个点,求最小的趟数,我们用floyd算法将所有路径中的最小边的最大值求出来,然后用计算trips就行了。求最小边的最大值的方程为:d[i][j]=max{d[i][j],min{d[i][k],d[k][j]}}初始化:所有 d[i][j]=0;#include<stdio.h>#define MAXD 110int u, v, w, S, D, T, R, N;int d[MAXD][MAXD];int min( int a, int b){ return a < b ? a : b;}void init(){ for( int i = 1; i
阅读全文
posted @
2011-11-28 20:08
找回失去的
阅读(252)
推荐(0)
UVA 10026 - Shoemaker's Problem
摘要:这道题是贪心的题,因为鞋匠做事情都是得持续做的,所以我们根据事件的价值降序排序。这里用的依然是间接排序。#include<stdio.h>#include<stdlib.h>#define MAXD 1010double t[MAXD], s[MAXD], w[MAXD];int r[MAXD];int cmp( const void *_p, const void *_q){ int *p = (int *)_p; int *q = (int *)_q; return w[*p] < w[*q];}int main(){ int cas, n; scanf( &
阅读全文
posted @
2011-11-27 23:57
找回失去的
阅读(255)
推荐(0)
UVA 10397 - Connect the Campus
摘要:这是求最小生成树的一道题,搬了白书上的Kruskal算法,再一次见到了间接排序。值得注意的是得先用并查集将给的已经有连接的点合并。#include<stdio.h>#include<stdlib.h>#include<string.h>#include<math.h>#define MAXD 760double x[MAXD], y[MAXD], w[10000 * MAXD], ans;int p[MAXD], u[MAXD*MAXD], v[MAXD*MAXD], r[MAXD*MAXD];int N, M, tt, nx, ny, a, b
阅读全文
posted @
2011-11-26 16:22
找回失去的
阅读(215)
推荐(1)
UVA 567 - Risk
摘要:这道题是求图里任意两点的最短路的长度并且输出,用floyd算法可以解决这个问题,但是题目的输入比较奇葩吧,从i = 1 到 i = 19,第 i 行先输入一个数字n,代表有几个数字,后面跟着的数字都代表与 i 点直接连接的点,他们的距离为1.后面输入m,代表有多少对点,要求我们输出每队点之间的最短路径长度。#include<stdio.h>#include<string.h>#define INF 1000001int n, m, d[25][25];int x, y;int min( int a, int b){ return a < b ? a : b;}in
阅读全文
posted @
2011-11-25 23:41
找回失去的
阅读(257)
推荐(1)
UVA 10608-Friends
摘要:UVA晚上活过来了,一看到这题就感觉是并查集的题,还真是,并查集找到一个最大的集合,然后输出它的元素个数。#include<stdio.h>#include<string.h>#define MAXD 30010int p[MAXD], cnt[MAXD];int n, m;int find_set( int x){ return p[x] == x ? x : (p[x] = find_set(p[x]) );}void union_set( int x, int y){ x = find_set(x); y = find_set(y); if( x == y) re
阅读全文
posted @
2011-11-25 21:21
找回失去的
阅读(182)
推荐(0)
HDU 1016-Prime Ring Problem
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1016素数环,回溯法/*Accepted 1016 203MS 248K 1111 B C++ Yu*/#include<stdio.h>#include<string.h>int A[25], p[40], vis[25], n;void isprime(){ int i, j; for(i = 2; i < 40; i ++) p[i] = 1; p[1] = 0; for(i = 2; i * i < 40; i ++) {...
阅读全文
posted @
2011-11-25 13:10
找回失去的
阅读(193)
推荐(0)
POJ 2255-Tree Recovery
摘要:http://poj.org/problem?id=2255这道题是输入二叉树的先序遍历、中序遍历,然后要求我们输出它的后序遍历,参考了白书上的二叉树重建的函数,先找到根结点,然后分别通过递归构造左右子树的后序遍历,然后将根节点放到输出字符串的最后。/*Accepted 160K 0MS C++ 563B 2012-07-31 12:16:15*/#include<stdio.h>#include<string.h>const int MAXN = 1 << 5;char s1[MAXN], s2[MAXN], ans[MAXN];void build(in
阅读全文
posted @
2011-11-25 12:13
找回失去的
阅读(191)
推荐(1)
UVA 10405-Longest Common Subsequence
摘要:最长公共子序列,值得注意的是这道题不能用scanf读字符串。#include<stdio.h>#include<string.h>#define MAXD 1005char s1[MAXD], s2[MAXD];int dp[MAXD][MAXD];int max( int a, int b){ return a > b ? a : b;}void solve(){ int len1 = strlen(s1 + 1); int len2 = strlen(s2 + 1); memset( dp, 0, sizeof dp); for( int i = 1; i &
阅读全文
posted @
2011-11-24 23:54
找回失去的
阅读(166)
推荐(0)
UVA 401- Palindromes
摘要:这是一道关于回文字符串的题,但是加上了镜像,我用了两个函数分别判断一个字符串是不是回文和镜像,然后用两个flag,最后根据题目的要求来输出结果。#include<stdio.h>#include<string.h>#include<ctype.h>char *ch = "A 3 HIL JM O 2TUVWXY5";char *num = "1SE Z 8 ";bool is_pal( char *s){ int len = strlen(s); for( int i = 0; i < len; i ++) if
阅读全文
posted @
2011-11-24 12:47
找回失去的
阅读(327)
推荐(0)
UVA 10041 - Vito's Family
摘要:一个排序好的数组中位于中间的数到其他的数的总和总是最小的,所以根据这个就可以写了。#include<cstdio>#include<stdlib.h>#include<algorithm>#define MAXD 505using namespace std;int a[MAXD];int main(){ int cas, r; scanf( "%d", &cas); while( cas --) { scanf( "%d", &r); for( int i = 0; i < r; i ++) s
阅读全文
posted @
2011-11-23 00:17
找回失去的
阅读(182)
推荐(1)
CSUOJ 1203-购置游泳圈
摘要:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1203这是道贪心题,要求最少的救生圈的个数。先将每个人要求的救生圈范围 a 、b按照b的升序、b相等的时候按照a的降序排序。将第一个的b作为第一个判断点,直到第i个人的a大于之前的b,再将b换成第i个b,依此类推!#include<iostream>#include<stdlib.h>using namespace std;typedef struct d{ int a; int b;}B;B t[10005];int cmp( const void *_p, const
阅读全文
posted @
2011-11-21 14:16
找回失去的
阅读(202)
推荐(0)
CSUOJ 1208-排队
摘要:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1208亏我还想用并查集去做,这道题用数组模拟链表就行,数组下标为编号,一个一个接下去就行,在接的同时计数。这题有点恶心的是必须把数组开到101000以上,要不就会RE,这也是考查的一个方面吧!#include<iostream>#include<string.h>using namespace std;const int N = 101005;int next[N];int main(){ int n, m; int a, b; while( cin >> n
阅读全文
posted @
2011-11-21 14:11
找回失去的
阅读(370)
推荐(0)
POJ 1321-棋盘问题
摘要:http://poj.org/problem?id=1321这是手册上的深搜题,首先我们要将棋盘的位置标记,因为行和列都不同,所以必须用两个数组分别来存点的横坐标和纵坐标,然后开始一个棋子一个棋子地摆上去,计算有几种摆法。#include<iostream>#include<string.h>using namespace std;const int N = 8*8 + 10;int x[N], y[N];bool visx[N], visy[N];int n, k, m;long long cnt;char ch[N];void dfs( int a, int ans
阅读全文
posted @
2011-11-19 23:24
找回失去的
阅读(215)
推荐(0)
UVA 10420 - List of Conquests
摘要:继续跟着白书做,这道题是要找每个国家的人的数量,然后按国家的字典序输出,很水的一道题,但是因为少了个getchar()浪费了不少时间。#include<stdio.h>#include<string.h>#include<stdlib.h>char ct[2005][80], s[80];int cmp( const void *_p, const void *_q){ char *p = (char *)_p; char *q = (char *)_q; return strcmp( p, q);}int main(){ int N; scanf( &qu
阅读全文
posted @
2011-11-18 11:52
找回失去的
阅读(876)
推荐(1)
CSUOJ 1150-食用油
摘要:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1150这是道BFS的题,打油师要用容量为A和B的油桶打出C升的油,有六种操作方式,我们从前一种状态到后一种状态的变化是有规律的,广搜所有的操作方式,对每种状态都标记,直到找到需要的C升油。搞了好久才把队列和广搜搞明白一点,纠结ING!#include<iostream>#include<string.h>#define N 105using namespace std;int vis[N][N], qa[64*N], qb[64*N], d[64*N];int A, B,
阅读全文
posted @
2011-11-17 23:20
找回失去的
阅读(259)
推荐(0)
POJ 1915-Knight Moves
摘要:http://poj.org/problem?id=1915这道题还是求骑士从一个点移动到另一个点距离的问题,不同的是棋盘的规格是由我们输入的,最大有300*300,数组要开足够大,我把2243的代码修改了下就AC了...好好理解下BFS#include<iostream>#include<string.h>using namespace std;int x1, y1, x2, y2;int dist[305][305], qx[90005], qy[90005];int dx[] = { -1, -2, -2, -1, 1, 2, 2, 1};int dy[] = {
阅读全文
posted @
2011-11-14 23:36
找回失去的
阅读(217)
推荐(0)
POJ 2243-Knight Moves
摘要:http://poj.org/problem?id=2243我是在看staginner大牛的博客的时候看到这道题的,因为看到了BFS,所以就拿来做了,但是发现好像之前没写过BFS这玩意,所以就基本照着搬了一遍他的代码,自己写了一下,理解了下队列和广搜。题目要我们找到从一个点到另一个点的骑士移动的步数,按照staginner的做法是记录在找到终点之前的所有点到起点的步数。#include<iostream>#include<string.h>#include<stdio.h>using namespace std;char a[5], b[5];int x1,
阅读全文
posted @
2011-11-14 22:43
找回失去的
阅读(207)
推荐(0)
UVA 10305 - Ordering Tasks
摘要:#include<iostream>#include<string.h>#define N 105using namespace std;int n, m;int c[N];int topo[N], t;int tt[N][N];void dfs( int u){ int v; c[u] = 1; for( v = 1; v <= n; v ++) if( tt[u][v] && !c[v] ) dfs( v); topo[ -- t] = u;}int main(){ int a, b, i; while( cin >>...
阅读全文
posted @
2011-11-12 15:41
找回失去的
阅读(188)
推荐(0)
CSUOJ 1196- Staginner 去爬山
摘要:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1196这道题也是搜索题,还是dfs,我们只需对最后一行进行搜索,然后找到能爬到的最高高度即可,每走一步我们就计算一次现在处于的高度h,然后与maxh比较。因为开始少写了一句memset而WA了一遍...#include<stdio.h>#include<string.h>#define N 105int dx[] = { 0, 0, 1, -1};int dy[] = { 1, -1, 0, 0};int map[N][N], vis[N][N];int n, m;int
阅读全文
posted @
2011-11-10 10:45
找回失去的
阅读(266)
推荐(0)
UVA 572 - Oil Deposits
摘要:这道题是一道搜索题,要我们找有多少块不同的油田,@与其相邻的八个方向的@属于同一片油田,我选择用dfs来做。#include<stdio.h>#include<string.h>#define N 105int map[N][N], vis[N][N];char str[N];int dx[] = { -1, 1, 0, 0, -1, -1, 1, 1};int dy[] = { 0, 0, -1, 1, -1, 1, -1, 1};void dfs( int x, int y){ int newx, newy; for( int i = 0; i < 8; i
阅读全文
posted @
2011-11-10 09:50
找回失去的
阅读(206)
推荐(0)
CSUOJ 1197- Staginner 买葡萄
摘要:这道题是基础背包问题,我们手里有n块钱,然后求n块钱能买到的最大葡萄个数,价格为j的一串葡萄有除开j本身外其所有约数之和个葡萄。先将每串葡萄的个数求出来,再用状态转移方程,dp[i] = max( dp[i], dp[n - j] +w[j])即可,i从n到0。#include<stdio.h>#include<string.h>#define N 26using namespace std;int w[N];int dp[N];int max( int a, int b){ return a > b ? a : b;}int f( int n){ int sum
阅读全文
posted @
2011-11-10 09:17
找回失去的
阅读(204)
推荐(0)
UVA 674-Coin Change
摘要:#include<iostream>using namespace std;int dp[7500];int coin[5] = { 1, 5, 10, 25, 50};int main(){ dp[0] = 1; for( int i = 0; i < 5; i ++) { for( int j = 1; j < 7500; j ++) { if( j >= coin[i] ) dp[j] += dp[ j - coin[i] ]; } } int cents; ...
阅读全文
posted @
2011-11-07 10:28
找回失去的
阅读(520)
推荐(1)
【转】在c++中qsort()排序函数的使用qsort函数应用大全
摘要:转自http://www.cnblogs.com/ihainan/archive/2011/06/04/2072699.html七种qsort排序方法<本文中排序都是采用的从小到大排序>一、对int类型数组排序int num[100];Sample:int cmp ( const void *a , const void *b ) { return *(int *)a - *(int *)b; }qsort(num,100,sizeof(num[0]),cmp);二、对char类型数组排序(同int类型)char word[100];Sample:int cmp( const vo
阅读全文
posted @
2011-11-07 08:44
找回失去的
阅读(237)
推荐(0)
【转】POJ 题目列表(DP)
摘要:这份列表当然不是我原创的,转自月田人的博客http://www.cnblogs.com/qijinbiao/archive/2011/09/02/2163460.html※最近更新:Poj斜率优化题目1180,2018,3709列表一:经典题目题号:容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1191,1208, 1276, 1322, 1414, 1456, 1458, 1609, 1644, 1664, 1690, 1699, 1740, 1742, 1887,1926, 1936, 1952,
阅读全文
posted @
2011-11-07 08:34
找回失去的
阅读(327)
推荐(0)
UVA 10905 - Children's Game
摘要:这道题是要我们将给的数字进行接龙成一个很长的数字,然后找出其中最大的数字。所以排序不是比较两个数字的大小,而是比较两个数字按照前后顺序接龙获得的数字的大小,因为数字可能很大所以用数组,借助strcmp函数来排序。最后再将排序后的数组输出。这次熟悉了下strcat函数,看来蛮好用。#include<stdio.h>#include<stdlib.h>#include<string.h>char num[55][100];int n;void init(){ for( int i = 0; i < n; i ++) scanf( "%s"
阅读全文
posted @
2011-11-07 00:09
找回失去的
阅读(566)
推荐(0)
UVA 10714 - Ants
摘要:这道题是要我们找出所有蚂蚁中最靠近端点和最靠近中间的蚂蚁所在的位置,计算端点的蚂蚁爬到另一个端点的时间和计算靠近中间的蚂蚁爬到离他近的端点的时间。我们只需分输入的位置在棒的左边还是右边来讨论就行。#include<iostream>using namespace std;int main(){ int cas; int l, n, a; cin >> cas; while( cas --) { cin >> l >> n; int ans, earliest = 0, latest = 0; while( n --) ...
阅读全文
posted @
2011-11-04 14:41
找回失去的
阅读(242)
推荐(0)
UVA 490-Rotating Sentences
摘要:#include<stdio.h>#include<string.h>char ch[105][105];int main(){ int cnt = 0, ans = 0; while( gets(ch[cnt])) { int len = strlen( ch[cnt]); if( len > ans) ans = len; cnt ++; } for(int j = 0; j < ans; j ++) { for( int i = cnt - 1; i >= 0; i --) { ...
阅读全文
posted @
2011-11-04 13:01
找回失去的
阅读(552)
推荐(0)
UVA 488-Triangle Wave
摘要:本来这应该是一道很简单的题的,但是题目对空格的要求很高,所以我WA了好几次。而且读入数据的时候我居然加了getchar();...这也是WA的罪魁祸首!贴代码:#include<iostream>#include<stdio.h>using namespace std;int main(){ int n, a, f; cin >> n; int cas = 0; while( n --) { if( cas ++) printf("\n"); cin >> a >> f; while( f -- ) { ...
阅读全文
posted @
2011-11-03 22:53
找回失去的
阅读(509)
推荐(0)
POJ 2503-Babelfish
摘要:http://poj.org/problem?id=2503这是一道字符串处理的题。有点写字典,查字典的意思,首先要解决的是输入字典和查字典时输入单词中间的那一个空行,我是用gets输入前面的东西,所以只需一个判断就可以跳出输入字典的循环。然后是输入单词翻译,因为字典当中的单词比较多,我又不会hash,所以用了二分查找。#include<stdio.h>#include<string.h>#include<stdlib.h>#define M 100003typedef struct dictionary{ char f[15]; char n[15];}D
阅读全文
posted @
2011-11-03 12:42
找回失去的
阅读(288)
推荐(1)
UVA 694-The Collatz Sequence
摘要:继续做白书的题,发现基础还得补。这道题是变化版的3n+1问题,给定初始的A,然后再给一个limit数,当运算过程中的中间数超过了limit或者A = 1结束运算,要我们求这个过程的运算次数ans。#include<cstdio>int main(){ long long a, b; int cnt = 0; while( true) { cnt ++; scanf( "%lld%lld", &a, &b); if( a == -1 && b == - 1) break; long long n = a; ...
阅读全文
posted @
2011-11-03 10:16
找回失去的
阅读(300)
推荐(0)
UVA 458(The Decoder)
摘要:每个字符剪去7就能翻译过来了#include<stdio.h>#include<string.h>char ch[1000];int main(){ while( gets(ch) != NULL ) { int len = strlen(ch); for(int i = 0; i < len; i ++) { printf("%c", ch[i] - 7); } printf("\n"); } return 0;}
阅读全文
posted @
2011-11-03 00:23
找回失去的
阅读(225)
推荐(0)
UVA 494(Kindergarten Counting Game)
摘要:这道题的标题中的幼儿园让人看得郁闷,计算单词的数目...#include<stdio.h>#include<stdlib.h>#include<ctype.h>#include<string.h>char ch[1000];int main(){ char a[100]; while( gets(ch) != NULL) { int len = strlen(ch); int cnt = 0; for(int i = 0; i <= len; i ++) { if( isalpha( ch[i] )...
阅读全文
posted @
2011-11-03 00:21
找回失去的
阅读(279)
推荐(0)
UVA 445(Marvelous Mazes)
摘要:这道题做的很郁闷,用gets始终A不了,然后用了fegets就A了,难道是题意理解不对?#include<stdio.h>#include<string.h>#include<stdlib.h>char ch[150];int main(){ int k = 0; while( fgets( ch, 150, stdin) ) { int len = strlen(ch); for(int i = 0; i < len; i ++) { if( ch[i] > '0' && ch[i] <= '9
阅读全文
posted @
2011-11-03 00:14
找回失去的
阅读(314)
推荐(0)
UVA 10300(Ecological Premium)
摘要:这题看得有点迷糊,但是用了下灵格斯就懂题意了,直接上代码:#include<iostream>using namespace std;int main(){ int n, f, a, b, c; while(cin >> n) { while( n --) { cin >> f; int sum = 0; for( int i = 0; i < f; i ++) { cin >> a >> b >> c; ...
阅读全文
posted @
2011-11-03 00:10
找回失去的
阅读(178)
推荐(0)