
算法详解&模板
文章平均质量分 82
synapse7
这个作者很懒,什么都没留下…
展开
-
Manacher 算法详解:O(n) 复杂度求最长回文子串
先预处理下:在每个字符的两边都插入一个特殊的符号,比如abba变成#a#b#b#a#,aba变成#a#b#a#(因为Manacher算法只能处理奇数长度的字符串)。同时,为了避免数组越界,在字符串开头添加另一特殊符号,比如$#a#b#a#。以字符串3212343219为例,处理后变成S[] = "$#3#2#1#2#3#4#3#2#1#9#"。然后用一个数组Len[i]来记录以字符S[i原创 2014-02-03 12:27:09 · 4408 阅读 · 2 评论 -
使用STL输出组合序列 + UVa 441 Lotto
#includeusing namespace std;int a[140];bool ok[140];/*输出从n个数中选m个数的组合字典序以n=7,m=3为例首先构造ok=1110000然后ok的前一个排列为1101000再前一个排列为1100100...1100001然后是1011000...直到0000111*/void printfC(int n, int m原创 2014-02-13 00:05:39 · 1396 阅读 · 0 评论 -
HDU 2602 Bone Collector (经典0-1背包)
http://acm.hdu.edu.cn/showproblem.php?pid=2602/*31ms,240KB*/#include#include#includeusing namespace std;const int mx = 1005;int w[mx], v[mx], dp[mx];void _01pack(int n, int maxw){ me原创 2014-02-20 05:38:11 · 1073 阅读 · 0 评论 -
并查集(union-find)模板
#includeusing namespace std;const int mx = 100005;int fa[mx], rk[mx];int find(int x) {return fa[x] = (fa[x] == x ? x : find(fa[x]));}void union(int x, int y){ x = find(x), y = find(y); if (原创 2014-01-13 08:43:49 · 1173 阅读 · 0 评论 -
线段树模板(二)——成段更新 + POJ 3468 A Simple Problem with Integers
http://poj.org/problem?id=3468/*2391ms,4508KB*/#include #include using namespace std;#define lson l , m , rt << 1#define rson m + 1 , r , rt << 1 | 1#define root 1, N, 1#define LL long l原创 2014-02-19 11:22:02 · 1035 阅读 · 0 评论 -
UVa 1401 Remember the Word (Trie树模板题)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=505&page=show_problem&problem=4147递推写法:L = strlen(text);for (i = L - 1; i >= 0; --i){ trie.find_prefixes(text +原创 2014-03-02 09:31:51 · 1083 阅读 · 0 评论 -
中国剩余定理算法详解 + POJ 1006 Biorhythms 生理周期
此题相当于解方程组x+d≡p(mod 23)x+d≡e(mod 28)x+d≡i(mod 33)由于23,28,33两两互素,所以M=23*28*33=21252,M1=924,M2=759,M3=644.则有924y1≡1(mod 23),由924/23=(40,5,1,3)计算得y1=6(mod 23)则有759y2≡1(mod 28),由759/28=(27,9,2,1)计算得y2=19(mod 28)则有644y3≡1(mod 33),由644/33=(19,1,1,16)计算得y3=原创 2013-08-13 13:07:19 · 8211 阅读 · 0 评论 -
POJ 1276 Cash Machine (多重背包&单调队列)
http://poj.org/problem?id=1276模板题。完整代码:/*110ms,4316KB*/#include#includeconst int mx = 1005;const int mxw = 1000005;int w[mx], m[mx], dp[mxw], deq[mxw], deqv[mxw];int solve(int m原创 2014-02-27 14:48:50 · 1771 阅读 · 0 评论 -
最短路模板:使用priority_queue实现的Dijkstra算法
#include#include#include#include#include#includeusing namespace std;const int mx = 10005;typedef pair P; ///first是最短距离,second是顶点编号struct edge{ int to, cost;};vector G[mx];int disTo[mx]原创 2014-02-15 01:12:26 · 4876 阅读 · 0 评论 -
UVa 10000 Longest Paths (单源最长路 - floyd or 拓扑排序)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=941由于n很小,floyd算法写起来方便,先用这个A了一下:/*0.162s*/#includeusing namespace std;const int mx = 105;i原创 2014-02-16 14:22:23 · 2073 阅读 · 0 评论 -
最短路模板(二)——用拓扑排序解决有向无环图中的最短路
测试数据:8 135 4 0.354 7 0.375 7 0.285 1 0.324 0 0.380 2 0.263 7 0.391 3 0.297 2 0.346 2 0.403 6 0.526 0 0.586 4 0.93测试结果:5 to 0 : 0.735 to 1 : 0.325 to 2 : 0.625 t原创 2014-02-16 13:50:11 · 2421 阅读 · 0 评论 -
HDU 2066 一个人的旅行(最短路&Dijkstra)
一个人的旅行http://acm.hdu.edu.cn/showproblem.php?pid=2066Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Problem Description虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里原创 2013-08-08 23:12:44 · 1096 阅读 · 0 评论 -
欧拉函数表的O(NloglogN)和O(N)预处理
代码思想来自埃拉托斯特尼筛法,参考:http://blog.youkuaiyun.com/synapse7/article/details/18727405#include const int mx = 50001;int prime[mx], phi[mx];bool unprime[mx];///O(NloglogN),推荐void phi_table(){ int i, j原创 2014-02-23 11:23:34 · 2174 阅读 · 0 评论 -
整数划分小记
一、简单介绍参加wiki页面:http://en.wikipedia.org/wiki/Partition_(number_theory)二、基本形式例题:POJ 3014 Cake Pieces and Plates题意:(m的n划分的总数)给你m个东西,放在n个相同的盒子中,且每个盒子可以放任意多,问有多少种放法。思路:记原创 2014-02-21 00:12:26 · 1369 阅读 · 0 评论 -
UVa 11686 Pick up sticks (BFS拓扑排序)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2733算是一种模板题吧。。两个发现:1. 重复初始化queue相当费时间!2. 能不用iterator就不用!完整代码:/*0.732s*/#includeusi原创 2014-02-12 17:56:54 · 1722 阅读 · 0 评论 -
UVa 10305 Ordering Tasks (拓扑排序模板)
10305 - Ordering TasksTime limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1246模板:/*0.022s*/#includeusi原创 2013-12-02 00:27:56 · 1439 阅读 · 0 评论 -
POJ 1113 Wall (凸包周长)
http://poj.org/problem?id=1113使用Graham-Scan算法。为什么扫描阶段的复杂度是O(n)的?因为在扫描的过程中,每个点至多进栈一次,至多出栈一次,也就是说对每个点我们至多进行两次叉积运算,所以复杂度是O(n)的。完整代码:/*16ms,592KB*/#include#include#includeusing name原创 2014-02-13 17:52:22 · 909 阅读 · 0 评论 -
二分图最大匹配——匈牙利算法
复杂度均为O(VE)DFS实现,适用于稠密图:#includeusing namespace std;const int mx = 105;vector G[mx];int match[mx]; ///match表示匹配的对象编号bool vis[mx];bool dfs(int i){ vis[i] = true; for (int j = 0; j < G[i].原创 2014-03-05 23:19:58 · 903 阅读 · 0 评论 -
Codeforces Round #235 (Div. 2) / 410D Roman and Numbers (带有整除性质的数位DP)
http://codeforces.com/problemset/problem/401/D解释全部在代码的注释中:/*78ms,205464KB*/#includeusing namespace std;const int mx = 1 << 18;long long dp[mx][100];///dp[mask][j]表示余数为j时的mask对应的x的个数///原创 2014-03-11 20:25:34 · 2100 阅读 · 0 评论 -
数位DP小记 + HDU 2089 不要62
【背景】如何求出在给定区间[A,B]内,符合条件P(i)的数i的个数?条件P(i)一般与数的大小无关,而与数的组成有关,有一下几种P(i):数i是递增/递减的:1234, 2579,…双峰的:19280,26193,…含/不含某一数字的,比如含49:49, 149, 1492,… 被某一数m整除的,比如m=13:39,130,650...【思路】采用原创 2014-03-11 13:14:59 · 1977 阅读 · 1 评论 -
最长递增子序列(LIS)的O(NlogN)打印算法
题目:求一个一维数组arr[n]中的最长递增子序列的长度,如在序列1,5,8,3,6,7中,最长递增子序列长度为4 (即1,3,6,7)。方法一:一般的DP方法(O(N^2))像LCS一样,从后向前分析,很容易想到,第i个元素之前的最长递增子序列的长度要么是1(单独成一个序列),要么就是第i-1个元素之前的最长递增子序列加1,这样得到状态方程:原创 2013-09-17 09:39:02 · 7339 阅读 · 0 评论 -
POJ 1141 / UVa 1626 Brackets Sequence (区间DP&打印路径)
http://poj.org/problem?id=1141怎么记录路径?定义divide_pos[i][j]为i到j这一段的最佳添加括号位置,若这一段本身就不需要添加括号,则置-1完整代码:/*0ms,416KB*/#include#includeconst int mx = 100;char s[mx];int dp[mx][mx];///最小添加原创 2014-02-28 16:53:38 · 1188 阅读 · 0 评论 -
类型转换与字符串赋值、分割、合并、复制、比较、查询、翻转详解 (完整代码)
一、类型转换char数组转int、__int64或double:#include#include#include#includechar str[100];int main(void){ char *stopstr; gets(str); if (strchr(str, '.') == NULL) { if (strlen(str) < 10) print原创 2013-08-24 20:44:46 · 2953 阅读 · 1 评论 -
MST模板:使用priority_queue实现的Prim算法
测试数据:8 164 5 .354 7 .375 7 .280 7 .161 5 .320 4 .382 3 .171 7 .190 2 .261 2 .361 3 .292 7 .346 2 .403 6 .526 0 .586 4 .93测试结果:1.81代码:#include#include#原创 2014-02-16 00:05:11 · 1720 阅读 · 6 评论 -
UVa 305 / POJ 1012 Joseph (如何得到约瑟夫环的下一个位置?)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=241注意代码中的递推式并不能指出实际的位置,但是要让m是满足题意的,这个位置(在减一映射后)必须>=k当然你也可以打表实现。/*0.079s*/#include原创 2014-03-05 19:14:59 · 1162 阅读 · 0 评论 -
DFS相关的一些模板
/*请从main开始看*//*SampleInput:6 8 70 52 42 31 20 13 43 50 21 40 01 12 23 34 45 54 2 20 12 32 10 0*/#include#include#include#include#includeusing namespace std;const int原创 2013-08-21 02:05:23 · 1092 阅读 · 0 评论 -
O(NloglogN)素数筛法与O(N)素数筛法的对比测试
#include #include #include using namespace std;const int mx = 1000000 + 1; ///在(1,mx)的范围内寻找素数const int sqrt_mx = (int)sqrt((double)mx);bool vis[mx];int prime[mx / 10]; ///在mx>65000时建议写成 int pr原创 2014-01-24 09:09:12 · 3038 阅读 · 0 评论 -
使用bitset实现二进制和十进制的相互转换
#include#include#include#include#include#includeusing namespace std;const int Size = 32;const double log_2 = log(2.0);char str[Size];int main(void){ int num; char *endstr; while (~scan原创 2013-09-26 21:36:07 · 9455 阅读 · 0 评论 -
LightOJ 1255 Substring Frequency (KMP模板)
http://lightoj.com/volume_showproblem.php?problem=1255/*0.068s,7548KB*/#includeusing namespace std;const int mx = 1000005;char t[mx], p[mx];int f[mx];void getfail(){ f[0] = f[1] = 0原创 2014-03-26 23:27:28 · 1447 阅读 · 0 评论 -
HDU 4741 Save Labman No.004 (异面直线距离&直线与平面的交点)
http://acm.hdu.edu.cn/showproblem.php?pid=4741模板题。理论知识见代码注释。这题背景居然是命运石之门。。/*218ms,276KB*/#include#includeconst double eps = 1e-9;struct P3{ double x, y, z; P3(double x = 0.0, do原创 2014-03-16 19:20:13 · 1290 阅读 · 0 评论 -
POJ 2932 Coneology (扫描线判断最外面的圆&set维护最近的圆)
http://poj.org/problem?id=2932先给圆的最左端和最右端的点排个序,当两点x相同时,左端点排在前面。然后就是扫描了,若扫描到的是圆的左端点,就判断圆心(y坐标)在其上方且离其最近的圆是否包含此圆,以及圆心(y坐标)在其下方且离其最近的圆是否包含此圆,若包含就continue,不包含就insert到set中;若扫描到的是圆的右端点,就从set中era原创 2014-03-20 15:08:49 · 1180 阅读 · 0 评论 -
LCA问题的Tarjan离线算法 + POJ 1470
树的最近公共祖先(Lowest Common Ancestor)问题是树结构上最经典的问题之一。给一棵树T,每个询问形如:“点u和点v 的公共祖先是哪个点?”,此问题的答案被记为LCA(u, v)。LCA问题的算法分为在线和离线(Tarjan算法)两种,前者要求在回答后一个问题之前必须给出前一个问题的输出,而离线问题允许在读入所有询问之后一次性给出所有问题的答案。LCA问题的应用很多,例如原创 2014-02-08 11:50:22 · 1713 阅读 · 0 评论 -
Central Europe 1996 / UVa 311 / POJ 1017 Packets (贪心)
311 - PacketsTime limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=247A factory produces products packed in sq原创 2013-10-31 13:16:55 · 1366 阅读 · 0 评论 -
UVa 443 / POJ 2247 Humble Numbers (4因子-丑数&STL灵活运用)
443 - Humble NumbersTime limit: 3.000 secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=384A number whose only prime factors a原创 2013-10-06 10:39:39 · 1268 阅读 · 0 评论 -
用getchar()和putchar()加速IO(含整型快速IO和浮点型快速IO)
****转载请注明http://blog.youkuaiyun.com/synapse7/article/details/19096049****概述:使用getchar()和putchar(),以及math.h头文件中的一些函数,基本实现了以下函数scanf("%u",&x)scanf("%d",&x)scanf("%lf",&x)printf("%u",x)printf("%d",x原创 2014-02-12 00:19:27 · 2843 阅读 · 0 评论 -
树状数组、线段树模板(一)——单点更新 + HDU 1166 敌兵布阵
http://acm.hdu.edu.cn/showproblem.php?pid=1166此题考查RSQ(Range Sum Query)。模板如下:/*343ms,744KB*//*我们使用如下缩写:root--整棵树的根节点rt--当前区间的根节点l,r--区间左右端点m--区间的二等分点,注意m是落在左子区间的(右端点)lson,rson-原创 2013-07-26 00:21:10 · 1134 阅读 · 0 评论 -
扩展欧几里得算法&同余方程&模m乘法逆元详解
首先介绍扩展欧几里得定理:对于两个不全为0的整数a,b,必存在一组解x,y,使得ax+by=gcd(a,b)换句话说,形如ax+by的最小正整数等于gcd(a,b)。实现代码如下:(一般题目都要用64位)(复杂度:O(log max(a,b)))原创 2013-08-11 18:18:27 · 4327 阅读 · 0 评论 -
动态规划(DP)——入门篇(11.24更新)
零、先修课程首先,在开始理解DP的思想前,你需要1. 完成HDU里面的递推求解专题练习(For Beginner)那7道题(这些题很简单,题解请在博客中搜索),这对你理解DP有很大的帮助。2. 对递归搜索(比如深度优先搜索,DFS)有一定了解。一、递归与记忆化搜索我们从POJ 3176入手来学习这一思想。(题目很短,请快速读完)从上往下看,最大和值无非是往左走和往右原创 2013-11-24 21:58:22 · 2750 阅读 · 1 评论 -
三个重要的同余式——威尔逊定理、费马小定理、欧拉定理 + 求幂大法的证明
一、威尔逊定理若p为质数,则p|(p-1)!+1亦:(p-1)! ≡ p-1 ≡ -1(mod p)例题:HDU 2973 YAPTCHA (威尔逊定理及其逆定理)解题报告见http://blog.youkuaiyun.com/synapse7/article/details/18728157二、费马小定理假如p是质数,且gcd(a,p)=1,那么a^(原创 2014-02-21 16:28:11 · 13407 阅读 · 0 评论 -
BFS相关的一些模板
/*请从main开始看*//*SampleInput:6 8 30 52 42 31 20 13 43 50 21 52 42 54 2 10 12 32 1*/#include#include#include#include#includeusing namespace std;const int maxn = 1000002;li原创 2013-08-21 22:11:31 · 897 阅读 · 0 评论