Fragmentary thoughts(PAT刷题碎碎念)

本人2023年上岸浙大计算机学院专硕,无算法竞赛基础在初试后准备机试最后拿到了95分。刷题过程中遇到卡壳的地方,以及刷完之后觉得值得记下几笔以供后续参照的都在笔记上“碎碎念”了。内容碎片化,但你可能会从中得到一些启发。

1.A+B:字符可以与字符串相加,直接用加法;res=n[i]+res;to_string(a):把数字转成字符串

2.拼写正确:遍历,for(auto a:n) / for (int i=0;i<n.size();i++) 下标代替+映射方法

3.签到与签出:字典序比较(时间 15:47:26>15:23:49不用转成秒才比较);寻找最小值或最大值如何处理第一个数据 if (!i || a[i]>max) max=i;

4.字符串判空:empty();灵活使用int/char/string

5.输入包含空格字符串:getline(cin,s1)。哈希表优化法:#include<unordered_set>; unordered_set<char> hash; for(auto c:s2) hash.insert(c); if(!hash.count(c)) res+=c (count表示是否存在)。set、map、multiset、multimap都是平衡树,有序,效率O(log n);unordered_xxx无序,效率O(1)

6.文本中提取单词:双指针法及哈希表(单词计数max值)。unordered_map<string,int> hash;增加(插入):hash[str]++;

7.scanf和printf对字符串输入输出时要用字符数组,string会报错,但加一个.c_str()可以避免。scanf和printf比cin和cout快得多

8.sort排序自己写cmp函数没有结构内部重载运算符速度快,e.x.

bool operator < (const Person& t) const{

return arrive_time<t.arrive_time;

};

9.多项式A*B注意float和double的精度,别搞错

10.高精度乘法:每位的数据和进位值的确定;存储访问的正序和倒序

11.高精度回文:循环边界size()或length();交换系数巧用递归

12.进制:long long代替int可能有奇效;短串进制不设上限,越界可以试试用小于0来判断

13.getchar接在读入一行之前(getline(cin,line));输入#include<sstream> ->string line

stringstream ssin(line);//建立“数据流”,之后可以ssin>>a(int) 或ssin>>word(string)

14.除法一般用截断,四舍五入round()

15.line.substr(3):从line[3]到末尾;set:count(info)可判存在性

16.链表排序:先开大数组,100000、1000000那种;new函数执行时间与一次的空间无关,但和执行次数有关

17.resize(10,-1):vector长度10,填充-1

18.插入or堆排序:插入-前段递增,后段不变;堆-后面全是最大,堆顶摘下与堆尾互换,再把新堆顶依次下降,递归交换

19.树的遍历:中序+后序构造递归,注意参数,抓点分段

if (il<k) l[root]=build(il,k-1,pl,pl+(k-1-il)); if (k<ir) r[root]=build(k+1,ir,pr-1-(ir-k-1),pr-1);

unordered_map可以从值导到索引

20.无向边和有向边存储2次或1次;并查集看2个结点是否同树

21.二叉搜索树:前序+中序判定合法性+输出后序序列

22.完全二叉搜索树:BST中序递增;可依此法进行构造;数组模拟BT从1开始,2*k-left,2*k+1-right

23.构建二叉搜索树:存储l[N],r[N](结构框架);value[N](按序,存点权值);tree[N](对应位权值)

24.反转二叉树:递归对称交换+反向遍历

25.完全BST:函数stoi:string->int;注意携带参数递归的思想方法

26.BST最低两层:先序递归;deptu每次+1;开数组逐层统计

27.Z字形遍历:(1)中后序递归建树;(2)存储:unordered_map<int,int>l,r,pos;(3)甩龙层序:设置head,tail每层首位标识,需要反转reverse(p+head,p+tail+1)

28.AVL树:开数组l[N],r[N],value[N],height[N],主函数insert,操作函数update,L,R,get_balance

29.等重路径:dfs邻接矩阵+vector数组;判断叶节点的方法;bool二维数组做标记

30.最大一代:debug特地检查诸如i<n和i<=n的错误

31.紧急情况:dijkstra统计路数、最大权值;dist[N]最短路径长度数组,每次先选一个最小且未选过的点作为中介,进行更新

32.旅行计划:初始dist设置成0x3f,一方面避免两个7fffffff相加溢出,也容易memset;用pre数组记录每一个点最短路径的前导点,方便回溯路径

33.拓扑排序:每次删掉入度为0的点,当然也要注意第一个点从0开始还是从1开始

34.全局变量替代传引用;dfs或者bfs引入全局变量可以随便统计些东西

35.质因子:枚举到i<=n/i,从2开始除尽为止,无需判质,边运算边输出

36.有理数之和:gcd: return b?gcd(b,a%b):a;范围long long :a/b +c/d = (ad+bc)/bd

37.注意long double高精度,输出printf用lf

38.用无序set统计每个数字是否出现过,再从1开始看哪个数没冒过头

39.每次创造一个set,遍历双数组,看是否“排斥反应”的物品同时出现(用count),若出现break

40.二次探测法:hash长度n,0,1,-1,4,-4,9,-9,...

41.照片同树:各点只需循环一轮即可确定全部连接关系,构建并查集无需两轮

42.相关string函数:int k=s.find("sjifs"),找到则返回第一次出现的索引,找不到返回-1

43.建议提前存储str.length(),如果在循环中对str操作会导致长度改变,循环次数会变,产生不想要的后果

44.大字符串从左到右双指针看缺失,标记并cout

45.stof字符串转浮点数,但要注意负号

46.取整时注意计算机小数不准确,如14可能因为多次小数加减乘除运算变成13.999999,所以要在取整前加上一个较小数,如1e-6

47.插入/归并操作:归并段倍增,插入中间出现的子序列前面递增,后面和原序一致

48.bfs;每轮遍历入队出队深度+1,注意因为路径复杂,不可以用dfs(树则可以)

49.注意queue顶用front不用top

50.输入时若从字符串读数字,采取scanf(结构固定)或stoi配合substr剪贴工具,注意边界,pat里面三个数据卡上限长度,sort时填“+1000”无法通过(因为存储时从1开始计数)

51.字符串加减时候str+=s比str=str+s快很多,小心卡常数

52.浮点数比大小注意考虑误差,不要用==

53.字符数组vs string

int len=strlen(str) //char str[10]

int cmp=strcmp(s1,s2) (大于:1,等于:0,小于:-1)

strcpy(s1,s2)//s1=s2; strcat(s1,s2)//s1+=s2

54.输出格式

printf("%5d\n",a) a<5位用高位空格补齐;

printf("%05d\n",a)用0补齐;

printf("%.2f\n",d2)小数点后2位

55.选择排序和插入排序别搞混

56.哈希表解决冲突:线性探查、平方探查、链地址

57.组合数递推公式:C(n,m)=C(n-1,m)+C(n-1,m-1)

long long C(long long n,long long m){

if (m==0 || m==n) return 1;

return C(n-1,m)+C(n-1,m-1);

}

58.algorithm库要点:(1)max、min、abs(2)swap(3)reverse(指针或迭代器,a[]:reverse(a,a+4))(4)next_permutation:全排列中下一序列,如int a[3]=[1,2,3],231的下一序列为312(5)fill(a,a+5,233)可以赋任意值,memset面向字节(6)sort

59.Huffman树用小顶堆依次选点

60.小根堆priority_queue<int,vector<int>,greater<int>> windows; 单纯pr_q为大根堆

  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Once in a while a book comes along that covers an area of development that has previously been largely ignored. Both MySQL and Perl are well documented in several tutorials, but in-depth coverage of Web development using the combination of the two is rare. MySQL and Perl for the Web zeroes in on this interesting pairing, illustrating practical application development possibilities using this popular duo. The book is perfect for developers of Web sites running on Apache on Unix. Loyalties vary in terms of operating systems, Web servers, and scripting languages, but those who find their home in the Unix and Perl environments will find this book right up their alleys. The target audience is developers somewhat familiar with Perl and the Unix-based Web server environment, but you'll find the book illustrative even if your knowledge is sketchy. After showing how to make database connections using Perl and MySQL, author Paul DuBois dives into some useful real-world examples to help you build your skills. He shows how to implement a simple to-do list application, an online product registration site, contests, online polls, image databases, an online greeting card service, and more. There is also excellent coverage of search facilities and session management. The sample code for the applications in this text is presented in small pieces in context with the discussion; most of the space is devoted to explanations of the issues and implementation. All of the code is available, however, from the book's companion Web site. This tutorial is an excellent way for Perl developers to move to the next level of development and make the most of some powerful, free tools. --Stephen W. Plain Product Description MySQL and Perl for the Web provides a much-needed handbook for database and Web developers seeking an extensive and detailed guide for using the combination of MySQL and Perl to build dynamic and interactive database-backed Web sites. It shows how to use Perl's DBI database access module, pairing it with with the CGI.pm module that allows Web pages and forms to be generated and processed easily. These tools provide developers with a solid foundation for creating applications that incorporate database content to create dynamic, up-to-date Web sites. The book employs a practical learn-by-doing approach that demonstrates development techniques by building complete applications, not just fragmentary pieces. It shows what to do at each step of the way during the process of building live, working examples. Applications are fully explained so you can understand how they work and apply the techniques they illustrate to your own projects.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值