ACM
文章平均质量分 61
LB_莫贺延碛
没智商,没毅力,仅仅有一点点运气。
努力向各位前辈大牛学习。
展开
-
一道算法笔试题
问题描述 给定参数n(n为正整数),请计算n的阶乘n!末尾所含有“0”的个数。 例如,5!=120,其末尾所含有的“0”的个数为1;10!= 3628800,其末尾所含有的“0”的个数为2;20!= 2432902008176640000,其末尾所含有的“0”的个数为4。 计算公式 这里先给出其计算公式,后面给出推导过程。 令f(x)表示转载 2014-09-04 23:53:37 · 290 阅读 · 0 评论 -
求序列逆序数
我就是随笔记一下。首先,逆序数的定义我们在线性代数的课程中应该学到过:在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序。一个排列中逆序的总数就称为这个排列的逆序数。逆序数为偶数的排列称为偶排列;逆序数为奇数的排列称为奇排列。如2431中,21,43,41,31是逆序,逆序数是4,为偶排列。对于求一个序列的逆序数,O(n^2)的方法是很原创 2016-08-08 13:09:21 · 3235 阅读 · 0 评论 -
float 和 double类型范围与精度
转自:http://blog.csdn.net/dxy612/article/details/55184771 范围float和double的范围是由指数的位数来决定的。float的指数位有8位,而double的指数位有11位,分布如下:float:1bit(符号位)8bits(指数位)23bits(尾数位)dou转载 2016-08-08 15:15:13 · 384 阅读 · 0 评论 -
素数的一般筛法与线性筛法
素数的打表是一个经常涉及到的内容,原创 2014-10-10 14:52:44 · 620 阅读 · 2 评论 -
poj 1988 并查集
#include #include const int maxn = 100000 + 100;int set[maxn];//记录每个元素的栈低(代表元素)元素int top[maxn];//记录每个元素的栈顶元素int cnt[maxn];//j记录每个元素到栈低的元素个数int set_find(int p)//查找元素p的栈底元素,并进行路径压缩,并更新 p原创 2014-07-24 22:47:38 · 536 阅读 · 0 评论 -
poj2472
#include #include using namespace std;const int maxn = 100 + 10;double map[maxn][maxn];int n,m;void init(){// memset(map,-1,sizeof(map)); for(int i = 0;i maxn;i++) for(int j = 0;j maxn;原创 2014-08-04 22:55:26 · 683 阅读 · 0 评论 -
poj 1061 青蛙的约会 (扩展gcd, 模线性方程)
好长时间没贴代码了,这段时间原创 2014-09-17 14:40:11 · 407 阅读 · 0 评论 -
poj 1811 rabinMiller素数测试
最近稍微忙些,没有太多时间去做题,在bestcoder上水了几次,说实话,我写博文的态度很不认真,每次都是草草说几句后帖代码,自己也没太多收获。这段时间反思了自己的行为和态度,明白自己虽然弱,但仍要对自己负责,所以我希望自己以后能有所改善。下面说题目,题意说的很清楚,给一个大数,判断是否是素数,如果不是素数,打印出它的最小质因数。这道题包含了rabinmiller素数测试和pollard算原创 2014-10-09 19:10:12 · 837 阅读 · 0 评论 -
uva 657
哎,本来以为是一道简单题,但是WA了5,6次。。。这道题说给一个骰子的图,其中‘.’表示背景,‘*’表示是骰子的一个面,‘X’表示骰子面中的点。其中面和骰子的点都是4连接的,也就是说考虑上下左右四个方向。思路应该比较清晰:首先对骰子的面进行搜索,碰到面中的‘X’再对‘X’进行搜索。注意:对‘X’进行搜索时,需要注意‘X’不仅和相邻的‘X’是连接的,‘X’和相邻‘*’也是连接的(属于骰子的同一个面)原创 2016-10-25 10:35:43 · 265 阅读 · 0 评论 -
uva 327
表达式求值,这道题的检测点很多, 给大家贴几个:a + bb - za+b--+c++c+f--+--af-- + c-- + d-++e++x++y--x--yx++-yx+--yx--+yx++-++yx++---yx--+++yx--+--yx+++--yx---++yz+d+c++-++b-a++b-++c+-原创 2016-09-30 17:05:41 · 251 阅读 · 0 评论 -
stringstream 使用误区
stringstream是个好东西,网上有不少文章,讨论如何用它实现各种数据类型的转换(比如把double或int转换为string类型)。但如果stringstream使用不当,当心内存出问题(我就吃过亏^_^)。试试下面的代码,运行程序前打开任务管理器,过不了几十秒,所有的内存都将被耗尽!#include #include #include using namespace s转载 2016-08-01 21:58:15 · 4814 阅读 · 0 评论 -
uva 10815
这道题可以map存储字符串,当然使用set更好,因为可以在加入集合时直接进行排序(当然,这道题比较简单,如果是自定义的数据可以重载运算符)。如果使用map就需要另外使用qsort对字符串进行排序。#include #include #include #include #include #include #include using namespace std;const in原创 2016-07-29 08:35:31 · 321 阅读 · 0 评论 -
趣写算法系列之--匈牙利算法
原作在这里:http://blog.csdn.net/dark_scope/article/details/8880547【书本上的算法往往讲得非常复杂,我和我的朋友计划用一些简单通俗的例子来描述算法的流程】匈牙利算法是由匈牙利数学家Edmonds于1965年提出,因而得名。匈牙利算法是基于Hall定理中充分性证明的思想,它是部图匹配最常见的算法,该算法的核心就是寻找增广路径,转载 2016-06-01 11:17:51 · 329 阅读 · 0 评论 -
KM算法详解+模板
晕了, 我2014年时打过hdu2255(KM算法), 竟然没想起来。。。原作在这, 代码我的和原作的各贴一份好了KM算法用来求二分图最大权完美匹配。本文配合该博文服用更佳:趣写算法系列之--匈牙利算法 现在有N男N女,男生和女生每两个人之间有好感度,我们希望把他们两两配对,并且最后希望好感度和最大。怎么选择最优的配对方法呢?首先,每个妹子会有一个期望转载 2016-06-01 12:06:27 · 4885 阅读 · 7 评论 -
uva 401
这几天老板有事出去开会了, 所以有了点时间能够看看别的内容。算法的东西好长时间没打了, 都是在看有关开发和数理统计的内容。于是打算开个新坑。大部分题目是入门经典中的uva 401 这道题比较简单, 在回文字符串的概念上增加了一个镜像对称的概念。code:#include #include #include #include #include using namespa原创 2016-07-23 22:54:42 · 326 阅读 · 0 评论 -
uva 537
一道关于字符串的简单题,主要就是从字符串中找到两个等号的位置后,进行分析便可。另外注意前缀所代表的数值大小(k, M, m)#include #include #include #include #include using namespace std;int kasenum;string str;double power, volt, curr;int pos[3]原创 2016-07-23 23:00:15 · 274 阅读 · 0 评论 -
uva 10010
关于搜索的一道简单题。从一个字符块中搜出指定的单词,单词在字符块中的位置可以是水平,垂直,或者对角线共8个方向中的任意一个。注意:单词的所在的位置一定是一条直线。刚开始我把这道题当做dfs做,结果wa了好多次。#include #include #include #include #include using namespace std;const int maxn = 5原创 2016-07-23 23:05:24 · 359 阅读 · 0 评论 -
uva 10361
一道水水的题。注意的地方就是对字符串顺序处理时每个s串进行正确的分配,注意好边界问题就可以了。另外就是在进行测试用例之前注意好变量的初始化。code:#include #include #include #include #include using namespace std;int n;char ch;string s[6], strone, strtwo, sec原创 2016-07-23 23:10:14 · 306 阅读 · 0 评论 -
uva 409 poj 1598
uva 409和poj 1598是同一道题。这道题也算是基础题了,主要有两点需要注意1. A keyword "occurs" in an excuse if and only if it exists in the string in contiguous form and is delimited by the beginning or end of the line or any non原创 2016-07-28 23:12:06 · 720 阅读 · 0 评论 -
uva 10878
呵呵,比较容易想到,只要找几个相应的字符对比一下就能发现规律了。不难#include #include #include #include #include using namespace std;const string endline = "___________";string str;bool flag = false;int first, second, res原创 2016-07-28 23:17:31 · 310 阅读 · 0 评论 -
sort qsort 用法总结
一篇很好的对于sort、qsort函数用法的总结。原博在这里:http://blog.sina.com.cn/s/blog_6439f26f01012xw3.htmlsort函数的用法做ACM题的时候,排序是一种经常要用到的操作。如果每次都自己写个冒泡之类的O(n^2)排序,不但程序容易超时,而且浪费宝贵的比赛时间,还很有可能写错。STL里面有个sort函数,可以直接对数组排序,复杂度转载 2016-07-28 23:23:35 · 335 阅读 · 0 评论 -
uva 839
这道题直接通过递归计算,思路比较直观,如果树的节点是父节点,计算左右两个孩子节点的权重乘以重量进行判断就可以了。唯一注意的是需要用两个if 判断wl 和wr#include #include #include #include #include using namespace std;bool flag = true;int buildTree(){ i原创 2016-09-30 17:10:00 · 301 阅读 · 0 评论 -
uva 699
#include#include#include #include using namespace std;const int maxn = 200;int sum[maxn];// 输入并统计一棵子树,树根水平位置为pvoid build(int p){ int v; cin >> v; if(v == -1) return; // 空树 s原创 2016-09-30 17:11:56 · 220 阅读 · 0 评论 -
uva 297
#include #include #include #include #include #include using namespace std;typedef struct Tnode{ int color;//0 for white, 1 for black, -1 for parent. -2 for not sure Tnode* sons[4];}Node;原创 2016-09-30 19:30:54 · 354 阅读 · 0 评论 -
uva 548
由后续遍历和中序遍历推理出树的结构。使用递归方法做的,有点麻烦,,。注意好在递归过程中参数的设置#include #include #include #include #include #include using namespace std;const int maxn = 10000 + 1000;int inorder[maxn], postorder[maxn],原创 2016-09-30 19:34:00 · 286 阅读 · 0 评论 -
uva 712
#include #include #include #include #include #include #include using namespace std;const int maxn = 1024 + 512;int depth, kasenum, testnum = 0;char str[maxn], ch, ans[maxn]; int values[max原创 2016-09-30 19:37:12 · 402 阅读 · 0 评论 -
单调队列 poj2823
这段话转自:http://www.cnblogs.com/szy-wlxy/p/4622662.html 初谈这个话题,相信许多人会有一种似有所悟,但又不敢确定的感觉。没错,这正是因为其中“单调”一词的存在,所谓单调是什么,学过函数的people都知道单调函数或者函数的单调性,直白一点说单调就是一直增或一直减。例如:1,3,5,9就是一个单调增数列,数列中不存在后一个数比前一个数原创 2016-10-31 17:37:06 · 327 阅读 · 0 评论 -
莫比乌斯反演
转自:http://blog.csdn.net/acdreamers/article/details/8542292莫比乌斯反演在数论中占有重要的地位,许多情况下能大大简化运算。那么我们先来认识莫比乌斯反演公式。 定理:和是定义在非负整数集合上的两个函数,并且满足条件,那么我们得到结论 在上面的公式中有一个函数,它的定义如下: (1)若,转载 2016-10-31 22:00:14 · 315 阅读 · 0 评论 -
组合数学之卡特兰数
卡特兰数:1 通项公式:h(n)=C(n,2n)/(n+1)=(2n)!/((n!)*(n+1)!)2递推公式:h(n)=((4*n-2)/(n+1))*h(n-1); h(n)=h(0)*h(n-1)+h(1)*h(n-2)+...+h(n-1)*h(0).3前几项为:h(0)=1,h(1)=1,h(2)=2,h(3)=5,h(4)=14,h(5)=42,......4应用场转载 2015-03-13 19:00:18 · 9309 阅读 · 0 评论 -
poj1330
//当时在写这道题的时候#include #include #include using namespace std;const int N = 10000;vector a[N];int f[N],r[N];void DFS(int u,int dep){ r[u] = dep; for(vector::iterator i原创 2014-07-25 22:41:38 · 534 阅读 · 0 评论 -
uva 112
#include #include #include #include #include using namespace std;const int maxn = 50000 + 100;int parent[maxn], fnode = -1;int values[maxn];bool isleaf[maxn];int num, root, target, nodeCnt原创 2016-09-30 19:29:31 · 319 阅读 · 0 评论 -
uva 11234
#include #include #include #include #include #include #include #include using namespace std;const int maxn = 10000 + 100;char expre[maxn];vector sons[maxn];int parent[maxn];stack st;qu原创 2016-09-30 19:27:38 · 237 阅读 · 0 评论 -
uva 101
#include #include #include using namespace std;const int maxn = 25 + 10;int place[maxn];int stack[maxn][maxn];int top[maxn];int number;char oper[maxn], prep[maxn];int a, b;void init_pla原创 2016-09-30 17:22:23 · 358 阅读 · 0 评论 -
uva 127
#include #include #include #include #include using namespace std;const int maxn = 60;stack pokerPile[maxn];string str;bool matches(string s1, string s2){ if (s1[0] == s2[0] || s1[1] == s2原创 2016-09-30 17:25:45 · 320 阅读 · 0 评论 -
uva 133
#include #include #include using namespace std;const int maxn = 20 + 20;int numbers[maxn], leftp[maxn], rightp[maxn];int N, k, m;void link(int x, int y){ rightp[x] = y; leftp[y] = x;}i原创 2016-09-30 17:28:00 · 280 阅读 · 0 评论 -
uva 442
#include #include #include #include #include using namespace std;const int maxn = 100 + 50;struct Matrix{ int rows; int cols;};Matrix mat[26];int n, rows, cols;char ch;char str[maxn];原创 2016-09-30 17:29:11 · 249 阅读 · 0 评论 -
uva 540
这道题是早期做的,有点麻烦。#include #include using namespace std;const int maxp = 1000000; //元素序号const int maxt = 1000; //队列数目const int maxn = 200000 + 10; //元素规模struct node{ int p; //元素序号原创 2016-09-30 17:31:09 · 314 阅读 · 0 评论 -
uva 673
经典的判断括号匹配问题。#include #include #include #include using namespace std;stack ch_stack;string str;int main(){ freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); int kasenum; c原创 2016-09-30 19:13:32 · 281 阅读 · 0 评论 -
uva 10050
#include #include #include using namespace std;const int maxn = 4000 + 10;bool days[maxn];int hparm[maxn], simdays;int ans = 0;int main(){ freopen("in.txt", "r", stdin); freopen("out.txt"原创 2016-09-30 19:14:52 · 332 阅读 · 0 评论 -
uva 10152
#include #include #include #include using namespace std;const int maxn = 200 + 100;char ori_names[maxn][150];char res_names[maxn][150];int main(){ freopen("in.txt", "r", stdin); freopen("原创 2016-09-30 19:16:38 · 226 阅读 · 0 评论