PAT 刷题笔记

121 篇文章 0 订阅
115 篇文章 0 订阅

常见专业名词:

recursively  递归地

traversal  遍历,横越;横断物

iterates:迭代

iteratively 不断地,迭代的

Invert :反转(二叉树反转前后互为镜像)

insertions 插入

Clusters :n. [植] 簇;丛(cluster的复数形式);[计] 群集

acyclic: 无环的

adjacent  :相邻的

permutation : 排列

case insensitive : 不区分大小写

configuration :组合,配置,布局

the greatest common divisor 最大公约数

 syntax tree 语法树

parentheses 括号

precedence 优先权

C++与STL库应用:

结构体初始化:

struct node
{
	string name;
	double score;
	int num;
	node() {
		num = 0;
		score = 0;
	}
};

字符串大小写转换:

//转换为小写
transform(school.begin(), school.end(), school.begin(), ::tolower);

// 法二
for (auto& c : str) c = tolower(c);
//大写:
toupper

注意bool数组的初始化最好用fill()函数。

因为bool = { true };这样的写法只能初始化第一个值!!

    string::iterator it;
    for (it = str.begin(); it < str.end(); it++)
    {
        if (*it == ch)
        {
            str.erase(it);
            it--;
            /*
            it--很重要,因为使用erase()删除it指向的字符后,后面的字符就移了过来,
            it指向的位置就被后一个字符填充了,而for语句最后的it++,又使it向后移
            了一个位置,所以就忽略掉了填充过来的这个字符。在这加上it--后就和for
            语句的it++抵消了,使迭代器能够访问所有的字符。
            */
        }
    }

getline()之前需要把上一行的回车去掉:getchar()一下。

str.substr(a,b); a 是起点下标,b是子串长度。

s.find("parent") != string::npos

sscanf(s.c_str(), "%d is the parent of %d", &a, &b); 

STL优先队列:

基本数据类型就是int型, double型, char型等可以直接使用的数据类型
优先队列对他们的优先级设置一般是数字大的优先级越高
因此队首元素就是优先队列内元素最大的那个(如果是char型,则是字典序最大的)
下面两种优先队列的定义是等价的(以int型为例,注意最后两个>之间有一个空格):

priority_queue<int> q;
priority_queue<int, vector<int>, less<int>> q;


第二种定义方式的尖括号内多出两个参数:vector<int>, less<int>
其中vector<int>填写的是来承载底层数据结构堆(heap)的容器
如果第一个参数是double型或char型,则此处只需要填写vector<double>或vector<char
第三个参数less<int>则是对第一个参数的比较类。
less<int>表示数字大的优先级越大,而greater<int>表示数字小优先级越大

优先队列总数把最小的元素放在队首,定义

priority_queue<int, vector<int>, greater<int>>q;


//示例
#include <stdio.h>
#include <queue>
using namespace std;
int main() {
    priority_queue<int, vector<int>, greater<int>> q;
    q.push(3);
    q.push(4);
    q.push(1);
    printf("%d\n", q.top());
    return 0;
}

DFS 与 BFS

在DFS中关键点是递归以及回溯,在BFS中,关键点则是状态的选取和标记。

DFS用递归的形式,用到了栈结构,先进后出。(根据题不同注意递归点与回溯点)

BFS选取状态用队列的形式+循环,先进先出。

已知中序与前序遍历重建二叉树:

不断拆分节点如下代码是先从左子树开始构建,递归到最左端的叶子节点后开始返回→开始当前右端的构建

preL 与 preR为判断是否到底的条件。

preL每次根据当前的左子树或右子树加一即可,因为pre[]相当于保存的根节点的信息必须以前序的循序为基础进行递归

以中序in[]为搜索和确定左右子树的标准。

numLeft = 0 时 会出现 preL == preR 或者 小于的情况,也说明已经到达叶子。

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;
}
unordered_map <int, int > pos;

int build(int il, int ir, int pl, int pr, int d)
{
    int root = post[pr];
    depth[root] = d;
    int k = pos[root];
    if(k < ir) 
    {
        int son = build(k + 1, ir, pr - 1 - (ir - (k + 1)), pr - 1, d + 1);
        r[root] = son;
        parent[son] = root;
    }
    if(k > il)
    {
        int son = build(il, k - 1, pl, pl + k - 1 - il, d + 1);
        l[root] = son;
        parent[son] = root;
    }
    return root;
}

AVL树的小总结:

复习过一遍后开始刷题时忘记具体操作了ORZ,在此总结加深印象

首先树节点的结构:

struct Node
{
	int v, height;
	Node *lchild, *rchild;
};

创建节点:

Node* newNode(int v) {
	Node* node = new Node;
	node->v = v;
	node->height = 1;
	node->lchild = node->rchild = NULL;
	return node;
}

困扰最大的插入与旋转: 

自己的理解方式:

左旋(Left Rotation):是指将现在的根节点移动到左子树的位置。

void L(Node* &root) {
	Node* temp = root->rchild;
	root->rchild = temp->lchild;
	temp->lchild = root;
	updateHeight(root);
	updateHeight(temp);
	root = temp;
}

右旋(Right Rotation):将当前根节点移动到右子树位置。

void R(Node* &root) {
	Node* temp = root->lchild;
	root->lchild = temp->rchild;
	temp->rchild = root;
	updateHeight(root);
	updateHeight(temp);
	root = temp;
}

插入:(建议根据树形图像记忆代码)

如:

void insert(Node* &root, int v) {
	if (root == NULL)
	{
		root = newNode(v);
		return;
	}
	if (v<root->v)
	{
		insert(root->lchild, v);
		updateHeight(root);
		if (getBalanceFactor(root) == 2)
		{
			if (getBalanceFactor(root->lchild) == 1)
				R(root);
			else if (getBalanceFactor(root->lchild) == -1)
			{
				L(root->lchild);
				R(root);
			}
		}
	}
	else
	{
		insert(root->rchild, v);
		updateHeight(root);
		if (getBalanceFactor(root) == -2)
		{
			if (getBalanceFactor(root->rchild) == -1)
				L(root);
			else if (getBalanceFactor(root->rchild) == 1)
			{
				R(root->rchild);
				L(root);
			}
		}
	}
}

并查集基本结构:(开始不要忘记初始化)

简化版:

int find(int x)
{
    if (p[x] != x) p[x] = find(p[x]);
    return p[x];
}

初始化:

void init(int n) {
	for (int i = 1; i <= n; i++)
	{
		father[i] = i;
	}
}

查找父节点:

int findFather(int x) {
	int a = x;
	while (x != father[x])
		x = father[x];
	//压缩路径
	while (a!=father[a])
	{
		int z = a;
		a = father[a];
		father[z] = x;
	}
	return x;
}

合并: 


void Union(int a, int b) {
	int faA = findFather(a);
	int faB = findFather(b);
	if (faA != faB) {
		father[faA] = faB;
	}
}

堆排序:

要点:向下调整是注意保持 i(需要调整的节点)j(i的子节点中最大的那个)

创建堆时n/2即是最右下的第一个非叶子节点。

}
void downAdjust(int low, int high) {
	int i = low, j = i * 2;
	while (j <= high)
	{
		if (j + 1 <= high && temp[j + 1] > temp[j])
			j = j + 1;
		if (temp[j] > temp[i]) {
			swap(temp[i], temp[j]);
			i = j;
			j = i * 2;
		}
		else break;
	}
}

void heapSort() {//建堆 交换排序
	bool flag = false;
	for (int i = n / 2; i >= 1; i--)//1~n/2(非叶子节点)进行枚举
		downAdjust(i, n);
	for (int i = n; i > 1; i--)
	{
		swap(temp[i], temp[1]);
		downAdjust(1, i - 1);
	}
}

Dijkstra模板:

1.先把最短路dist刷成无穷

2.设置初值,起始节点dist = 0,最短路条数 1·····

3.外层大for 循环 遍历n个节点。

4.用 t 存dist中未使用的最小的点去更新其他节点。

        int t = -1;
        for (int j = 1; j <= n; j ++ )
            if (!st[j] && (t == -1 || dist[t] > dist[j]))
                t = j;
        st[t] = true;

5.用 t点更新其他节点。 按题目要求更新其他值

void Dijkstra(int s) {
	fill(d, d + MAXV, INF);
	d[s] = 0;
	for (int i = 0; i < n; i++)
	{
		int u = -1, MIN = INF;
		for (int j = 0; j < n; j++)//找到下一个可以到达且距离最短的点u
		{
			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++)//遍历从u可以到达的点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();//设置v点的前驱为u
					pre[v].push_back(u);
				}
				else if (d[u]+G[u][v] == d[v])
				{
					pre[v].push_back(u);
				}
			}
		}
	}
}
void dijkstra()
{
    memset(dist, 0x3f, sizeof dist);
    dist[1] = 0, cnt[1] = 1;

    for (int i = 0; i < n; i ++ )
    {
        int t = -1;
        for (int j = 1; j <= n; j ++ )
            if (!st[j] && (t == -1 || dist[t] > dist[j]))
                t = j;
        st[t] = true;

        for (int j = 1; j <= n; j ++ )
            if (dist[j] > dist[t] + d[t][j])
            {
                dist[j] = dist[t] + d[t][j];
                cnt[j] = cnt[t];
                cost[j] = cost[t] + w[j];
                sum[j] = sum[t] + 1;
                pre[j] = t;
            }
            else if (dist[j] == dist[t] + d[t][j])
            {
                cnt[j] += cnt[t];
                if (cost[j] < cost[t] + w[j])
                {
                    cost[j] = cost[t] + w[j];
                    sum[j] = sum[t] + 1;
                    pre[j] = t;
                }
                else if (cost[j] == cost[t] + w[j])
                {
                    if (sum[j] > sum[t] + 1)
                    {
                        sum[j] = sum[t] + 1;
                        pre[j] = t;
                    }
                }
            }
    }
}

数组模拟链表:

#include <iostream>

using namespace std;

const int N = 100010;


// head 表示头结点的下标
// e[i] 表示节点i的值
// ne[i] 表示节点i的next指针是多少
// idx 存储当前已经用到了哪个点
int head, e[N], ne[N], idx;

// 初始化
void init()
{
    head = -1;
    idx = 0;
}

// 将x插到头结点
void add_to_head(int x)
{
    e[idx] = x, ne[idx] = head, head = idx ++ ;
}

// 将x插到下标是k的点后面
void add(int k, int x)
{
    e[idx] = x, ne[idx] = ne[k], ne[k] = idx ++ ;
}

// 将下标是k的点后面的点删掉
void remove(int k)
{
    ne[k] = ne[ne[k]];
}

int main()
{
    int m;
    cin >> m;

    init();

    while (m -- )
    {
        int k, x;
        char op;

        cin >> op;
        if (op == 'H')
        {
            cin >> x;
            add_to_head(x);
        }
        else if (op == 'D')
        {
            cin >> k;
            if (!k) head = ne[head];
            else remove(k - 1);
        }
        else
        {
            cin >> k >> x;
            add(k - 1, x);
        }
    }

    for (int i = head; i != -1; i = ne[i]) cout << e[i] << ' ';
    cout << endl;

    return 0;
}

求最大公约数:

LL gcd(LL a, LL b)
{
    return b ? gcd(b, a % b) : a;
}

判断素数:

bool isPrime(int x) {
	if (x <= 1) return false;
	for (int i = 2; i <= x / i; i++)
		if (x%i == 0)
			return false;
	return true;
}

高精度加法:

vector<int> add(vector<int> a, vector<int> b) {
	vector<int> c;
	for (int i = 0, t = 0; i < a.size() || i < b.size() || t; i++)
	{
		if (i < a.size()) t += a[i];
		if (i < b.size()) t += b[i];
		c.push_back(t % 10);
		t /= 10;
	}
	return c;
}

排名:

    int rank = 1;
    for (int i = 0; i < schools.size(); i ++ )
    {
        auto s = schools[i];
        if (i && s.sum != schools[i - 1].sum) rank = i + 1;

        //其他操作
    }

记录排名:

stu[0].r = 1;
for(int i = 1; i < n; i++)
{
    if(stu[i].s == stu[i-1].s)
        stu[i].r = stu[i-1].r;
    else 
        stu[i].r = i+1;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值