算法
文章平均质量分 72
紫杉丶
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
POJ 1330 Nearest Common Ancestors (LAC)
LCA转RMQ 在线算法,,,//#pragma commmpnt(linkmpr, "/STACK:102400000,102400000")#include #include #include #include #include #include using namespace std;typedef long long LL;const int M = 100010;原创 2015-09-20 10:51:12 · 325 阅读 · 0 评论 -
HDU 2222 Keywords Search
经典自动模板机#include #include #include #include #include using namespace std;struct Trie{ int next[500010][26],fail[500010],end[500010]; int root,L; int newnode() //开辟空间 {原创 2015-07-08 09:40:56 · 312 阅读 · 0 评论 -
Dijstra与prim
Dijstra模板:const int MAXSIZE=100;const int INF=1000000;//除对角线外最初都要初始化为无穷int dist[MAXSIZE];void Dijkstra(int cost[][MAXSIZE],int n,int v)//求v到各个顶点的最短路径{ int i,j,k,min,set[MAXSIZE]; memset(原创 2015-04-16 21:21:58 · 380 阅读 · 0 评论 -
ZOJ 2836 Number Puzzle (容斥原理)
理解的好辛苦啊,,ym final爷,几分钟就能A出来!! 二进制真是好东西啊 #include "string"#include "iostream"#include "cstdio"#include "cmath"#include "set"#include "queue"#include "vector"#include "cctype"#inc原创 2015-04-18 21:05:39 · 416 阅读 · 0 评论 -
HDU 2612 Find a way (bfs)
简单题,,,#include "string"#include "iostream"#include "cstdio"#include "cmath"#include "set"#include "queue"#include "vector"#include "cctype"#include "sstream"#include "cstdlib"#in原创 2015-04-07 19:28:30 · 318 阅读 · 0 评论 -
POJ 1511 Invitation Cards (spfa)
求到某点的来回最短距离,数据太大,要用spfa邻接矩阵存储,将两点互换再求一次最短路,所有距离和即为所求#include "string"#include "iostream"#include "cstdio"#include "cmath"#include "set"#include "queue"#include "vector"#include "cctype原创 2015-04-19 10:13:41 · 361 阅读 · 0 评论 -
POJ 3984 迷宫问题 (bfs 水)
#include "string"#include "iostream"#include "cstdio"#include "cmath"#include "set"#include "queue"#include "vector"#include "cctype"#include "sstream"#include "cstdlib"#include "c原创 2015-04-07 19:27:41 · 320 阅读 · 0 评论 -
POJ 2406 Power Strings(循环节)
#include "string"#include "iostream"#include "cstdio"#include "cmath"#include "set"#include "queue"#include "vector"#include "cctype"#include "sstream"#include "cstdlib"#include "c原创 2015-04-06 21:56:36 · 316 阅读 · 0 评论 -
容斥原理
来源:http://www.cppblog.com/vici/archive/2011/09/05/155103.html 前言:这篇文章发表于http://e-maxx.ru/algo/inclusion_exclusion_principle,原文是俄语的。由于文章确实很实用,而且鉴于国内俄文资料翻译的匮乏,我下决心将其翻译之。由于俄语对我来说如同乱码,而用Google转载 2015-04-18 21:07:38 · 523 阅读 · 0 评论 -
扩展欧几里得
首先、扩展欧几里得定理:对于两个不全为0的整数a、b,必存在一组解x,y,使得ax+by==gcd(a,b);实现如下:int exgcd(int a,int b){ int t,d; if(b==0) { x=1; y=0; //注释1return a; } d=gcd(b,a%b);原创 2015-03-31 22:12:05 · 421 阅读 · 0 评论 -
CodeForces 7C Line (扩展欧几里得)
前面忘记修改gcd,一直求不出来。。。套套模板轻松水,,#include "string" #include "iostream" #include "cstdio" #include "cmath" #include "set" #include "queue" #include "vector" #include "cctype"原创 2015-04-02 22:33:02 · 424 阅读 · 0 评论 -
HDU 2669 Romantic(exgcd)
扩展欧几里得定理:对于两个不全为0的整数a、b,必存在一组解x,y,使得ax+by==gcd(a,b);然后就没有然后了。。不得不说exgcd用法博大精深#include "string"#include "iostream"#include "cstdio"#include "cmath"#include "set"#include "queue"#include原创 2015-03-31 19:17:37 · 524 阅读 · 0 评论 -
POJ 2142 The Balance(exgcd应用)
有一种天平,这种天平只有两种重量的砝码a和b,现在要称出重量为c的物品,问你至少需要多少a和b,答案需要满足a的数量加上b的数量和最小,并且他们的重量和也要最小。(两个盘都可以放砝码) 分析:这题我刚刚开始还以为是动规或者bfs,也算是碰了一鼻子的灰吧。假设a砝码我们用了x个,b砝码我们用了y个。那么天平平衡时,就应该满足ax+by==c。x,y为正时表示放在和c物品的另一边,为负时表原创 2015-03-31 22:07:27 · 464 阅读 · 0 评论 -
字符串匹配的KMP算法
字符串匹配是计算机的基本任务之一。 举例来说,有一个字符串"BBC ABCDAB ABCDABCDABDE",我想知道,里面是否包含另一个字符串"ABCDABD"? 许多算法可以完成这个任务,Knuth-Morris-Pratt算法(简称KMP)是最常用的之一。它以三个发明者命名,起头的那个K就是著名科学家Donald Knuth。 这种算法不太容易理解,网上有很多解释,原创 2015-03-29 22:25:51 · 360 阅读 · 0 评论 -
HDU 1896 Stones
题意:sempr走在路上会遇见一些石头,如果遇见第奇数个石子,则将它扔到前面,如果是偶数,则什么也不做,如果某一个位置上有多个石头,则先遇见扔的比较近的那个,现在给出一些石头的初始位置和能够扔的距离,问到最后最远处的石头离初始位置多远!#include "stack"#include "cstdio"#include "iostream"#include "cmath"#i原创 2015-03-12 21:50:02 · 393 阅读 · 0 评论 -
Base64_解码 C
int change(char ch) //解码表转换 转成2进制 由于小于64 所以最多6位二进制{ if( ch>='A'&&ch<='Z' ) return ch-'A'; if( ch>='a'&&ch<='z' ) return ch-'a'+26; if( ch>='0'&&ch<='9' ) ret原创 2015-07-09 10:04:15 · 444 阅读 · 0 评论 -
有向图的强连通分量 模板
const int MAXN = 10010; //点数const int MAXM = 100100; //边数struct edge{ int to,next;} edge[MAXM];int head[MAXN],tot;int Low[MAXN],DFN[MAXN],Stack[MAXN];int Belong[MAXN]; //标记当前点属于第几个强连通分量原创 2015-07-25 10:29:26 · 453 阅读 · 0 评论 -
树的直径
任意点开始搜索,找到当前点能到达最远的点,再对这个点进行一次搜索,得到的即为树的直径void bfs(int s){ queueq; memset ( vis, 0, sizeof(vis) ); qu.push(s); dist[s] = 0; vis[s] = 1; maxs = 0; while ( !qu.empty()原创 2015-07-25 10:47:26 · 412 阅读 · 0 评论 -
最短路模板合集~(Dij+Floyd+Spfa)
自己整理的最短路模板,,对于最短路问题主要就是难在构图方面~~ //Dijstravoid Dijstra(){ int i,j; for(i=0; i<n; ++i) { dis[i]=INF; vis[i]=0; } dis[1]=0; int v; for(i=1原创 2015-04-20 11:52:29 · 1788 阅读 · 0 评论 -
HDU 1698 Just a Hook(线段树成段更新)
#include "string"#include "iostream"#include "cstdio"#include "cmath"#include "algorithm"#define INF 0x3f3f3f3f#define INFL 0x3f3f3f3f3f3f3f3fLLusing namespace std;typedef long long LL原创 2015-04-06 14:42:45 · 324 阅读 · 0 评论 -
HDU 1394 Minimum Inversion Number (线段树求逆序数 )
#include "string"#include "iostream"#include "cstdio"#include "cmath"#include "set"#include "queue"#include "vector"#include "cctype"#include "sstream"#include "cstdlib"#include "c原创 2015-04-05 09:54:31 · 318 阅读 · 0 评论 -
后缀表达式计算
将中缀表达式转换为后缀表达式:与转换为前缀表达式相似,遵循以下步骤:(1) 初始化两个栈:运算符栈S1和储存中间结果的栈S2;(2) 从左至右扫描中缀表达式;(3) 遇到操作数时,将其压入S2;(4) 遇到运算符时,比较其与S1栈顶运算符的优先级:(4-1) 如果S1为空,或栈顶运算符为左括号“(”,则直接将此运算符入栈;(4-2) 否则,若优先级比栈顶运算符的高,也将原创 2015-08-05 21:04:50 · 520 阅读 · 0 评论 -
RMQ 一维模板
void intRMQ(int n,int b[]){ mm[0]=-1; for(int i=1; i<=n; ++i) { mm[i]=((i&(i-1))==0)?mm[i-1]+1:mm[i-1]; //计算长度 n为2的倍数 n&n-1==0 dp[i][0]=b[i]; //初值 }原创 2015-07-22 10:56:55 · 400 阅读 · 0 评论 -
POJ 2420 A Star not a Tree?
求多边形的费马点,,爬山算法#include "cstring"#include "iostream"#include "algorithm"#include "cstdio"#include "queue"#include "set"#include "cmath"using namespace std;typedef long long LL;const int M=51原创 2015-07-20 18:47:51 · 321 阅读 · 0 评论 -
POJ 1113 Wall
求围墙的长度 就是里面凸包的周长+半径为L的圆周长#include "cstring"#include "iostream"#include "algorithm"#include "cstdio"#include "queue"#include "set"#include "cmath"using namespace std;typedef long long LL;con原创 2015-07-20 19:18:46 · 357 阅读 · 0 评论 -
LightOJ 1153 Internet Bandwidth
On the Internet, machines (nodes) are richly interconnected, and many paths may exist between a given pair of nodes. The total message-carrying capacity (bandwidth) between two given nodes is the maxi原创 2015-07-19 18:53:56 · 384 阅读 · 0 评论 -
HDU 3068 最长回文
Manacher算法使用,O(n)回文字串算法#include "cstring"#include "iostream"#include "cstdio"using namespace std;typedef long long LL;const int M=55;const int maxn=2147483648;//string s;char s[110000*2+10]原创 2015-07-05 10:52:49 · 400 阅读 · 0 评论 -
HDU 5285 wyh2000 and pupil
用染色法判断一下就好,不认识的人不同的颜色即不同分组,人数最多的一组在前#include#include#includeusing namespace std;typedef long long LL;int edgenum;int n,m;int flag;int color[100010];int colornum[2];int head[100010];int v原创 2015-07-19 08:46:41 · 747 阅读 · 0 评论 -
HDU 5237 Base64 (Java大法好)
~!#~#@~!@~~~import java.util.Base64;import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int casee = 0; int t = sc.nex原创 2015-08-09 17:00:55 · 625 阅读 · 0 评论 -
ZOJ 3885 The Exchange of Items
最小费用最大流~。。 建图需要理解理解,其他都是模板#include #include #include #include #include #include #include #include #include #include using namespace std;typedef long long ll;const int MAXN = 10000;c原创 2015-07-28 09:02:02 · 674 阅读 · 0 评论 -
HDU 2444 The Accomodation of Students
典型的二分图匹配,首先要判断是否为二分图 用染色法,然后就是匈牙利算法。#include#include#include#includeusing namespace std;typedef long long LL;const int M = 210;struct node{ int from,to,next;} edge[210*210];int原创 2015-07-06 15:49:57 · 325 阅读 · 0 评论 -
Trie树
在Trie树中主要有3个操作,插入、查找和删除。一般情况下Trie树中很少存在删除单独某个结点的情况,因此只考虑删除整棵树。1.插入 假设存在字符串str,Trie树的根结点为root。i=0,p=root。 1)取str[i],判断p->next[str[i]-97]是否为空,若为空,则建立结点temp,并将p->next[str[i]-97]指向temp,然后p指向原创 2015-07-07 09:40:04 · 313 阅读 · 0 评论 -
HDU 1686 Oulipo(KMP)
模板题。。新技能get#include #include int next[10005];char str1[1000005],str2[10005];int cnt;void get_next(int len2)//生成next数组{ int i = 0,j = -1; next[0] = -1; while (i<len2)原创 2015-03-30 22:25:38 · 351 阅读 · 0 评论 -
HDU 1873 看病要排队(优先队列)
#include "stack"#include "cstdio"#include "iostream"#include "cmath"#include "set"#include "sstream"#include "cctype"#include "string"#include "cstring"#include "algorithm"#include原创 2015-03-12 20:45:09 · 334 阅读 · 0 评论 -
HDU 1757 A Simple Math Problem (矩阵快速幂)
/*构造矩阵If x If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10);|f(10) | |a0 a1 a2 ...a8 a9| |f(9)|| f(9) | | 1 0 0 ... 0 0| |f(8)|| .....| = |.. ... ... ... ..| |原创 2015-03-10 19:07:27 · 342 阅读 · 0 评论 -
HDU 2059 龟兔赛跑 (DP)
龟兔赛跑Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 12597 Accepted Submission(s): 4735Problem Description据说在很久很久以前,可怜的兔子经历了人生中最原创 2015-03-05 19:06:48 · 407 阅读 · 0 评论 -
HDU 3405 World Islands(最短路)
最短路变形。。#include "stack"#include "cstdio"#include "iostream"#include "cmath"#include "set"#include "sstream"#include "cctype"#include "string"#include "cstring"#include "algorithm"原创 2015-03-15 08:40:39 · 403 阅读 · 0 评论 -
HDU 2128 Tempter of the Bone II(BFS)
#include "stack"#include "cstdio"#include "iostream"#include "cmath"#include "set"#include "sstream"#include "cctype"#include "string"#include "cstring"#include "algorithm"#include原创 2015-03-15 08:32:38 · 374 阅读 · 0 评论 -
HDU 1045 Fire Net(DFS)
Fire NetTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6948 Accepted Submission(s): 3932Problem DescriptionSuppose that we hav原创 2015-02-09 21:08:04 · 341 阅读 · 0 评论 -
~~背包九讲
P01: 01背包问题 题目 有N件物品和一个容量为V的背包。第i件物品的费用是c[i],价值是w[i]。求解将哪些物品装入背包可使这些物品的费用总和不超过背包容量,且价值总和最大。 基本思路 这是最基础的背包问题,特点是:每种物品仅有一件,可以选择放或不放。 用子问题定义状态:即f[i][v]表示前i件物品恰放入一个容量为v的背包可以获得的最大价值。则其状态转移方程便是:f[转载 2015-02-09 19:21:59 · 318 阅读 · 0 评论
分享