07 2012 档案

摘要:http://poj.org/problem?id=1942View Code 1 #include<stdio.h> 2 int main() 3 { 4 __int64 s,n,m,i,j,k; 5 while(scanf("%I64d%I64d", &n,&m)!=EOF) 6 { 7 if(n==0&&m==0) 8 break; 9 s = 1;10 if(n>m)11 k = m;12 else13 k = n;14 ... 阅读全文
posted @ 2012-07-31 20:58 _雨 阅读(209) 评论(0) 推荐(0) 编辑
摘要:http://poj.org/problem?id=2049与一般的BFS不大一样 是把网格转换成我们常用的点以左下角的点表示整个方格 用优先队列来保证每次搜的都是最小的 注意Nemo的位置有可能不在墙内之前把sx看成tx一直调试不出来 ,,纠结了一天 第二天一眼看出它俩不一样。。改完之后 找了找结构体的优先队列怎么用 按知道上回答写的 一直WA 后来看人家的解题报告 发现不是那么用的,改过来交上AC。。View Code 1 #include<stdio.h> 2 #include<string.h> 3 #include<iostream> 4 #inc 阅读全文
posted @ 2012-07-31 18:22 _雨 阅读(214) 评论(0) 推荐(0) 编辑
摘要:View Code 1 #include 2 #include 3 char c1[1000011],c2[1000011]; 4 int next[1000011]; 5 int kmp() 6 { 7 int k = strlen(c1),i,j,y = -1; 8 memset(next,0,sizeof(next)); 9 next[0] = -1;10 for(i = 1; i -1&&c1[i]!=c1[y+1])13 y = next[y];14 if(c1[y+1]==c1[i])15 ... 阅读全文
posted @ 2012-07-30 22:00 _雨 阅读(316) 评论(0) 推荐(0) 编辑
摘要:http://poj.org/problem?id=2689筛选素数 先建一个小素数表 2-sqrt(max) 再根据这个表来进行区间素数的筛选View Code 1 #include<math.h> 2 #include<stdio.h> 3 #include<string.h> 4 __int64 x[1000011]; 5 int pr[46500],pd[46500],pg[1000011]; 6 int main() 7 { 8 __int64 n,m,x1,x2,x3,x4,y; 9 int i,j,k;10 for(i = 2 ; i < 阅读全文
posted @ 2012-07-30 15:55 _雨 阅读(183) 评论(0) 推荐(0) 编辑
摘要:http://poj.org/problem?id=2299求逆序数第一种做法 利用归并排序 在进行两个升序数组归并时 如果右边指针指向的那个数比左边小 那从左边那个位置到mid的数都比右边那个数大 所以比它大的数的个数就是 左边那个数到mid之间的数 依次类推View Code 1 #include <stdio.h> 2 #include <string.h> 3 long long a[500001],num,x[500001],y[500001]; 4 long long msort(long long low,long long mid,long long h 阅读全文
posted @ 2012-07-28 13:22 _雨 阅读(208) 评论(0) 推荐(0) 编辑
摘要:1. 按位与运算 按位与运算符"&"是双目运算符。其功能是参与运算的两数各对应的二进位相与。只有对应的两个二进位均为1时,结果位才为1 ,否则为0。参与运算的数以补码方式出现。 例如:9&5可写算式如下: 00001001 (9的二进制补码)&00000101 (5的二进制补码) 00000001 (1的二进制补码)可见9&5=1。 按位与运算通常用来对某些位清0或保留某些位。例如把a 的高八位清 0 , 保留低八位, 可作 a&255 运算 ( 255 的二进制数为0000000011111111)。 应用: a. 清零特定位 (m 阅读全文
posted @ 2012-07-28 10:07 _雨 阅读(769) 评论(2) 推荐(0) 编辑
摘要:http://poj.org/problem?id=2777网上的一段解释经典的线段树题目。成段更新,在利用一个数组在查询顺次整段区间时,登记未曾被遮蔽到的颜色,并对其所对应的编号符号为1.而在成段更新结点时,若某区间将揭示混杂色,就让其子节点更新为它的颜色,并符号该结点。总之,对于成段更新区间属性的题目等闲用线段树来处理,利用时要保留结点属性的单一性,但更新摧毁了这种单一性时,则应递归地坚持该结点孩子的属性的单一性。只有这么,在更新垄断才不至于退化到O(n)的混杂度,保证高效率。View Code 1 #include 2 #include 3 #include 4 #inc... 阅读全文
posted @ 2012-07-27 21:48 _雨 阅读(195) 评论(0) 推荐(0) 编辑
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1867这题输出有点麻烦 两个KMP 找出从一个字符串开头到令一个字符串结尾匹配的最大长度 再根据字符串比较函数字典序输出View Code 1 #include 2 #include 3 char c1[100001],c2[200001],c3[100001],c4[200001]; 4 int next[100001]; 5 int kmp(char *a,char *b) 6 { 7 int k1 = strlen(a),k2 = strlen(b),i,j,y; 8 y = -1; ... 阅读全文
posted @ 2012-07-27 17:16 _雨 阅读(222) 评论(0) 推荐(0) 编辑
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=4313贪心算法:类似kruskal最小生成树的过程,不过此处将边按权值从大到小排列,每次将边加进来时要判断是否会使两个危险的点连通,是的话这条边就是需要被删除的,否则将它加到树上。还有一种 说是树形DP 不懂这题一直WA 看了秦老师的代码 用的--int64 就改了下数据类型 过了。。。并查集+快排+去掉危险的结点View Code 1 #include<stdio.h> 2 #include<string.h> 3 #include<stdlib.h> 4 typedef 阅读全文
posted @ 2012-07-27 09:36 _雨 阅读(176) 评论(2) 推荐(0) 编辑
摘要:线段树 单点更新http://acm.hdu.edu.cn/showproblem.php?pid=1166View Code 1 #include<stdio.h> 2 #include<string.h> 3 int sum[1000001]; 4 void push(int w) 5 { 6 sum[w] = sum[2*w]+sum[2*w+1];//更新节点值 7 } 8 void build(int l,int r,int w) 9 {10 if(l==r)11 {12 scanf("%d",&sum[w]);13 retu... 阅读全文
posted @ 2012-07-26 09:26 _雨 阅读(158) 评论(0) 推荐(0) 编辑
摘要:http://dongxicheng.org/structure/segment-tree/线段树,也叫区间树,是一个完全二叉树,它在各个节点保存一条线段(即“子数组”),因而常用于解决数列维护问题,它基本能保证每个操作的复杂度为O(lgN)。线段树的基本操作主要包括构造线段树,区间查询和区间修改。struct node{ int ld,rd;//左右边界 node *lc,*rc;//左右孩子 int key;//信息域 如RMQ问题中,信息域中存储的是区间最大值};建树//空树的建立,内含key值的初始化; //一般在主函数中首先调用 Node* root= buildt... 阅读全文
posted @ 2012-07-25 17:25 _雨 阅读(234) 评论(2) 推荐(0) 编辑
摘要:百练2981 http://poj.grids.cn/practice/2981/View Code 1 #include<stdio.h> 2 #include<string.h> 3 int num[301]; 4 int main() 5 { 6 int i,j,f = 0,f1; 7 char c1[201],c2[201]; 8 gets(c1); 9 gets(c2);10 i = strlen(c1)-1;11 j = strlen(c2)-1;12 int g = 0;13 while(i>=0&&j>=0)14... 阅读全文
posted @ 2012-07-25 16:32 _雨 阅读(214) 评论(0) 推荐(0) 编辑
摘要:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1135好繁琐的一道模拟题 细节很重要 编译错误一次 把字符串结尾写错WA一次 多了getchar()WA一次 多了以空格PE一次 最后AC...3个多小时用了一下map map还是很方便的View Code 1 #include <iostream> 2 #include<map> 3 #include<stdio.h> 4 #include&l 阅读全文
posted @ 2012-07-25 10:44 _雨 阅读(323) 评论(2) 推荐(0) 编辑
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1251将每次走过得字母统计数量 查找的时候输出字母数量的最小值View Code 1 #include<stdio.h> 2 #include<iostream.h> 3 #include<string.h> 4 struct node 5 { 6 int count; 7 node *next[27]; 8 node() 9 {10 count = 0;11 memset(next,NULL,sizeof(next));12 }13 ... 阅读全文
posted @ 2012-07-24 20:59 _雨 阅读(192) 评论(0) 推荐(0) 编辑
摘要:http://poj.org/problem?id=10941A 这题下午想了好久 一直没调出来 由于是边输入边拓扑排序 入度改变之后下次再排会出错 晚上回来想到这一点 每次全复制一遍 只对入度进行操作 不进行改变这个题要 判断三种情况一种是有环在给出几种关系时出现矛盾二是在给出几种关系时 n个数已经排好了序三是输完还是无法排出一种序 这是属于拓扑排序不唯一的情况 也就是同时有多个结点出现的入度为0View Code 1 #include 2 #include 3 int f[100],g[50][50],kk[30],q[50]; 4 int topo(int n,int m) ... 阅读全文
posted @ 2012-07-24 19:19 _雨 阅读(316) 评论(4) 推荐(0) 编辑
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=2094其实这题也不算是拓扑排序了 只需要判断最开始入度是否唯一就可以了 如果是0 肯定有环了 不是0 会有多个冠军也不对View Code 1 #include 2 #include 3 #include 4 #include 5 using namespace std; 6 int de[1001]; 7 int main() 8 { 9 int i,j,k,g,n,fg;10 string s1,s2;11 while(cin>>n)12 {13 if(!... 阅读全文
posted @ 2012-07-24 16:59 _雨 阅读(198) 评论(0) 推荐(0) 编辑
摘要:http://poj.org/problem?id=2632好繁琐的一道模拟题 对着测试数据改了将近三个小时才过View Code 1 #include<stdio.h> 2 #include<string.h> 3 int main() 4 { 5 int i,j,t,n,m,a,b,c[101],f[101][101],x[101],y[101],xi,yi,cs1,cs2,crash; 6 char ci,c1; 7 scanf("%d",&t); 8 while(t--) 9 { 10 scanf("%d%d".. 阅读全文
posted @ 2012-07-24 15:31 _雨 阅读(207) 评论(0) 推荐(0) 编辑
摘要:第一次做 移动不给力啊 WLAN连了不下10次 第一题写完再那无奈的等了20分钟 WLAN艰难的以非常低的信号给连了几分钟 交完又掉了D题不难 就是数据类型用错了A. Dubsteptime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputVasya works as a DJ in the best Berland nightclub, and he often uses dubstep music in his performance. Rece 阅读全文
posted @ 2012-07-24 01:42 _雨 阅读(341) 评论(2) 推荐(0) 编辑
摘要:http://poj.org/problem?id=3278三次RE 以为是队列开小了 一直加大队列 忘记是标记数组的事了 改了判断条件 觉得队列不用开那么大 WA。。又开到100W AC..View Code 1 #include<stdio.h> 2 #include<string.h> 3 struct node 4 { 5 int x,num; 6 }q[1000001]; 7 int f[1000010]; 8 int p,d; 9 int main()10 {11 int i,j,n,k,flag = 0;12 scanf("%d%d", 阅读全文
posted @ 2012-07-23 20:52 _雨 阅读(163) 评论(0) 推荐(0) 编辑
摘要:http://poj.org/problem?id=1068View Code 1 #include<stdio.h> 2 #include<string.h> 3 int stack[20001],top,b[10001]; 4 void inst(int x,int y) 5 { 6 int i; 7 for(i = top+1 ; i <= top+(x-y) ; i++) 8 stack[i] = 1; 9 top = top+x-y+1;10 stack[i] = 0;11 b[x] = top;12 }13 int main()14... 阅读全文
posted @ 2012-07-23 20:29 _雨 阅读(165) 评论(0) 推荐(0) 编辑
摘要:http://www.cplusplus.com/reference/stl/http://net.pku.edu.cn/~yhf/UsingSTL.htm#include<set>View Code 1 #include<iostream> 2 #include<set> 3 using namespace std; 4 int main() 5 { 6 int i,j,n,a; 7 cin>> n; 8 set <int> h;//定义一个h 9 for(i = 1 ;i <= n ; i++)10 {11 cin > 阅读全文
posted @ 2012-07-23 09:36 _雨 阅读(254) 评论(0) 推荐(0) 编辑
摘要:http://baike.baidu.com/view/8150013.htmhttp://acm.hdu.edu.cn/showproblem.php?pid=2222今天看到这道题 以为是KMP 刚学完 想拿来练手 谁知写完超时。去discuss看了看 说是用AC自动机过的 今天也没什么安排 就去了解了下 它是建立在KMP和trie树基础上的一种高效串匹配的算法先将字符串建成一个字典树 标记每个字符串的尾部 建完之后 输入待匹配的字符串 这是只对这个字符串进行循环查找即可 判断每个字符是否是在字典树里出现 当循环到一个字符串的尾部时 num就会加上这个字符串的数量。看着别人的模板打了一晚上 阅读全文
posted @ 2012-07-22 12:08 _雨 阅读(195) 评论(0) 推荐(0) 编辑
摘要:http://dongxicheng.org/structure/trietree/http://hi.baidu.com/piaoshi111/item/ad5f7c12ca63f38889a95622http://poj.org/problem?id=3630刚开始套字典树 将字符串末尾标记 查询到末尾时标记 动态的 超时了 参考着别人的写了个静态的 过了 172MSView Code 1 #include 2 #include 3 #include 4 using namespace std; 5 struct node 6 { 7 int count; 8 int n... 阅读全文
posted @ 2012-07-22 11:25 _雨 阅读(254) 评论(0) 推荐(0) 编辑
摘要:就是判断有向图中是否有环 由于开始用dfs搜忘记return了 纠结了一下午 还是经虎学长指点 才找到错误递归View Code 1 #include<stdio.h> 2 #include<string.h> 3 int g[101][101],c[101]; 4 int dfs(int u,int n) 5 { 6 int v; 7 c[u] = -1; 8 for(v = 1 ; v <= n ; v++) 9 if(g[u][v])10 {11 if(c[v]<0)12 {13 ... 阅读全文
posted @ 2012-07-21 20:58 _雨 阅读(281) 评论(0) 推荐(0) 编辑
摘要:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2142View Code 1 #include<stdio.h> 2 #include<string.h> 3 int q[5001]; 4 int d; 5 void inque(int x) 6 { 7 d++; 8 q[d] = x; 9 }10 int main()11 {12 int t,n,m,i,j,k,f[101][101],fg[101],a,b;13 scanf("%d",& 阅读全文
posted @ 2012-07-21 20:28 _雨 阅读(268) 评论(0) 推荐(0) 编辑
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1181一次AC 没什么细节要注意 就是输入正确 加上dfs 可以 了View Code 1 #include<stdio.h> 2 #include<string.h> 3 typedef struct node 4 { 5 char c[101]; 6 int f; 7 }st; 8 st q[1001]; 9 int flag;10 void dfs(int x,int n)11 {12 int i,j;13 if(flag)14 return ;15 ... 阅读全文
posted @ 2012-07-21 19:29 _雨 阅读(237) 评论(0) 推荐(0) 编辑
摘要:堆排序 堆排序利用了大根堆(或小根堆)堆顶记录的关键字最大(或最小)这一特征,使得在当前无序区中选取最大(或最小)关键字的记录变得简单。 (1)用大根堆排序的基本思想 ① 先将初始文件R[1..n]建成一个大根堆,此堆为初始的无序区 ② 再将关键字最大的记录R[1](即堆顶)和无序区的最后一个记录R 阅读全文
posted @ 2012-07-21 09:37 _雨 阅读(320) 评论(0) 推荐(0) 编辑
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1180这题交了7次 悲剧了 前几次 因为把+行想成是左右走了 一直WA 后来看出来了 又把走过的楼梯给标记了 这里的楼梯不能标记因为可以在原地等着楼梯转变 加的时间有可能是2 这样普通队列满足不了最少的先搜 就用优先队列了View Code 1 #include<stdio.h> 2 #include<string.h> 3 typedef struct node 4 { 5 int x,y,num; 6 }st; 7 int f[22][22]; 8 char c[22][22]; 阅读全文
posted @ 2012-07-21 00:02 _雨 阅读(387) 评论(0) 推荐(0) 编辑
摘要:http://www.cppblog.com/shyli/archive/2007/04/06/21366.htmlstl中的优先队列的模板:#include priority_queue q;(队头为大,top为大)这是按照从大到小的队列;priority_queue, greater > q;这是从小到大的优先队列;#include struct cmp { bool operator ()(const int &i,const int &j) { return i>j; } }; http://acm.sdut.edu.cn/sdutoj/pro... 阅读全文
posted @ 2012-07-20 16:56 _雨 阅读(395) 评论(6) 推荐(0) 编辑
摘要:http://www.matrix67.com/blog/archives/115上面的文章虽然讲的很详细 不过还是没看太懂看 了下面的公式和讲解 思想明了简单KMP练习http://poj.org/problem?id=3461View Code 1 #include<stdio.h> 2 #include<string.h> 3 char w[10001],p[1000001]; 4 int next[10001]; 5 long num; 6 void fnext(char *c1) 7 { 8 int i,j,k1; 9 k1 = strlen(c1);10 . 阅读全文
posted @ 2012-07-20 13:17 _雨 阅读(322) 评论(0) 推荐(0) 编辑
摘要:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=341排版问题 错了n次 注意除出来的列有可能为0 不处理为1 会RE 还有如果超过n个字符串 要break 有可能那一列不会满View Code 1 #include<stdio.h> 2 #include<string.h> 3 #include<stdlib.h> 4 typedef struct node 5 { 6 char c[61]; 阅读全文
posted @ 2012-07-19 23:48 _雨 阅读(190) 评论(0) 推荐(0) 编辑
摘要:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2128有些忘记了View Code 1 #include<stdio.h> 2 #include<malloc.h> 3 typedef struct tree 4 { 5 struct tree *l,*r; 6 int data; 7 }tt; 8 int k; 9 void creat(tt *head,int a)10 {11 tt *p;12 p = (tt *)malloc(sizeof(tt));13 .. 阅读全文
posted @ 2012-07-19 20:39 _雨 阅读(205) 评论(0) 推荐(0) 编辑
摘要:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2144转自http://www.cnblogs.com/pony1993/archive/2012/07/17/2595237.html克鲁斯卡尔(Kruskal)算法(只与边相关)算法描述:克鲁斯卡尔算法需要对图的边进行访问,所以克鲁斯卡尔算法的时间复杂度只和边又关系,可以证明其时间复杂度为O(eloge)。算法过程:1.将图各边按照权值进行排序2.将图遍历一次,找出权值最小的边,(条件:此次找出的边不能和已加入最小生成树集合的边构成环),若 阅读全文
posted @ 2012-07-19 19:54 _雨 阅读(377) 评论(0) 推荐(0) 编辑
摘要:View Code 1 #include<stdio.h> 2 #include<string.h> 3 typedef struct que 4 { 5 int x,y; 6 long num; 7 }st; 8 st q[10001]; 9 char str[1001][1001]; 10 int f[1001][1001]; 11 int p,d; 12 void inque(int a,int b) 13 { 14 d++; 15 if(d>10000) 16 d = d%10000; 17 q[d].x = a; 1... 阅读全文
posted @ 2012-07-19 16:51 _雨 阅读(300) 评论(0) 推荐(0) 编辑
摘要:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=276实在没读懂题意 搜的意思 真佩服UVA的表达能力输入n,表示每个代码的长度为n;输入有多组数据,当n=0时表示结束;而在每组中,以最后输n个0结束。对于每组数据,第一行编码为出题者的(S1,S2,...,Sn),接下来的为猜测者的编码(g1,g2,g3,...gn)。若Si=gj并且i=j,你将得到个A;若Si=gj但i≠j,你将得到B。程序先从1找到n找能得到多少A,然后在找 阅读全文
posted @ 2012-07-19 11:11 _雨 阅读(320) 评论(0) 推荐(0) 编辑
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=2544稍微补充了下 求任意两点间的最短距离DijkstraView Code 1 #include 2 #include 3 #define INF 0x3f3f3f3f 4 int d[101],w[101][101],f[101]; 5 int Dijkstra(int st,int en,int n) 6 { 7 int i,j,min,k; 8 d[st] = 0; 9 memset(f,0,sizeof(f));10 for(i = 1 ;i =d[j])18 ... 阅读全文
posted @ 2012-07-18 20:07 _雨 阅读(281) 评论(0) 推荐(0) 编辑
摘要:从书上大体看了看思想 然后照着模板打 由于没考虑重边的问题WA一次Dijkstra算法View Code 1 #include<stdio.h> 2 #include<string.h> 3 int w[101][101]; 4 #define INF 0x3f3f3f3f 5 int main() 6 { 7 int i, j, k,n,m,d[5000],f[101],x,y,e; 8 while(scanf("%d%d", &n, &m)!=EOF) 9 {10 memset(f, 0, sizeof(f));11 memset 阅读全文
posted @ 2012-07-18 18:07 _雨 阅读(184) 评论(0) 推荐(0) 编辑
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1042集训第一天比赛上有这个题 以前做过一个求150的 效率比较低 用在这个上面就超时 然后在刘汝佳书上看到一种方法 勉强可以过 3400+msView Code 1 #include<stdio.h> 2 #include<string.h> 3 int x[40000]; 4 int main() 5 { 6 int i, n,g,s,d; 7 while(scanf("%d", &n)!=EOF) 8 { 9 int max = 40000;10 .. 阅读全文
posted @ 2012-07-18 16:05 _雨 阅读(187) 评论(0) 推荐(0) 编辑
摘要:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2404一直TLE 参考的qc的代码先将素数求出 然后把连续的素数和求出 看和是否是素数标记上View Code 1 #include<stdio.h> 2 int p[100001],o[100001],sum[100001],q[100001]; 3 int main() 4 { 5 int i,j, k = 0, m, n,g,f,x; 6 g = 10; 7 for(i = 2 ; i <= 100000 ; i++) 阅读全文
posted @ 2012-07-18 15:26 _雨 阅读(178) 评论(0) 推荐(0) 编辑
摘要:参考着cz的写的判重问题 :保存每一层的找过的节点 再在这一层找的时候 要保证与之前没有相同的View Code 1 #include<stdio.h> 2 #include<string.h> 3 long count,f[11],s,x[11],a[11]; 4 void dfs(long v) 5 { 6 long j,flag = 0,g,w,q[11]; 7 if(v>9) 8 { 9 if(x[1]+x[2]+x[3]==x[4]+x[5]+x[6]&&x[4]+x[5]+x[6]==x[7]+x[8]+x[9]&&x[ 阅读全文
posted @ 2012-07-18 10:49 _雨 阅读(296) 评论(0) 推荐(0) 编辑
摘要:以前貌似做过这个题 WA2次 一次因为复制输出的时候 把8复制上去了 还有理解错了 以为小于等于1的时候要去S呢View Code 1 #include<stdio.h> 2 #include<string.h> 3 int main() 4 { 5 int n, m, t, i, j, k,num,a[12][12],x,y,count1,count2; 6 char c[11][11]; 7 while(scanf("%d%d%d%*c", &n, &m, &t)&&n&&m&&am 阅读全文
posted @ 2012-07-15 21:29 _雨 阅读(185) 评论(0) 推荐(0) 编辑
摘要:题目描写的乱七八糟 就是找无论顺序大小写 不重复出现的字符串 按字典序输出http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=92代码乱七八糟的写的真长View Code 1 #include <stdio.h> 2 #include<string.h> 3 #include<stdlib.h> 4 typedef struct node 5 { 6 char c[25]; 7 int x; 8 } 阅读全文
posted @ 2012-07-13 23:27 _雨 阅读(337) 评论(0) 推荐(0) 编辑
摘要:View Code 1 #include <stdio.h> 2 #include<math.h> 3 int tr(int x,int y,int z) 4 { 5 if(x+y>z&&y+z>x&&x+z>y&&abs(x-y)<z&&abs(y-z)<x&&abs(x-z)<y) 6 return 1; 7 else 8 return 0; 9 }10 int tq(int x,int y,int z)11 {12 if(x == 0||y==0|| 阅读全文
posted @ 2012-07-13 14:10 _雨 阅读(536) 评论(0) 推荐(0) 编辑
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1242刚开始由于 没标记已走过的 导致RE TLE 纠结了好久 同样纠结为什么正着搜就WAView Code 1 #include <stdio.h> 2 #include<string.h> 3 int n, m,p = 0,q = 0; 4 char c[201][201]; 5 typedef struct queue 6 { 7 int num, x, y; 8 }st; 9 st Q[100001];10 void inque(int a, int b)11 {12 q++; 阅读全文
posted @ 2012-07-12 23:38 _雨 阅读(176) 评论(0) 推荐(0) 编辑
摘要:看了看题 没什么想法 参考着解题报告写的状态方程 dp[i][j] = max{dp[i+1][j],dp[i+1][j+1],dp[i+1][j-1]}View Code 1 #include <stdio.h> 2 #include<string.h> 3 int dp[100001][12]; 4 int max1(int x, int y, int z) 5 { 6 int m = x; 7 if(m<y) 8 m = y; 9 if(m<z)10 m = z;11 return m;12 }13 int main()14 {15 ... 阅读全文
posted @ 2012-07-12 21:32 _雨 阅读(223) 评论(0) 推荐(0) 编辑
摘要:http://poj.org/problem?id=1321由于忘记取消起点那里的标记 WA了一次 有点类似皇后View Code 1 #include <stdio.h> 2 #include<string.h> 3 int n, m,count,x[11],y[11]; 4 char c[10][10]; 5 void dfs(int i, int j, int v) 6 { 7 int p,q; 8 if(v == m) 9 count++;10 else11 {12 for(p = i+1 ; p <= n ; p++)13 ... 阅读全文
posted @ 2012-07-12 16:59 _雨 阅读(136) 评论(0) 推荐(0) 编辑
摘要:这题主要是重复问题怎么删除 由于是排好序的 有重复的 肯定是相邻的在同一层上的结点值 保证不相同就行 不同层可以相同 比如 4 = 2+1+1 可以有两个1具体看代码View Code 1 #include <stdio.h> 2 #include<string.h> 3 int a[1001],n,s,v,y,flag,num[1001],ch; 4 void dfs(int x, int i,int v) 5 { 6 int j,k; 7 y+= x; 8 num[v] = x; 9 if(y == s)10 {11 for... 阅读全文
posted @ 2012-07-12 15:37 _雨 阅读(182) 评论(0) 推荐(0) 编辑
摘要:转自http://hi.baidu.com/zhangwp999/blog/item/185f9afba454ba63024f5675.html*六类qsort排序方法P.S.:qsort函数是ANSI C标准中提供的,其声明在stdlib.h文件中,是根据二分发写的,其时间复杂度为n*log(n),其结构为:void qsort(void *base,size_t nelem,size_t width,int (*Comp)(const void *,const void *));其中:*base 为要排序的数组nelem 为要排序的数组的长度width 为数组元素的大小(一字结为单位)(* 阅读全文
posted @ 2012-07-11 23:01 _雨 阅读(339) 评论(0) 推荐(0) 编辑
摘要:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1415水题 简单排序查找 快排+哈希View Code 1 #include <stdio.h> 2 #include<string.h> 3 int cmp(const void*a,const void *b) 4 { 5 return *(int *)a-*(int *)b; 6 } 7 int main() 8 { 9 int i,j,n, m, a 阅读全文
posted @ 2012-07-11 22:20 _雨 阅读(188) 评论(0) 推荐(0) 编辑
摘要:http://poj.org/problem?id=2488小错误不断 数组开的a[8][2] 我居然循环到8 还一直纠结哪错了改完后 交了一次WA 看了看讨论里面 要根据字典序 所以移动坐标差a[8][2]只能那么定义 具体看代码View Code 1 #include <stdio.h> 2 #include <string.h> 3 int n,m,x[27][27],q[27],flag; 4 char w[27]; 5 int u[8][2]={{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}}; 阅读全文
posted @ 2012-07-11 21:28 _雨 阅读(160) 评论(0) 推荐(0) 编辑
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1016这几天做题第一次一次性AC 自信心上涨简单dfs一个大圈 中有n个小圈 填进去数 相邻圈中数的和为素数View Code 1 #include <stdio.h> 2 #include<string.h> 3 int x[21][21],v,f,y[21],c[21],n; 4 int prime(int x) 5 { 6 int i,flag = 1; 7 for(i = 2; i < x ; i++) 8 if(x%i == 0) 9 {10 ... 阅读全文
posted @ 2012-07-11 19:45 _雨 阅读(224) 评论(0) 推荐(0) 编辑
摘要:http://poj.org/problem?id=2258dfs 边是双向的 节点可以走多次 开个二维数组标记边 因为可以从任一节点出发所以n次dfsView Code 1 #include <stdio.h> 2 #include<string.h> 3 int x[26][26],v,w,n; 4 void dfs(int i,int v) 5 { 6 int j; 7 for(j = 0 ; j <= n-1 ; j++) 8 { 9 if(x[i][j] == 1)10 {11 x[i][j] = 0;... 阅读全文
posted @ 2012-07-11 19:03 _雨 阅读(258) 评论(0) 推荐(0) 编辑
摘要:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2139BFS 借助队列 将节点的邻接点依次存入队中View Code 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include<string.h> 4 int k[1001][1001],f[1001],n,m,p = 0,w; 5 typedef struct node 6 { 7 int num,y; 8 }st; 9 st q[1001];10 void 阅读全文
posted @ 2012-07-11 16:15 _雨 阅读(468) 评论(0) 推荐(0) 编辑
摘要:刚开始把路径标记给取消了就一直TLEView Code 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include<string.h> 4 int k[1001][1001],f[1001],n,m,a,b,flag,x; 5 void dfs(int x) 6 { 7 int j; 8 f[x] = 1; 9 if(flag == 1)10 return ;11 for(j = n ;j>=1 ; j-- )12 if(k[x][j] == 1&&f[j] == 0)13 ... 阅读全文
posted @ 2012-07-11 11:24 _雨 阅读(235) 评论(0) 推荐(0) 编辑
摘要:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1882搜索 递归回溯View Code 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include<math.h> 4 int q[21][21],flag,a[21],f = 0; 5 void dfs(int i, int n) 6 { 7 int j,k,p,l,m; 8 if(f == 1) 9 return ;10 if(i>n)11 {12 . 阅读全文
posted @ 2012-07-11 10:20 _雨 阅读(163) 评论(0) 推荐(0) 编辑
摘要:原文地址http://princetonboy.ycool.com/post.2805302.html【摘要】本文讨论了搜索算法中“剪枝”这一常见的优化技巧.首先由回溯法解决迷宫问题展开论述,介绍了什么是剪枝;而后分析剪枝的三个原则正确、准确、高效,并分别就剪枝的两种思路:可行性剪枝及最优性剪枝,结合例题作进一步的阐述;最后对剪枝优化方法进行了一些总结.【关键字】搜索、优化、剪枝、时间复杂度引论在竞赛中,我们有时会碰到一些题目,它们既不能通过建立数学模型解决,又没有现成算法可以套用,或者非遍历所有状况才可以得出正确结果.这时,我们就必须采用搜索算法来解决问题.搜索算法按搜索的方式分有两类,一类 阅读全文
posted @ 2012-07-10 11:58 _雨 阅读(494) 评论(0) 推荐(0) 编辑
摘要:原本因为简单的dfs就可以 写完却发现不知道如何保留一条路的路径 不能保证一条路中的节点只被走一次 导致死循环还有各种剪枝代码算是跟别人一个模子刻出来的 递归 一直走那一条路 走过就标记上 回溯的时候再取消标记View Code 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include<string.h> 4 #include<math.h> 5 int k,d,num,flag,n,m,t,a[6]; 6 char c[10][10]; 7 void dfs(int x, int y) 8 { 阅读全文
posted @ 2012-07-09 01:52 _雨 阅读(467) 评论(0) 推荐(0) 编辑
摘要:简单并查集 有联通的就合并在一起 最后看共有多少棵树 就有多少个灾民集中区域View Code 1 #include <stdio.h> 2 #include <stdlib.h> 3 int father[201],num[201]; 4 int find(int x) 5 { 6 if(x!=father[x]) 7 { 8 father[x] = find(father[x]); 9 }10 return father[x];11 }12 void union1(int x, int y)13 {14 father[x] = y;... 阅读全文
posted @ 2012-07-08 22:07 _雨 阅读(170) 评论(0) 推荐(0) 编辑
摘要:http://poj.org/problem?id=2492跟1703一样 两种状态 并查集+偏移量两只虫子发射架关系 判断有没有虫子是同性恋 两两合并 在一棵树上找右没有两个节点跟跟节点的关系(只有0,1两种)是相同的,相同就是gay注意一点 两组数据间有空行View Code 1 #include <stdio.h> 2 #include <stdlib.h> 3 int father[2001],num[2001],r[2001]; 4 int find(int x) 5 { 6 if(x!=father[x]) 7 { 8 int y = father... 阅读全文
posted @ 2012-07-08 21:45 _雨 阅读(398) 评论(2) 推荐(0) 编辑
摘要:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=98&page=show_problem&problem=1361题不难 我交了八次。。 刚开始以为还要判断一个国家会不会重复妇女的名字 写的复杂了 一直runtime 看了下别人的代码 知道不用判断 删去了一大部分 之后开始WA 最后找到原因 是因为除国家外 因为不确定名字的字符串数 名字用gets输入开两个数组 一个存国家 一个存数量 有输入重复的国家 就剪掉View Code 1 #include &l 阅读全文
posted @ 2012-07-08 17:02 _雨 阅读(181) 评论(0) 推荐(0) 编辑
摘要:纠结了好几个小时 不明白哪错了 跟别人的代码仔仔细细的对照才发现题目要求把原来的数据再输出一遍 石化。。选择排序的思想 每次找到最大的那个数将它放在该放的位置输入用字符串 再转化成数字UVA就是麻烦 来来回回的逆转 都转晕了View Code 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include<string.h> 4 int n ; 5 void deal(char *s,int *a) 6 { 7 int begin,end,len=strlen(s); 8 n=0; 9 begin=0;10 end 阅读全文
posted @ 2012-07-08 15:14 _雨 阅读(197) 评论(0) 推荐(0) 编辑
摘要:导弹拦截系统 随时更新dp[i]的值 满足第i个导弹的高度比第j个高的而且dp[i]<dp[j]+1 就更新dp[i]的值View Code 1 #include <stdio.h> 2 #include <stdlib.h> 3 int main() 4 { 5 int n, i, j, dp[10001], h[10001], min; 6 while(scanf("%d", &n)!=EOF) 7 { 8 for(i = 1 ; i <= n ; i++) 9 {10 scanf("%d" , & 阅读全文
posted @ 2012-07-07 23:00 _雨 阅读(180) 评论(0) 推荐(0) 编辑
摘要:简单并查集 这题WA了20几次 相当无语 写了半小时 找错找了2小时 就因为输入用的字符而不是字符串 真无语 做poj要相当细心啊相当小心啊还有一点注意一下 N是1001 刚开始我直接开的1001将已经修好的距离可达到的电脑合并为一个集合 比较两个节点的根节点是否相同就是View Code 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include<math.h> 4 int father[1010], deep[1010]; 5 int find(int u) 6 { 7 if(u!=father[u]) 8 阅读全文
posted @ 2012-07-07 01:52 _雨 阅读(183) 评论(0) 推荐(0) 编辑
摘要:这题实在是郁闷 检查了几个小时的错误 十几次runtime 居然是没定义成全局变量m[][]存当前最大的公共子序列m[i][j] = m[i-1][j-1] +1 (x[i] = y[i])elsem[i][j] = max(m[i-1][j],m[i][j-1]);View Code 1 #include<stdio.h> 2 #include<string.h> 3 int m[1001][1001]; 4 char c1[1001], c2[1001]; 5 int main() 6 { 7 int i,j, k1, k2; 8 while(scanf(" 阅读全文
posted @ 2012-07-06 21:09 _雨 阅读(163) 评论(0) 推荐(0) 编辑
摘要:并查集 r[]记录节点到跟节点的距离 num[] 树的节点数View Code 1 #include <stdio.h> 2 int father[30005],r[30005],num[30001]; 3 int find(int x) 4 { 5 if(x!=father[x]) 6 { 7 int t = father[x]; 8 father[x] = find(father[x]); 9 r[x]+=r[t];10 }11 return father[x];12 }13 14 void union1(i... 阅读全文
posted @ 2012-07-06 14:30 _雨 阅读(153) 评论(0) 推荐(0) 编辑
摘要:与1182一个思路 并查集加偏移量 不过比1182好做一些View Code 1 #include 2 int father[100005], r[100005]; 3 void init(int n) 4 { 5 int i; 6 for(i = 1 ; i <= n ; i++) 7 { 8 r[i] = 0; 9 father[i] = i;10 }11 }12 int find(int x)13 {14 if(x!=father[x])15 {16 int pre = father[x]... 阅读全文
posted @ 2012-07-05 22:32 _雨 阅读(205) 评论(0) 推荐(0) 编辑
摘要:把两个节点的关系转换为对根节点的关系的比较View Code 1 #include <stdio.h> 2 int father[50001] ,r[50001]; 3 void init(int n) 4 { 5 int i; 6 for(i = 1 ; i <= n ; i++) 7 { 8 father[i] = i; 9 r[i] = 0;10 }11 }12 int find(int x)13 {14 if(x == father[x])15 return father[x];16 else17... 阅读全文
posted @ 2012-07-05 20:51 _雨 阅读(197) 评论(0) 推荐(0) 编辑
摘要:http://poj.org/problem?id=1611题意是找出可能携带病毒的人数 就是把有交集的集合合并在一起 答案就是最后0所在集合的人数并查集的一个主要思想就是合并两个不相交集合 在这个题中 也就是不含重复元素 比如 第一个集合中的1,2 就是把1, 2合并起来作为一个集合 同时把num【】更新一下 就是集合中的元素个数 把每个集合中的第一个数作为根节点 这样集合里的所有元素的祖先就是第一个数x如果两个集合没有交集 比如1,2和10 11 12 13 14 15 这两个集合就不会合并 因为只是集合中的元素合并 1和2合并 10和其它的4个数合并 但是要是有交集1,2和0,1 这两个 阅读全文
posted @ 2012-07-05 14:59 _雨 阅读(275) 评论(3) 推荐(0) 编辑
摘要:作者:Dong | 可以转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明网址:http://dongxicheng.org/structure/union-find-set/1、 概述并查集(Disjoint set或者Union-find set)是一种树型的数据结构,常用于处理一些不相交集合(Disjoint Sets)的合并及查询问题。2、 基本操作并查集是一种非常简单的数据结构,它主要涉及两个基本操作,分别为:A. 合并两个不相交集合B. 判断两个元素是否属于同一个集合(1) 合并两个不相交集合(Union(x,y))合并操作很简单:先设置一个数组Father[x],表示 阅读全文
posted @ 2012-07-04 21:53 _雨 阅读(217) 评论(0) 推荐(0) 编辑
摘要:http://poj.org/problem?id=1308题目要求是判断是不是为一棵树主要看是否满足2个条件共有n个不同的数 也就是n个节点1 有n-1个入度 去除根节点 一个节点只有一个入度2 不能有环 就是单方向的其实只要判断条件1就可以 了 只要是环肯定不会为n-1个入度 所以只判断第一个条件就好代码如下 用a[]判断节点的入度是否唯一 用K来计算总入度是否为n-1用b[]来判断n的值为几这个题还要注意一点 0 0是空树 也是树View Code 1 #include <stdio.h> 2 #include<string.h> 3 int main() 4 { 阅读全文
posted @ 2012-07-04 21:51 _雨 阅读(268) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示