v1.0
一、头文件
#include<cstdio>
#include<cstring>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<set>
#include<string>
#include<map>
#include<queue>
#include<stack>
#include<math.h>
using namespace std;
注:math.h中,abs(x)求整数x绝对值,
fabs(x)求double型x绝对值,
ceil(x)取上整,floor(x)取下整,
pow(x,y) x的y次方,sqrt(x)开根
fabs、ceil、floor、pow、sqrt均返回double型
二、排序
1、快速排序
void quickSort(int A[],int left,int right){
if(left<right){
int pos = Partition(A,left,right);
quickSort(A,left,pos-1);
quickSort(A,pos+1,right);
}
}
int Partition(int A[],int left,int right){
int temp = A[left];
while(left<right){
while (left<right && A[right]>temp) right--;
A[left] = A[right];
while(left<right && A[left]<=temp) left++;
A[right] = A[left];
}
A[left] = temp;
return left;
}
三、二叉树
1、存储结构与基本操作
struct node{
int data;
node* lchild;
node* rchild;
};
node* newNode(int v){
node* Node = new node;
Node->data = v;
Node-> lchild = Node->rchild = NULL;
return Node;
}
void search(node* root,int x,int newdata){
if(root == NULL){
return;
}
if(root->data == x){
root->data = newdata;
}
search(root->lchild,x,newdata);
search(root->rchild,x,newdata);
}
void insert(node* &root,int x){
if(root == NULL){
root = newNode(x);
return;
}
if(x应该插在左子树){
insert(root->lchild,x);
}
else{
insert(root->rchild,x);
}
}
node* Create(int data[],int n){
node* root = NULL;
for(int i = 0; i < n; i++){
insert(root,data[i]);
}
return root;
}
2、遍历
void preorder(node* root){
if(root == NULL){
return;
}
visit(root);
preorder(root->lchild);
preorder(root->rchild);
}
void inorder(node* root){
if(root == NULL){
return;
}
inorder(root->lchild);
visit(root);
inorder(root->rchild);
}
void postorder(node* root){
if(root == NULL){
return;
}
postorder(root->lchild);
postorder(root->rchild);
visit(root);
}
void LayerOrder(node* root){
queue<node*> q;
root->layer = 1;
q.push(root);
while(!q.empty()){
node* now = q.front();
q.pop();
visit(now);
if(now->lchild != NULL){
now->lchild->layer = now->layer + 1;
q.push(now->lchild);
}
if(now->rchild != NULL){
now->rchild->layer = now->layer + 1;
q.push(now->rchild);
}
}
}
node* create(int preL,int preR,int inL,int inR){
if(preL > preR){
return NULL;
}
node* root = new node;
root->data = pre[preL];
int k;
for(k = inL; k <= inR; k++){
if(in[k] == pre[preL]){
break;
}
}
int numLeft = k - inL;
root->lchild = create(preL + 1,preL + numLeft,inL,k-1);
root->rchild = create(preL + numLeft + 1,preR,k + 1,inR);
return root;
}
四、树相关
1、静态存储结构与基本操作
struct node{
int data;
vector<int> child;
} Node[maxn];
int index = 0;
int newNode(int v){
Node[index].data = v;
Node[index].child.clear();
return index++;
}
2、遍历
void PreOrder(int root){
visit(Node[root]);
for(int i = 0; i < Node[root].child.size(); i++){
PreOrder(Node[root].child[i]);
}
}
void LayerOrder(int root){
queue<int> Q;
Q.push(root);
Node[root].layer = 0;
while(!Q.empty()){
int front = Q.front;
Q.pop();
visit(Node[front]);
for(int i = 0; i < Node[front].child.size(); i++){
int child = Node[front].child[i];
Node[child].layer = Node[front].layer + 1;
Q.push(child);
}
}
}
3、堆
void createHeap(){
for(int i = n/2; i>= 1; i--){
downAdjust(i,n);
}
}
void downAdjust(int low, int high){
int i = low, j = i * 2;
while(j <= high){
if(j+1 <= high && heap[j+1] > heap[j]){
j = j + 1;
}
if(heap[j] > heap[i]){
swap(heap[j], heap[i]);
i = j;
j = i * 2;
}
else{
break;
}
}
}
void deleteTop(){
heap[1] = heap[n];
n--;
downAdjust(1, n);
}
void insert(int x){
n++;
heap[n] = x;
upAdjust(1, n);
}
void upAdjust(int low, int high){
int i = high, j = i / 2;
while(j >= low){
if(heap[j] < heap[i]){
swap(heap[j], heap[i]);
i = j;
j = i / 2;
}
else{
break;
}
}
}
void heapSort(){
createHeap();
for(int i = n; i > 1; i--){
swap(heap[i], heap[1]);
downAdjust(1, i - 1);
}
}
五、图
1、图的存储
int G[N][N];
vector<int> Adj[N];
struct Node{
int v;
int w;
Node(int _v, int_w) : v(_v), w(_w){}
}
vector<Node> Adj[N];
2、DFS遍历图
const int maxn = 1000;
const int INF = 0x3f3f3f3f;
int n,G[maxn][maxn];
bool vis[maxn] = {false};
void DFS(int u, int depth){
vis[u] = true;
for(int v = 0; v < n; v++){
if(vis[v] == false && G[u][v] != INF){
DFS(v, depth + 1);
}
}
}
void DFSTrave(){
for(int u = 0; u < n; u++){
if(vis[u] == false){
DFS(u, 1);
}
}
}
vector<int> Adj[maxn];
int n;
bool vis[maxn] = {false};
void DFS(int u, int depth){
vis[u] = true;
for(int i = 0; i < Adj[u].size(); i++){
int v = Adj[u][i];
if(vis[v] == false){
DFS(v, depth + 1);
}
}
}
void DFSTrave(){
for(int u = 0; u < n; u++){
if(vis[u] == false){
DFS(u, 1);
}
}
}
3、BFS遍历图
int n,G[maxn][maxn];
bool vis[maxn] = {false};
void BFS(int u){
queue<int> q;
q.push(u);
vis[u] = true;
while(!q.empty()){
int now = q.front();
q.pop();
for(int v = 0; v < n; v++){
if(vis[v] == false && G[now][v] != INF){
q.push(v);
vis[v] = true;
}
}
}
}
void BFSTrave(){
for(int u = 0; u < n; u++){
if(vis[u] == false){
BFS(u);
}
}
}
vector<int> Adj[maxn];
int n;
bool vis[maxn] = {false};
void BFS(int u){
queue<int> q;
q.push(u);
vis[u] = true;
while(!q.empty()){
int now = q.front();
q.pop();
for(int i = 0; i < Adj[now].size(); i++){
int v = Adj[now][i];
if(vis[v] == false){
q.push(v);
vis[v] = true;
}
}
}
}
void BFSTrave(){
for(int u = 0; u < n; u++){
if(vis[u] == false){
BFS(u);
}
}
}
4、最短路径Dij + DFS
int G[maxn][maxn];
int d[maxn];
vector<int> pre[maxn];
bool vis[maxn] = {false};
void Dijkstra(int s){
fill(d, d + maxn, INF);
d[s] = 0;
for(int i = 0; i < n; i++){
int u = -1, MIN = INF;
for(int j = 0; j < n; j++){
if(vis[j] == false && d[j] < MIN){
u = j;
MIN = d[j];
}
}
if(u == -1) return;
vis[u] = true;
for(int v = 0; v < n; v++){
if(vis[v] == false && G[u][v] != INF){
if(d[u] + G[u][v] < d[v]){
d[v] = d[u] + G[u][v];
pre[v].clear();
pre[v].push_back(u);
}else if(d[u] + G[u][v] == d[v]){
pre[v].push_back(u);
}
}
}
}
}
int optvalue = 初值0或INF;
vector<int> pre[maxn];
vector<int> path, temppath;
void DFS(int v){
if(v == st){
temppath.push_back(v);
int value;
if(value优于optvalue){
optvalue = value;
path = temppath;
}
temppath.pop_back();
return;
}
temppath.push_back(v);
for(int i = 0; i < pre[v].size(); i++){
DFS(pre[v][i]);
}
temppath.pop_back();
}
5、Floyd算法
int dis[maxn][maxn];
void Floyd(){
for(int k = 0; k < n; k++){
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(dis[i][k] + dis[k][j] < dis[i][j] && dis[i][k] != INF && dis[k][j] != INF){
dis[i][j] = dis[i][k] + dis[k][j];
}
}
}
}
}
六、其他
1、大整数运算
struct bign{
int d[1000];
int len;
bign(){
memset(d,0,sizeof(d));
len = 0;
}
};
bign change(char str[]){
bign a;
a.len = strlen(str);
for(int i = 0; i < a.len; i++){
a.d[i] = str[a.len - 1 - i] - '0';
}
return a;
}
int compare(bign a, bign b){
if(a.len > b.len) return 1;
else if(a.len < b.len) return -1;
else{
for(int i = a.len - 1; i >= 0; i--){
if(a.d[i] > b.d[i]) return 1;
else if(a.d[i] < b.d[i]) return -1;
}
return 0;
}
}
void print(bign a){
for(int i = a.len - 1; i >= 0; i--){
printf("%d", a.d[i]);
}
}
bign add(bign a, bign b){
bign c;
int carry = 0;
for(int i = 0; i < a.len || i < b.len; i++){
int temp = a.d[i] + b.d[i] + carry;
c.d[c.len++] = temp % 10;
carry = temp / 10;
}
if(carry != 0){
c.d[c.len++] = carry;
}
return c;
}
bign sub(bign a, bign b){
bign c;
for(int i = 0; i < a.len || i < b.len; i++){
if(a.d[i] < b.d[i]){
a.d[i + 1]--;
a.d[i] += 10;
}
c.d[c.len ++] = a.d[i] - b.d[i];
}
while(c.len - 1 >= 1 && c.d[c.len - 1] == 0){
c.len--;
}
return c;
}
bign multi(bign a, int b){
bign c;
int carry = 0;
for(int i = 0; i < a.len; i++){
int temp = a.d[i] * b + carry;
c.d[c.len++] = temp % 10;
carry = temp / 10;
}
while(carry != 0){
c.d[c.len++] = carry % 10;
carry /= 10;
}
return c;
}
2、最大公约数与最小公倍数
int gcd(int a, int b){
if(b == 0) return a;
else return gcd(b, a % b);
}
3、分数运算
struct Fraction{
int up, down;
}
Fraction reduction(Fraction result){
if(result.down < 0){
result.up = -result.up;
result.down = -result.down;
}
if(result.up == 0){
result.down = 1;
}else{
int d = gcd(abs(result.up), result.down);
result.up /= d;
result.down /= d;
}
return result;
}
void print(Fraction r){
r = reduction(r);
if(r.down == 1){
printf("%lld", r.up);
}
else if(abs(r.up) > r.down){
printf("%d %d/%d",r.up / r.down, abs(r.up) % r.down, r.down);
}
else{
printf("%d/%d", r.up, r.down);
}
}
Fraction add(Fraction f1, Fraction f2){
Fraction res;
res.up = f1.up * f2.down + f2.up * f1.down;
res.down = f1.down * f2.down;
return reduction(res);
}
|