-
Notifications
You must be signed in to change notification settings - Fork 0
/
local-search.xml
475 lines (229 loc) · 803 KB
/
local-search.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
<?xml version="1.0" encoding="utf-8"?>
<search>
<entry>
<title>记铜尾acmer的省赛之旅</title>
<link href="/posts/3/"/>
<url>/posts/3/</url>
<content type="html"><![CDATA[<blockquote><p>在我所在的学校是一个ACM弱校(目前是的),没有ACM集训队,自然也就没有专门的实验室以供训练,甚至难以找到两个愿意持续花精力搞算法的同学,想打ACM只能用爱发电。</p><p>而我自己也不是一个那么积极上进的人,上课靠水,考试抱佛脚。在没有队友老师监督的情况下,也就只是隔三岔五的做做题而已。</p><p>本文单纯记录一下这次的经历,如果有不妥当的措辞请联系本菜狗。</p></blockquote><blockquote><p><a href="https://www.zhihu.com/question/461892045">如何评价 2021 年第十三届四川省 ACM-ICPC 大学生程序设计竞赛?</a></p><p><a href="https://codeforces.com/gym/103117">cf_gym补题链接</a></p></blockquote><h1 id="1-校内选拔"><a href="#1-校内选拔" class="headerlink" title="1. 校内选拔"></a>1. 校内选拔</h1><p>按照惯例,蓝桥杯初赛的举办在acm之前。</p><p>由于这学期负责相关竞赛的老师(ccls)事情有些多,所以直接就把蓝桥杯校内选拔赛(第十二届蓝桥杯大赛模拟赛(第三期))当作了acm校内选拔赛。(按照蓝桥杯的排名来选择参加acm的同学,然后在这些同学中组队)</p><p><a href="https://blog.likeqc.top/">likeqc</a>同学有写过这个模拟赛的<a href="https://blog.likeqc.top/posts/5e9e8135/index.html">题解</a>,就不重复写了。</p><p><em>吐槽一下有些同学校内选拔成绩和初赛成绩相比也太不稳定了趴</em></p><h1 id="2-组队"><a href="#2-组队" class="headerlink" title="2. 组队"></a>2. 组队</h1><p>队友1:<a href="https://blog.csdn.net/weixin_44469806">舒化奶</a>(大学期间最最好的朋友之一,老早就确定的队友,下图被ccls称为小鹏)</p><p>队友2:<a href="https://grapeoni.xyz/">jxx</a>(ccls推荐队友,舒化奶前女友,下图李XX)</p><p><em>两个卷王,综合排名年级第一第二那种。</em></p><p><img src="https://cdn.jsdelivr.net/gh/oopstls/PicBed/blog/scpc2021/20210606131020.png" alt="image-20210606131018861"></p><p>被学校指定了队友,到这里其实ccls还不知到我的两个队友是前男女朋友,然后有了下面的对话。</p><p><img src="https://cdn.jsdelivr.net/gh/oopstls/PicBed/blog/scpc2021/20210606131038.png" alt="image-20210606131036673"></p><p><mark>编程就好好编程</mark>成了我们队后来常说的一句话</p><h1 id="3-分工"><a href="#3-分工" class="headerlink" title="3. 分工"></a>3. 分工</h1><p><img src="https://cdn.jsdelivr.net/gh/oopstls/PicBed/blog/scpc2021/20210606211332.png" alt="image-20210606211327536"></p><p>在我们完成组队的时候,ccls说我们是全村的希望(感动),原本觉得打差了只是自己难过而已,这样一来还多了一层对不起老师期望了(压力倍增),而且这个时候已经距离比赛只有一个月了(省赛5.30)。</p><p>顶着压力,赶紧分工,抱抱佛脚,经过一番讨论(并没有讨论,直接就确定了😂),简单确定了如下分工。</p><ul><li>舒化奶:数论、计算几何</li><li>jxx:DP、贪心</li><li>特仑苏:搜索、图论</li></ul><p>这个时候还得知川大的校赛在5.21举办,还会开放外校的同学游玩,所以决定各自学20天,然后在川大校赛上训练。(感谢川大,感谢丁丁妹)</p><h1 id="4-训练"><a href="#4-训练" class="headerlink" title="4. 训练"></a>4. 训练</h1><h2 id="4-1-第一场—Sichuan-University-Programming-Contest-2021-Preliminary"><a href="#4-1-第一场—Sichuan-University-Programming-Contest-2021-Preliminary" class="headerlink" title="4.1 第一场—Sichuan University Programming Contest 2021 Preliminary"></a>4.1 第一场—<a href="http://acm.scu.edu.cn/soj/contest/contest.action?cid=391">Sichuan University Programming Contest 2021 Preliminary</a></h2><p>一晃眼20天就过去了,时间来到了5.21,比赛时间是从12:00到17:00,本着反正写完会写的题用不了5个小时的想法(菜狗只能做个签到题的样子),我们决定先睡个午觉13:00再开始😂。由于没有实验室可用(有实验室也没空调,火焰山大学没空调可顶不住坐5小时),所以就在学校里找了个奶茶店打。</p><p>我们三个之前有过一次数学建模的组队经历,互相还算了解,但是没有一起打过acm,所以上来就开了三线程,自己做自己负责的题目先试试看。</p><p>我进入oj的时候已经有很多队过题了,跟榜很快过了签到题,然后开了一个BFS板子题,这个时候舒化奶开了一个最小公倍数的题,jxx开了解方程的题。</p><p>从这个时候开始,我们的问题就开始逐渐暴露了。舒化奶的问题是思路没毛病,但是代码实现会被一点小细节卡住,而且难以debug那种。jxx负责的dp和贪心并没有准备好,所以就能做啥就开啥了,但是她做题的思维是深搜,也就是把一个思路想到底,不撞墙不回头那种(甚至撞了墙也不回头)。这个时候他们两个还有情感隔阂,没办法交流的那种。所以我就一哈帮舒化奶debug,一哈听jxx讲思路,就这样持续了两个多小时,板子题我都还没写完,期间jxx居然还写了个巨复杂的1e9复杂度的代码还交了,debug都看了我好几分钟,无语自闭。冷静下来想了一想,发现我没有时间自己写题,所以决定先不看队友的题,把手上的板子题过了再说(早就该过了)。果然静下来马上就过了。</p><p>到这个时候离结束已经只差一个小时了,舒化奶的最小公倍数已经wa了五六次了,一开始交了1e16范围能过的代码没过,而且题目没有给出数据范围,我们就以为是需要用高精度来写的,所以让舒化奶抄高精度板子,然后我和jxx推方程。</p><p>离结束最后半小时,舒化奶又wa了两次,找不到办法直接放弃了,jxx这个题推出了一个线性方程组,直接上了高斯消元板子,但是直到结束也没有调出来正确答案,就这样以两题结束了第一次模拟。</p><p>结束之后我又看了看舒化奶的最小公倍数的题,抄板子就完了,感觉应该不会有问题才对,然后自己写了个交了,ac,一对比发现舒化奶输出答案没换行。</p><p>赛后总结:</p><ul><li>我应该不管其他事先把能过的题先过了再说</li><li>舒化奶需要注意细节</li><li>jxx写代码前应当先计算复杂度</li><li>debug耗时多,第二场12:00开始</li></ul><h2 id="4-2-第二场—Sichuan-University-Programming-Contest-2021-Final-team"><a href="#4-2-第二场—Sichuan-University-Programming-Contest-2021-Final-team" class="headerlink" title="4.2 第二场—Sichuan University Programming Contest 2021 Final(team)"></a>4.2 第二场—<a href="http://acm.scu.edu.cn/soj/contest/contest.action?cid=392">Sichuan University Programming Contest 2021 Final(team)</a></h2><p>第二天我们开始已经是12:30了,照样跟榜快速过了个递推序列的前缀和题。</p><p>然后就记不清了😂😂😂,反正比第一场更自闭了,发现我大部分时间都在为队友的代码debug,于是商量后定下我写代码,他们两个翻译题目以及提出思路的战略。</p><h2 id="4-3-第三场—2019四川省赛模拟-Virtual-Judge-vjudge-net"><a href="#4-3-第三场—2019四川省赛模拟-Virtual-Judge-vjudge-net" class="headerlink" title="4.3 第三场—2019四川省赛模拟 - Virtual Judge (vjudge.net)"></a>4.3 第三场—<a href="https://vjudge.net/contest/440257">2019四川省赛模拟 - Virtual Judge (vjudge.net)</a></h2><p>第三场模拟,向ccls申请和学校里选拔出的其他几个队一起模拟一下历年比赛,本来准备模拟2020的,但是死活没找到哪个oj有题,最后用了2019。</p><p>19的题是做过一次的,但是当时太菜,很多题都没读懂,基本上可以说是全忘了,拿来模拟也是可以的。</p><p>众所周知19四川省赛是阅读理解专场,开场我一个英语渣直接愣住,没几个是看得懂的,所以开场一直在看榜,翻译直接交给两个英语学霸队友。</p><p>直到20多分钟后,看榜找到了签到题是B题,舒化奶很快翻译完给出了题意,简单写了一发暴力枚举每个方向的多边形就交了,结果wa了两发才过,两罚都是粗心小错误,而且这个时候发现另一个队伍wa了一发就过了,排名在我们前面,心态有点慌张了。</p><p>调整了下心态,jxx翻译完H题给出题意,想了一下感觉是个贪心,把时间排个序就能过。然后写完给舒化奶和jxx看了看感觉没问题就交了,结果又得了个wa。又看了看代码,确实没找到问题,感觉可能jxx给的题意有问题,这阅读理解题目我也看不太懂啊,泪目,然后复制到有道直接翻译了(道歉),发现jxx的翻译确实有漏洞,还是可以贪心,但是需要排序时间和次数两个序列,加了一行sort又交了,ac。这个时候时间才过1小时多一点点,按照19省赛的排名算的话,这个速度的两题队已经有个铜了,哈哈哈哈,心态一下就舒服了。</p><p>然后舒化奶又给出了A的题意,A是模拟自走棋,刚出的时候还沉迷了不短时间,第一版代码写了个<code>unordered_map<pair<string, int>, vecotr<int> ></code>这样的结构来通过{名字:等级}映射所在棋盘上的位置,然后发现pair不能放在unordered_map的key位置,然后第二版代码写了个二维的哈希+vector来映射<code>unordered_map<string, unordered_map<int, vector<int> > > </code>,交了两发,得了两个wa,实在是找不到错误,持续到比赛结束。期间还开了个博弈,但是我当时还不怎么会博弈,舒化奶也还没看到博弈,推不出结论,只能作罢。</p><p>第二天又把这个代码拿出来看了看,发现输出的答案序列每一个后面都有空格,然后把最后一个后面的空格去掉又交了一次,ac,woc,过分,这也卡。。。。</p><p>自以为应该是能过3题的😂,而19年3题队有个银了,所以打出了”保铜争银“的口号。</p><p>jxx总结了我们这三场比赛出现的问题,<a href="https://docs.qq.com/doc/DU1lBaUJVTXR1Zm1y">机场路幼儿园一队避坑指南</a>,准备后面按照这个指南避坑。</p><h1 id="5-热身赛"><a href="#5-热身赛" class="headerlink" title="5. 热身赛"></a>5. 热身赛</h1><p>热身赛前一天在火车上睡了不少时间,再加上当时遇到个因为自己傻逼造成的烦心事,本来就不太困,结果越想越睡不着,又搞了个傻逼操作(捂脸.jpg),然后直接就通宵了。</p><p>早上5点多还没睡着,不想继续躺着就起床了,想着再看看线段树,结果也一点没看进去😭😭😭。ccls安排我们下午13:30去民大报到,就一上午都在酒店无所事事,也没有困意。</p><p>下午报道发现是有发放午餐券的,没机会用了血亏。</p><p>报完到没多久就开始热身赛了,我们那个机房是远程主机,键鼠都有一些延迟,着实有点不爽,还有题目只发了一份。</p><p>A、B题都挺简单的,很快就过了,C很快也找到了思路,写了一发直交,得了个wa,找不到原因就开了D,算了算复杂度感觉暴力枚举就行,写了发dfs交了,又得了个wa,然后就是将近一个小时的debug,最后也没找到问题。</p><p>结束之后问了问前面的ZJUT7队C思路,发现是漏了个特殊情况,可惜。</p><p>然后看了看D,又发现D可以用next_permutation写,可能会好调试一点?</p><p>打完就去民大食堂干饭了,由于jxx不去食堂吃,所以我们多了一张餐券,用这张多的餐券整了25的西瓜😂,另外餐券值25,餐盘饭怕是最多值十二三块,有点亏。</p><p>吃完晚饭在民大逛了一圈,统一的建筑风格真好看,还有没见过这么大只的鲤鱼。</p><p>回酒店趴床上继续打acwing周赛,结果就做了一个题,然后贼困就睡着了,醒来已经22点。点了个外卖吃完继续睡,就又睡不着了,泪目,睡着大概又是两三点了。</p><h1 id="6-正式赛"><a href="#6-正式赛" class="headerlink" title="6. 正式赛"></a>6. 正式赛</h1><p>早上6点多起床,贼困,状态很差。</p><p>在酒店吃了个早饭就去民大了,正式赛给换了个没啥键鼠延迟的教室(好评)。</p><p>坐在位置上之后贼紧张,作为全村的希望,而且不知道下一次还会不会参加,说不定这次就是退役赛了,慌的一批。</p><p>开始之后我就开始打常用,同时把代码框架写出来复制A~M个,我才复制到D题,这个时候舒化奶给出了A的题意,貌似很简单,我就写了一发,直接就交了,果得了个wa,我还有点懵逼,心想这怎么会wa呢?然后舒化奶指出交到了D题上,我什么都没想,又一波操作,submit->select problem->A,又交了,然后得了个out empty。打开dev一看,A写在了D里面(因为我刚刚在复制D的框架,然后就把A直接写在了D上,然后交题自动识别D.cpp到D题上我也没注意,第二次直接忘了写在了D里面,又交了A,A只有框架),然后冷静了一下交了一发ac。整个过程完全没有想起来我们还有一个“避坑指南”,可见状态之差。</p><p>这个时候心态已经崩了,本来写的挺快排名在同题目数里面应该还比较靠前,结果因为我状态问题wa了两发(给队友道歉),ac了A之后冷静两一两分钟,稳定了一下情绪。</p><p>很快舒化奶又跟榜给出了K的题意,想了个贪心的思路,一把ac,心态稍微好了一点点。</p><p>然后jxx给出了本场唯一一个阅读理解题H的题意,题目很长,但是写起来还蛮简单的,wa了一发,很快发现了坑点,第二发过了。这个时候大概距离开始两个多小时。</p><p>紧接着舒化奶就给出了唯一一个博弈的题意(出题组说是披着博弈外衣的思维题),想了一下,猜了个结论讲给舒化奶听,好像没什么问题,然后写完就交了,有一个wa,很快就发现是没用long long导致的,改了就ac。泪目,尽是这种问题。</p><p>这个时候距离结束还有两个小时,jxx给出了M的题意,很快有了思路,但是计算复杂度发现大于了1e8,不敢写(后来发现题意又给错了)。</p><p>然后舒化奶给出了最短路题目的题意,一眼看出是多源汇最短路问题,想着用Floyd写,但是板子居然带错了,我的锅,抱歉抱歉😭😭😭。然后也没注意看数据范围,源点只有100个,多源汇BFS就可以过的题,最后没写。</p><p>随后舒化奶给出了A的题意,还快找到了结论,有一个映射不太好写,两个小时硬写了两三版代码,没debug过。</p><p>比赛结束工作人员让每队派两个同学去礼堂颁奖,我们算了算排名,封榜前就是铜尾了,一度不想去礼堂。</p><p>想着去看看其他大佬的风采还是去了颁奖典礼,没想到还能有个铜,大家都是没打破封榜不过题定律啊。</p><p><img src="https://cdn.jsdelivr.net/gh/oopstls/PicBed/blog/se/20210613115638.png" alt="image-20210613115630739"></p><p>现在想来A、M、最短路着实可惜,状态正常的话应该是能过两个的,虽然6题还是铜。</p><p>感谢两位队友在紧张复习考研期间陪我打一次acm。由于不好的状态给队友带来了不佳的竞赛体验,抱歉抱歉。</p><p>最后就是回想起一开始的目标”保铜争银“最后变成”保铁争铜“有点难受。</p><p><img src="https://cdn.jsdelivr.net/gh/oopstls/PicBed/blog/se/20210613000658.png" alt="image-20210613000652662"></p>]]></content>
<categories>
<category>记录</category>
</categories>
<tags>
<tag>acm</tag>
<tag>scpc</tag>
<tag>icpc</tag>
<tag>算法</tag>
</tags>
</entry>
<entry>
<title>记一个蒟蒻的简单社工</title>
<link href="/posts/2/"/>
<url>/posts/2/</url>
<content type="html"><![CDATA[<blockquote><p>事情的起因较长,是个人问题,且和社工过程关系不大,所以就不详细写了。</p><p>总而言之,我想要找到一个和我在同一个团队的男同学的照片。</p><p>既然在同一个团队为什么要社工呢?因为我不认识他,而他认识我,且这个团队上一次开会已经是一年多以前了。</p><p>既然是同学为什么不问问交集的同学呢?因为不想被卖。</p><p>出于隐私保护,这里我们叫这个同学<code>A</code></p></blockquote><h1 id="0x01-已知信息"><a href="#0x01-已知信息" class="headerlink" title="0x01 已知信息"></a>0x01 已知信息</h1><p>我只从给我提到这个人的口中获知了A的名字,以及A是认识我的。</p><p>A认识我,所以我们可能有交集,但是我没有注意到他,所以在QQ上直接搜索A的名字。</p><p><img src="https://cdn.jsdelivr.net/gh/oopstls/PicBed/blog/se_sad_story/20210605225238.png" alt="image-20210605225237065"></p><p>这里发现我们在同一个团队,获取到他的QQ账号以及年级专业。</p><h1 id="0x02-扩展信息范围"><a href="#0x02-扩展信息范围" class="headerlink" title="0x02 扩展信息范围"></a>0x02 扩展信息范围</h1><ol><li>获取到QQ号,那么直接想到的就是q绑库了,运气还不错,获取到如下信息(这里思维惯性了,其实既然知道年级专业,直接去翻各个班级群课程群可以直接获取到很多信息)</li></ol><p><img src="https://cdn.jsdelivr.net/gh/oopstls/PicBed/blog/se_sad_story/20210605225121.png" alt="image-20210605225119143"></p><ol start="2"><li><p>先看看微博</p><p><img src="https://cdn.jsdelivr.net/gh/oopstls/PicBed/blog/se_sad_story/20210605225528.png" alt="image-20210605225526776"></p><p> 几乎没有可用信息。</p><ol start="3"><li>手机号查看微信、支付宝等常用软件</li></ol><p><img src="https://cdn.jsdelivr.net/gh/oopstls/PicBed/blog/se_sad_story/20210605225856.png" alt="image-20210605225854222"></p><p> 除了发现和另一个同学是情侣头像外没有任何可用信息。</p><ol start="4"><li>手机号查询注册平台,查出来十多二十个,大部分都挺平常的,但是这个从未听说过的名字以及这个粉嫩的logo告诉我,不对劲,这怕是个变态lsp。(这些都是后话,感兴趣的可以自行搜索这个平台)</li></ol><p><img src="https://cdn.jsdelivr.net/gh/oopstls/PicBed/blog/se_sad_story/20210605230327.png" alt="image-20210605230325194"></p></li></ol><h1 id="0x03-回过头来缩小范围"><a href="#0x03-回过头来缩小范围" class="headerlink" title="0x03 回过头来缩小范围"></a>0x03 回过头来缩小范围</h1><ol><li><p>这个时候我突然想起,都是一个院系的,直接从学校内下手不是更容易吗</p><p>这里省略一段翻群文件、学校通告的过程</p><p>最终获得了这个同学的学号以及身份证号</p></li></ol><p><img src="https://cdn.jsdelivr.net/gh/oopstls/PicBed/blog/se_sad_story/20210605231009.png" alt="image-20210605231006725"></p><p><img src="https://cdn.jsdelivr.net/gh/oopstls/PicBed/blog/se_sad_story/20210605231127.png" alt="image-20210605231125337"></p><p> 这里不得不批评一下学校收集信息的方式,这不相当于建了个公共的个人信息库吗。</p><ol start="2"><li><p>有了学号事情就好办了,总所周知各个高校都有自己的各种教务系统,教务系统里面有可能会存放学生的照片,且学生账号多是以学号+学号或是学号+弱密码为初始验证,而且大部分同学并不会刻意去修改密码。</p><p>这里扩展一下,既然大部分同学都不会去修改密码,那么会不会有有心的同学将整个学校没有修改密码的同学的数据全部爬取下来呢?</p></li></ol><p><img src="https://cdn.jsdelivr.net/gh/oopstls/PicBed/blog/se_sad_story/20210605231724.png" alt="image-20210605231723010"></p><p> 个人信息很多,却唯独没有我想要的照片。</p><p> 再往下翻翻</p><p><img src="https://cdn.jsdelivr.net/gh/oopstls/PicBed/blog/se_sad_story/20210605231934.png" alt="image-20210605231932358"></p><p> 找到了宿舍信息,好家伙,刚好有一个同学我认识。这里我们管他叫<code>B</code></p><p> 一个寝室的同学一起行动是很常见的,所以在B同学的空间朋友圈很有可能可以翻到A的照片</p><ol start="3"><li><p>在B的空间翻了翻,很快就找到了疑似室友外出游玩的合照(这里就不放出来了),但是并不能直接确定这就是寝室合照(因为除了B我一个也不认识)。但是发现发空间的时间并不是寒暑假,所以必定需要请假。</p></li><li><p>使用前面获取到的学号信息查看他们4个人请假情况</p><p><img src="https://cdn.jsdelivr.net/gh/oopstls/PicBed/blog/se_sad_story/20210605232649.png" alt="image-20210605232647577"></p><p>这里就只放一个图,因为都差不多,基本上可以断定是室友一起出行。(4个人只有一个人改了密码</p></li><li><p>到这里就只剩下最后一个问题了,那就是我只认识一个人,怎么确认剩下三个中谁才是我要找的人?</p><p>正准备社工另外3个人的时候</p><p>巧了,我室友刚吃完饭回来看到我在看空间照片,直接指出他认识其中一个<code>C</code>。而且还有一个同学<code>D</code>他虽然不认识却加了好友,根据D空间中的自拍,成功排除C、D,再加我认识的B,直接得到答案。(感叹一句,校园圈子真小</p></li></ol><h1 id="0x04-其他"><a href="#0x04-其他" class="headerlink" title="0x04 其他"></a>0x04 其他</h1><p>按理说到这里我的目的以及达到了,但是回想起2.4步骤那个平台,好家伙。</p><p>平台截图就不放这里了(不符合我博客的风格,<code>密日小怪兽</code>,自行搜索</p><ol><li><p>手机号查询的注册平台有可能有误,所以我决定手动查查看,结果发现这个平台web不能直接登录,必须下载APP,而这个APP是他们产品的遥控器,同学会玩啊。</p></li><li><p>下载APP,然后尝试A的手机号登录,提示密码错误,再使用我的手机号登录,提示用户不存在,到这里可以说是坐实他是这个平台的用户了,但是不知道有没有买产品(这个产品是给女生用的,啊这</p></li><li><p>不知道怎么描述我这个时候的想法,反正决定试试看能不能看到A在这个平台的信息,有点好奇A同学的操作</p></li><li><p>前期我获取了很多他的个人信息(这里没有完全罗列),根据这些个人信息,我们可以生成A的社工密码库,然后通过这个密码库来尝试爆破密码。</p><blockquote><p> 手动尝试了十几二十次错误密码,并没有错误次数过多而冻结账号,也就是说爆破方案可以进行。</p></blockquote></li><li><p>传统艺能,小黄鸟抓包</p></li></ol><p><img src="https://cdn.jsdelivr.net/gh/oopstls/PicBed/blog/se_sad_story/20210605234946.png" alt="image-20210605234944100"></p><ol start="6"><li><p>编写代码,debug(代码简单,细节就不说了,直接看),生成社工密码库(工具生成,就不展示了</p><div class="code-wrapper"><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token comment"># -*- coding: utf8 -*-</span><span class="token keyword">import</span> requestsapi <span class="token operator">=</span> <span class="token string">"https://www.sistalk.cn/api/user/login?viewer=0"</span>headers <span class="token operator">=</span> <span class="token punctuation">{</span> <span class="token string">'Sistalk-App-Area'</span><span class="token punctuation">:</span> <span class="token string">'CN'</span><span class="token punctuation">,</span> <span class="token string">'Accept'</span><span class="token punctuation">:</span> <span class="token string">'application/vnd.monsterpub.v2+json'</span><span class="token punctuation">,</span> <span class="token string">'Sistalk-App-Device'</span><span class="token punctuation">:</span> <span class="token string">'ffffffff-d3f5-9109-d3f5-910900000000'</span><span class="token punctuation">,</span> <span class="token string">'Sistalk-Language'</span><span class="token punctuation">:</span> <span class="token string">'zh-CN'</span><span class="token punctuation">,</span> <span class="token string">'User-Agent'</span><span class="token punctuation">:</span> <span class="token string">'MonsterPubCN/4.5.7(samsung:SM-N9600)/Android'</span><span class="token punctuation">,</span> <span class="token string">'Accept-Language'</span><span class="token punctuation">:</span> <span class="token string">'zh-CN'</span><span class="token punctuation">,</span> <span class="token string">'Content-Type'</span><span class="token punctuation">:</span> <span class="token string">'application/x-www-form-urlencoded'</span><span class="token punctuation">,</span> <span class="token comment"># 'Content-Length': '99',</span> <span class="token string">'Host'</span><span class="token punctuation">:</span> <span class="token string">'www.sistalk.cn'</span><span class="token punctuation">,</span> <span class="token string">'Connection'</span><span class="token punctuation">:</span> <span class="token string">'Keep-Alive'</span><span class="token punctuation">,</span> <span class="token string">'Accept-Encoding'</span><span class="token punctuation">:</span> <span class="token string">'gzip'</span><span class="token punctuation">}</span>params <span class="token operator">=</span> <span class="token punctuation">{</span> <span class="token string">'telephone'</span><span class="token punctuation">:</span> <span class="token string">'13xxxxxxxxx'</span><span class="token punctuation">,</span> <span class="token string">'password'</span><span class="token punctuation">:</span> <span class="token string">'xxx'</span><span class="token punctuation">,</span> <span class="token string">'login_mode'</span><span class="token punctuation">:</span> <span class="token string">'phone'</span><span class="token punctuation">,</span> <span class="token string">'uuid'</span><span class="token punctuation">:</span> <span class="token string">'ffffffff-d3f5-9109-d3f5-910900000000'</span><span class="token punctuation">}</span><span class="token keyword">def</span> <span class="token function">login</span><span class="token punctuation">(</span>pwd<span class="token punctuation">)</span><span class="token punctuation">:</span> params<span class="token punctuation">[</span><span class="token string">'password'</span><span class="token punctuation">]</span> <span class="token operator">=</span> pwd resp <span class="token operator">=</span> requests<span class="token punctuation">.</span>post<span class="token punctuation">(</span>api<span class="token punctuation">,</span> params<span class="token operator">=</span>params<span class="token punctuation">,</span> headers<span class="token operator">=</span>headers<span class="token punctuation">)</span> <span class="token keyword">print</span><span class="token punctuation">(</span>resp<span class="token punctuation">.</span>text<span class="token punctuation">.</span>encode<span class="token punctuation">(</span><span class="token string">'utf-8'</span><span class="token punctuation">)</span><span class="token punctuation">.</span>decode<span class="token punctuation">(</span><span class="token string">'unicode_escape'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">if</span> <span class="token string">'\\u5bc6\\u7801\\u9519\\u8bef'</span> <span class="token keyword">not</span> <span class="token keyword">in</span> resp<span class="token punctuation">.</span>text<span class="token punctuation">:</span> <span class="token keyword">print</span><span class="token punctuation">(</span>pwd<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">True</span><span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">'__main__'</span><span class="token punctuation">:</span> f <span class="token operator">=</span> <span class="token builtin">open</span><span class="token punctuation">(</span><span class="token string">'D:\\Code\\Script\\xxx.txt'</span><span class="token punctuation">,</span> <span class="token string">'r'</span><span class="token punctuation">)</span> <span class="token keyword">for</span> line <span class="token keyword">in</span> f<span class="token punctuation">.</span>readlines<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">if</span> login<span class="token punctuation">(</span>line<span class="token punctuation">.</span>strip<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token boolean">True</span><span class="token punctuation">:</span> <span class="token keyword">break</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div></li></ol><p>然而最后并没有成功爆破出密码。。。</p><ol start="7"><li>尝试简单渗透平台,结果发现是阿里的waf,算了算了,要不起,溜溜球。</li><li>钓鱼,临近考试,没时间搞钓鱼了,未完待续……..</li></ol><p>很累,不想搞了。</p>]]></content>
<categories>
<category>记录</category>
</categories>
<tags>
<tag>社工</tag>
<tag>网络空间安全</tag>
</tags>
</entry>
<entry>
<title>学习通自动打卡</title>
<link href="/posts/1/"/>
<url>/posts/1/</url>
<content type="html"><![CDATA[<blockquote><p>上学期开学前一段时间,学校就让我们在学习通上打卡,用来确定每个同学返校前的健康状况?这学期又是这样的操作,而且要求的更严格了,放寒假还天天被人催着打卡,就很无语。讲道理哪个同学有异常情况的话,当地相关部门会直接通报到学校的吧。所以我个人觉得这个打卡没什么意义,而且我天天宅在家里,应该也不会有什么异常情况,所以搞一个自动打卡应该没问题吧😂。</p></blockquote><h1 id="20220402-最后一更"><a href="#20220402-最后一更" class="headerlink" title="20220402 最后一更"></a>20220402 最后一更</h1><p>快毕业了,不出意外这是最后一次更新,分析看下面,这里只给出最后一版代码。</p><p>虽然隔一段时间就会修改一下入口表单啥的,但是总的原理是一样的,小修小改一下就又能用了。</p><p>原理是获取历史记录里的数据填写到表单再提交,所以需要有历史记录才能用</p><p> 每日健康上报取前一次的记录</p><p> 日常健康检测取上上次的记录,因为这样能保证是晨间打卡和午间打卡交替出现,也是这个原因导致这里需要有两个历史记录。</p><p>综上,只需要填写账号密码即可使用。</p><p>此外,添加了server酱通知,需要填入server酱sendkey</p><div class="code-wrapper"><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token comment"># -*- coding: utf8 -*-</span><span class="token keyword">import</span> requests<span class="token keyword">import</span> re<span class="token keyword">import</span> json<span class="token keyword">from</span> urllib<span class="token punctuation">.</span>parse <span class="token keyword">import</span> quoteparams_login <span class="token operator">=</span> <span class="token punctuation">{</span> <span class="token string">'uname'</span><span class="token punctuation">:</span> <span class="token string">'这里改为账号'</span><span class="token punctuation">,</span> <span class="token string">'code'</span><span class="token punctuation">:</span> <span class="token string">'这里改为密码'</span><span class="token punctuation">,</span> <span class="token string">'loginType'</span><span class="token punctuation">:</span> <span class="token string">'1'</span><span class="token punctuation">,</span> <span class="token string">'roleSelect'</span><span class="token punctuation">:</span> <span class="token string">'true'</span><span class="token punctuation">}</span>sendkey <span class="token operator">=</span> <span class="token string">'这里改为server酱的sendkey.send'</span>res <span class="token operator">=</span> <span class="token punctuation">{</span> <span class="token string">"isBase64Encoded"</span><span class="token punctuation">:</span> <span class="token boolean">False</span><span class="token punctuation">,</span> <span class="token string">"statusCode"</span><span class="token punctuation">:</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token string">"headers"</span><span class="token punctuation">:</span> <span class="token punctuation">{</span> <span class="token string">"Content-Type"</span><span class="token punctuation">:</span> <span class="token string">"text/html; charset=utf-8"</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token string">"body"</span><span class="token punctuation">:</span> <span class="token string">""</span><span class="token punctuation">,</span> <span class="token string">"status"</span><span class="token punctuation">:</span> <span class="token boolean">True</span><span class="token punctuation">}</span><span class="token comment"># 登录 获取checkCode</span>api_login <span class="token operator">=</span> <span class="token string">'https://passport2-api.chaoxing.com/v11/loginregister?cx_xxt_passport=json'</span>api_apply <span class="token operator">=</span> <span class="token string">'http://office.chaoxing.com/front/web/apps/forms/fore/apply?uid=83260789&code=2iVkZKN1&mappId=3820629&appId=92610a6cf5e6415c8b4c2ac9c6a9a0f1&appKey=DhXDEM4W331834H2&id=1651&enc=3e98bd89c8ce6bcabf08dbad8553f3c1&state=1432&formAppId=&fidEnc=49b6a032fb2a0f2b'</span><span class="token comment"># 每日健康上报 历史记录 打卡</span>api_last3 <span class="token operator">=</span> <span class="token string">'http://office.chaoxing.com/data/apps/forms/fore/user/list?cpage=1&formId=92597&enc=e8a70d13bac504a2ec42ad5d72aa8abd&formAppId='</span>api_save3 <span class="token operator">=</span> <span class="token string">'http://office.chaoxing.com/data/apps/forms/fore/user/save?lookuid=%s'</span><span class="token comment"># 日常健康检测 历史记录 打卡</span>api_last4 <span class="token operator">=</span> <span class="token string">'http://office.chaoxing.com/data/apps/forms/fore/user/list?cpage=1&pageSize=20&formId=4824&enc=ffc335abbf0c8cee0f943ca0bc82d40a&formAppId='</span>api_save4 <span class="token operator">=</span> <span class="token string">'http://office.chaoxing.com/data/apps/forms/fore/user/save?lookuid=%s'</span><span class="token keyword">def</span> <span class="token function">login</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">:</span> resp <span class="token operator">=</span> requests<span class="token punctuation">.</span>post<span class="token punctuation">(</span>api_login<span class="token punctuation">,</span> params<span class="token operator">=</span>params_login<span class="token punctuation">)</span> <span class="token keyword">if</span> json<span class="token punctuation">.</span>loads<span class="token punctuation">(</span>resp<span class="token punctuation">.</span>text<span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token string">'status'</span><span class="token punctuation">]</span><span class="token punctuation">:</span> <span class="token keyword">return</span> resp<span class="token punctuation">.</span>cookies <span class="token keyword">elif</span> json<span class="token punctuation">.</span>loads<span class="token punctuation">(</span>resp<span class="token punctuation">.</span>text<span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token string">'status'</span><span class="token punctuation">]</span> <span class="token keyword">is</span> <span class="token boolean">False</span><span class="token punctuation">:</span> res<span class="token punctuation">[</span><span class="token string">'body'</span><span class="token punctuation">]</span> <span class="token operator">=</span> json<span class="token punctuation">.</span>loads<span class="token punctuation">(</span>resp<span class="token punctuation">.</span>text<span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token string">'mes'</span><span class="token punctuation">]</span> <span class="token keyword">return</span> <span class="token boolean">False</span><span class="token keyword">def</span> <span class="token function">get_checkCode</span><span class="token punctuation">(</span>resp<span class="token punctuation">)</span><span class="token punctuation">:</span> code <span class="token operator">=</span> re<span class="token punctuation">.</span>findall<span class="token punctuation">(</span><span class="token string">r"checkCode.*'(.*)'"</span><span class="token punctuation">,</span> resp<span class="token punctuation">.</span>text<span class="token punctuation">)</span> <span class="token keyword">if</span> code<span class="token punctuation">:</span> <span class="token keyword">return</span> code<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token keyword">else</span><span class="token punctuation">:</span> <span class="token keyword">raise</span> Exception<span class="token punctuation">(</span><span class="token string">"checkCode解析失败"</span><span class="token punctuation">)</span><span class="token comment"># 每日健康上报</span><span class="token keyword">def</span> <span class="token function">checkin3</span><span class="token punctuation">(</span>cookies<span class="token punctuation">)</span><span class="token punctuation">:</span> checkCode <span class="token operator">=</span> get_checkCode<span class="token punctuation">(</span>requests<span class="token punctuation">.</span>get<span class="token punctuation">(</span>api_apply<span class="token punctuation">,</span> cookies<span class="token operator">=</span>cookies<span class="token punctuation">)</span><span class="token punctuation">)</span> form <span class="token operator">=</span> json<span class="token punctuation">.</span>loads<span class="token punctuation">(</span>requests<span class="token punctuation">.</span>post<span class="token punctuation">(</span> api_last3<span class="token punctuation">,</span> cookies<span class="token operator">=</span>cookies<span class="token punctuation">)</span><span class="token punctuation">.</span>text<span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token string">'data'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'formsUserList'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'formData'</span><span class="token punctuation">]</span> params_c3 <span class="token operator">=</span> quote<span class="token punctuation">(</span> <span class="token string">'[{\"compt\":\"captiontext\",\"layoutRatio\":1,\"alias\":\"1\",\"id\":1,\"fields\":[{\"visible\":true,\"editable\":false,\"tip\":{\"imgs\":[],\"text\":\"<p style=\\\"text-align: center;\\\"><span style=\\\"font-size: x-large;\\\">学生基本信息</span></p><p style=\\\"text-align: center;\\\"><span style=\\\"font-size: medium;\\\">以下信息涉及公共安全,要求每位学生如实填报</span></p>\"},\"label\":\"说明文字\",\"fieldType\":{\"type\":\"string\"}}],\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":true,\"hasAuthority\":false},{\"linkInfo\":{\"condFields\":[],\"linkFormType\":\"\",\"linkFormId\":0,\"linkFormValueFieldCompt\":\"\",\"linkFormIdEnc\":\"\",\"linkFormValueFieldId\":0,\"linked\":false},\"compt\":\"contact\",\"loginUserForValue\":true,\"layoutRatio\":1,\"alias\":\"44\",\"id\":44,\"fields\":%s,\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":true,\"hasAuthority\":true},{\"linkInfo\":{\"condFields\":[],\"linkFormType\":\"\",\"linkFormId\":0,\"linkFormValueFieldCompt\":\"\",\"linkFormIdEnc\":\"\",\"linkFormValueFieldId\":0,\"linked\":false},\"compt\":\"editinput\",\"layoutRatio\":1,\"alias\":\"45\",\"formula\":{\"selIndex\":-1,\"calculateFieldId\":0,\"status\":false},\"latestValShow\":false,\"id\":45,\"fields\":%s,\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":true,\"hasAuthority\":true},{\"linkInfo\":{\"condFields\":[],\"linkFormType\":\"\",\"linkFormId\":0,\"linkFormValueFieldCompt\":\"\",\"linkFormIdEnc\":\"\",\"linkFormValueFieldId\":0,\"linked\":false},\"compt\":\"editinput\",\"layoutRatio\":1,\"alias\":\"46\",\"formula\":{\"selIndex\":-1,\"calculateFieldId\":0,\"status\":false},\"latestValShow\":false,\"id\":46,\"fields\":%s,\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":true,\"hasAuthority\":true},{\"linkInfo\":{\"condFields\":[],\"linkFormType\":\"\",\"linkFormId\":0,\"linkFormValueFieldCompt\":\"\",\"linkFormIdEnc\":\"\",\"linkFormValueFieldId\":0,\"linked\":false},\"compt\":\"editinput\",\"layoutRatio\":1,\"alias\":\"48\",\"formula\":{\"selIndex\":-1,\"calculateFieldId\":0,\"status\":false},\"latestValShow\":false,\"id\":48,\"fields\":%s,\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":true,\"hasAuthority\":true},{\"linkInfo\":{\"condFields\":[{\"linkFormCondFieldId\":6,\"linkFormCondFieldCompt\":\"contact\",\"currFormCondFieldId\":44}],\"linkFormType\":\"customForm\",\"linkFormId\":92598,\"linkFormValueFieldCompt\":\"editinput\",\"linkFormIdEnc\":\"6490576bf20c15e2b37ff7e6b44cf382\",\"linkFormValueFieldId\":7,\"linked\":true,\"currFormValueFieldId\":3,\"currFormValueFieldCompt\":\"editinput\"},\"compt\":\"editinput\",\"layoutRatio\":1,\"alias\":\"3\",\"formula\":{\"selIndex\":-1,\"calculateFieldId\":\"0\",\"status\":false},\"latestValShow\":false,\"id\":3,\"fields\":%s,\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":true,\"hasAuthority\":true},{\"linkInfo\":{\"condFields\":[{\"linkFormCondFieldId\":6,\"linkFormCondFieldCompt\":\"contact\",\"currFormCondFieldId\":44}],\"linkFormType\":\"customForm\",\"linkFormId\":92598,\"linkFormValueFieldCompt\":\"editinput\",\"linkFormIdEnc\":\"6490576bf20c15e2b37ff7e6b44cf382\",\"linkFormValueFieldId\":5,\"linked\":true,\"currFormValueFieldId\":4,\"currFormValueFieldCompt\":\"editinput\"},\"compt\":\"editinput\",\"layoutRatio\":1,\"alias\":\"4\",\"formula\":{\"selIndex\":-1,\"calculateFieldId\":\"0\",\"status\":false},\"latestValShow\":false,\"id\":4,\"fields\":%s,\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":true,\"hasAuthority\":true},{\"compt\":\"location\",\"layoutRatio\":1,\"alias\":\"6\",\"distanceRange\":100,\"id\":6,\"fields\":%s,\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":true,\"hasAuthority\":true},{\"linkInfo\":{\"condFields\":[{\"linkFormCondFieldId\":1,\"linkFormCondFieldCompt\":\"contact\",\"currFormCondFieldId\":44}],\"linkFormType\":\"customForm\",\"linkFormId\":92596,\"linkFormValueFieldCompt\":\"numberinput\",\"linkFormIdEnc\":\"dfd6989f283227cde9e82fee8033771a\",\"linkFormValueFieldId\":2,\"linked\":true,\"currFormValueFieldId\":58,\"currFormValueFieldCompt\":\"numberinput\"},\"compt\":\"numberinput\",\"layoutRatio\":1,\"alias\":\"58\",\"latestValShow\":false,\"id\":58,\"fields\":[{\"hasDefaultValue\":false,\"capital\":false,\"visible\":false,\"editable\":false,\"values\":[{\"val\":\"\"}],\"defaultValueStr\":\"[{\\\"val\\\":\\\"\\\"}]\",\"label\":\"上报次数\",\"placeholderMsg\":\"\",\"micrometer\":false,\"statsable\":false,\"verify\":{\"minValue\":{\"errMsg\":\"输入的值不能小于\",\"range\":\"\"},\"realNumber\":{\"isInteger\":false,\"precision\":\"\",\"errMsg\":\"请输入正确的数字类型值\"},\"maxValue\":{\"errMsg\":\"输入的值不能大于\",\"range\":\"\"}},\"tip\":{\"imgs\":[],\"text\":\"(选填)\"},\"fieldType\":{\"type\":\"number\"}}],\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":true,\"hasAuthority\":false},{\"compt\":\"captiontext\",\"layoutRatio\":1,\"alias\":\"7\",\"id\":7,\"fields\":[{\"visible\":true,\"editable\":false,\"tip\":{\"imgs\":[],\"text\":\"<p style=\\\"text-align: center;\\\"><span style=\\\"font-size: x-large;\\\">健康状态填写</span></p>\"},\"label\":\"说明文字\",\"fieldType\":{\"type\":\"string\"}}],\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":true,\"hasAuthority\":false},{\"compt\":\"radiobutton\",\"otherAllowed\":false,\"comptCombined\":true,\"layoutRatio\":1,\"alias\":\"8\",\"latestValShow\":true,\"id\":8,\"optionColor\":true,\"fields\":[{\"hasDefaultValue\":true,\"visible\":true,\"editable\":true,\"values\":[{\"val\":\"否\",\"isOther\":false}],\"options\":[{\"idArr\":[],\"score\":0,\"color\":\"\",\"checked\":true,\"className\":\"color6\",\"title\":\"否\"},{\"idArr\":[],\"score\":0,\"color\":\"\",\"checked\":false,\"className\":\"color2\",\"title\":\"是\"}],\"verify\":{\"required\":{}},\"tip\":{\"imgs\":[],\"text\":\"\"},\"defaultValueStr\":\"[{\\\"val\\\":\\\"否\\\"}]\",\"label\":\"自测体温是否超过37.3℃\",\"fieldType\":{\"type\":\"string\"}}],\"optionScoreShow\":false,\"optionScoreUsed\":false,\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":true,\"hasAuthority\":true},{\"compt\":\"checklist\",\"otherAllowed\":false,\"comptCombined\":true,\"layoutRatio\":1,\"alias\":\"9\",\"latestValShow\":true,\"id\":9,\"optionColor\":false,\"fields\":[{\"hasDefaultValue\":true,\"visible\":true,\"editable\":true,\"values\":[{\"val\":\"健康\"}],\"options\":[{\"idArr\":[],\"score\":0,\"color\":\"\",\"checked\":true,\"className\":\"\",\"title\":\"健康\"},{\"idArr\":[],\"score\":0,\"color\":\"\",\"checked\":false,\"className\":\"\",\"title\":\"出现发热、咳嗽、乏力、咽痛、腹泻、呼吸困难\"},{\"idArr\":[10],\"score\":0,\"color\":\"\",\"checked\":false,\"className\":\"\",\"title\":\"其他身体不适\"}],\"verify\":{\"minValue\":{\"errMsg\":\"输入的值不能小于\",\"range\":\"0\"},\"maxValue\":{\"errMsg\":\"输入的值不能大于\",\"range\":\"0\"},\"required\":{}},\"tip\":{\"imgs\":[],\"text\":\"\"},\"defaultValueStr\":\"[{\\\"val\\\":\\\"健康\\\"}]\",\"label\":\"当前身体状况\",\"fieldType\":{\"type\":\"string\"}}],\"optionScoreShow\":false,\"optionScoreUsed\":false,\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":true,\"hasAuthority\":true},{\"linkInfo\":{\"condFields\":[],\"linkFormType\":\"\",\"linkFormId\":0,\"linkFormValueFieldCompt\":\"\",\"linkFormIdEnc\":\"\",\"linkFormValueFieldId\":0,\"linked\":false},\"compt\":\"edittextarea\",\"layoutRatio\":1,\"alias\":\"10\",\"latestValShow\":true,\"id\":10,\"fields\":[{\"hasDefaultValue\":false,\"visible\":true,\"editable\":true,\"values\":[],\"verify\":{\"charLimit\":{\"size\":200,\"open\":false},\"required\":{}},\"tip\":{\"imgs\":[],\"text\":\"\"},\"defaultValueStr\":\"[{\\\"val\\\":\\\"\\\"}]\",\"label\":\"其他身体不适情况说明\",\"fieldType\":{\"type\":\"string\"}}],\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":false,\"hasAuthority\":true},{\"compt\":\"radiobutton\",\"otherAllowed\":false,\"comptCombined\":true,\"layoutRatio\":1,\"alias\":\"11\",\"latestValShow\":true,\"id\":11,\"optionColor\":true,\"fields\":[{\"hasDefaultValue\":true,\"visible\":true,\"editable\":true,\"values\":[{\"val\":\"否\",\"isOther\":false}],\"options\":[{\"idArr\":[],\"score\":0,\"color\":\"\",\"checked\":true,\"className\":\"color6\",\"title\":\"否\"},{\"idArr\":[12],\"score\":0,\"color\":\"\",\"checked\":false,\"className\":\"color2\",\"title\":\"是\"}],\"verify\":{\"required\":{}},\"tip\":{\"imgs\":[],\"text\":\"\"},\"defaultValueStr\":\"[{\\\"val\\\":\\\"否\\\"}]\",\"label\":\"本人近14天内是否为确诊/疑似患者或无症状感染者\",\"fieldType\":{\"type\":\"string\"}}],\"optionScoreShow\":false,\"optionScoreUsed\":false,\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":true,\"hasAuthority\":true},{\"linkInfo\":{\"condFields\":[],\"linkFormType\":\"\",\"linkFormId\":0,\"linkFormValueFieldCompt\":\"\",\"linkFormIdEnc\":\"\",\"linkFormValueFieldId\":0,\"linked\":false},\"compt\":\"edittextarea\",\"layoutRatio\":1,\"alias\":\"12\",\"latestValShow\":true,\"id\":12,\"fields\":[{\"hasDefaultValue\":false,\"visible\":true,\"editable\":true,\"values\":[],\"verify\":{\"charLimit\":{\"size\":200,\"open\":false},\"required\":{}},\"tip\":{\"imgs\":[],\"text\":\"\"},\"defaultValueStr\":\"[{\\\"val\\\":\\\"\\\"}]\",\"label\":\"情况说明\",\"fieldType\":{\"type\":\"string\"}}],\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":false,\"hasAuthority\":true},{\"compt\":\"radiobutton\",\"otherAllowed\":false,\"comptCombined\":true,\"layoutRatio\":1,\"alias\":\"13\",\"latestValShow\":true,\"id\":13,\"optionColor\":true,\"fields\":[{\"hasDefaultValue\":true,\"visible\":true,\"editable\":true,\"values\":[{\"val\":\"否\",\"isOther\":false}],\"options\":[{\"idArr\":[],\"score\":0,\"color\":\"\",\"checked\":true,\"className\":\"color6\",\"title\":\"否\"},{\"idArr\":[14],\"score\":0,\"color\":\"\",\"checked\":false,\"className\":\"color2\",\"title\":\"是\"}],\"verify\":{\"required\":{}},\"tip\":{\"imgs\":[],\"text\":\"\"},\"defaultValueStr\":\"[{\\\"val\\\":\\\"否\\\"}]\",\"label\":\"本人近14天内是否接触过确诊/疑似病例或无症状感染者\",\"fieldType\":{\"type\":\"string\"}}],\"optionScoreShow\":false,\"optionScoreUsed\":false,\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":true,\"hasAuthority\":true},{\"linkInfo\":{\"condFields\":[],\"linkFormType\":\"\",\"linkFormId\":0,\"linkFormValueFieldCompt\":\"\",\"linkFormIdEnc\":\"\",\"linkFormValueFieldId\":0,\"linked\":false},\"compt\":\"edittextarea\",\"layoutRatio\":1,\"alias\":\"14\",\"latestValShow\":true,\"id\":14,\"fields\":[{\"hasDefaultValue\":false,\"visible\":true,\"editable\":true,\"values\":[],\"verify\":{\"charLimit\":{\"size\":200,\"open\":false},\"required\":{}},\"tip\":{\"imgs\":[],\"text\":\"\"},\"defaultValueStr\":\"[{\\\"val\\\":\\\"\\\"}]\",\"label\":\"情况说明\",\"fieldType\":{\"type\":\"string\"}}],\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":false,\"hasAuthority\":true},{\"compt\":\"radiobutton\",\"otherAllowed\":false,\"comptCombined\":true,\"layoutRatio\":1,\"alias\":\"15\",\"latestValShow\":true,\"id\":15,\"optionColor\":true,\"fields\":[{\"hasDefaultValue\":true,\"visible\":true,\"editable\":true,\"values\":[{\"val\":\"否\",\"isOther\":false}],\"options\":[{\"idArr\":[],\"score\":0,\"color\":\"\",\"checked\":true,\"className\":\"color6\",\"title\":\"否\"},{\"idArr\":[16],\"score\":0,\"color\":\"\",\"checked\":false,\"className\":\"color2\",\"title\":\"是\"}],\"verify\":{\"required\":{}},\"tip\":{\"imgs\":[],\"text\":\"\"},\"defaultValueStr\":\"[{\\\"val\\\":\\\"否\\\"}]\",\"label\":\"本人近14天内是否有国内疫情防控中高风险区旅居史\",\"fieldType\":{\"type\":\"string\"}}],\"optionScoreShow\":false,\"optionScoreUsed\":false,\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":true,\"hasAuthority\":true},{\"linkInfo\":{\"condFields\":[],\"linkFormType\":\"\",\"linkFormId\":0,\"linkFormValueFieldCompt\":\"\",\"linkFormIdEnc\":\"\",\"linkFormValueFieldId\":0,\"linked\":false},\"compt\":\"edittextarea\",\"layoutRatio\":1,\"alias\":\"16\",\"latestValShow\":true,\"id\":16,\"fields\":[{\"hasDefaultValue\":false,\"visible\":true,\"editable\":true,\"values\":[],\"verify\":{\"charLimit\":{\"size\":200,\"open\":false},\"required\":{}},\"tip\":{\"imgs\":[],\"text\":\"\"},\"defaultValueStr\":\"[{\\\"val\\\":\\\"\\\"}]\",\"label\":\"情况说明\",\"fieldType\":{\"type\":\"string\"}}],\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":false,\"hasAuthority\":true},{\"compt\":\"radiobutton\",\"otherAllowed\":false,\"comptCombined\":true,\"layoutRatio\":1,\"alias\":\"17\",\"latestValShow\":true,\"id\":17,\"optionColor\":true,\"fields\":[{\"hasDefaultValue\":true,\"visible\":true,\"editable\":true,\"values\":[{\"val\":\"否\",\"isOther\":false}],\"options\":[{\"idArr\":[],\"score\":0,\"color\":\"\",\"checked\":true,\"className\":\"color6\",\"title\":\"否\"},{\"idArr\":[18],\"score\":0,\"color\":\"\",\"checked\":false,\"className\":\"color2\",\"title\":\"是\"}],\"verify\":{\"required\":{}},\"tip\":{\"imgs\":[],\"text\":\"\"},\"defaultValueStr\":\"[{\\\"val\\\":\\\"否\\\"}]\",\"label\":\"本人近14天内是否接触国内疫情防控中高风险区返乡人员\",\"fieldType\":{\"type\":\"string\"}}],\"optionScoreShow\":false,\"optionScoreUsed\":false,\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":true,\"hasAuthority\":true},{\"linkInfo\":{\"condFields\":[],\"linkFormType\":\"\",\"linkFormId\":0,\"linkFormValueFieldCompt\":\"\",\"linkFormIdEnc\":\"\",\"linkFormValueFieldId\":0,\"linked\":false},\"compt\":\"edittextarea\",\"layoutRatio\":1,\"alias\":\"18\",\"latestValShow\":true,\"id\":18,\"fields\":[{\"hasDefaultValue\":false,\"visible\":true,\"editable\":true,\"values\":[],\"verify\":{\"charLimit\":{\"size\":200,\"open\":false},\"required\":{}},\"tip\":{\"imgs\":[],\"text\":\"\"},\"defaultValueStr\":\"[{\\\"val\\\":\\\"\\\"}]\",\"label\":\"情况说明\",\"fieldType\":{\"type\":\"string\"}}],\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":false,\"hasAuthority\":true},{\"compt\":\"radiobutton\",\"otherAllowed\":false,\"comptCombined\":true,\"layoutRatio\":1,\"alias\":\"19\",\"latestValShow\":true,\"id\":19,\"optionColor\":true,\"fields\":[{\"hasDefaultValue\":true,\"visible\":true,\"editable\":true,\"values\":[{\"val\":\"否\",\"isOther\":false}],\"options\":[{\"idArr\":[],\"score\":0,\"color\":\"\",\"checked\":true,\"className\":\"color6\",\"title\":\"否\"},{\"idArr\":[20],\"score\":0,\"color\":\"\",\"checked\":false,\"className\":\"color2\",\"title\":\"是\"}],\"verify\":{\"required\":{}},\"tip\":{\"imgs\":[],\"text\":\"\"},\"defaultValueStr\":\"[{\\\"val\\\":\\\"否\\\"}]\",\"label\":\"本人近14天内是否有国(境)外旅居史\",\"fieldType\":{\"type\":\"string\"}}],\"optionScoreShow\":false,\"optionScoreUsed\":false,\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":true,\"hasAuthority\":true},{\"linkInfo\":{\"condFields\":[],\"linkFormType\":\"\",\"linkFormId\":0,\"linkFormValueFieldCompt\":\"\",\"linkFormIdEnc\":\"\",\"linkFormValueFieldId\":0,\"linked\":false},\"compt\":\"edittextarea\",\"layoutRatio\":1,\"alias\":\"20\",\"latestValShow\":true,\"id\":20,\"fields\":[{\"hasDefaultValue\":false,\"visible\":true,\"editable\":true,\"values\":[],\"verify\":{\"charLimit\":{\"size\":200,\"open\":false},\"required\":{}},\"tip\":{\"imgs\":[],\"text\":\"\"},\"defaultValueStr\":\"[{\\\"val\\\":\\\"\\\"}]\",\"label\":\"情况说明\",\"fieldType\":{\"type\":\"string\"}}],\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":false,\"hasAuthority\":true},{\"compt\":\"radiobutton\",\"otherAllowed\":false,\"comptCombined\":true,\"layoutRatio\":1,\"alias\":\"21\",\"latestValShow\":true,\"id\":21,\"optionColor\":true,\"fields\":[{\"hasDefaultValue\":true,\"visible\":true,\"editable\":true,\"values\":[{\"val\":\"否\",\"isOther\":false}],\"options\":[{\"idArr\":[],\"score\":0,\"color\":\"\",\"checked\":true,\"className\":\"color6\",\"title\":\"否\"},{\"idArr\":[22],\"score\":0,\"color\":\"\",\"checked\":false,\"className\":\"color2\",\"title\":\"是\"}],\"verify\":{\"required\":{}},\"tip\":{\"imgs\":[],\"text\":\"\"},\"defaultValueStr\":\"[{\\\"val\\\":\\\"否\\\"}]\",\"label\":\"本人近14天内是否接触过国(境)外返回人员\",\"fieldType\":{\"type\":\"string\"}}],\"optionScoreShow\":false,\"optionScoreUsed\":false,\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":true,\"hasAuthority\":true},{\"linkInfo\":{\"condFields\":[],\"linkFormType\":\"\",\"linkFormId\":0,\"linkFormValueFieldCompt\":\"\",\"linkFormIdEnc\":\"\",\"linkFormValueFieldId\":0,\"linked\":false},\"compt\":\"edittextarea\",\"layoutRatio\":1,\"alias\":\"22\",\"latestValShow\":true,\"id\":22,\"fields\":[{\"hasDefaultValue\":false,\"visible\":true,\"editable\":true,\"values\":[],\"verify\":{\"charLimit\":{\"size\":200,\"open\":false},\"required\":{}},\"tip\":{\"imgs\":[],\"text\":\"\"},\"defaultValueStr\":\"[{\\\"val\\\":\\\"\\\"}]\",\"label\":\"情况说明\",\"fieldType\":{\"type\":\"string\"}}],\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":false,\"hasAuthority\":true},{\"compt\":\"radiobutton\",\"otherAllowed\":false,\"comptCombined\":true,\"layoutRatio\":1,\"alias\":\"23\",\"latestValShow\":true,\"id\":23,\"optionColor\":true,\"fields\":[{\"hasDefaultValue\":true,\"visible\":true,\"editable\":true,\"values\":[{\"val\":\"否\",\"isOther\":false}],\"options\":[{\"idArr\":[],\"score\":0,\"color\":\"\",\"checked\":true,\"className\":\"color6\",\"title\":\"否\"},{\"idArr\":[24],\"score\":0,\"color\":\"\",\"checked\":false,\"className\":\"color2\",\"title\":\"是\"}],\"verify\":{\"required\":{}},\"tip\":{\"imgs\":[],\"text\":\"\"},\"defaultValueStr\":\"[{\\\"val\\\":\\\"否\\\"}]\",\"label\":\"共同生活亲属近14天内是否出现发热、咳嗽、乏力、咽痛、腹泻、等身体症状\",\"fieldType\":{\"type\":\"string\"}}],\"optionScoreShow\":false,\"optionScoreUsed\":false,\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":true,\"hasAuthority\":true},{\"linkInfo\":{\"condFields\":[],\"linkFormType\":\"\",\"linkFormId\":0,\"linkFormValueFieldCompt\":\"\",\"linkFormIdEnc\":\"\",\"linkFormValueFieldId\":0,\"linked\":false},\"compt\":\"edittextarea\",\"layoutRatio\":1,\"alias\":\"24\",\"latestValShow\":true,\"id\":24,\"fields\":[{\"hasDefaultValue\":false,\"visible\":true,\"editable\":true,\"values\":[],\"verify\":{\"charLimit\":{\"size\":200,\"open\":false},\"required\":{}},\"tip\":{\"imgs\":[],\"text\":\"\"},\"defaultValueStr\":\"[{\\\"val\\\":\\\"\\\"}]\",\"label\":\"情况说明\",\"fieldType\":{\"type\":\"string\"}}],\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":false,\"hasAuthority\":true},{\"compt\":\"radiobutton\",\"otherAllowed\":false,\"comptCombined\":true,\"layoutRatio\":1,\"alias\":\"25\",\"latestValShow\":true,\"id\":25,\"optionColor\":true,\"fields\":[{\"hasDefaultValue\":true,\"visible\":true,\"editable\":true,\"values\":[{\"val\":\"否\",\"isOther\":false}],\"options\":[{\"idArr\":[],\"score\":0,\"color\":\"\",\"checked\":true,\"className\":\"color6\",\"title\":\"否\"},{\"idArr\":[26],\"score\":0,\"color\":\"\",\"checked\":false,\"className\":\"color2\",\"title\":\"是\"}],\"verify\":{\"required\":{}},\"tip\":{\"imgs\":[],\"text\":\"\"},\"defaultValueStr\":\"[{\\\"val\\\":\\\"否\\\"}]\",\"label\":\"共同生活亲属近14天内是否为确诊/疑似患者或无症状感染者\",\"fieldType\":{\"type\":\"string\"}}],\"optionScoreShow\":false,\"optionScoreUsed\":false,\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":true,\"hasAuthority\":true},{\"linkInfo\":{\"condFields\":[],\"linkFormType\":\"\",\"linkFormId\":0,\"linkFormValueFieldCompt\":\"\",\"linkFormIdEnc\":\"\",\"linkFormValueFieldId\":0,\"linked\":false},\"compt\":\"edittextarea\",\"layoutRatio\":1,\"alias\":\"26\",\"latestValShow\":true,\"id\":26,\"fields\":[{\"hasDefaultValue\":false,\"visible\":true,\"editable\":true,\"values\":[],\"verify\":{\"charLimit\":{\"size\":200,\"open\":false},\"required\":{}},\"tip\":{\"imgs\":[],\"text\":\"\"},\"defaultValueStr\":\"[{\\\"val\\\":\\\"\\\"}]\",\"label\":\"情况说明\",\"fieldType\":{\"type\":\"string\"}}],\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":false,\"hasAuthority\":true},{\"compt\":\"radiobutton\",\"otherAllowed\":false,\"comptCombined\":true,\"layoutRatio\":1,\"alias\":\"27\",\"latestValShow\":true,\"id\":27,\"optionColor\":true,\"fields\":[{\"hasDefaultValue\":true,\"visible\":true,\"editable\":true,\"values\":[{\"val\":\"否\",\"isOther\":false}],\"options\":[{\"idArr\":[],\"score\":0,\"color\":\"\",\"checked\":true,\"className\":\"color6\",\"title\":\"否\"},{\"idArr\":[28],\"score\":0,\"color\":\"\",\"checked\":false,\"className\":\"color2\",\"title\":\"是\"}],\"verify\":{\"required\":{}},\"tip\":{\"imgs\":[],\"text\":\"\"},\"defaultValueStr\":\"[{\\\"val\\\":\\\"否\\\"}]\",\"label\":\"共同生活亲属近14天内是否接触过确诊/疑似病例或无症状感染者\",\"fieldType\":{\"type\":\"string\"}}],\"optionScoreShow\":false,\"optionScoreUsed\":false,\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":true,\"hasAuthority\":true},{\"linkInfo\":{\"condFields\":[],\"linkFormType\":\"\",\"linkFormId\":0,\"linkFormValueFieldCompt\":\"\",\"linkFormIdEnc\":\"\",\"linkFormValueFieldId\":0,\"linked\":false},\"compt\":\"edittextarea\",\"layoutRatio\":1,\"alias\":\"28\",\"latestValShow\":true,\"id\":28,\"fields\":[{\"hasDefaultValue\":false,\"visible\":true,\"editable\":true,\"values\":[],\"verify\":{\"charLimit\":{\"size\":200,\"open\":false},\"required\":{}},\"tip\":{\"imgs\":[],\"text\":\"\"},\"defaultValueStr\":\"[{\\\"val\\\":\\\"\\\"}]\",\"label\":\"情况说明\",\"fieldType\":{\"type\":\"string\"}}],\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":false,\"hasAuthority\":true},{\"compt\":\"radiobutton\",\"otherAllowed\":false,\"comptCombined\":true,\"layoutRatio\":1,\"alias\":\"29\",\"latestValShow\":true,\"id\":29,\"optionColor\":true,\"fields\":[{\"hasDefaultValue\":true,\"visible\":true,\"editable\":true,\"values\":[{\"val\":\"否\",\"isOther\":false}],\"options\":[{\"idArr\":[],\"score\":0,\"color\":\"\",\"checked\":true,\"className\":\"color6\",\"title\":\"否\"},{\"idArr\":[30],\"score\":0,\"color\":\"\",\"checked\":false,\"className\":\"color2\",\"title\":\"是\"}],\"verify\":{\"required\":{}},\"tip\":{\"imgs\":[],\"text\":\"\"},\"defaultValueStr\":\"[{\\\"val\\\":\\\"否\\\"}]\",\"label\":\"共同生活亲属近14天内是否有国内疫情防控中高风险区旅居史\",\"fieldType\":{\"type\":\"string\"}}],\"optionScoreShow\":false,\"optionScoreUsed\":false,\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":true,\"hasAuthority\":true},{\"linkInfo\":{\"condFields\":[],\"linkFormType\":\"\",\"linkFormId\":0,\"linkFormValueFieldCompt\":\"\",\"linkFormIdEnc\":\"\",\"linkFormValueFieldId\":0,\"linked\":false},\"compt\":\"edittextarea\",\"layoutRatio\":1,\"alias\":\"30\",\"latestValShow\":true,\"id\":30,\"fields\":[{\"hasDefaultValue\":false,\"visible\":true,\"editable\":true,\"values\":[],\"verify\":{\"charLimit\":{\"size\":200,\"open\":false},\"required\":{}},\"tip\":{\"imgs\":[],\"text\":\"\"},\"defaultValueStr\":\"[{\\\"val\\\":\\\"\\\"}]\",\"label\":\"情况说明\",\"fieldType\":{\"type\":\"string\"}}],\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":false,\"hasAuthority\":true},{\"compt\":\"radiobutton\",\"otherAllowed\":false,\"comptCombined\":true,\"layoutRatio\":1,\"alias\":\"31\",\"latestValShow\":true,\"id\":31,\"optionColor\":true,\"fields\":[{\"hasDefaultValue\":true,\"visible\":true,\"editable\":true,\"values\":[{\"val\":\"否\",\"isOther\":false}],\"options\":[{\"idArr\":[],\"score\":0,\"color\":\"\",\"checked\":true,\"className\":\"color6\",\"title\":\"否\"},{\"idArr\":[32],\"score\":0,\"color\":\"\",\"checked\":false,\"className\":\"color2\",\"title\":\"是\"}],\"verify\":{\"required\":{}},\"tip\":{\"imgs\":[],\"text\":\"\"},\"defaultValueStr\":\"[{\\\"val\\\":\\\"否\\\"}]\",\"label\":\"共同生活亲属近14天内是否接触国内疫情防控中高风险区返乡人员\",\"fieldType\":{\"type\":\"string\"}}],\"optionScoreShow\":false,\"optionScoreUsed\":false,\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":true,\"hasAuthority\":true},{\"linkInfo\":{\"condFields\":[],\"linkFormType\":\"\",\"linkFormId\":0,\"linkFormValueFieldCompt\":\"\",\"linkFormIdEnc\":\"\",\"linkFormValueFieldId\":0,\"linked\":false},\"compt\":\"edittextarea\",\"layoutRatio\":1,\"alias\":\"32\",\"latestValShow\":true,\"id\":32,\"fields\":[{\"hasDefaultValue\":false,\"visible\":true,\"editable\":true,\"values\":[],\"verify\":{\"charLimit\":{\"size\":200,\"open\":false},\"required\":{}},\"tip\":{\"imgs\":[],\"text\":\"\"},\"defaultValueStr\":\"[{\\\"val\\\":\\\"\\\"}]\",\"label\":\"情况说明\",\"fieldType\":{\"type\":\"string\"}}],\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":false,\"hasAuthority\":true},{\"compt\":\"radiobutton\",\"otherAllowed\":false,\"comptCombined\":true,\"layoutRatio\":1,\"alias\":\"33\",\"latestValShow\":true,\"id\":33,\"optionColor\":true,\"fields\":[{\"hasDefaultValue\":true,\"visible\":true,\"editable\":true,\"values\":[{\"val\":\"否\",\"isOther\":false}],\"options\":[{\"idArr\":[],\"score\":0,\"color\":\"\",\"checked\":true,\"className\":\"color6\",\"title\":\"否\"},{\"idArr\":[34],\"score\":0,\"color\":\"\",\"checked\":false,\"className\":\"color2\",\"title\":\"是\"}],\"verify\":{\"required\":{}},\"tip\":{\"imgs\":[],\"text\":\"\"},\"defaultValueStr\":\"[{\\\"val\\\":\\\"否\\\"}]\",\"label\":\"共同生活亲属近14天内是否有国(境)外旅居史\",\"fieldType\":{\"type\":\"string\"}}],\"optionScoreShow\":false,\"optionScoreUsed\":false,\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":true,\"hasAuthority\":true},{\"linkInfo\":{\"condFields\":[],\"linkFormType\":\"\",\"linkFormId\":0,\"linkFormValueFieldCompt\":\"\",\"linkFormIdEnc\":\"\",\"linkFormValueFieldId\":0,\"linked\":false},\"compt\":\"edittextarea\",\"layoutRatio\":1,\"alias\":\"34\",\"latestValShow\":true,\"id\":34,\"fields\":[{\"hasDefaultValue\":false,\"visible\":true,\"editable\":true,\"values\":[],\"verify\":{\"charLimit\":{\"size\":200,\"open\":false},\"required\":{}},\"tip\":{\"imgs\":[],\"text\":\"\"},\"defaultValueStr\":\"[{\\\"val\\\":\\\"\\\"}]\",\"label\":\"情况说明\",\"fieldType\":{\"type\":\"string\"}}],\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":false,\"hasAuthority\":true},{\"compt\":\"radiobutton\",\"otherAllowed\":false,\"comptCombined\":true,\"layoutRatio\":1,\"alias\":\"35\",\"latestValShow\":true,\"id\":35,\"optionColor\":true,\"fields\":[{\"hasDefaultValue\":true,\"visible\":true,\"editable\":true,\"values\":[{\"val\":\"否\",\"isOther\":false}],\"options\":[{\"idArr\":[],\"score\":0,\"color\":\"\",\"checked\":true,\"className\":\"color6\",\"title\":\"否\"},{\"idArr\":[36],\"score\":0,\"color\":\"\",\"checked\":false,\"className\":\"color2\",\"title\":\"是\"}],\"verify\":{\"required\":{}},\"tip\":{\"imgs\":[],\"text\":\"\"},\"defaultValueStr\":\"[{\\\"val\\\":\\\"否\\\"}]\",\"label\":\"共同生活亲属近14天内是否接触过国(境)外返回人员\",\"fieldType\":{\"type\":\"string\"}}],\"optionScoreShow\":false,\"optionScoreUsed\":false,\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":true,\"hasAuthority\":true},{\"linkInfo\":{\"condFields\":[],\"linkFormType\":\"\",\"linkFormId\":0,\"linkFormValueFieldCompt\":\"\",\"linkFormIdEnc\":\"\",\"linkFormValueFieldId\":0,\"linked\":false},\"compt\":\"edittextarea\",\"layoutRatio\":1,\"alias\":\"36\",\"latestValShow\":true,\"id\":36,\"fields\":[{\"hasDefaultValue\":false,\"visible\":true,\"editable\":true,\"values\":[],\"verify\":{\"charLimit\":{\"size\":200,\"open\":false},\"required\":{}},\"tip\":{\"imgs\":[],\"text\":\"\"},\"defaultValueStr\":\"[{\\\"val\\\":\\\"\\\"}]\",\"label\":\"情况说明\",\"fieldType\":{\"type\":\"string\"}}],\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":false,\"hasAuthority\":true},{\"compt\":\"captiontext\",\"layoutRatio\":1,\"alias\":\"37\",\"id\":37,\"fields\":[{\"visible\":true,\"editable\":false,\"tip\":{\"imgs\":[],\"text\":\"<p style=\\\"text-align: center;\\\"><span style=\\\"font-size: x-large;\\\">出行情况&nbsp;&nbsp;&nbsp;&nbsp;</span><br></p>\"},\"label\":\"说明文字\",\"fieldType\":{\"type\":\"string\"}}],\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":true,\"hasAuthority\":false},{\"compt\":\"radiobutton\",\"otherAllowed\":false,\"comptCombined\":true,\"layoutRatio\":1,\"alias\":\"38\",\"latestValShow\":true,\"id\":38,\"optionColor\":true,\"fields\":[{\"hasDefaultValue\":true,\"visible\":true,\"editable\":true,\"values\":[{\"val\":\"否\",\"isOther\":false}],\"options\":[{\"idArr\":[],\"score\":0,\"color\":\"\",\"checked\":true,\"className\":\"color6\",\"title\":\"否\"},{\"idArr\":[39,41,42,43],\"score\":0,\"color\":\"\",\"checked\":false,\"className\":\"color2\",\"title\":\"是\"}],\"verify\":{\"required\":{}},\"tip\":{\"imgs\":[],\"text\":\"\"},\"defaultValueStr\":\"[{\\\"val\\\":\\\"否\\\"}]\",\"label\":\"当日是否有外出活动(跨市)\",\"fieldType\":{\"type\":\"string\"}}],\"optionScoreShow\":false,\"optionScoreUsed\":false,\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":true,\"hasAuthority\":true},{\"compt\":\"datetimerange\",\"visible\":true,\"editable\":true,\"layoutRatio\":1,\"alias\":\"39\",\"verify\":{\"dateRange\":{\"isInclude\":false,\"errMsg\":\"开始时间不能晚于结束时间\",\"format\":\"yyyy-MM-dd HH:mm\"},\"validateRange\":{\"beginDate\":\"\",\"dynamicRangeStartValue\":1,\"dynamicRangeEndValue\":1,\"rangeType\":0,\"endDate\":\"\",\"errMsg\":\"请输入合法的日期范围\",\"dynamicRangeStartType\":1,\"dynamicRangeEndType\":3},\"required\":{}},\"id\":39,\"fields\":[{\"tip\":{\"imgs\":[],\"text\":\"请选择\"},\"label\":\"外出开始时间\",\"fieldType\":{\"format\":\"yyyy-MM-dd HH:mm\",\"type\":\"date\"},\"values\":[]},{\"tip\":{\"imgs\":[],\"text\":\"请选择\"},\"label\":\"预计结束时间\",\"fieldType\":{\"format\":\"yyyy-MM-dd HH:mm\",\"type\":\"date\"},\"values\":[]},{\"label\":\"时长\",\"fieldType\":{\"unit\":\"hour\",\"calculate\":true,\"type\":\"timerange\"},\"values\":[]}],\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":false,\"hasAuthority\":true},{\"linkInfo\":{\"condFields\":[],\"linkFormType\":\"\",\"linkFormId\":0,\"linkFormValueFieldCompt\":\"\",\"linkFormIdEnc\":\"\",\"linkFormValueFieldId\":0,\"linked\":false},\"compt\":\"areamultiselect\",\"level\":3,\"layoutRatio\":1,\"alias\":\"41\",\"latestValShow\":true,\"id\":41,\"fields\":[{\"hasDefaultValue\":false,\"visible\":true,\"multiOptions\":[],\"editable\":true,\"values\":[],\"verify\":{\"required\":{}},\"tip\":{\"imgs\":[],\"text\":\"\"},\"defaultValueStr\":\"[{\\\"val\\\":\\\"\\\"},{\\\"val\\\":\\\"\\\"},{\\\"val\\\":\\\"\\\"},{\\\"val\\\":\\\"\\\"}]\",\"label\":\"外出目的地\",\"fieldType\":{\"type\":\"multibox\"}}],\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":false,\"hasAuthority\":true},{\"linkInfo\":{\"condFields\":[],\"linkFormType\":\"\",\"linkFormId\":0,\"linkFormValueFieldCompt\":\"\",\"linkFormIdEnc\":\"\",\"linkFormValueFieldId\":0,\"linked\":false},\"compt\":\"areamultiselect\",\"level\":2,\"layoutRatio\":1,\"alias\":\"42\",\"latestValShow\":false,\"id\":42,\"fields\":[{\"hasDefaultValue\":false,\"visible\":true,\"multiOptions\":[],\"editable\":true,\"values\":[],\"verify\":{\"required\":{}},\"tip\":{\"imgs\":[],\"text\":\"\"},\"defaultValueStr\":\"[{\\\"val\\\":\\\"\\\"},{\\\"val\\\":\\\"\\\"}]\",\"label\":\"外出途径地\",\"fieldType\":{\"type\":\"multibox\"}}],\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":false,\"hasAuthority\":true},{\"linkInfo\":{\"condFields\":[],\"linkFormType\":\"\",\"linkFormId\":0,\"linkFormValueFieldCompt\":\"\",\"linkFormIdEnc\":\"\",\"linkFormValueFieldId\":0,\"linked\":false},\"compt\":\"editinput\",\"layoutRatio\":1,\"alias\":\"43\",\"formula\":{\"selIndex\":-1,\"calculateFieldId\":\"0\",\"status\":false},\"latestValShow\":false,\"id\":43,\"fields\":[{\"hasDefaultValue\":false,\"visible\":true,\"editable\":true,\"values\":[],\"verify\":{\"charLimit\":{\"size\":20,\"open\":false},\"regularExpress\":{\"errorTip\":\"格式错误!\",\"express\":\"\"},\"unique\":{\"errMsg\":\"此项内容已存在,不允许重复提交\",\"open\":false},\"format\":{\"type\":\"\"}},\"tip\":{\"imgs\":[],\"text\":\"\"},\"defaultValueStr\":\"[{\\\"val\\\":\\\"\\\"}]\",\"label\":\"外出交通工具(例:自驾、长途汽车、火车、飞机等)\",\"sweepCode\":false,\"fieldType\":{\"type\":\"string\"},\"codeChangeable\":false}],\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":false,\"hasAuthority\":true},{\"compt\":\"captiontext\",\"layoutRatio\":1,\"alias\":\"49\",\"id\":49,\"fields\":[{\"visible\":true,\"editable\":false,\"tip\":{\"imgs\":[],\"text\":\"<p style=\\\"text-align: center;\\\"><font size=\\\"5\\\">疫苗接种信息采集</font></p>\"},\"label\":\"说明文字\",\"fieldType\":{\"type\":\"string\"}}],\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":true,\"hasAuthority\":false},{\"compt\":\"radiobutton\",\"otherAllowed\":false,\"comptCombined\":true,\"layoutRatio\":1,\"alias\":\"50\",\"latestValShow\":true,\"id\":50,\"optionColor\":true,\"fields\":[{\"hasDefaultValue\":true,\"visible\":true,\"editable\":true,\"values\":[{\"val\":\"是\",\"isOther\":false}],\"options\":[{\"idArr\":[52,53],\"score\":0,\"color\":\"\",\"checked\":true,\"className\":\"color6\",\"title\":\"是\"},{\"idArr\":[54],\"score\":0,\"color\":\"\",\"checked\":false,\"className\":\"color2\",\"title\":\"否\"}],\"verify\":{\"required\":{}},\"tip\":{\"imgs\":[],\"text\":\"\"},\"defaultValueStr\":\"[{\\\"val\\\":\\\"是\\\"}]\",\"label\":\"是否已接种新冠疫苗\",\"fieldType\":{\"type\":\"string\"}}],\"optionScoreShow\":false,\"optionScoreUsed\":false,\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":true,\"hasAuthority\":true},{\"linkInfo\":{\"condFields\":[],\"linkFormType\":\"\",\"linkFormId\":0,\"linkFormValueFieldCompt\":\"\",\"linkFormIdEnc\":\"\",\"linkFormValueFieldId\":0,\"linked\":false},\"compt\":\"dateinput\",\"layoutRatio\":1,\"alias\":\"52\",\"latestValShow\":true,\"id\":52,\"fields\":%s,\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":true,\"hasAuthority\":true},{\"linkInfo\":{\"condFields\":[],\"linkFormType\":\"\",\"linkFormId\":0,\"linkFormValueFieldCompt\":\"\",\"linkFormIdEnc\":\"\",\"linkFormValueFieldId\":0,\"linked\":false},\"compt\":\"dateinput\",\"layoutRatio\":1,\"alias\":\"53\",\"latestValShow\":true,\"id\":53,\"fields\":%s,\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":true,\"hasAuthority\":true},{\"linkInfo\":{\"condFields\":[],\"linkFormType\":\"\",\"linkFormId\":0,\"linkFormValueFieldCompt\":\"\",\"linkFormIdEnc\":\"\",\"linkFormValueFieldId\":0,\"linked\":false},\"compt\":\"editinput\",\"layoutRatio\":1,\"alias\":\"54\",\"formula\":{\"selIndex\":-1,\"calculateFieldId\":\"0\",\"status\":false},\"latestValShow\":true,\"id\":54,\"fields\":[{\"hasDefaultValue\":false,\"visible\":true,\"editable\":true,\"values\":[],\"verify\":{\"charLimit\":{\"size\":20,\"open\":false},\"regularExpress\":{\"errorTip\":\"格式错误!\",\"express\":\"\"},\"unique\":{\"errMsg\":\"此项内容已存在,不允许重复提交\",\"open\":false},\"format\":{\"type\":\"\"},\"required\":{}},\"tip\":{\"imgs\":[],\"text\":\"\"},\"defaultValueStr\":\"[{\\\"val\\\":\\\"\\\"}]\",\"label\":\"未接种原因\",\"sweepCode\":false,\"fieldType\":{\"type\":\"string\"},\"codeChangeable\":false}],\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":false,\"hasAuthority\":true},{\"compt\":\"radiobutton\",\"otherAllowed\":false,\"comptCombined\":true,\"layoutRatio\":1,\"alias\":\"55\",\"latestValShow\":false,\"id\":55,\"optionColor\":true,\"fields\":[{\"hasDefaultValue\":false,\"visible\":true,\"editable\":true,\"values\":[{\"val\":\"是\",\"isOther\":false}],\"options\":[{\"idArr\":[],\"score\":100,\"color\":\"\",\"checked\":true,\"className\":\"color6\",\"title\":\"是\"},{\"idArr\":[],\"score\":0,\"color\":\"\",\"checked\":false,\"className\":\"color2\",\"title\":\"否\"}],\"verify\":{\"required\":{}},\"tip\":{\"imgs\":[],\"text\":\"\"},\"defaultValueStr\":\"[]\",\"label\":\"本人承诺:以上信息填报真实有效。本人严格遵守相关防疫工作要求,及时、准确向学校申报健康状况、接触情况、出行行程安排等,并认真做好个人防护。\",\"fieldType\":{\"type\":\"string\"}}],\"optionScoreShow\":false,\"optionScoreUsed\":true,\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":true,\"hasAuthority\":true},{\"compt\":\"signature\",\"layoutRatio\":1,\"alias\":\"56\",\"id\":56,\"fields\":%s,\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":true,\"hasAuthority\":true}]'</span> <span class="token operator">%</span> <span class="token punctuation">(</span>json<span class="token punctuation">.</span>dumps<span class="token punctuation">(</span>form<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'fields'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span> json<span class="token punctuation">.</span>dumps<span class="token punctuation">(</span>form<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'fields'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span> json<span class="token punctuation">.</span>dumps<span class="token punctuation">(</span>form<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'fields'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span> json<span class="token punctuation">.</span>dumps<span class="token punctuation">(</span>form<span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'fields'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span> json<span class="token punctuation">.</span>dumps<span class="token punctuation">(</span>form<span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'fields'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span> json<span class="token punctuation">.</span>dumps<span class="token punctuation">(</span>form<span class="token punctuation">[</span><span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'fields'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span> json<span class="token punctuation">.</span>dumps<span class="token punctuation">(</span>form<span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'fields'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span> json<span class="token punctuation">.</span>dumps<span class="token punctuation">(</span>form<span class="token punctuation">[</span><span class="token number">47</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'fields'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span> json<span class="token punctuation">.</span>dumps<span class="token punctuation">(</span>form<span class="token punctuation">[</span><span class="token number">48</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'fields'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span> json<span class="token punctuation">.</span>dumps<span class="token punctuation">(</span>form<span class="token punctuation">[</span><span class="token number">51</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'fields'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">'utf-8'</span><span class="token punctuation">)</span> params_c3 <span class="token operator">=</span> <span class="token string">'formId=92597&formAppId=&version=0&formData='</span> <span class="token operator">+</span> params_c3 <span class="token operator">+</span> <span class="token string">'&ext=&t=1&enc=e8a70d13bac504a2ec42ad5d72aa8abd&checkCode=%s&gatherId=0&anonymous=0&uniqueCondition=[]&gverify='</span> <span class="token operator">%</span> <span class="token punctuation">(</span> checkCode<span class="token punctuation">)</span> url <span class="token operator">=</span> api_save3 <span class="token operator">%</span> <span class="token punctuation">(</span>cookies<span class="token punctuation">[</span><span class="token string">'_uid'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> resp <span class="token operator">=</span> requests<span class="token punctuation">.</span>post<span class="token punctuation">(</span>url<span class="token punctuation">,</span> cookies<span class="token operator">=</span>cookies<span class="token punctuation">,</span> params<span class="token operator">=</span>params_c3<span class="token punctuation">)</span> <span class="token keyword">if</span> <span class="token string">'每个用户只能填写1次,您的提交已达上限哦~'</span> <span class="token keyword">in</span> resp<span class="token punctuation">.</span>text <span class="token keyword">or</span> <span class="token string">'"success":true'</span> <span class="token keyword">in</span> resp<span class="token punctuation">.</span>text<span class="token punctuation">:</span> res<span class="token punctuation">[</span><span class="token string">'body'</span><span class="token punctuation">]</span> <span class="token operator">+=</span> <span class="token string">'每日健康上报打卡成功或已打卡过\n'</span> <span class="token keyword">else</span><span class="token punctuation">:</span> res<span class="token punctuation">[</span><span class="token string">'body'</span><span class="token punctuation">]</span> <span class="token operator">+=</span> <span class="token string">'每日健康上报打卡失败\n'</span> <span class="token operator">+</span> resp<span class="token punctuation">.</span>text <span class="token operator">+</span> <span class="token string">'\n'</span> res<span class="token punctuation">[</span><span class="token string">'status'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">False</span><span class="token comment"># 日常健康检测</span><span class="token keyword">def</span> <span class="token function">checkin4</span><span class="token punctuation">(</span>cookies<span class="token punctuation">)</span><span class="token punctuation">:</span> checkCode <span class="token operator">=</span> get_checkCode<span class="token punctuation">(</span>requests<span class="token punctuation">.</span>get<span class="token punctuation">(</span>api_apply<span class="token punctuation">,</span> cookies<span class="token operator">=</span>cookies<span class="token punctuation">)</span><span class="token punctuation">)</span> form <span class="token operator">=</span> json<span class="token punctuation">.</span>loads<span class="token punctuation">(</span>requests<span class="token punctuation">.</span>post<span class="token punctuation">(</span> api_last4<span class="token punctuation">,</span> cookies<span class="token operator">=</span>cookies<span class="token punctuation">)</span><span class="token punctuation">.</span>text<span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token string">'data'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'formsUserList'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'formData'</span><span class="token punctuation">]</span> params_c4 <span class="token operator">=</span> quote<span class="token punctuation">(</span> <span class="token string">'[{\"linkInfo\":{\"linkFormType\":\"customForm\",\"condFields\":[],\"linkFormId\":0,\"linkFormValueFieldCompt\":\"\",\"linkFormIdEnc\":\"\",\"linkFormValueFieldId\":0,\"linked\":false},\"compt\":\"location\",\"locationScope\":{\"mapValue\":[],\"linkedInfo\":{},\"defaultRange\":500,\"select\":false,\"type\":0},\"layoutRatio\":1,\"alias\":10,\"distanceRange\":0,\"id\":10,\"fields\":%s,\"defaultValueConfig\":0,\"locationValue\":0,\"inDetailGroupIndex\":-1,\"inDetailGroupGeneralId\":-1,\"fromDetail\":false,\"isShow\":true,\"hasAuthority\":true},{\"linkInfo\":{\"linkFormType\":\"\",\"condFields\":[],\"linkFormId\":0,\"linkFormValueFieldCompt\":\"\",\"linkFormIdEnc\":\"\",\"linkFormValueFieldId\":0,\"linked\":false},\"compt\":\"selectbox\",\"layoutRatio\":1,\"optionScoreShow\":false,\"optionScoreUsed\":false,\"otherAllowed\":false,\"alias\":5,\"latestValShow\":false,\"id\":5,\"optionsLoadFromUrl\":{\"isLoadFromUrl\":false,\"response\":[],\"url\":[],\"urlHeaders\":[]},\"optionColor\":false,\"fields\":%s,\"optionBindInfo\":{\"bindFormIdEnc\":\"\",\"bindFieldId\":0,\"bindFieldIdx\":-1,\"bindFormType\":\"customForm\",\"isBinded\":false,\"bindFormId\":0,\"originalOptions\":[{\"idArr\":[],\"score\":0,\"checked\":false,\"title\":\"选项1\"},{\"idArr\":[],\"score\":0,\"checked\":false,\"title\":\"选项2\"},{\"idArr\":[],\"score\":0,\"checked\":false,\"title\":\"选项3\"}],\"bindFieldCompt\":\"\"},\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":true,\"hasAuthority\":true},{\"linkInfo\":{\"linkFormCondFieldId\":0,\"linkFormType\":\"\",\"condFields\":[],\"linkFormId\":0,\"linkFormValueFieldCompt\":\"\",\"linkFormIdEnc\":\"\",\"linkFormValueFieldId\":0,\"linked\":false,\"currFormCondFieldId\":0},\"compt\":\"selectbox\",\"layoutRatio\":1,\"optionScoreShow\":false,\"optionScoreUsed\":false,\"otherAllowed\":false,\"alias\":1,\"latestValShow\":false,\"id\":1,\"optionsLoadFromUrl\":{\"isLoadFromUrl\":false,\"response\":[],\"url\":[],\"urlHeaders\":[]},\"optionColor\":false,\"fields\":%s,\"optionBindInfo\":{\"bindFormIdEnc\":\"\",\"bindFieldId\":0,\"bindFieldIdx\":-1,\"isBinded\":false,\"bindFormId\":0,\"originalOptions\":[{\"idArr\":[],\"score\":0,\"checked\":false,\"title\":\"选项1\"},{\"idArr\":[],\"score\":0,\"checked\":false,\"title\":\"选项2\"},{\"idArr\":[],\"score\":0,\"checked\":false,\"title\":\"选项3\"}]},\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":true,\"hasAuthority\":true},{\"linkInfo\":{\"linkFormCondFieldId\":0,\"linkFormType\":\"\",\"condFields\":[],\"linkFormId\":0,\"linkFormValueFieldCompt\":\"\",\"linkFormIdEnc\":\"\",\"linkFormValueFieldId\":0,\"linked\":false,\"currFormCondFieldId\":0},\"compt\":\"numberinput\",\"layoutRatio\":1,\"alias\":4,\"formula\":{\"selIndex\":-1,\"calculateFieldId\":0,\"status\":false},\"latestValShow\":false,\"id\":4,\"fields\":[{\"hasDefaultValue\":false,\"visible\":true,\"values\":[{\"val\":\"35.5\"}],\"percentage\":false,\"verify\":{\"minValue\":{\"errMsg\":\"输入的值不能小于\",\"range\":\"35.0\"},\"realNumber\":{\"isInteger\":false,\"precisionLen\":\"\",\"precision\":\"1\",\"errMsg\":\"请输入正确的数字类型值\"},\"maxValue\":{\"errMsg\":\"输入的值不能大于\",\"range\":\"40.0\"},\"required\":{}},\"tip\":{\"imgs\":[],\"text\":\"\"},\"label\":\"请输入体温+℃\",\"defaultValueStr\":\"[{\\\"val\\\":\\\"\\\"}]\",\"placeholderMsg\":\"限35.0~40.0,限小数位数1位\",\"fieldType\":{\"type\":\"number\"},\"statsable\":false,\"editable\":true}],\"formulaEdit\":{\"formula\":\"\"},\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":true,\"hasAuthority\":true},{\"compt\":\"radiobutton\",\"otherAllowed\":false,\"comptCombined\":true,\"layoutRatio\":1,\"alias\":6,\"latestValShow\":false,\"showType\":0,\"id\":6,\"optionColor\":true,\"fields\":[{\"hasDefaultValue\":false,\"visible\":true,\"editable\":true,\"values\":[{\"val\":\"绿码\",\"isOther\":false}],\"options\":[{\"idArr\":[],\"score\":0,\"color\":\"\",\"checked\":true,\"className\":\"color6\",\"title\":\"绿码\"},{\"idArr\":[],\"score\":0,\"color\":\"\",\"checked\":false,\"className\":\"color4\",\"title\":\"黄码\"},{\"idArr\":[],\"score\":0,\"color\":\"\",\"checked\":false,\"className\":\"color2\",\"title\":\"红码\"}],\"verify\":{\"required\":{}},\"tip\":{\"imgs\":[],\"text\":\"\"},\"label\":\"健康码状态\",\"defaultValueStr\":\"[]\",\"fieldType\":{\"type\":\"string\"}}],\"optionScoreShow\":false,\"optionScoreUsed\":false,\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":true,\"hasAuthority\":true},{\"compt\":\"radiobutton\",\"otherAllowed\":false,\"comptCombined\":true,\"layoutRatio\":1,\"alias\":9,\"latestValShow\":true,\"showType\":0,\"id\":9,\"optionColor\":true,\"fields\":[{\"hasDefaultValue\":false,\"visible\":true,\"editable\":true,\"values\":[{\"val\":\"否\",\"isOther\":false}],\"options\":[{\"idArr\":[],\"score\":0,\"color\":\"\",\"checked\":true,\"className\":\"color6\",\"title\":\"否\"},{\"idArr\":[],\"score\":0,\"color\":\"\",\"checked\":false,\"className\":\"color2\",\"title\":\"是\"}],\"verify\":{\"required\":{}},\"tip\":{\"imgs\":[],\"text\":\"\"},\"label\":\"是否咳嗽\",\"defaultValueStr\":\"[]\",\"fieldType\":{\"type\":\"string\"}}],\"optionScoreShow\":false,\"optionScoreUsed\":false,\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":true,\"hasAuthority\":true},{\"linkInfo\":{\"linkFormType\":\"customForm\",\"condFields\":[],\"linkFormId\":0,\"linkFormValueFieldCompt\":\"\",\"linkFormIdEnc\":\"\",\"linkFormValueFieldId\":0,\"linked\":false},\"compt\":\"contact\",\"loginUserForValue\":false,\"layoutRatio\":1,\"relationValueConfig\":{\"condFieldId\":0,\"type\":0,\"open\":false},\"alias\":7,\"optionalScope\":{\"options\":\"[{\\\"id\\\":9738,\\\"name\\\":\\\"生物与化学工程学院辅导员\\\"},{\\\"id\\\":9737,\\\"name\\\":\\\"土木与建筑工程学院辅导员\\\"},{\\\"id\\\":9739,\\\"name\\\":\\\"数学与计算机学院辅导员\\\"},{\\\"id\\\":9728,\\\"name\\\":\\\"钒钛学院辅导员\\\"},{\\\"id\\\":9729,\\\"name\\\":\\\"智能制造学院辅导员\\\"},{\\\"id\\\":9730,\\\"name\\\":\\\"经济与管理学院辅导员\\\"},{\\\"id\\\":9732,\\\"name\\\":\\\"医学院辅导员\\\"},{\\\"id\\\":9733,\\\"name\\\":\\\"康养学院辅导员\\\"},{\\\"id\\\":9734,\\\"name\\\":\\\"艺术学院辅导员\\\"},{\\\"id\\\":9735,\\\"name\\\":\\\"外国语学院辅导员\\\"},{\\\"id\\\":12942,\\\"name\\\":\\\"电气信息工程学院辅导员\\\"},{\\\"id\\\":12943,\\\"name\\\":\\\"文学院辅导员\\\"},{\\\"id\\\":12944,\\\"name\\\":\\\"法学院辅导员\\\"}]\",\"type\":3},\"id\":7,\"fields\":%s,\"inDetailGroupIndex\":-1,\"fromDetail\":false,\"isShow\":true,\"hasAuthority\":true}]'</span> <span class="token operator">%</span> <span class="token punctuation">(</span>json<span class="token punctuation">.</span>dumps<span class="token punctuation">(</span>form<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'fields'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span> json<span class="token punctuation">.</span>dumps<span class="token punctuation">(</span>form<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'fields'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span> json<span class="token punctuation">.</span>dumps<span class="token punctuation">(</span>form<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'fields'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span> json<span class="token punctuation">.</span>dumps<span class="token punctuation">(</span>form<span class="token punctuation">[</span><span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'fields'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">'utf-8'</span><span class="token punctuation">)</span> params_c4 <span class="token operator">=</span> <span class="token string">'formId=4824&formAppId=&version=6&formData='</span> <span class="token operator">+</span> params_c4 <span class="token operator">+</span> <span class="token string">'&ext=&t=1&enc=ffc335abbf0c8cee0f943ca0bc82d40a&checkCode=%s&gatherId=0&anonymous=0&uuid=fe458af18987400791de5c2f4303d7cf&uniqueCondition=[]&gverify='</span> <span class="token operator">%</span> <span class="token punctuation">(</span> checkCode<span class="token punctuation">)</span> url <span class="token operator">=</span> api_save4 <span class="token operator">%</span> <span class="token punctuation">(</span>cookies<span class="token punctuation">[</span><span class="token string">'_uid'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> resp <span class="token operator">=</span> requests<span class="token punctuation">.</span>post<span class="token punctuation">(</span>url<span class="token punctuation">,</span> cookies<span class="token operator">=</span>cookies<span class="token punctuation">,</span> params<span class="token operator">=</span>params_c4<span class="token punctuation">)</span> <span class="token keyword">if</span> <span class="token string">'请勿重复提交'</span> <span class="token keyword">in</span> resp<span class="token punctuation">.</span>text <span class="token keyword">or</span> <span class="token string">'表单提交成功'</span> <span class="token keyword">in</span> resp<span class="token punctuation">.</span>text <span class="token keyword">or</span> <span class="token string">'每个用户每日填写2次,您的提交已达上限哦'</span> <span class="token keyword">in</span> resp<span class="token punctuation">.</span>text<span class="token punctuation">:</span> res<span class="token punctuation">[</span><span class="token string">'body'</span><span class="token punctuation">]</span> <span class="token operator">+=</span> <span class="token string">'日常健康监测打卡成功\n'</span> <span class="token keyword">else</span><span class="token punctuation">:</span> res<span class="token punctuation">[</span><span class="token string">'body'</span><span class="token punctuation">]</span> <span class="token operator">+=</span> <span class="token string">'日常健康监测打卡失败\n'</span> <span class="token operator">+</span> resp<span class="token punctuation">.</span>text <span class="token operator">+</span> <span class="token string">'\n'</span> res<span class="token punctuation">[</span><span class="token string">'status'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">False</span><span class="token comment"># server酱打卡失败通知</span><span class="token keyword">def</span> <span class="token function">server</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> api <span class="token operator">=</span> <span class="token string">"https://sc.ftqq.com/%s"</span> <span class="token operator">%</span> <span class="token punctuation">(</span>sendkey<span class="token punctuation">)</span> title <span class="token operator">=</span> params_login<span class="token punctuation">[</span><span class="token string">'uname'</span><span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token string">'打卡失败'</span> content <span class="token operator">=</span> res<span class="token punctuation">[</span><span class="token string">'body'</span><span class="token punctuation">]</span> data <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">"text"</span><span class="token punctuation">:</span> title<span class="token punctuation">,</span> <span class="token string">"desp"</span><span class="token punctuation">:</span> content<span class="token punctuation">}</span> requests<span class="token punctuation">.</span>post<span class="token punctuation">(</span>api<span class="token punctuation">,</span> data<span class="token operator">=</span>data<span class="token punctuation">)</span><span class="token comment"># 云函数入口</span><span class="token keyword">def</span> <span class="token function">main_handler</span><span class="token punctuation">(</span>event<span class="token punctuation">,</span> context<span class="token punctuation">)</span><span class="token punctuation">:</span> res<span class="token punctuation">[</span><span class="token string">'body'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">''</span> cookies <span class="token operator">=</span> login<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">if</span> cookies <span class="token keyword">is</span> <span class="token boolean">False</span><span class="token punctuation">:</span> res<span class="token punctuation">[</span><span class="token string">'status'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">False</span> <span class="token keyword">return</span> res <span class="token keyword">try</span><span class="token punctuation">:</span> checkin3<span class="token punctuation">(</span>cookies<span class="token punctuation">)</span> <span class="token keyword">except</span> BaseException<span class="token punctuation">:</span> res<span class="token punctuation">[</span><span class="token string">'body'</span><span class="token punctuation">]</span> <span class="token operator">+=</span> <span class="token string">'每日健康上报打卡出错\n'</span> <span class="token keyword">try</span><span class="token punctuation">:</span> checkin4<span class="token punctuation">(</span>cookies<span class="token punctuation">)</span> <span class="token keyword">except</span> BaseException<span class="token punctuation">:</span> res<span class="token punctuation">[</span><span class="token string">'body'</span><span class="token punctuation">]</span> <span class="token operator">+=</span> <span class="token string">'日常健康检测打卡出错\n'</span> <span class="token keyword">if</span> res<span class="token punctuation">[</span><span class="token string">'status'</span><span class="token punctuation">]</span> <span class="token keyword">is</span> <span class="token boolean">False</span><span class="token punctuation">:</span> server<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">return</span> res<span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">'__main__'</span><span class="token punctuation">:</span> main_handler<span class="token punctuation">(</span><span class="token boolean">None</span><span class="token punctuation">,</span> <span class="token boolean">None</span><span class="token punctuation">)</span> <span class="token keyword">print</span><span class="token punctuation">(</span>res<span class="token punctuation">[</span><span class="token string">'body'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><p>分割线</p><hr><p>以下为分析和历史代码,仅供参考</p><h1 id="20220817二更"><a href="#20220817二更" class="headerlink" title="20220817二更"></a>20220817二更</h1><p>改为从提交记录中获取信息,故只需要填写账号密码即可(之前的两个模块没有修改)</p><h1 id="20220817一更"><a href="#20220817一更" class="headerlink" title="20220817一更"></a>20220817一更</h1><p><mark>昨天晚上通知多了个“每日健康上报”</mark></p><p>代码已改,需要填写更多个人信息,签名那个地址需要手动抓包提取(应该可以自动从历史记录中获取,但是没时间写了)</p><h1 id="20220213更正"><a href="#20220213更正" class="headerlink" title="20220213更正"></a>20220213更正</h1><p>昨天晚上测试的时候<code>学生健康登记</code>这个模块在首页是没有显示的,当时我以为不用打卡这个模块,只需要定位签到就行了,没想到今天它又回来了(佛了。</p><p>解决这个问题只需要将注释的代码取消注释就可以了(下面代码已改</p><p>直接用我这个代码的同学需要注意</p><ul><li>代码前面需要填写账号密码和地址(这个地址是用来定位打卡的</li><li>学生健康登记打卡里面的formData中也有内容需要自己修改,我把需要改动的地方用‘📌’这个标签括起来了,这样比较显眼😂</li><li>formData内容比较多,建议复制代码到本地,然后在编辑器中打开自动换行再修改(不然比较伤眼</li></ul><h1 id="分析打卡流程"><a href="#分析打卡流程" class="headerlink" title="分析打卡流程"></a>分析打卡流程</h1><p>要自动打卡应该分为两个步骤</p><ol><li>登录</li><li>打卡</li></ol><p>平时打卡都是在学习通APP上进行的,所以直接在手机抓包分析。</p><h2 id="登录流程"><a href="#登录流程" class="headerlink" title="登录流程"></a>登录流程</h2><p>APP到登录页面,然后打开抓包,然后登录,分析抓取到的请求,在众多请求包中一眼看到这个域名是passport开头的,点进来一看,好家伙,设置了13个cookie,多半是这个api没错了,再确认一下,看看请求内容,带了账号密码,那就是这个包在做登录操作了。</p><p><img src="https://cdn.jsdelivr.net/gh/oopstls/PicBed/blog/XXT_check-in/image-20210211110139434.png" alt="image-20210211110139434"></p><p><img src="https://cdn.jsdelivr.net/gh/oopstls/PicBed/blog/XXT_check-in/image-20210211105918543.png" alt="image-20210211105918543"></p><p>测试看看,把这个api拿到浏览器上试试,验证通过,那没问题了。</p><p><img src="https://cdn.jsdelivr.net/gh/oopstls/PicBed/blog/XXT_check-in/image-20210211110359697.png" alt="image-20210211110359697"></p><p><img src="https://cdn.jsdelivr.net/gh/oopstls/PicBed/blog/XXT_check-in/image-20210211110509172.png" alt="image-20210211110509172"></p><p>同时在刚刚手机上抓到的包中找到了学习通首页的url,后面的操作就可以在电脑上完成了,体验+。</p><p><img src="https://cdn.jsdelivr.net/gh/oopstls/PicBed/blog/XXT_check-in/image-20210212013902597.png" alt="image-20210212013902597"></p><p>再然后就是一步一步的模拟打卡过程(其实就用浏览器的f12里面的network功能就行了</p><p>整个打卡过程貌似有四次post请求。</p><p>从域名上看😂😂</p><ul><li>第一个是获取表单内容的(后面发现同时还设置了一个CheckCode来控制每天打卡一次</li><li>第二个是用来计数的(并不知道计数什么,但是貌似不影响</li><li>第三个是用来校验填写的内容是否合法的(不用校验也能进行第四步</li><li>第四个是保存我们填写的信息的</li></ul><p><img src="https://cdn.jsdelivr.net/gh/oopstls/PicBed/blog/XXT_check-in/image-20210212015254019.png" alt="image-20210212015254019"></p><p><img src="https://cdn.jsdelivr.net/gh/oopstls/PicBed/blog/XXT_check-in/image-20210212014858781.png" alt="image-20210212014858781"></p><p><img src="https://cdn.jsdelivr.net/gh/oopstls/PicBed/blog/XXT_check-in/image-20210212015405610.png" alt="image-20210212015405610"></p><p><img src="https://cdn.jsdelivr.net/gh/oopstls/PicBed/blog/XXT_check-in/image-20210212015539797.png" alt="image-20210212015539797"></p><p>经过一些测试,得出结论,我们只需要访问第一个api获取checkCode,然后用这个checkCode去第四个api保存我们的打卡信息就可以了。</p><h1 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h1><div class="code-wrapper"><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token comment"># -*- coding: utf8 -*-</span><span class="token keyword">import</span> requests<span class="token keyword">import</span> re<span class="token keyword">import</span> json<span class="token keyword">from</span> urllib<span class="token punctuation">.</span>parse <span class="token keyword">import</span> quoteparams_login <span class="token operator">=</span> <span class="token punctuation">{</span> <span class="token string">'uname'</span><span class="token punctuation">:</span> <span class="token string">'账号'</span><span class="token punctuation">,</span> <span class="token string">'code'</span><span class="token punctuation">:</span> <span class="token string">'密码'</span><span class="token punctuation">,</span> <span class="token string">'loginType'</span><span class="token punctuation">:</span> <span class="token string">'1'</span><span class="token punctuation">,</span> <span class="token string">'roleSelect'</span><span class="token punctuation">:</span> <span class="token string">'true'</span><span class="token punctuation">}</span>api_save <span class="token operator">=</span> <span class="token string">'http://office.chaoxing.com/data/apps/forms/fore/user/save?lookuid=%s'</span>api_login <span class="token operator">=</span> <span class="token string">'https://passport2-api.chaoxing.com/v11/loginregister?cx_xxt_passport=json'</span>api_apply <span class="token operator">=</span> <span class="token string">'http://office.chaoxing.com/front/web/apps/forms/fore/apply?uid=83260789&code=2iVkZKN1&mappId=3820629&appId=92610a6cf5e6415c8b4c2ac9c6a9a0f1&appKey=DhXDEM4W331834H2&id=1651&enc=3e98bd89c8ce6bcabf08dbad8553f3c1&state=1432&formAppId=&fidEnc=49b6a032fb2a0f2b'</span>api_last <span class="token operator">=</span> <span class="token string">'http://office.chaoxing.com/data/apps/forms/fore/user/list?cpage=1&formId=92597&enc=e8a70d13bac504a2ec42ad5d72aa8abd&formAppId='</span><span class="token keyword">def</span> <span class="token function">login</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> resp <span class="token operator">=</span> requests<span class="token punctuation">.</span>post<span class="token punctuation">(</span>api_login<span class="token punctuation">,</span> params<span class="token operator">=</span>params_login<span class="token punctuation">)</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>resp<span class="token punctuation">.</span>status_code <span class="token operator">==</span> <span class="token number">403</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">raise</span> Exception<span class="token punctuation">(</span><span class="token string">'403登录失败'</span><span class="token punctuation">)</span> <span class="token keyword">return</span> resp<span class="token punctuation">.</span>cookies<span class="token keyword">def</span> <span class="token function">get_checkCode</span><span class="token punctuation">(</span>resp<span class="token punctuation">)</span><span class="token punctuation">:</span> code <span class="token operator">=</span> re<span class="token punctuation">.</span>findall<span class="token punctuation">(</span><span class="token string">r"checkCode.*'(.*)'"</span><span class="token punctuation">,</span> resp<span class="token punctuation">.</span>text<span class="token punctuation">)</span> <span class="token keyword">if</span> code<span class="token punctuation">:</span> <span class="token keyword">return</span> code<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token keyword">else</span><span class="token punctuation">:</span> <span class="token keyword">raise</span> Exception<span class="token punctuation">(</span><span class="token string">"checkCode解析失败"</span><span class="token punctuation">)</span><span class="token comment"># 学生健康登记</span><span class="token comment"># 已弃用</span><span class="token keyword">def</span> <span class="token function">checkin1</span><span class="token punctuation">(</span>cookies<span class="token punctuation">)</span><span class="token punctuation">:</span> checkCode <span class="token operator">=</span> get_checkCode<span class="token punctuation">(</span>requests<span class="token punctuation">.</span>get<span class="token punctuation">(</span>api_apply<span class="token punctuation">,</span> cookies<span class="token operator">=</span>cookies<span class="token punctuation">)</span><span class="token punctuation">)</span> params_c1 <span class="token operator">=</span> <span class="token punctuation">{</span> <span class="token string">'formId'</span><span class="token punctuation">:</span> <span class="token string">'1651'</span><span class="token punctuation">,</span> <span class="token string">'formAppId'</span><span class="token punctuation">:</span> <span class="token string">''</span><span class="token punctuation">,</span> <span class="token string">'version'</span><span class="token punctuation">:</span> <span class="token string">'2'</span><span class="token punctuation">,</span> <span class="token string">'formData'</span><span class="token punctuation">:</span> <span class="token string">'[{"linkInfo":{"linkFormType":"","condFields":[],"linkFormId":0,"linkFormValueFieldCompt":"","linkFormIdEnc":"","linkFormValueFieldId":0,"linked":false,"currFormCondFieldId":0},"compt":"selectbox","otherAllowed":false,"layoutRatio":1,"latestValShow":true,"id":13,"optionsLoadFromUrl":{"isLoadFromUrl":false,"response":[],"url":[],"urlHeaders":[]},"fields":[{"visible":true,"editable":true,"values":[{"val":"数学与计算机学院"}],"options":[{"idArr":[],"checked":false,"title":"钒钛学院"},{"idArr":[],"checked":false,"title":"经济与管理学院"},{"idArr":[],"checked":false,"title":"交通与汽车工程学院"},{"idArr":[],"checked":false,"title":"康养学院"},{"idArr":[],"checked":false,"title":"人文社科学院"},{"idArr":[],"checked":false,"title":"生物与化学工程学院"},{"idArr":[],"checked":true,"title":"数学与计算机学院"},{"idArr":[],"checked":false,"title":"土木与建筑工程学院"},{"idArr":[],"checked":false,"title":"外国语学院"},{"idArr":[],"checked":false,"title":"艺术学院"},{"idArr":[],"checked":false,"title":"医学院"},{"idArr":[],"checked":false,"title":"智能制造学院"},{"idArr":[],"checked":false,"title":"研究生处"}],"verify":{"required":{}},"tip":{"imgs":[],"text":""},"label":"二级学院","fieldType":{"type":"string"}}],"optionScoreShow":false,"optionScoreUsed":false,"optionBindInfo":{"bindFieldId":0,"bindFieldIdx":-1,"isBinded":false,"bindFormId":0,"originalOptions":[{"idArr":[],"checked":false,"title":"选项1"},{"idArr":[],"checked":false,"title":"选项2"},{"idArr":[],"checked":false,"title":"选项3"}]},"inDetailGroupIndex":-1,"fromDetail":false,"isShow":true,"hasAuthority":true},{"linkInfo":{"linkFormType":"","condFields":[],"linkFormId":0,"linkFormValueFieldCompt":"","linkFormIdEnc":"","linkFormValueFieldId":0,"linked":false,"currFormCondFieldId":0},"compt":"editinput","layoutRatio":1,"formula":{"selIndex":-1,"calculateFieldId":0,"status":false},"latestValShow":false,"id":1,"fields":[{"visible":true,"values":[{"val":"2018级计算机科学与技术1班"}],"verify":{"unique":{"errMsg":"此项内容已存在,不允许重复提交","open":false},"format":{"type":""},"required":{}},"tip":{"imgs":[],"text":""},"label":"年级、专业、班级","sweepCode":false,"fieldType":{"type":"string"},"codeChangeable":false,"editable":true}],"inDetailGroupIndex":-1,"fromDetail":false,"isShow":true,"hasAuthority":true},{"compt":"radiobutton","otherAllowed":false,"comptCombined":true,"layoutRatio":1,"latestValShow":true,"id":2,"fields":[{"visible":true,"values":[{"val":"健康","isOther":false}],"options":[{"idArr":[],"checked":true,"title":"健康"},{"idArr":[11],"checked":false,"title":"有发烧/咳嗽等症状"},{"idArr":[],"checked":false,"title":"其他"}],"verify":{"required":{}},"tip":{"imgs":[],"text":""},"label":"健康状况","fieldType":{"type":"string"},"editable":true}],"optionScoreShow":false,"optionScoreUsed":false,"inDetailGroupIndex":-1,"fromDetail":false,"isShow":true,"hasAuthority":true},{"linkInfo":{"linkFormType":"","condFields":[],"linkFormId":0,"linkFormValueFieldCompt":"","linkFormIdEnc":"","linkFormValueFieldId":0,"linked":false,"currFormCondFieldId":0},"compt":"editinput","layoutRatio":1,"formula":{"selIndex":-1,"calculateFieldId":0,"status":false},"latestValShow":false,"id":3,"fields":[{"visible":true,"values":[{"val":"四川省成都市新都区"}],"verify":{"unique":{"errMsg":"此项内容已存在,不允许重复提交","open":false},"format":{"type":""},"required":{}},"tip":{"imgs":[],"text":""},"label":"当前所在省、市、县(区)","sweepCode":false,"fieldType":{"type":"string"},"codeChangeable":false,"editable":true}],"inDetailGroupIndex":-1,"fromDetail":false,"isShow":true,"hasAuthority":true},{"compt":"radiobutton","otherAllowed":false,"comptCombined":true,"layoutRatio":1,"latestValShow":true,"id":14,"fields":[{"visible":true,"values":[{"val":"否","isOther":false}],"options":[{"idArr":[11],"checked":false,"title":"是"},{"idArr":[],"checked":true,"title":"否"}],"verify":{"required":{}},"tip":{"imgs":[],"text":""},"label":"14天内是否曾前往疫情防控重点区域","fieldType":{"type":"string"},"editable":true}],"optionScoreShow":false,"optionScoreUsed":false,"inDetailGroupIndex":-1,"fromDetail":false,"isShow":true,"hasAuthority":true},{"compt":"radiobutton","otherAllowed":false,"comptCombined":true,"layoutRatio":1,"latestValShow":true,"id":4,"fields":[{"visible":true,"values":[{"val":"否","isOther":false}],"options":[{"idArr":[11],"checked":false,"title":"是"},{"idArr":[],"checked":true,"title":"否"}],"verify":{"required":{}},"tip":{"imgs":[],"text":""},"label":"14天内是否有与高危人员有密切接触历史","fieldType":{"type":"string"},"editable":true}],"optionScoreShow":false,"optionScoreUsed":false,"inDetailGroupIndex":-1,"fromDetail":false,"isShow":true,"hasAuthority":true},{"compt":"radiobutton","otherAllowed":false,"comptCombined":true,"layoutRatio":1,"latestValShow":true,"id":5,"fields":[{"visible":true,"values":[{"val":"否","isOther":false}],"options":[{"idArr":[11],"checked":false,"title":"是"},{"idArr":[],"checked":true,"title":"否"}],"verify":{"required":{}},"tip":{"imgs":[],"text":""},"label":"家中或亲人是否有感染者或疑似感染者","fieldType":{"type":"string"},"editable":true}],"optionScoreShow":false,"optionScoreUsed":false,"inDetailGroupIndex":-1,"fromDetail":false,"isShow":true,"hasAuthority":true},{"compt":"radiobutton","otherAllowed":false,"comptCombined":true,"layoutRatio":1,"latestValShow":true,"id":6,"fields":[{"visible":true,"values":[{"val":"否","isOther":false}],"options":[{"idArr":[11,7,8],"checked":false,"title":"是"},{"idArr":[],"checked":true,"title":"否"}],"verify":{"required":{}},"tip":{"imgs":[],"text":""},"label":"自己是否为疑似病例","fieldType":{"type":"string"},"editable":true}],"optionScoreShow":false,"optionScoreUsed":false,"inDetailGroupIndex":-1,"fromDetail":false,"isShow":true,"hasAuthority":true},{"compt":"radiobutton","otherAllowed":false,"comptCombined":true,"layoutRatio":1,"latestValShow":true,"id":7,"fields":[{"visible":true,"values":[],"options":[{"idArr":[11],"checked":false,"title":"是"},{"idArr":[],"checked":false,"title":"否"}],"verify":{},"tip":{"imgs":[],"text":""},"label":"是否被隔离","fieldType":{"type":"string"},"editable":true}],"optionScoreShow":false,"optionScoreUsed":false,"inDetailGroupIndex":-1,"fromDetail":false,"isShow":false,"hasAuthority":true},{"linkInfo":{"linkFormType":"","condFields":[],"linkFormId":0,"linkFormValueFieldCompt":"","linkFormIdEnc":"","linkFormValueFieldId":0,"linked":false,"currFormCondFieldId":0},"compt":"editinput","layoutRatio":1,"formula":{"selIndex":-1,"calculateFieldId":0,"status":false},"latestValShow":false,"id":8,"fields":[{"visible":true,"values":[{"val":""}],"verify":{"unique":{"errMsg":"此项内容已存在,不允许重复提交","open":false},"format":{"type":""}},"tip":{"imgs":[],"text":""},"label":"隔离地点","sweepCode":false,"fieldType":{"type":"string"},"codeChangeable":false,"editable":true}],"inDetailGroupIndex":-1,"fromDetail":false,"isShow":false,"hasAuthority":true},{"compt":"radiobutton","otherAllowed":false,"comptCombined":true,"layoutRatio":1,"latestValShow":true,"id":9,"fields":[{"visible":true,"values":[{"val":"否","isOther":false}],"options":[{"idArr":[11,10],"checked":false,"title":"是"},{"idArr":[],"checked":true,"title":"否"}],"verify":{"required":{}},"tip":{"imgs":[],"text":""},"label":"是否已确诊病例","fieldType":{"type":"string"},"editable":true}],"optionScoreShow":false,"optionScoreUsed":false,"inDetailGroupIndex":-1,"fromDetail":false,"isShow":true,"hasAuthority":true},{"linkInfo":{"linkFormType":"","condFields":[],"linkFormId":0,"linkFormValueFieldCompt":"","linkFormIdEnc":"","linkFormValueFieldId":0,"linked":false,"currFormCondFieldId":0},"compt":"editinput","layoutRatio":1,"formula":{"selIndex":-1,"calculateFieldId":0,"status":false},"latestValShow":false,"id":10,"fields":[{"visible":true,"values":[{"val":""}],"verify":{"unique":{"errMsg":"此项内容已存在,不允许重复提交","open":false},"format":{"type":""}},"tip":{"imgs":[],"text":""},"label":"住院地点","sweepCode":false,"fieldType":{"type":"string"},"codeChangeable":false,"editable":true}],"inDetailGroupIndex":-1,"fromDetail":false,"isShow":false,"hasAuthority":true},{"linkInfo":{"linkFormType":"","condFields":[],"linkFormId":0,"linkFormValueFieldCompt":"","linkFormIdEnc":"","linkFormValueFieldId":0,"linked":false,"currFormCondFieldId":0},"compt":"edittextarea","layoutRatio":1,"latestValShow":false,"id":11,"fields":[{"visible":true,"values":[{"val":""}],"verify":{},"tip":{"imgs":[],"text":""},"label":"需要说明的情况","fieldType":{"type":"string"},"editable":true}],"inDetailGroupIndex":-1,"fromDetail":false,"isShow":false,"hasAuthority":true}]'</span><span class="token punctuation">,</span> <span class="token string">'uniqueCondition'</span><span class="token punctuation">:</span> <span class="token string">'[]'</span><span class="token punctuation">,</span> <span class="token string">'t'</span><span class="token punctuation">:</span> <span class="token string">'1'</span><span class="token punctuation">,</span> <span class="token string">'enc'</span><span class="token punctuation">:</span> <span class="token string">'3e98bd89c8ce6bcabf08dbad8553f3c1'</span><span class="token punctuation">,</span> <span class="token string">'checkCode'</span><span class="token punctuation">:</span> checkCode<span class="token punctuation">,</span> <span class="token string">'gatherId'</span><span class="token punctuation">:</span> <span class="token string">'0'</span><span class="token punctuation">,</span> <span class="token string">'gverify'</span><span class="token punctuation">:</span> <span class="token string">''</span> <span class="token punctuation">}</span> url <span class="token operator">=</span> api_save <span class="token operator">%</span> <span class="token punctuation">(</span>cookies<span class="token punctuation">[</span><span class="token string">'_uid'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> resp <span class="token operator">=</span> requests<span class="token punctuation">.</span>post<span class="token punctuation">(</span>url<span class="token punctuation">,</span> params<span class="token operator">=</span>params_c1<span class="token punctuation">,</span> cookies<span class="token operator">=</span>cookies<span class="token punctuation">)</span> <span class="token keyword">if</span> <span class="token string">'每个用户每日填写1次,您的提交已达上限哦~'</span> <span class="token keyword">in</span> resp<span class="token punctuation">.</span>text <span class="token keyword">or</span> <span class="token string">'"success":true'</span> <span class="token keyword">in</span> resp<span class="token punctuation">.</span>text<span class="token punctuation">:</span> <span class="token keyword">return</span> <span class="token string">'健康登记打卡成功或已打卡过'</span> <span class="token keyword">else</span><span class="token punctuation">:</span> <span class="token keyword">print</span><span class="token punctuation">(</span>resp<span class="token punctuation">.</span>text<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token string">'健康登记打卡失败'</span><span class="token comment"># 定位打卡</span><span class="token comment"># 已弃用</span><span class="token keyword">def</span> <span class="token function">checkin2</span><span class="token punctuation">(</span>cookies<span class="token punctuation">)</span><span class="token punctuation">:</span> checkCode <span class="token operator">=</span> get_checkCode<span class="token punctuation">(</span>requests<span class="token punctuation">.</span>get<span class="token punctuation">(</span>api_apply<span class="token punctuation">,</span> cookies<span class="token operator">=</span>cookies<span class="token punctuation">)</span><span class="token punctuation">)</span> params_c2 <span class="token operator">=</span> <span class="token punctuation">{</span> <span class="token string">'formId'</span><span class="token punctuation">:</span> <span class="token string">'8273'</span><span class="token punctuation">,</span> <span class="token string">'formAppId'</span><span class="token punctuation">:</span> <span class="token string">''</span><span class="token punctuation">,</span> <span class="token string">'version'</span><span class="token punctuation">:</span> <span class="token string">'1'</span><span class="token punctuation">,</span> <span class="token string">'formData'</span><span class="token punctuation">:</span> <span class="token string">'[{"compt":"location","id":1,"fields":[{"verify":{},"tip":{"imgs":[],"text":""},"label":"定位","fieldType":{"type":"point"},"editable":true,"visible":true,"values":[{"address":"%s","lng":104.083898,"lat":30.759099}]}],"inDetailGroupIndex":-1,"fromDetail":false,"isShow":true,"hasAuthority":true}]'</span> <span class="token operator">%</span> <span class="token punctuation">(</span>address<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">'uniqueCondition'</span><span class="token punctuation">:</span> <span class="token string">'[]'</span><span class="token punctuation">,</span> <span class="token string">'t'</span><span class="token punctuation">:</span> <span class="token string">'1'</span><span class="token punctuation">,</span> <span class="token string">'enc'</span><span class="token punctuation">:</span> <span class="token string">'61d91ce3a502d4f32332ed15d8c42e33'</span><span class="token punctuation">,</span> <span class="token string">'checkCode'</span><span class="token punctuation">:</span> checkCode<span class="token punctuation">,</span> <span class="token string">'gatherId'</span><span class="token punctuation">:</span> <span class="token string">'0'</span><span class="token punctuation">,</span> <span class="token string">'gverify'</span><span class="token punctuation">:</span> <span class="token string">''</span> <span class="token punctuation">}</span> url <span class="token operator">=</span> api_save <span class="token operator">%</span> <span class="token punctuation">(</span>cookies<span class="token punctuation">[</span><span class="token string">'_uid'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> resp <span class="token operator">=</span> requests<span class="token punctuation">.</span>post<span class="token punctuation">(</span>url<span class="token punctuation">,</span> params<span class="token operator">=</span>params_c2<span class="token punctuation">,</span> cookies<span class="token operator">=</span>cookies<span class="token punctuation">)</span> <span class="token keyword">if</span> <span class="token string">'每个用户每日填写1次,您的提交已达上限哦~'</span> <span class="token keyword">in</span> resp<span class="token punctuation">.</span>text <span class="token keyword">or</span> <span class="token string">'"success":true'</span> <span class="token keyword">in</span> resp<span class="token punctuation">.</span>text<span class="token punctuation">:</span> <span class="token keyword">return</span> <span class="token string">'定位打卡成功或已打卡过'</span> <span class="token keyword">else</span><span class="token punctuation">:</span> <span class="token keyword">print</span><span class="token punctuation">(</span>resp<span class="token punctuation">.</span>text<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token string">'定位打卡失败'</span><span class="token comment"># 每日健康上报</span><span class="token keyword">def</span> <span class="token function">checkin3</span><span class="token punctuation">(</span>cookies<span class="token punctuation">)</span><span class="token punctuation">:</span> checkCode <span class="token operator">=</span> get_checkCode<span class="token punctuation">(</span>requests<span class="token punctuation">.</span>get<span class="token punctuation">(</span>api_apply<span class="token punctuation">,</span> cookies<span class="token operator">=</span>cookies<span class="token punctuation">)</span><span class="token punctuation">)</span> form <span class="token operator">=</span> json<span class="token punctuation">.</span>loads<span class="token punctuation">(</span>requests<span class="token punctuation">.</span>post<span class="token punctuation">(</span> api_last<span class="token punctuation">,</span> cookies<span class="token operator">=</span>cookies<span class="token punctuation">)</span><span class="token punctuation">.</span>text<span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token string">'data'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'formsUserList'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'formData'</span><span class="token punctuation">]</span> params_c3 <span class="token operator">=</span> quote<span class="token punctuation">(</span> <span class="token string">'[{"compt":"captiontext","layoutRatio":1,"alias":"1","id":1,"fields":[{"visible":true,"editable":false,"tip":{"imgs":[],"text":"<p style=\\"text-align: center;\\"><span style=\\"font-size: x-large;\\">学生基本信息</span></p><p style=\\"text-align: center;\\"><span style=\\"font-size: medium;\\">以下信息涉及公共安全,要求每位学生如实填报</span></p>"},"label":"说明文字","fieldType":{"type":"string"}}],"inDetailGroupIndex":-1,"fromDetail":false,"isShow":true,"hasAuthority":false},{"linkInfo":{"condFields":[],"linkFormType":"","linkFormId":0,"linkFormValueFieldCompt":"","linkFormIdEnc":"","linkFormValueFieldId":0,"linked":false},"compt":"contact","loginUserForValue":true,"layoutRatio":1,"alias":"44","id":44,"fields":[{"hasDefaultValue":true,"visible":true,"editable":false,"values":[{"puid":%s,"uname":"%s"}],"name":"点击选择人员","verify":{"required":{}},"tip":{"imgs":[],"text":""},"defaultValueStr":"[]","label":"学生姓名","sweepCode":false,"fieldType":{"multiple":false,"type":"contact"}}],"inDetailGroupIndex":-1,"fromDetail":false,"isShow":true,"hasAuthority":true},{"linkInfo":{"condFields":[],"linkFormType":"","linkFormId":0,"linkFormValueFieldCompt":"","linkFormIdEnc":"","linkFormValueFieldId":0,"linked":false},"compt":"editinput","layoutRatio":1,"alias":"45","formula":{"selIndex":-1,"calculateFieldId":0,"status":false},"latestValShow":false,"id":45,"fields":[{"hasDefaultValue":false,"visible":true,"editable":true,"values":[{"val":"%s"}],"verify":{"charLimit":{"size":20,"open":false},"regularExpress":{"errorTip":"格式错误!","express":""},"unique":{"errMsg":"此项内容已存在,不允许重复提交","open":false},"format":{"type":""},"required":{}},"tip":{"imgs":[],"text":""},"defaultValueStr":"[{\\"val\\":\\"\\"}]","label":"学号","sweepCode":false,"fieldType":{"type":"string"},"codeChangeable":false}],"inDetailGroupIndex":-1,"fromDetail":false,"isShow":true,"hasAuthority":true},{"linkInfo":{"condFields":[],"linkFormType":"","linkFormId":0,"linkFormValueFieldCompt":"","linkFormIdEnc":"","linkFormValueFieldId":0,"linked":false},"compt":"editinput","layoutRatio":1,"alias":"46","formula":{"selIndex":-1,"calculateFieldId":0,"status":false},"latestValShow":false,"id":46,"fields":[{"hasDefaultValue":false,"visible":true,"editable":true,"values":[{"val":"%s"}],"verify":{"charLimit":{"size":20,"open":false},"regularExpress":{"errorTip":"格式错误!","express":""},"unique":{"errMsg":"此项内容已存在,不允许重复提交","open":false},"format":{"type":""},"required":{}},"tip":{"imgs":[],"text":""},"defaultValueStr":"[{\\"val\\":\\"\\"}]","label":"学院","sweepCode":false,"fieldType":{"type":"string"},"codeChangeable":false}],"inDetailGroupIndex":-1,"fromDetail":false,"isShow":true,"hasAuthority":true},{"linkInfo":{"condFields":[],"linkFormType":"","linkFormId":0,"linkFormValueFieldCompt":"","linkFormIdEnc":"","linkFormValueFieldId":0,"linked":false},"compt":"editinput","layoutRatio":1,"alias":"48","formula":{"selIndex":-1,"calculateFieldId":0,"status":false},"latestValShow":false,"id":48,"fields":[{"hasDefaultValue":false,"visible":true,"editable":true,"values":[{"val":"%s"}],"verify":{"charLimit":{"size":20,"open":false},"regularExpress":{"errorTip":"格式错误!","express":""},"unique":{"errMsg":"此项内容已存在,不允许重复提交","open":false},"format":{"type":""},"required":{}},"tip":{"imgs":[],"text":""},"defaultValueStr":"[{\\"val\\":\\"\\"}]","label":"专业班级","sweepCode":false,"fieldType":{"type":"string"},"codeChangeable":false}],"inDetailGroupIndex":-1,"fromDetail":false,"isShow":true,"hasAuthority":true},{"linkInfo":{"condFields":[{"linkFormCondFieldId":6,"linkFormCondFieldCompt":"contact","currFormCondFieldId":44}],"linkFormType":"customForm","linkFormId":92598,"linkFormValueFieldCompt":"editinput","linkFormIdEnc":"6490576bf20c15e2b37ff7e6b44cf382","linkFormValueFieldId":7,"linked":true,"currFormValueFieldId":3,"currFormValueFieldCompt":"editinput"},"compt":"editinput","layoutRatio":1,"alias":"3","formula":{"selIndex":-1,"calculateFieldId":"0","status":false},"latestValShow":false,"id":3,"fields":[{"hasDefaultValue":false,"visible":true,"editable":true,"values":[{"val":"%s"}],"verify":{"charLimit":{"size":20,"open":false},"regularExpress":{"errorTip":"格式错误!","express":""},"unique":{"errMsg":"此项内容已存在,不允许重复提交","open":false},"format":{"type":""},"required":{}},"tip":{"imgs":[],"text":""},"defaultValueStr":"[{\\"val\\":\\"\\"}]","label":"身份证号","sweepCode":false,"fieldType":{"type":"string"},"codeChangeable":false}],"inDetailGroupIndex":-1,"fromDetail":false,"isShow":true,"hasAuthority":true},{"linkInfo":{"condFields":[{"linkFormCondFieldId":6,"linkFormCondFieldCompt":"contact","currFormCondFieldId":44}],"linkFormType":"customForm","linkFormId":92598,"linkFormValueFieldCompt":"editinput","linkFormIdEnc":"6490576bf20c15e2b37ff7e6b44cf382","linkFormValueFieldId":5,"linked":true,"currFormValueFieldId":4,"currFormValueFieldCompt":"editinput"},"compt":"editinput","layoutRatio":1,"alias":"4","formula":{"selIndex":-1,"calculateFieldId":"0","status":false},"latestValShow":false,"id":4,"fields":[{"hasDefaultValue":false,"visible":true,"editable":true,"values":[{"val":"%s"}],"verify":{"charLimit":{"size":20,"open":false},"regularExpress":{"errorTip":"格式错误!","express":""},"unique":{"errMsg":"此项内容已存在,不允许重复提交","open":false},"format":{"type":"mobile"},"required":{}},"tip":{"imgs":[],"text":""},"defaultValueStr":"[{\\"val\\":\\"\\"}]","label":"联系电话","sweepCode":false,"fieldType":{"type":"string"},"codeChangeable":false}],"inDetailGroupIndex":-1,"fromDetail":false,"isShow":true,"hasAuthority":true},{"compt":"location","layoutRatio":1,"alias":"6","distanceRange":100,"id":6,"fields":[{"visible":true,"editable":true,"verify":{"required":{}},"tip":{"imgs":[],"text":""},"label":"当前位置","fieldType":{"type":"point"},"values":[{"address":"%s","lng":%s,"lat":%s}]}],"inDetailGroupIndex":-1,"fromDetail":false,"isShow":true,"hasAuthority":true},{"linkInfo":{"condFields":[{"linkFormCondFieldId":1,"linkFormCondFieldCompt":"contact","currFormCondFieldId":44}],"linkFormType":"customForm","linkFormId":92596,"linkFormValueFieldCompt":"numberinput","linkFormIdEnc":"dfd6989f283227cde9e82fee8033771a","linkFormValueFieldId":2,"linked":true,"currFormValueFieldId":58,"currFormValueFieldCompt":"numberinput"},"compt":"numberinput","layoutRatio":1,"alias":"58","latestValShow":false,"id":58,"fields":[{"hasDefaultValue":false,"capital":false,"visible":false,"editable":false,"values":[{"val":""}],"defaultValueStr":"[{\\"val\\":\\"\\"}]","label":"上报次数","placeholderMsg":"","micrometer":false,"statsable":false,"verify":{"minValue":{"errMsg":"输入的值不能小于","range":""},"realNumber":{"isInteger":false,"precision":"","errMsg":"请输入正确的数字类型值"},"maxValue":{"errMsg":"输入的值不能大于","range":""}},"tip":{"imgs":[],"text":"(选填)"},"fieldType":{"type":"number"}}],"inDetailGroupIndex":-1,"fromDetail":false,"isShow":true,"hasAuthority":false},{"compt":"captiontext","layoutRatio":1,"alias":"7","id":7,"fields":[{"visible":true,"editable":false,"tip":{"imgs":[],"text":"<p style=\\"text-align: center;\\"><span style=\\"font-size: x-large;\\">健康状态填写</span></p>"},"label":"说明文字","fieldType":{"type":"string"}}],"inDetailGroupIndex":-1,"fromDetail":false,"isShow":true,"hasAuthority":false},{"compt":"radiobutton","otherAllowed":false,"comptCombined":true,"layoutRatio":1,"alias":"8","latestValShow":true,"id":8,"optionColor":true,"fields":[{"hasDefaultValue":true,"visible":true,"editable":true,"values":[{"val":"否","isOther":false}],"options":[{"idArr":[],"score":0,"color":"","checked":true,"className":"color6","title":"否"},{"idArr":[],"score":0,"color":"","checked":false,"className":"color2","title":"是"}],"verify":{"required":{}},"tip":{"imgs":[],"text":""},"defaultValueStr":"[{\\"val\\":\\"否\\"}]","label":"自测体温是否超过37.3℃","fieldType":{"type":"string"}}],"optionScoreShow":false,"optionScoreUsed":false,"inDetailGroupIndex":-1,"fromDetail":false,"isShow":true,"hasAuthority":true},{"compt":"checklist","otherAllowed":false,"comptCombined":true,"layoutRatio":1,"alias":"9","latestValShow":true,"id":9,"optionColor":false,"fields":[{"hasDefaultValue":true,"visible":true,"editable":true,"values":[{"val":"健康"}],"options":[{"idArr":[],"score":0,"color":"","checked":true,"className":"","title":"健康"},{"idArr":[],"score":0,"color":"","checked":false,"className":"","title":"出现发热、咳嗽、乏力、咽痛、腹泻、呼吸困难"},{"idArr":[10],"score":0,"color":"","checked":false,"className":"","title":"其他身体不适"}],"verify":{"minValue":{"errMsg":"输入的值不能小于","range":"0"},"maxValue":{"errMsg":"输入的值不能大于","range":"0"},"required":{}},"tip":{"imgs":[],"text":""},"defaultValueStr":"[{\\"val\\":\\"健康\\"}]","label":"当前身体状况","fieldType":{"type":"string"}}],"optionScoreShow":false,"optionScoreUsed":false,"inDetailGroupIndex":-1,"fromDetail":false,"isShow":true,"hasAuthority":true},{"linkInfo":{"condFields":[],"linkFormType":"","linkFormId":0,"linkFormValueFieldCompt":"","linkFormIdEnc":"","linkFormValueFieldId":0,"linked":false},"compt":"edittextarea","layoutRatio":1,"alias":"10","latestValShow":true,"id":10,"fields":[{"hasDefaultValue":false,"visible":true,"editable":true,"values":[],"verify":{"charLimit":{"size":200,"open":false},"required":{}},"tip":{"imgs":[],"text":""},"defaultValueStr":"[{\\"val\\":\\"\\"}]","label":"其他身体不适情况说明","fieldType":{"type":"string"}}],"inDetailGroupIndex":-1,"fromDetail":false,"isShow":false,"hasAuthority":true},{"compt":"radiobutton","otherAllowed":false,"comptCombined":true,"layoutRatio":1,"alias":"11","latestValShow":true,"id":11,"optionColor":true,"fields":[{"hasDefaultValue":true,"visible":true,"editable":true,"values":[{"val":"否","isOther":false}],"options":[{"idArr":[],"score":0,"color":"","checked":true,"className":"color6","title":"否"},{"idArr":[12],"score":0,"color":"","checked":false,"className":"color2","title":"是"}],"verify":{"required":{}},"tip":{"imgs":[],"text":""},"defaultValueStr":"[{\\"val\\":\\"否\\"}]","label":"本人近14天内是否为确诊/疑似患者或无症状感染者","fieldType":{"type":"string"}}],"optionScoreShow":false,"optionScoreUsed":false,"inDetailGroupIndex":-1,"fromDetail":false,"isShow":true,"hasAuthority":true},{"linkInfo":{"condFields":[],"linkFormType":"","linkFormId":0,"linkFormValueFieldCompt":"","linkFormIdEnc":"","linkFormValueFieldId":0,"linked":false},"compt":"edittextarea","layoutRatio":1,"alias":"12","latestValShow":true,"id":12,"fields":[{"hasDefaultValue":false,"visible":true,"editable":true,"values":[],"verify":{"charLimit":{"size":200,"open":false},"required":{}},"tip":{"imgs":[],"text":""},"defaultValueStr":"[{\\"val\\":\\"\\"}]","label":"情况说明","fieldType":{"type":"string"}}],"inDetailGroupIndex":-1,"fromDetail":false,"isShow":false,"hasAuthority":true},{"compt":"radiobutton","otherAllowed":false,"comptCombined":true,"layoutRatio":1,"alias":"13","latestValShow":true,"id":13,"optionColor":true,"fields":[{"hasDefaultValue":true,"visible":true,"editable":true,"values":[{"val":"否","isOther":false}],"options":[{"idArr":[],"score":0,"color":"","checked":true,"className":"color6","title":"否"},{"idArr":[14],"score":0,"color":"","checked":false,"className":"color2","title":"是"}],"verify":{"required":{}},"tip":{"imgs":[],"text":""},"defaultValueStr":"[{\\"val\\":\\"否\\"}]","label":"本人近14天内是否接触过确诊/疑似病例或无症状感染者","fieldType":{"type":"string"}}],"optionScoreShow":false,"optionScoreUsed":false,"inDetailGroupIndex":-1,"fromDetail":false,"isShow":true,"hasAuthority":true},{"linkInfo":{"condFields":[],"linkFormType":"","linkFormId":0,"linkFormValueFieldCompt":"","linkFormIdEnc":"","linkFormValueFieldId":0,"linked":false},"compt":"edittextarea","layoutRatio":1,"alias":"14","latestValShow":true,"id":14,"fields":[{"hasDefaultValue":false,"visible":true,"editable":true,"values":[],"verify":{"charLimit":{"size":200,"open":false},"required":{}},"tip":{"imgs":[],"text":""},"defaultValueStr":"[{\\"val\\":\\"\\"}]","label":"情况说明","fieldType":{"type":"string"}}],"inDetailGroupIndex":-1,"fromDetail":false,"isShow":false,"hasAuthority":true},{"compt":"radiobutton","otherAllowed":false,"comptCombined":true,"layoutRatio":1,"alias":"15","latestValShow":true,"id":15,"optionColor":true,"fields":[{"hasDefaultValue":true,"visible":true,"editable":true,"values":[{"val":"否","isOther":false}],"options":[{"idArr":[],"score":0,"color":"","checked":true,"className":"color6","title":"否"},{"idArr":[16],"score":0,"color":"","checked":false,"className":"color2","title":"是"}],"verify":{"required":{}},"tip":{"imgs":[],"text":""},"defaultValueStr":"[{\\"val\\":\\"否\\"}]","label":"本人近14天内是否有国内疫情防控中高风险区旅居史","fieldType":{"type":"string"}}],"optionScoreShow":false,"optionScoreUsed":false,"inDetailGroupIndex":-1,"fromDetail":false,"isShow":true,"hasAuthority":true},{"linkInfo":{"condFields":[],"linkFormType":"","linkFormId":0,"linkFormValueFieldCompt":"","linkFormIdEnc":"","linkFormValueFieldId":0,"linked":false},"compt":"edittextarea","layoutRatio":1,"alias":"16","latestValShow":true,"id":16,"fields":[{"hasDefaultValue":false,"visible":true,"editable":true,"values":[],"verify":{"charLimit":{"size":200,"open":false},"required":{}},"tip":{"imgs":[],"text":""},"defaultValueStr":"[{\\"val\\":\\"\\"}]","label":"情况说明","fieldType":{"type":"string"}}],"inDetailGroupIndex":-1,"fromDetail":false,"isShow":false,"hasAuthority":true},{"compt":"radiobutton","otherAllowed":false,"comptCombined":true,"layoutRatio":1,"alias":"17","latestValShow":true,"id":17,"optionColor":true,"fields":[{"hasDefaultValue":true,"visible":true,"editable":true,"values":[{"val":"否","isOther":false}],"options":[{"idArr":[],"score":0,"color":"","checked":true,"className":"color6","title":"否"},{"idArr":[18],"score":0,"color":"","checked":false,"className":"color2","title":"是"}],"verify":{"required":{}},"tip":{"imgs":[],"text":""},"defaultValueStr":"[{\\"val\\":\\"否\\"}]","label":"本人近14天内是否接触国内疫情防控中高风险区返乡人员","fieldType":{"type":"string"}}],"optionScoreShow":false,"optionScoreUsed":false,"inDetailGroupIndex":-1,"fromDetail":false,"isShow":true,"hasAuthority":true},{"linkInfo":{"condFields":[],"linkFormType":"","linkFormId":0,"linkFormValueFieldCompt":"","linkFormIdEnc":"","linkFormValueFieldId":0,"linked":false},"compt":"edittextarea","layoutRatio":1,"alias":"18","latestValShow":true,"id":18,"fields":[{"hasDefaultValue":false,"visible":true,"editable":true,"values":[],"verify":{"charLimit":{"size":200,"open":false},"required":{}},"tip":{"imgs":[],"text":""},"defaultValueStr":"[{\\"val\\":\\"\\"}]","label":"情况说明","fieldType":{"type":"string"}}],"inDetailGroupIndex":-1,"fromDetail":false,"isShow":false,"hasAuthority":true},{"compt":"radiobutton","otherAllowed":false,"comptCombined":true,"layoutRatio":1,"alias":"19","latestValShow":true,"id":19,"optionColor":true,"fields":[{"hasDefaultValue":true,"visible":true,"editable":true,"values":[{"val":"否","isOther":false}],"options":[{"idArr":[],"score":0,"color":"","checked":true,"className":"color6","title":"否"},{"idArr":[20],"score":0,"color":"","checked":false,"className":"color2","title":"是"}],"verify":{"required":{}},"tip":{"imgs":[],"text":""},"defaultValueStr":"[{\\"val\\":\\"否\\"}]","label":"本人近14天内是否有国(境)外旅居史","fieldType":{"type":"string"}}],"optionScoreShow":false,"optionScoreUsed":false,"inDetailGroupIndex":-1,"fromDetail":false,"isShow":true,"hasAuthority":true},{"linkInfo":{"condFields":[],"linkFormType":"","linkFormId":0,"linkFormValueFieldCompt":"","linkFormIdEnc":"","linkFormValueFieldId":0,"linked":false},"compt":"edittextarea","layoutRatio":1,"alias":"20","latestValShow":true,"id":20,"fields":[{"hasDefaultValue":false,"visible":true,"editable":true,"values":[],"verify":{"charLimit":{"size":200,"open":false},"required":{}},"tip":{"imgs":[],"text":""},"defaultValueStr":"[{\\"val\\":\\"\\"}]","label":"情况说明","fieldType":{"type":"string"}}],"inDetailGroupIndex":-1,"fromDetail":false,"isShow":false,"hasAuthority":true},{"compt":"radiobutton","otherAllowed":false,"comptCombined":true,"layoutRatio":1,"alias":"21","latestValShow":true,"id":21,"optionColor":true,"fields":[{"hasDefaultValue":true,"visible":true,"editable":true,"values":[{"val":"否","isOther":false}],"options":[{"idArr":[],"score":0,"color":"","checked":true,"className":"color6","title":"否"},{"idArr":[22],"score":0,"color":"","checked":false,"className":"color2","title":"是"}],"verify":{"required":{}},"tip":{"imgs":[],"text":""},"defaultValueStr":"[{\\"val\\":\\"否\\"}]","label":"本人近14天内是否接触过国(境)外返回人员","fieldType":{"type":"string"}}],"optionScoreShow":false,"optionScoreUsed":false,"inDetailGroupIndex":-1,"fromDetail":false,"isShow":true,"hasAuthority":true},{"linkInfo":{"condFields":[],"linkFormType":"","linkFormId":0,"linkFormValueFieldCompt":"","linkFormIdEnc":"","linkFormValueFieldId":0,"linked":false},"compt":"edittextarea","layoutRatio":1,"alias":"22","latestValShow":true,"id":22,"fields":[{"hasDefaultValue":false,"visible":true,"editable":true,"values":[],"verify":{"charLimit":{"size":200,"open":false},"required":{}},"tip":{"imgs":[],"text":""},"defaultValueStr":"[{\\"val\\":\\"\\"}]","label":"情况说明","fieldType":{"type":"string"}}],"inDetailGroupIndex":-1,"fromDetail":false,"isShow":false,"hasAuthority":true},{"compt":"radiobutton","otherAllowed":false,"comptCombined":true,"layoutRatio":1,"alias":"23","latestValShow":true,"id":23,"optionColor":true,"fields":[{"hasDefaultValue":true,"visible":true,"editable":true,"values":[{"val":"否","isOther":false}],"options":[{"idArr":[],"score":0,"color":"","checked":true,"className":"color6","title":"否"},{"idArr":[24],"score":0,"color":"","checked":false,"className":"color2","title":"是"}],"verify":{"required":{}},"tip":{"imgs":[],"text":""},"defaultValueStr":"[{\\"val\\":\\"否\\"}]","label":"共同生活亲属近14天内是否出现发热、咳嗽、乏力、咽痛、腹泻、等身体症状","fieldType":{"type":"string"}}],"optionScoreShow":false,"optionScoreUsed":false,"inDetailGroupIndex":-1,"fromDetail":false,"isShow":true,"hasAuthority":true},{"linkInfo":{"condFields":[],"linkFormType":"","linkFormId":0,"linkFormValueFieldCompt":"","linkFormIdEnc":"","linkFormValueFieldId":0,"linked":false},"compt":"edittextarea","layoutRatio":1,"alias":"24","latestValShow":true,"id":24,"fields":[{"hasDefaultValue":false,"visible":true,"editable":true,"values":[],"verify":{"charLimit":{"size":200,"open":false},"required":{}},"tip":{"imgs":[],"text":""},"defaultValueStr":"[{\\"val\\":\\"\\"}]","label":"情况说明","fieldType":{"type":"string"}}],"inDetailGroupIndex":-1,"fromDetail":false,"isShow":false,"hasAuthority":true},{"compt":"radiobutton","otherAllowed":false,"comptCombined":true,"layoutRatio":1,"alias":"25","latestValShow":true,"id":25,"optionColor":true,"fields":[{"hasDefaultValue":true,"visible":true,"editable":true,"values":[{"val":"否","isOther":false}],"options":[{"idArr":[],"score":0,"color":"","checked":true,"className":"color6","title":"否"},{"idArr":[26],"score":0,"color":"","checked":false,"className":"color2","title":"是"}],"verify":{"required":{}},"tip":{"imgs":[],"text":""},"defaultValueStr":"[{\\"val\\":\\"否\\"}]","label":"共同生活亲属近14天内是否为确诊/疑似患者或无症状感染者","fieldType":{"type":"string"}}],"optionScoreShow":false,"optionScoreUsed":false,"inDetailGroupIndex":-1,"fromDetail":false,"isShow":true,"hasAuthority":true},{"linkInfo":{"condFields":[],"linkFormType":"","linkFormId":0,"linkFormValueFieldCompt":"","linkFormIdEnc":"","linkFormValueFieldId":0,"linked":false},"compt":"edittextarea","layoutRatio":1,"alias":"26","latestValShow":true,"id":26,"fields":[{"hasDefaultValue":false,"visible":true,"editable":true,"values":[],"verify":{"charLimit":{"size":200,"open":false},"required":{}},"tip":{"imgs":[],"text":""},"defaultValueStr":"[{\\"val\\":\\"\\"}]","label":"情况说明","fieldType":{"type":"string"}}],"inDetailGroupIndex":-1,"fromDetail":false,"isShow":false,"hasAuthority":true},{"compt":"radiobutton","otherAllowed":false,"comptCombined":true,"layoutRatio":1,"alias":"27","latestValShow":true,"id":27,"optionColor":true,"fields":[{"hasDefaultValue":true,"visible":true,"editable":true,"values":[{"val":"否","isOther":false}],"options":[{"idArr":[],"score":0,"color":"","checked":true,"className":"color6","title":"否"},{"idArr":[28],"score":0,"color":"","checked":false,"className":"color2","title":"是"}],"verify":{"required":{}},"tip":{"imgs":[],"text":""},"defaultValueStr":"[{\\"val\\":\\"否\\"}]","label":"共同生活亲属近14天内是否接触过确诊/疑似病例或无症状感染者","fieldType":{"type":"string"}}],"optionScoreShow":false,"optionScoreUsed":false,"inDetailGroupIndex":-1,"fromDetail":false,"isShow":true,"hasAuthority":true},{"linkInfo":{"condFields":[],"linkFormType":"","linkFormId":0,"linkFormValueFieldCompt":"","linkFormIdEnc":"","linkFormValueFieldId":0,"linked":false},"compt":"edittextarea","layoutRatio":1,"alias":"28","latestValShow":true,"id":28,"fields":[{"hasDefaultValue":false,"visible":true,"editable":true,"values":[],"verify":{"charLimit":{"size":200,"open":false},"required":{}},"tip":{"imgs":[],"text":""},"defaultValueStr":"[{\\"val\\":\\"\\"}]","label":"情况说明","fieldType":{"type":"string"}}],"inDetailGroupIndex":-1,"fromDetail":false,"isShow":false,"hasAuthority":true},{"compt":"radiobutton","otherAllowed":false,"comptCombined":true,"layoutRatio":1,"alias":"29","latestValShow":true,"id":29,"optionColor":true,"fields":[{"hasDefaultValue":true,"visible":true,"editable":true,"values":[{"val":"否","isOther":false}],"options":[{"idArr":[],"score":0,"color":"","checked":true,"className":"color6","title":"否"},{"idArr":[30],"score":0,"color":"","checked":false,"className":"color2","title":"是"}],"verify":{"required":{}},"tip":{"imgs":[],"text":""},"defaultValueStr":"[{\\"val\\":\\"否\\"}]","label":"共同生活亲属近14天内是否有国内疫情防控中高风险区旅居史","fieldType":{"type":"string"}}],"optionScoreShow":false,"optionScoreUsed":false,"inDetailGroupIndex":-1,"fromDetail":false,"isShow":true,"hasAuthority":true},{"linkInfo":{"condFields":[],"linkFormType":"","linkFormId":0,"linkFormValueFieldCompt":"","linkFormIdEnc":"","linkFormValueFieldId":0,"linked":false},"compt":"edittextarea","layoutRatio":1,"alias":"30","latestValShow":true,"id":30,"fields":[{"hasDefaultValue":false,"visible":true,"editable":true,"values":[],"verify":{"charLimit":{"size":200,"open":false},"required":{}},"tip":{"imgs":[],"text":""},"defaultValueStr":"[{\\"val\\":\\"\\"}]","label":"情况说明","fieldType":{"type":"string"}}],"inDetailGroupIndex":-1,"fromDetail":false,"isShow":false,"hasAuthority":true},{"compt":"radiobutton","otherAllowed":false,"comptCombined":true,"layoutRatio":1,"alias":"31","latestValShow":true,"id":31,"optionColor":true,"fields":[{"hasDefaultValue":true,"visible":true,"editable":true,"values":[{"val":"否","isOther":false}],"options":[{"idArr":[],"score":0,"color":"","checked":true,"className":"color6","title":"否"},{"idArr":[32],"score":0,"color":"","checked":false,"className":"color2","title":"是"}],"verify":{"required":{}},"tip":{"imgs":[],"text":""},"defaultValueStr":"[{\\"val\\":\\"否\\"}]","label":"共同生活亲属近14天内是否接触国内疫情防控中高风险区返乡人员","fieldType":{"type":"string"}}],"optionScoreShow":false,"optionScoreUsed":false,"inDetailGroupIndex":-1,"fromDetail":false,"isShow":true,"hasAuthority":true},{"linkInfo":{"condFields":[],"linkFormType":"","linkFormId":0,"linkFormValueFieldCompt":"","linkFormIdEnc":"","linkFormValueFieldId":0,"linked":false},"compt":"edittextarea","layoutRatio":1,"alias":"32","latestValShow":true,"id":32,"fields":[{"hasDefaultValue":false,"visible":true,"editable":true,"values":[],"verify":{"charLimit":{"size":200,"open":false},"required":{}},"tip":{"imgs":[],"text":""},"defaultValueStr":"[{\\"val\\":\\"\\"}]","label":"情况说明","fieldType":{"type":"string"}}],"inDetailGroupIndex":-1,"fromDetail":false,"isShow":false,"hasAuthority":true},{"compt":"radiobutton","otherAllowed":false,"comptCombined":true,"layoutRatio":1,"alias":"33","latestValShow":true,"id":33,"optionColor":true,"fields":[{"hasDefaultValue":true,"visible":true,"editable":true,"values":[{"val":"否","isOther":false}],"options":[{"idArr":[],"score":0,"color":"","checked":true,"className":"color6","title":"否"},{"idArr":[34],"score":0,"color":"","checked":false,"className":"color2","title":"是"}],"verify":{"required":{}},"tip":{"imgs":[],"text":""},"defaultValueStr":"[{\\"val\\":\\"否\\"}]","label":"共同生活亲属近14天内是否有国(境)外旅居史","fieldType":{"type":"string"}}],"optionScoreShow":false,"optionScoreUsed":false,"inDetailGroupIndex":-1,"fromDetail":false,"isShow":true,"hasAuthority":true},{"linkInfo":{"condFields":[],"linkFormType":"","linkFormId":0,"linkFormValueFieldCompt":"","linkFormIdEnc":"","linkFormValueFieldId":0,"linked":false},"compt":"edittextarea","layoutRatio":1,"alias":"34","latestValShow":true,"id":34,"fields":[{"hasDefaultValue":false,"visible":true,"editable":true,"values":[],"verify":{"charLimit":{"size":200,"open":false},"required":{}},"tip":{"imgs":[],"text":""},"defaultValueStr":"[{\\"val\\":\\"\\"}]","label":"情况说明","fieldType":{"type":"string"}}],"inDetailGroupIndex":-1,"fromDetail":false,"isShow":false,"hasAuthority":true},{"compt":"radiobutton","otherAllowed":false,"comptCombined":true,"layoutRatio":1,"alias":"35","latestValShow":true,"id":35,"optionColor":true,"fields":[{"hasDefaultValue":true,"visible":true,"editable":true,"values":[{"val":"否","isOther":false}],"options":[{"idArr":[],"score":0,"color":"","checked":true,"className":"color6","title":"否"},{"idArr":[36],"score":0,"color":"","checked":false,"className":"color2","title":"是"}],"verify":{"required":{}},"tip":{"imgs":[],"text":""},"defaultValueStr":"[{\\"val\\":\\"否\\"}]","label":"共同生活亲属近14天内是否接触过国(境)外返回人员","fieldType":{"type":"string"}}],"optionScoreShow":false,"optionScoreUsed":false,"inDetailGroupIndex":-1,"fromDetail":false,"isShow":true,"hasAuthority":true},{"linkInfo":{"condFields":[],"linkFormType":"","linkFormId":0,"linkFormValueFieldCompt":"","linkFormIdEnc":"","linkFormValueFieldId":0,"linked":false},"compt":"edittextarea","layoutRatio":1,"alias":"36","latestValShow":true,"id":36,"fields":[{"hasDefaultValue":false,"visible":true,"editable":true,"values":[],"verify":{"charLimit":{"size":200,"open":false},"required":{}},"tip":{"imgs":[],"text":""},"defaultValueStr":"[{\\"val\\":\\"\\"}]","label":"情况说明","fieldType":{"type":"string"}}],"inDetailGroupIndex":-1,"fromDetail":false,"isShow":false,"hasAuthority":true},{"compt":"captiontext","layoutRatio":1,"alias":"37","id":37,"fields":[{"visible":true,"editable":false,"tip":{"imgs":[],"text":"<p style=\\"text-align: center;\\"><span style=\\"font-size: x-large;\\">出行情况&nbsp;&nbsp;&nbsp;&nbsp;</span><br></p>"},"label":"说明文字","fieldType":{"type":"string"}}],"inDetailGroupIndex":-1,"fromDetail":false,"isShow":true,"hasAuthority":false},{"compt":"radiobutton","otherAllowed":false,"comptCombined":true,"layoutRatio":1,"alias":"38","latestValShow":true,"id":38,"optionColor":true,"fields":[{"hasDefaultValue":true,"visible":true,"editable":true,"values":[{"val":"否","isOther":false}],"options":[{"idArr":[],"score":0,"color":"","checked":true,"className":"color6","title":"否"},{"idArr":[39,41,42,43],"score":0,"color":"","checked":false,"className":"color2","title":"是"}],"verify":{"required":{}},"tip":{"imgs":[],"text":""},"defaultValueStr":"[{\\"val\\":\\"否\\"}]","label":"当日是否有外出活动(跨市)","fieldType":{"type":"string"}}],"optionScoreShow":false,"optionScoreUsed":false,"inDetailGroupIndex":-1,"fromDetail":false,"isShow":true,"hasAuthority":true},{"compt":"datetimerange","visible":true,"editable":true,"layoutRatio":1,"alias":"39","verify":{"dateRange":{"isInclude":false,"errMsg":"开始时间不能晚于结束时间","format":"yyyy-MM-dd HH:mm"},"validateRange":{"beginDate":"","dynamicRangeStartValue":1,"dynamicRangeEndValue":1,"rangeType":0,"endDate":"","errMsg":"请输入合法的日期范围","dynamicRangeStartType":1,"dynamicRangeEndType":3},"required":{}},"id":39,"fields":[{"tip":{"imgs":[],"text":"请选择"},"label":"外出开始时间","fieldType":{"format":"yyyy-MM-dd HH:mm","type":"date"},"values":[]},{"tip":{"imgs":[],"text":"请选择"},"label":"预计结束时间","fieldType":{"format":"yyyy-MM-dd HH:mm","type":"date"},"values":[]},{"label":"时长","fieldType":{"unit":"hour","calculate":true,"type":"timerange"},"values":[]}],"inDetailGroupIndex":-1,"fromDetail":false,"isShow":false,"hasAuthority":true},{"linkInfo":{"condFields":[],"linkFormType":"","linkFormId":0,"linkFormValueFieldCompt":"","linkFormIdEnc":"","linkFormValueFieldId":0,"linked":false},"compt":"areamultiselect","level":3,"layoutRatio":1,"alias":"41","latestValShow":true,"id":41,"fields":[{"hasDefaultValue":false,"visible":true,"multiOptions":[],"editable":true,"values":[],"verify":{"required":{}},"tip":{"imgs":[],"text":""},"defaultValueStr":"[{\\"val\\":\\"\\"},{\\"val\\":\\"\\"},{\\"val\\":\\"\\"},{\\"val\\":\\"\\"}]","label":"外出目的地","fieldType":{"type":"multibox"}}],"inDetailGroupIndex":-1,"fromDetail":false,"isShow":false,"hasAuthority":true},{"linkInfo":{"condFields":[],"linkFormType":"","linkFormId":0,"linkFormValueFieldCompt":"","linkFormIdEnc":"","linkFormValueFieldId":0,"linked":false},"compt":"areamultiselect","level":2,"layoutRatio":1,"alias":"42","latestValShow":false,"id":42,"fields":[{"hasDefaultValue":false,"visible":true,"multiOptions":[],"editable":true,"values":[],"verify":{"required":{}},"tip":{"imgs":[],"text":""},"defaultValueStr":"[{\\"val\\":\\"\\"},{\\"val\\":\\"\\"}]","label":"外出途径地","fieldType":{"type":"multibox"}}],"inDetailGroupIndex":-1,"fromDetail":false,"isShow":false,"hasAuthority":true},{"linkInfo":{"condFields":[],"linkFormType":"","linkFormId":0,"linkFormValueFieldCompt":"","linkFormIdEnc":"","linkFormValueFieldId":0,"linked":false},"compt":"editinput","layoutRatio":1,"alias":"43","formula":{"selIndex":-1,"calculateFieldId":"0","status":false},"latestValShow":false,"id":43,"fields":[{"hasDefaultValue":false,"visible":true,"editable":true,"values":[],"verify":{"charLimit":{"size":20,"open":false},"regularExpress":{"errorTip":"格式错误!","express":""},"unique":{"errMsg":"此项内容已存在,不允许重复提交","open":false},"format":{"type":""}},"tip":{"imgs":[],"text":""},"defaultValueStr":"[{\\"val\\":\\"\\"}]","label":"外出交通工具(例:自驾、长途汽车、火车、飞机等)","sweepCode":false,"fieldType":{"type":"string"},"codeChangeable":false}],"inDetailGroupIndex":-1,"fromDetail":false,"isShow":false,"hasAuthority":true},{"compt":"captiontext","layoutRatio":1,"alias":"49","id":49,"fields":[{"visible":true,"editable":false,"tip":{"imgs":[],"text":"<p style=\\"text-align: center;\\"><font size=\\"5\\">疫苗接种信息采集</font></p>"},"label":"说明文字","fieldType":{"type":"string"}}],"inDetailGroupIndex":-1,"fromDetail":false,"isShow":true,"hasAuthority":false},{"compt":"radiobutton","otherAllowed":false,"comptCombined":true,"layoutRatio":1,"alias":"50","latestValShow":true,"id":50,"optionColor":true,"fields":[{"hasDefaultValue":true,"visible":true,"editable":true,"values":[{"val":"是","isOther":false}],"options":[{"idArr":[52,53],"score":0,"color":"","checked":true,"className":"color6","title":"是"},{"idArr":[54],"score":0,"color":"","checked":false,"className":"color2","title":"否"}],"verify":{"required":{}},"tip":{"imgs":[],"text":""},"defaultValueStr":"[{\\"val\\":\\"是\\"}]","label":"是否已接种新冠疫苗","fieldType":{"type":"string"}}],"optionScoreShow":false,"optionScoreUsed":false,"inDetailGroupIndex":-1,"fromDetail":false,"isShow":true,"hasAuthority":true},{"linkInfo":{"condFields":[],"linkFormType":"","linkFormId":0,"linkFormValueFieldCompt":"","linkFormIdEnc":"","linkFormValueFieldId":0,"linked":false},"compt":"dateinput","layoutRatio":1,"alias":"52","latestValShow":true,"id":52,"fields":[{"hasDefaultValue":false,"dayIndex":0,"visible":true,"editable":true,"values":[{"val":"%s"}],"appoint":false,"verify":{"validateRange":{"beginDate":"","dynamicRangeStartValue":1,"dynamicRangeEndValue":1,"rangeType":0,"endDate":"","errMsg":"请输入合法的日期范围","dynamicRangeStartType":1,"dynamicRangeEndType":3},"required":{}},"tip":{"imgs":[],"text":""},"defaultValueStr":"[{\\"val\\":\\"\\"}]","label":"第一针接种时间","fieldType":{"format":"yyyy-MM-dd","type":"date"}}],"inDetailGroupIndex":-1,"fromDetail":false,"isShow":true,"hasAuthority":true},{"linkInfo":{"condFields":[],"linkFormType":"","linkFormId":0,"linkFormValueFieldCompt":"","linkFormIdEnc":"","linkFormValueFieldId":0,"linked":false},"compt":"dateinput","layoutRatio":1,"alias":"53","latestValShow":true,"id":53,"fields":[{"hasDefaultValue":false,"dayIndex":0,"visible":true,"editable":true,"values":[{"val":"%s"}],"appoint":false,"verify":{"validateRange":{"beginDate":"","dynamicRangeStartValue":1,"dynamicRangeEndValue":1,"rangeType":0,"endDate":"","errMsg":"请输入合法的日期范围","dynamicRangeStartType":1,"dynamicRangeEndType":3}},"tip":{"imgs":[],"text":""},"defaultValueStr":"[{\\"val\\":\\"\\"}]","label":"第二针接种时间","fieldType":{"format":"yyyy-MM-dd","type":"date"}}],"inDetailGroupIndex":-1,"fromDetail":false,"isShow":true,"hasAuthority":true},{"linkInfo":{"condFields":[],"linkFormType":"","linkFormId":0,"linkFormValueFieldCompt":"","linkFormIdEnc":"","linkFormValueFieldId":0,"linked":false},"compt":"editinput","layoutRatio":1,"alias":"54","formula":{"selIndex":-1,"calculateFieldId":"0","status":false},"latestValShow":true,"id":54,"fields":[{"hasDefaultValue":false,"visible":true,"editable":true,"values":[],"verify":{"charLimit":{"size":20,"open":false},"regularExpress":{"errorTip":"格式错误!","express":""},"unique":{"errMsg":"此项内容已存在,不允许重复提交","open":false},"format":{"type":""},"required":{}},"tip":{"imgs":[],"text":""},"defaultValueStr":"[{\\"val\\":\\"\\"}]","label":"未接种原因","sweepCode":false,"fieldType":{"type":"string"},"codeChangeable":false}],"inDetailGroupIndex":-1,"fromDetail":false,"isShow":false,"hasAuthority":true},{"compt":"radiobutton","otherAllowed":false,"comptCombined":true,"layoutRatio":1,"alias":"55","latestValShow":false,"id":55,"optionColor":true,"fields":[{"hasDefaultValue":false,"visible":true,"editable":true,"values":[{"val":"是","isOther":false}],"options":[{"idArr":[],"score":100,"color":"","checked":true,"className":"color6","title":"是"},{"idArr":[],"score":0,"color":"","checked":false,"className":"color2","title":"否"}],"verify":{"required":{}},"tip":{"imgs":[],"text":""},"defaultValueStr":"[]","label":"本人承诺:以上信息填报真实有效。本人严格遵守相关防疫工作要求,及时、准确向学校申报健康状况、接触情况、出行行程安排等,并认真做好个人防护。","fieldType":{"type":"string"}}],"optionScoreShow":false,"optionScoreUsed":true,"inDetailGroupIndex":-1,"fromDetail":false,"isShow":true,"hasAuthority":true},{"compt":"signature","layoutRatio":1,"alias":"56","id":56,"fields":[{"visible":true,"editable":true,"values":[{"val":"%s"}],"verify":{"required":{}},"tip":{"imgs":[],"text":""},"label":"手写签名","fieldType":{"type":"signature"}}],"inDetailGroupIndex":-1,"fromDetail":false,"isShow":true,"hasAuthority":true}]'</span> <span class="token operator">%</span> <span class="token punctuation">(</span>cookies<span class="token punctuation">[</span><span class="token string">'_uid'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> form<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'fields'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'values'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'uname'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> form<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'fields'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'values'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'val'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> form<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'fields'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'values'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'val'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> form<span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'fields'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'values'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'val'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> form<span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'fields'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'values'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'val'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> form<span class="token punctuation">[</span><span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'fields'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'values'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'val'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> form<span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'fields'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'values'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'address'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> form<span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'fields'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'values'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'lng'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> form<span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'fields'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'values'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'lat'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> form<span class="token punctuation">[</span><span class="token number">47</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'fields'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'values'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'val'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> form<span class="token punctuation">[</span><span class="token number">48</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'fields'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'values'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'val'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> form<span class="token punctuation">[</span><span class="token number">51</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'fields'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'values'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'val'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">'utf-8'</span><span class="token punctuation">)</span> params_c3 <span class="token operator">=</span> <span class="token string">'formId=92597&formAppId=&version=0&formData='</span> <span class="token operator">+</span> params_c3 <span class="token operator">+</span> <span class="token string">'&ext=&t=1&enc=e8a70d13bac504a2ec42ad5d72aa8abd&checkCode=%s&gatherId=0&anonymous=0&uniqueCondition=[]&gverify='</span> <span class="token operator">%</span> <span class="token punctuation">(</span> checkCode<span class="token punctuation">)</span> url <span class="token operator">=</span> api_save <span class="token operator">%</span> <span class="token punctuation">(</span>cookies<span class="token punctuation">[</span><span class="token string">'_uid'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> resp <span class="token operator">=</span> requests<span class="token punctuation">.</span>post<span class="token punctuation">(</span>url<span class="token punctuation">,</span> cookies<span class="token operator">=</span>cookies<span class="token punctuation">,</span> params<span class="token operator">=</span>params_c3<span class="token punctuation">)</span> <span class="token keyword">if</span> <span class="token string">'每个用户只能填写1次,您的提交已达上限哦~'</span> <span class="token keyword">in</span> resp<span class="token punctuation">.</span>text <span class="token keyword">or</span> <span class="token string">'"success":true'</span> <span class="token keyword">in</span> resp<span class="token punctuation">.</span>text<span class="token punctuation">:</span> <span class="token keyword">return</span> <span class="token string">'每日健康上报打卡成功或已打卡过'</span> <span class="token keyword">else</span><span class="token punctuation">:</span> <span class="token keyword">print</span><span class="token punctuation">(</span>resp<span class="token punctuation">.</span>text<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token string">'每日健康上报打卡失败'</span><span class="token comment"># 云函数入口</span><span class="token keyword">def</span> <span class="token function">main_handler</span><span class="token punctuation">(</span>event<span class="token punctuation">,</span> context<span class="token punctuation">)</span><span class="token punctuation">:</span> cookies <span class="token operator">=</span> login<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">return</span> checkin3<span class="token punctuation">(</span>cookies<span class="token punctuation">)</span><span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">'__main__'</span><span class="token punctuation">:</span> <span class="token keyword">print</span><span class="token punctuation">(</span>main_handler<span class="token punctuation">(</span><span class="token boolean">None</span><span class="token punctuation">,</span> <span class="token boolean">None</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><p>本地测试通过</p><p><img src="https://cdn.jsdelivr.net/gh/oopstls/PicBed/blog/XXT_check-in/image-20210212020408353.png" alt="image-20210212020408353"></p><p><img src="https://cdn.jsdelivr.net/gh/oopstls/PicBed/blog/XXT_check-in/image-20210212020504083.png" alt="image-20210212020504083"></p><p><img src="https://cdn.jsdelivr.net/gh/oopstls/PicBed/blog/XXT_check-in/image-20210212020551151.png" alt="image-20210212020551151"></p><p><img src="https://cdn.jsdelivr.net/gh/oopstls/PicBed/blog/XXT_check-in/image-20210212020625136.png" alt="image-20210212020625136"></p><h1 id="部署到云函数或者github-action"><a href="#部署到云函数或者github-action" class="headerlink" title="部署到云函数或者github action"></a>部署到云函数或者github action</h1><p>我这里选择部署到国内运营商的云函数,因为比较稳定(并不,github action也是可以的。</p><p>这里是阿里云函数服务的流程。</p><p><img src="https://cdn.jsdelivr.net/gh/oopstls/PicBed/blog/XXT_check-in/image-20210212021739139.png" alt="image-20210212021739139"></p><p><img src="https://cdn.jsdelivr.net/gh/oopstls/PicBed/blog/XXT_check-in/image-20210212021756288.png" alt="image-20210212021756288"></p><p><img src="https://cdn.jsdelivr.net/gh/oopstls/PicBed/blog/XXT_check-in/image-20210212021835137.png" alt="image-20210212021835137"></p><p>翻车😭</p><p><img src="https://cdn.jsdelivr.net/gh/oopstls/PicBed/blog/XXT_check-in/image-20210212022307621.png" alt="image-20210212022307621"></p><p>没找到原因,直接换了腾讯云函数碰碰运气,结果就可以了??? 为什么啊(腾讯云函数的操作和阿里的大差不差</p><p><img src="https://cdn.jsdelivr.net/gh/oopstls/PicBed/blog/XXT_check-in/image-20210212022447353.png" alt="image-20210212022447353"></p><p>然后设置一个每天自动打卡的触发器</p><p><img src="https://cdn.jsdelivr.net/gh/oopstls/PicBed/blog/XXT_check-in/image-20210212022524763.png" alt="image-20210212022524763"></p><p>大年初一凌晨2.26,我居然在搞这个玩意。。。</p><p>脑子有点迷迷糊糊的,写的比较混乱,很多地方之前没截图就跳过了😂,有不懂可以发评论😂(会有人看吗😐,同时欢迎大佬指点。</p><p>顶不住了,睡了睡了。</p>]]></content>
<categories>
<category>瞎搞</category>
</categories>
<tags>
<tag>爬虫</tag>
<tag>python</tag>
<tag>抓包</tag>
</tags>
</entry>
<entry>
<title>背包九讲笔记</title>
<link href="/posts/0/"/>
<url>/posts/0/</url>
<content type="html"><![CDATA[<h1 id="定义"><a href="#定义" class="headerlink" title="定义"></a>定义</h1><p>一个背包,背包的容量为m。</p><p>n种物品,每个物品有两个属性,体积w[i],价值v[i]</p><p>每个物品只有一个,也就是说每个物品只有拿和不拿两种状态。</p><p>求能够装入背包的最大价值或取最大价值时物品的集合。</p><h1 id="01背包"><a href="#01背包" class="headerlink" title="01背包"></a><a href="https://www.acwing.com/problem/content/2/">01背包</a></h1><h2 id="用子问题定义状态"><a href="#用子问题定义状态" class="headerlink" title="用子问题定义状态"></a>用子问题定义状态</h2><p>$f[i][j]$ 前<code>i</code>个物品在背包体积为<code>j</code>的情况下总价值的最大值</p><p>状态转移方程: $f[i][j]=max(f[i-1][j],f[i-1][j-w[i]]+v[i])$</p><p><em>后面几个背包问题都是这个问题的衍生,这个方程看不懂的话后面的就难搞了,所以重点解释一下。</em></p><ol><li>每个物品只有选和不选两种状态。</li><li>无论选或者不选,这个物品的状态都是由前一个物品(<code>i-1</code>)的最大价值转移过来的。</li><li><ul><li>如果不选物品<code>i</code>的话,那么物品<code>i</code>就不消耗背包容量,所以从<code>f[i-1][j]</code>转移过来。</li><li>如果要选物品<code>i</code>的话,那么物品<code>i</code>就需要消耗<code>w[i]</code>的体积,所以从<code>f[i-1][j-w[i]]</code>转移过来。由于选择了物品<code>i</code>,所以价值需要加上<code>v[i]</code>,即<code>f[i-1][j-w[i]] + v[i]</code>。</li></ul></li><li>选择第3点提到的两种状态中较大的即为<code>f[i][j]</code>的最大价值</li></ol><h2 id="复杂度"><a href="#复杂度" class="headerlink" title="复杂度"></a>复杂度</h2><p><code>i</code>和<code>j</code>分别是物品种类和背包容量,显而易见时间空间复杂度都是$O(nm)$。</p><h2 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h2><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><iostream></span></span><span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span><span class="token keyword">const</span> <span class="token keyword">int</span> N <span class="token operator">=</span> <span class="token number">1010</span><span class="token punctuation">;</span><span class="token keyword">int</span> f<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token keyword">int</span> n<span class="token punctuation">,</span> m<span class="token punctuation">,</span> v<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">,</span> w<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> cin <span class="token operator">>></span> n <span class="token operator">>></span> m<span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator"><=</span> n<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span> <span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">"%d%d"</span><span class="token punctuation">,</span> <span class="token operator">&</span>w<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token operator">&</span>v<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator"><=</span> n<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span><span class="token comment">//循环物品</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> j <span class="token operator"><=</span> m<span class="token punctuation">;</span> <span class="token operator">++</span>j<span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token comment">//循环背包容量</span> f<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> f<span class="token punctuation">[</span>i <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//不选</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>w<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">></span> j<span class="token punctuation">)</span> <span class="token keyword">continue</span><span class="token punctuation">;</span><span class="token comment">//如果当前背包容量不足也不选</span> f<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">max</span><span class="token punctuation">(</span>f<span class="token punctuation">[</span>i <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">,</span> f<span class="token punctuation">[</span>i <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span>j <span class="token operator">-</span> w<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token operator">+</span> v<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//取最大价值</span> <span class="token punctuation">}</span> <span class="token keyword">int</span> res <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator"><=</span> m<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span><span class="token comment">//答案一定在物品选取前n个时</span> res <span class="token operator">=</span> <span class="token function">max</span><span class="token punctuation">(</span>res<span class="token punctuation">,</span> f<span class="token punctuation">[</span>n<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> cout <span class="token operator"><<</span> res <span class="token operator"><<</span> endl<span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h2 id="优化"><a href="#优化" class="headerlink" title="优化"></a>优化</h2><ol><li><ul><li>当我们循环第<code>i</code>个物品时,只会从<code>i-1</code>个物品的最大价值转移过来。</li><li>当我们循环第<code>i</code>个物品容量为<code>j</code>时,只会和容量为<code>j-w[i]</code>的作比较,显然$j >= j-w[i]$。</li></ul><p>所以只要我们倒着循环体积,就只需要一维数组就可以了。<br>此时的状态转移方程是: $f[j]=max(f[j],f[j-w[i]]+v[i])$</p><p>需要注意的是,二维的时候<code>j</code>从第一个开始循环,且先赋值<code>f[i-1][j]</code>到<code>f[i][j]</code>。是因为<code>f[i][j]</code>可能从<code>f[i-1][j-w[i]]</code>转移过来,而物品<code>i-1</code>的体积<code>w[i-1]</code>可能大于<code>j-w[i]</code>,所以一定要第一个开始而不是从<code>w[i]</code>开始循环,只有这样才可以将前面的最大价值转移到后面来。</p><p>但是在一维的情况下,小于<code>w[i]</code>的本身就是上一个物品的最大价值列表,所以不需要再转移一次。</p><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator"><=</span> n<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> m<span class="token punctuation">;</span> j <span class="token operator">>=</span> w<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token operator">--</span>j<span class="token punctuation">)</span>f<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">max</span><span class="token punctuation">(</span>f<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">,</span> f<span class="token punctuation">[</span>j <span class="token operator">-</span> w<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token operator">+</span> v<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre></div></li><li><p>当我们循环第<code>i</code>个物品的时候,只会用到这一个物品的价值和体积,而不会用到其他物品的,所以可以将输入和计算最大值放在一起。</p><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> n<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">"%d%d"</span><span class="token punctuation">,</span> <span class="token operator">&</span>w<span class="token punctuation">,</span> <span class="token operator">&</span>v<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> m<span class="token punctuation">;</span> j <span class="token operator">>=</span> w<span class="token punctuation">;</span> <span class="token operator">--</span>j<span class="token punctuation">)</span> f<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">max</span><span class="token punctuation">(</span>f<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">,</span> f<span class="token punctuation">[</span>j <span class="token operator">-</span> w<span class="token punctuation">]</span> <span class="token operator">+</span> v<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div></li><li><p>当<code>f</code>数组全部被初始化为<code>0</code>时,<code>f[i][j]</code>或<code>f[j]</code>表示的是前<code>i</code>个物品在背包容量为<code>j</code>时的最大价值。所以直接输出<code>f[m]</code>就是答案,我们将<code>f</code>数组作为全局变量初始化的,所以默认就是<code>0</code>。</p><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><iostream></span></span><span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span><span class="token keyword">const</span> <span class="token keyword">int</span> N <span class="token operator">=</span> <span class="token number">1010</span><span class="token punctuation">;</span><span class="token keyword">int</span> f<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token keyword">int</span> n<span class="token punctuation">,</span> m<span class="token punctuation">,</span> v<span class="token punctuation">,</span> w<span class="token punctuation">;</span><span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> cin <span class="token operator">>></span> n <span class="token operator">>></span> m<span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> n<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">"%d%d"</span><span class="token punctuation">,</span> <span class="token operator">&</span>w<span class="token punctuation">,</span> <span class="token operator">&</span>v<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> m<span class="token punctuation">;</span> j <span class="token operator">>=</span> w<span class="token punctuation">;</span> <span class="token operator">--</span>j<span class="token punctuation">)</span> f<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">max</span><span class="token punctuation">(</span>f<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">,</span> f<span class="token punctuation">[</span>j <span class="token operator">-</span> w<span class="token punctuation">]</span> <span class="token operator">+</span> v<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> cout <span class="token operator"><<</span> f<span class="token punctuation">[</span>m<span class="token punctuation">]</span> <span class="token operator"><<</span> endl<span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><ul><li>当问题是背包容量是多少时能恰好装满背包且背包价值最大的话,就需要将<code>f[0][0]</code>初始化为<code>0</code>,而其他的都初始化为负无穷。<br>再看状态转移方程<code>f[j-w[i]] + v[i]</code>,所有选择了物品的状态都是从上一个物品的最大价值加上<code>v[i]</code>而来,当初始化了负无穷时有,$j-w[i]==0$时能选择要选择这个物品,此时<code>f[j-w[i]] + v[i]</code>的值是<code>0 + v[i]</code>,否则是一个负值,所以<code>j</code>只能是物品体积的倍数时才能被选择。</li></ul></li></ol><h1 id="完全背包"><a href="#完全背包" class="headerlink" title="完全背包"></a><a href="https://www.acwing.com/problem/content/3/">完全背包</a></h1><p>完全背包是在01背包的基础上将物品只有选和不选的限制改为了每种物品都可以选择无限多个。</p><p>令<code>k</code>为每件物品取到的个数,可以直接在01背包的基础上得出完全背包的一个状态转移方程 :</p><p>$f[i][j]=max(f[i-1][j-k<em>w[i]]+k</em>v[i])$</p><p>直接使用这个状态转移方程需要在01背包的基础上加一层循环来枚举物品的个数,类似的代码参考后面的多重背包II。</p><p>回顾前面优化过的01背包,我们使用的是从后往前的方式,这是因为<code>j-w[i]</code>一定实在<code>j</code>的前面,所以取到的其实是<code>f[i-1][j-w[i]]</code>,所以<code>f[j-w[i]]+v[i]</code>的意思就是在<code>f[i-1][j-w[i]]</code>已经取到最优值的情况之上再取一个<code>i</code>所得到的价值。</p><p>由此,我们从前往后遍历,那么<code>f[j-w[i]]+v[i]</code>的意思就是在<code>f[i][j-w[i]]</code>已经取到最优值得情况之上再去一个<code>i</code>所得到的价值。而<code>f[i][j-w[i]]</code>已经取到了<code>j-w[i]/w[i]</code>个<code>i</code>物品。</p><h2 id="代码-1"><a href="#代码-1" class="headerlink" title="代码"></a>代码</h2><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><iostream></span></span><span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span><span class="token keyword">const</span> <span class="token keyword">int</span> N <span class="token operator">=</span> <span class="token number">1010</span><span class="token punctuation">;</span><span class="token keyword">int</span> f<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token keyword">int</span> n<span class="token punctuation">,</span> m<span class="token punctuation">,</span> v<span class="token punctuation">,</span> w<span class="token punctuation">;</span><span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> cin <span class="token operator">>></span> n <span class="token operator">>></span> m<span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> n<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">"%d%d"</span><span class="token punctuation">,</span> <span class="token operator">&</span>w<span class="token punctuation">,</span> <span class="token operator">&</span>v<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> w<span class="token punctuation">;</span> j <span class="token operator"><=</span> m<span class="token punctuation">;</span> <span class="token operator">++</span>j<span class="token punctuation">)</span> f<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">max</span><span class="token punctuation">(</span>f<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">,</span> f<span class="token punctuation">[</span>j <span class="token operator">-</span> w<span class="token punctuation">]</span> <span class="token operator">+</span> v<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> cout <span class="token operator"><<</span> f<span class="token punctuation">[</span>m<span class="token punctuation">]</span> <span class="token operator"><<</span> endl<span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h1 id="多重背包I"><a href="#多重背包I" class="headerlink" title="多重背包I"></a><a href="https://www.acwing.com/problem/content/4/">多重背包I</a></h1><p>多重背包是在完全背包的基础上将每个物品可以选择无限多次改为了每种物品只能选择<code>s[i]</code>次。</p><p>在完全背包里面提到了可以用第三重循环来枚举<code>k</code>的取值,这里就以这样的方式来写代码。</p><h2 id="代码-2"><a href="#代码-2" class="headerlink" title="代码"></a>代码</h2><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><iostream></span></span><span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span><span class="token keyword">const</span> <span class="token keyword">int</span> N <span class="token operator">=</span> <span class="token number">110</span><span class="token punctuation">;</span><span class="token keyword">int</span> f<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token keyword">int</span> n<span class="token punctuation">,</span> m<span class="token punctuation">,</span> s<span class="token punctuation">,</span> w<span class="token punctuation">,</span> v<span class="token punctuation">;</span><span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> cin <span class="token operator">>></span> n <span class="token operator">>></span> m<span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> n<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">"%d%d%d"</span><span class="token punctuation">,</span> <span class="token operator">&</span>w<span class="token punctuation">,</span> <span class="token operator">&</span>v<span class="token punctuation">,</span> <span class="token operator">&</span>s<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> m<span class="token punctuation">;</span> j <span class="token operator">>=</span> w<span class="token punctuation">;</span> <span class="token operator">--</span>j<span class="token punctuation">)</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> k <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> k <span class="token operator"><=</span> s <span class="token operator">&&</span> k <span class="token operator">*</span> w <span class="token operator"><=</span> j<span class="token punctuation">;</span> <span class="token operator">++</span>k<span class="token punctuation">)</span> f<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">max</span><span class="token punctuation">(</span>f<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">,</span> f<span class="token punctuation">[</span>j <span class="token operator">-</span> k <span class="token operator">*</span> w<span class="token punctuation">]</span> <span class="token operator">+</span> k <span class="token operator">*</span> v<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> cout <span class="token operator"><<</span> f<span class="token punctuation">[</span>m<span class="token punctuation">]</span> <span class="token operator"><<</span> endl<span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><p>这是最暴力的写法,它有两种优化方法,会在下面提到。</p><h1 id="多重背包II"><a href="#多重背包II" class="headerlink" title="多重背包II"></a><a href="https://www.acwing.com/problem/content/5/">多重背包II</a></h1><p>多重背包的二进制优化。</p><p>上一种方法的时间复杂度显而易见是$O(nms)$,现在当<code>n m s</code>取值都是最大1000时,那么就是$10^9$,c/c++在1秒内最多能做$10^7-10^8$次操作,在这种数据量下还是用上面那种方法必然TLE。</p><p>回顾上一种方法,在第三重循环是在枚举物品的个数<code>s</code>,这里可以考虑<mark>将每种物品有<code>s</code>个转换为有<code>s</code>个一样的物品。</mark>这样的话就变成了01背包问题,但是<code>n</code>的取值就变到了$10^6$,时间复杂度就是$O(nm)$,依然会到$10^9$。</p><p>再回过头来,现在<code>n</code>种物品当中有许多都是相同的物品,这样的话就可以通过二进制的每一位数的数值来代替物品的个数,举个例子:15以内的每一个数可以通过·<code>{1, 2, 4, 8}</code>中的数组合表示,当总的个数不是<code>2^k-1</code>时,最后一个数取总数减去前几个输的和,举个例子13以内的通过<code>{1, 2, 4, 6}</code>来表示,如此一来,时间复杂度就是$O(nmlog(s))$,是小于$10^8$的。</p><h2 id="代码-3"><a href="#代码-3" class="headerlink" title="代码"></a>代码</h2><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><iostream></span></span><span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span><span class="token keyword">const</span> <span class="token keyword">int</span> N <span class="token operator">=</span> <span class="token number">1e5</span><span class="token punctuation">;</span><span class="token keyword">int</span> f<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token keyword">int</span> n<span class="token punctuation">,</span> m<span class="token punctuation">,</span> v<span class="token punctuation">,</span> w<span class="token punctuation">,</span> s<span class="token punctuation">;</span><span class="token keyword">int</span> k<span class="token punctuation">,</span> vv<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">,</span> ww<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">,</span> cnt<span class="token punctuation">;</span><span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> cin <span class="token operator">>></span> n <span class="token operator">>></span> m<span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> n<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">"%d%d%d"</span><span class="token punctuation">,</span> <span class="token operator">&</span>w<span class="token punctuation">,</span> <span class="token operator">&</span>v<span class="token punctuation">,</span> <span class="token operator">&</span>s<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span>k <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> k <span class="token operator"><=</span> s<span class="token punctuation">;</span> k <span class="token operator"><<=</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token comment">//取2^x次方</span> s <span class="token operator">-=</span> k<span class="token punctuation">;</span> ww<span class="token punctuation">[</span>cnt<span class="token punctuation">]</span> <span class="token operator">=</span> k <span class="token operator">*</span> w<span class="token punctuation">;</span><span class="token comment">//体积</span> vv<span class="token punctuation">[</span>cnt<span class="token operator">++</span><span class="token punctuation">]</span> <span class="token operator">=</span> k <span class="token operator">*</span> v<span class="token punctuation">;</span><span class="token comment">//价值</span> <span class="token punctuation">}</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>s<span class="token punctuation">)</span> <span class="token punctuation">{</span> ww<span class="token punctuation">[</span>cnt<span class="token punctuation">]</span> <span class="token operator">=</span> s <span class="token operator">*</span> w<span class="token punctuation">;</span> vv<span class="token punctuation">[</span>cnt<span class="token operator">++</span><span class="token punctuation">]</span> <span class="token operator">=</span> s <span class="token operator">*</span> v<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token comment">//01背包模板</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> cnt<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> m<span class="token punctuation">;</span> j <span class="token operator">>=</span> ww<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token operator">--</span>j<span class="token punctuation">)</span> f<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">max</span><span class="token punctuation">(</span>f<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">,</span> f<span class="token punctuation">[</span>j <span class="token operator">-</span> ww<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token operator">+</span> vv<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> cout <span class="token operator"><<</span> f<span class="token punctuation">[</span>m<span class="token punctuation">]</span> <span class="token operator"><<</span> endl<span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h1 id="多重背包III"><a href="#多重背包III" class="headerlink" title="多重背包III"></a><a href="https://www.acwing.com/problem/content/6/">多重背包III</a></h1><p>多重背包的单调队列优化。</p><p>明天再写😂</p>]]></content>
<categories>
<category>算法</category>
</categories>
<tags>
<tag>DP</tag>
<tag>动态规划</tag>
<tag>背包问题</tag>
</tags>
</entry>
<entry>
<title>几个排序算法的模板略解</title>
<link href="/posts/5b4e49b2/"/>
<url>/posts/5b4e49b2/</url>
<content type="html"><![CDATA[<h1 id="快速排序"><a href="#快速排序" class="headerlink" title="快速排序"></a>快速排序</h1><ol><li>快速排序是在数组中随机的找一个值,将比这个值小的数放在该数的左边,大的放在右边,然后左右分别递归。</li><li>将每次递归的左右端点设为<code>l, r</code></li><li>首先在数组中随机取一个值存放在<code>x</code>中,我直接取数组中间位置的值。<code>int x = a[l + r >> 1]</code>(<code>>></code>是位运算)</li><li>然后定义两个指针<code>i, j</code>分别从左往右和从右往左。</li><li>先将<code>i</code>从左往右移动,直到遇到一个比<code>x</code>大的数就停下。<code>j</code>相反。</li><li>然后判断<code>i < j</code>成立的话就交换两个数。</li></ol><div class="code-wrapper"><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token keyword">void</span> <span class="token function">qs</span><span class="token punctuation">(</span><span class="token keyword">int</span> l<span class="token punctuation">,</span> <span class="token keyword">int</span> r<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>l <span class="token operator">>=</span> r<span class="token punctuation">)</span> <span class="token keyword">return</span><span class="token punctuation">;</span><span class="token comment">//此时只有一个值,直接返回</span> <span class="token keyword">int</span> x <span class="token operator">=</span> a<span class="token punctuation">[</span><span class="token punctuation">(</span>r <span class="token operator">-</span> l <span class="token operator">>></span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">+</span> l<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//取中间值作为随机值</span> <span class="token keyword">int</span> i <span class="token operator">=</span> l <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">,</span> j <span class="token operator">=</span> r <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span><span class="token comment">//左右指针指向数组外面1的位置,方便后面操作</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>i <span class="token operator"><</span> j<span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token comment">//调整位置,知道i < j</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>a<span class="token punctuation">[</span><span class="token operator">++</span>i<span class="token punctuation">]</span> <span class="token operator"><</span> x<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>a<span class="token punctuation">[</span><span class="token operator">--</span>j<span class="token punctuation">]</span> <span class="token operator">></span> x<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>i <span class="token operator"><</span> j<span class="token punctuation">)</span> <span class="token function">swap</span><span class="token punctuation">(</span>a<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span> a<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token function">qs</span><span class="token punctuation">(</span>l<span class="token punctuation">,</span> j<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//递归</span> <span class="token function">qs</span><span class="token punctuation">(</span>j <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> r<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h1 id="归并排序"><a href="#归并排序" class="headerlink" title="归并排序"></a>归并排序</h1><ol><li>二分数组,然后将排好序的左右数组合并成一个数组。</li></ol><div class="code-wrapper"><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token keyword">void</span> <span class="token function">ms</span><span class="token punctuation">(</span><span class="token keyword">int</span> l<span class="token punctuation">,</span> <span class="token keyword">int</span> r<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>l <span class="token operator">>=</span> r<span class="token punctuation">)</span> <span class="token keyword">return</span><span class="token punctuation">;</span><span class="token comment">//此时只有一个值,直接返回</span> <span class="token keyword">int</span> mid <span class="token operator">=</span> <span class="token punctuation">(</span>r <span class="token operator">-</span> l <span class="token operator">>></span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">+</span> l<span class="token punctuation">;</span><span class="token comment">//取中点</span> <span class="token function">ms</span><span class="token punctuation">(</span>l<span class="token punctuation">,</span> mid<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//左</span> <span class="token function">ms</span><span class="token punctuation">(</span>mid <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> r<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//右</span> <span class="token keyword">int</span> i <span class="token operator">=</span> l<span class="token punctuation">,</span> j <span class="token operator">=</span> mid <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> k <span class="token operator">=</span> l<span class="token punctuation">;</span><span class="token comment">//两个指针分别指向左右的第一个位置</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>i <span class="token operator"><=</span> mid <span class="token operator">&&</span> j <span class="token operator"><=</span> r<span class="token punctuation">)</span> tmp<span class="token punctuation">[</span>k<span class="token operator">++</span><span class="token punctuation">]</span> <span class="token operator">=</span> a<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator"><</span> a<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">?</span> a<span class="token punctuation">[</span>i<span class="token operator">++</span><span class="token punctuation">]</span> <span class="token operator">:</span> a<span class="token punctuation">[</span>j<span class="token operator">++</span><span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//比较指针位置大小,然后放进tmp,移动指针</span> <span class="token comment">//将剩下的放进tmp</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>i <span class="token operator"><=</span> mid<span class="token punctuation">)</span> tmp<span class="token punctuation">[</span>k<span class="token operator">++</span><span class="token punctuation">]</span> <span class="token operator">=</span> a<span class="token punctuation">[</span>i<span class="token operator">++</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>j <span class="token operator"><=</span> r<span class="token punctuation">)</span> tmp<span class="token punctuation">[</span>k<span class="token operator">++</span><span class="token punctuation">]</span> <span class="token operator">=</span> a<span class="token punctuation">[</span>j<span class="token operator">++</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> l<span class="token punctuation">;</span> i <span class="token operator"><=</span> r<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span> a<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> tmp<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//排好序之后放回源数组</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h1 id="堆排序"><a href="#堆排序" class="headerlink" title="堆排序"></a>堆排序</h1><ol><li>维护一个完全二叉树,由于是完全二叉树,所以直接用一个数组来模拟不会浪费空间。根节点为<code>i</code>,左右分别为<code>2*i</code>和<code>2*i+1</code></li><li>该二叉树的根节点小于等于两个子节点。(递归定义)</li><li>定义一个<code>down</code>操作,将不满足上述定义的根节点向下移动。</li><li>先直接将所有数值放进数组中</li><li>然后将数组的前半段执行<code>down</code>操作(只需要操作前半段就可以排序整个数组)</li></ol><div class="code-wrapper"><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><iostream></span></span>using namespace std<span class="token punctuation">;</span><span class="token keyword">const</span> <span class="token keyword">int</span> N <span class="token operator">=</span> <span class="token number">1e5</span> <span class="token operator">+</span> <span class="token number">10</span><span class="token punctuation">;</span><span class="token keyword">int</span> h<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">,</span> cnt<span class="token punctuation">,</span> n<span class="token punctuation">,</span> m<span class="token punctuation">;</span><span class="token keyword">void</span> <span class="token function">down</span><span class="token punctuation">(</span><span class="token keyword">int</span> u<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> t <span class="token operator">=</span> u<span class="token punctuation">,</span> uu <span class="token operator">=</span> u <span class="token operator"><<</span> <span class="token number">1</span><span class="token punctuation">;</span><span class="token comment">//计算根节点的2倍,减少后面的计算次数 </span> <span class="token keyword">if</span> <span class="token punctuation">(</span>uu <span class="token operator"><=</span> cnt <span class="token operator">&&</span> h<span class="token punctuation">[</span>uu<span class="token punctuation">]</span> <span class="token operator"><</span> h<span class="token punctuation">[</span>t<span class="token punctuation">]</span><span class="token punctuation">)</span> t <span class="token operator">=</span> uu<span class="token punctuation">;</span><span class="token comment">//和左边比较</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>uu <span class="token operator">+</span><span class="token number">1</span> <span class="token operator"><=</span> cnt <span class="token operator">&&</span> h<span class="token punctuation">[</span>uu <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator"><</span> h<span class="token punctuation">[</span>t<span class="token punctuation">]</span><span class="token punctuation">)</span> t <span class="token operator">=</span> uu <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span><span class="token comment">//和右边比较</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>t <span class="token operator">!=</span> u<span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token comment">//判断是否需要交换</span> <span class="token function">swap</span><span class="token punctuation">(</span>h<span class="token punctuation">[</span>t<span class="token punctuation">]</span><span class="token punctuation">,</span> h<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">down</span><span class="token punctuation">(</span>t<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> cin <span class="token operator">>></span> n <span class="token operator">>></span> m<span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator"><=</span> n<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span> <span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">"%d"</span><span class="token punctuation">,</span> <span class="token operator">&</span>h<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> cnt <span class="token operator">=</span> n<span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> n <span class="token operator">>></span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token punctuation">;</span> <span class="token operator">--</span>i<span class="token punctuation">)</span> <span class="token function">down</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>m<span class="token operator">--</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d "</span><span class="token punctuation">,</span> h<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> h<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> h<span class="token punctuation">[</span>cnt<span class="token operator">--</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token function">down</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h1 id="六种排序算法的性能分析"><a href="#六种排序算法的性能分析" class="headerlink" title="六种排序算法的性能分析"></a>六种排序算法的性能分析</h1><div class="code-wrapper"><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><algorithm></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><cstdlib></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><cstring></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><ctime></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><iostream></span></span>using namespace std<span class="token punctuation">;</span><span class="token keyword">const</span> <span class="token keyword">int</span> N <span class="token operator">=</span> <span class="token number">1e5</span> <span class="token operator">+</span> <span class="token number">10</span><span class="token punctuation">;</span><span class="token keyword">const</span> <span class="token keyword">int</span> M <span class="token operator">=</span> <span class="token number">1e4</span> <span class="token operator">+</span> <span class="token number">10</span><span class="token punctuation">;</span><span class="token keyword">int</span> a<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">,</span> a_bak<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">,</span> tmp<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">,</span> algo<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">,</span> n<span class="token punctuation">,</span> m<span class="token punctuation">,</span> cnt<span class="token punctuation">,</span> all<span class="token punctuation">[</span><span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">[</span>M<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token keyword">char</span> name<span class="token punctuation">[</span><span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">15</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">"Quick Sort"</span><span class="token punctuation">,</span> <span class="token string">"Merge Sort"</span><span class="token punctuation">,</span> <span class="token string">"Heap Sort"</span><span class="token punctuation">,</span> <span class="token string">"Shell Sort"</span><span class="token punctuation">,</span> <span class="token string">"Bubble Sort"</span><span class="token punctuation">,</span> <span class="token string">"Select Sort"</span><span class="token punctuation">}</span><span class="token punctuation">;</span>clock_t st<span class="token punctuation">,</span> ed<span class="token punctuation">;</span><span class="token keyword">void</span> <span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token function">srand</span><span class="token punctuation">(</span><span class="token function">time</span><span class="token punctuation">(</span><span class="token constant">NULL</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator"><=</span> n<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span> algo<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> a<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> a_bak<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">rand</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> a<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> INT32_MIN<span class="token punctuation">;</span> <span class="token function">sort</span><span class="token punctuation">(</span>algo <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> algo <span class="token operator">+</span> n <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token comment">//第x个算法的第y次测试</span><span class="token keyword">void</span> <span class="token function">judge</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span> <span class="token keyword">int</span> y<span class="token punctuation">)</span> <span class="token punctuation">{</span> bool flag <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator"><=</span> n<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>a<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">!=</span> algo<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%s\t Test %d Wrong Anwser\n"</span><span class="token punctuation">,</span> name<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">,</span> y<span class="token punctuation">)</span><span class="token punctuation">;</span> flag <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>flag<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%s\t Test %d Time Spent : %dms\n"</span><span class="token punctuation">,</span> name<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">,</span> y<span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">)</span><span class="token punctuation">(</span>ed <span class="token operator">-</span> st<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> all<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>y<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">)</span>ed <span class="token operator">-</span> st<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token function">memcpy</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> a_bak<span class="token punctuation">,</span> <span class="token punctuation">(</span>n <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token comment">//耗时分析</span><span class="token keyword">void</span> <span class="token function">analyze</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">double</span> ave<span class="token punctuation">,</span> max<span class="token punctuation">,</span> min<span class="token punctuation">,</span> sum<span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> <span class="token number">6</span><span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span> <span class="token punctuation">{</span> min <span class="token operator">=</span> max <span class="token operator">=</span> all<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span> sum <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> j <span class="token operator"><</span> m<span class="token punctuation">;</span> <span class="token operator">++</span>j<span class="token punctuation">)</span> <span class="token punctuation">{</span> sum <span class="token operator">+=</span> all<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>all<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator"><</span> min<span class="token punctuation">)</span> min <span class="token operator">=</span> all<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>all<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">></span> max<span class="token punctuation">)</span> max <span class="token operator">=</span> all<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> ave <span class="token operator">=</span> sum <span class="token operator">/</span> m<span class="token punctuation">;</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%s :\n Fastest : %.2lf\t Slowest : %.2lf\t Average : %.5lf\n\n"</span><span class="token punctuation">,</span> name<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span> min<span class="token punctuation">,</span> max<span class="token punctuation">,</span> ave<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token comment">// debug</span><span class="token keyword">void</span> <span class="token function">display</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator"><=</span> n<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d "</span><span class="token punctuation">,</span> a<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">puts</span><span class="token punctuation">(</span><span class="token string">""</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">void</span> <span class="token function">quick_sort</span><span class="token punctuation">(</span><span class="token keyword">int</span> l<span class="token punctuation">,</span> <span class="token keyword">int</span> r<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>l <span class="token operator">>=</span> r<span class="token punctuation">)</span> <span class="token keyword">return</span><span class="token punctuation">;</span> <span class="token keyword">int</span> x <span class="token operator">=</span> a<span class="token punctuation">[</span><span class="token punctuation">(</span>r <span class="token operator">-</span> l <span class="token operator">>></span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">+</span> l<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">int</span> i <span class="token operator">=</span> l <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">,</span> j <span class="token operator">=</span> r <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>i <span class="token operator"><</span> j<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>a<span class="token punctuation">[</span><span class="token operator">++</span>i<span class="token punctuation">]</span> <span class="token operator"><</span> x<span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>a<span class="token punctuation">[</span><span class="token operator">--</span>j<span class="token punctuation">]</span> <span class="token operator">></span> x<span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>i <span class="token operator"><</span> j<span class="token punctuation">)</span> <span class="token function">swap</span><span class="token punctuation">(</span>a<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span> a<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token function">quick_sort</span><span class="token punctuation">(</span>l<span class="token punctuation">,</span> j<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">quick_sort</span><span class="token punctuation">(</span>j <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> r<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">void</span> <span class="token function">merge_sort</span><span class="token punctuation">(</span><span class="token keyword">int</span> l<span class="token punctuation">,</span> <span class="token keyword">int</span> r<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>l <span class="token operator">>=</span> r<span class="token punctuation">)</span> <span class="token keyword">return</span><span class="token punctuation">;</span> <span class="token keyword">int</span> mid <span class="token operator">=</span> <span class="token punctuation">(</span>r <span class="token operator">-</span> l <span class="token operator">>></span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">+</span> l<span class="token punctuation">;</span> <span class="token function">merge_sort</span><span class="token punctuation">(</span>l<span class="token punctuation">,</span> mid<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">merge_sort</span><span class="token punctuation">(</span>mid <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> r<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">int</span> i <span class="token operator">=</span> l<span class="token punctuation">,</span> j <span class="token operator">=</span> mid <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> k <span class="token operator">=</span> l<span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>i <span class="token operator"><=</span> mid <span class="token operator">&&</span> j <span class="token operator"><=</span> r<span class="token punctuation">)</span> tmp<span class="token punctuation">[</span>k<span class="token operator">++</span><span class="token punctuation">]</span> <span class="token operator">=</span> a<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator"><</span> a<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">?</span> a<span class="token punctuation">[</span>i<span class="token operator">++</span><span class="token punctuation">]</span> <span class="token operator">:</span> a<span class="token punctuation">[</span>j<span class="token operator">++</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>i <span class="token operator"><=</span> mid<span class="token punctuation">)</span> tmp<span class="token punctuation">[</span>k<span class="token operator">++</span><span class="token punctuation">]</span> <span class="token operator">=</span> a<span class="token punctuation">[</span>i<span class="token operator">++</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>j <span class="token operator"><=</span> r<span class="token punctuation">)</span> tmp<span class="token punctuation">[</span>k<span class="token operator">++</span><span class="token punctuation">]</span> <span class="token operator">=</span> a<span class="token punctuation">[</span>j<span class="token operator">++</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> l<span class="token punctuation">;</span> i <span class="token operator"><=</span> r<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span> a<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> tmp<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token comment">// heap down操作</span><span class="token keyword">void</span> <span class="token function">down</span><span class="token punctuation">(</span><span class="token keyword">int</span> u<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> uu <span class="token operator">=</span> u <span class="token operator"><<</span> <span class="token number">1</span><span class="token punctuation">,</span> t <span class="token operator">=</span> u<span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>uu <span class="token operator"><=</span> cnt <span class="token operator">&&</span> a<span class="token punctuation">[</span>t<span class="token punctuation">]</span> <span class="token operator">></span> a<span class="token punctuation">[</span>uu<span class="token punctuation">]</span><span class="token punctuation">)</span> t <span class="token operator">=</span> uu<span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>uu <span class="token operator">+</span> <span class="token number">1</span> <span class="token operator"><=</span> cnt <span class="token operator">&&</span> a<span class="token punctuation">[</span>t<span class="token punctuation">]</span> <span class="token operator">></span> a<span class="token punctuation">[</span>uu <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span> t <span class="token operator">=</span> uu <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>t <span class="token operator">!=</span> u<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token function">swap</span><span class="token punctuation">(</span>a<span class="token punctuation">[</span>t<span class="token punctuation">]</span><span class="token punctuation">,</span> a<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">down</span><span class="token punctuation">(</span>t<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token keyword">void</span> <span class="token function">heap_sort</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> k <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> cnt <span class="token operator">=</span> n<span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> n <span class="token operator">>></span> <span class="token number">1</span><span class="token punctuation">;</span> i<span class="token punctuation">;</span> <span class="token operator">--</span>i<span class="token punctuation">)</span> <span class="token function">down</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>cnt<span class="token punctuation">)</span> <span class="token punctuation">{</span> tmp<span class="token punctuation">[</span>k<span class="token operator">++</span><span class="token punctuation">]</span> <span class="token operator">=</span> a<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span> a<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> a<span class="token punctuation">[</span>cnt<span class="token operator">--</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token function">down</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator"><=</span> n<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span> a<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> tmp<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token comment">// 步长3</span><span class="token comment">// void shell_sort(int n) {</span><span class="token comment">// int h = 1;</span><span class="token comment">// while (h < n / 3) h = 3 * h + 1;</span><span class="token comment">// while (h >= 1) {</span><span class="token comment">// for (int i = h; i < n; i++) {</span><span class="token comment">// for (int j = i; j >= h && a[j] < a[j - h]; j -= h) {</span><span class="token comment">// swap(a[j], a[j - h]);</span><span class="token comment">// }</span><span class="token comment">// }</span><span class="token comment">// h = h / 3;</span><span class="token comment">// }</span><span class="token comment">// }</span><span class="token keyword">int</span> <span class="token function">get_sedgewick_arr</span><span class="token punctuation">(</span><span class="token keyword">int</span> n<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> j<span class="token punctuation">,</span> k <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>true<span class="token punctuation">)</span> <span class="token punctuation">{</span> j <span class="token operator">=</span> <span class="token number">9</span> <span class="token operator">*</span> <span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token number">1</span> <span class="token operator"><<</span> <span class="token number">2</span> <span class="token operator">*</span> i<span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token punctuation">(</span><span class="token number">1</span> <span class="token operator"><<</span> i<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>j <span class="token operator"><=</span> n<span class="token punctuation">)</span> tmp<span class="token punctuation">[</span>k<span class="token operator">++</span><span class="token punctuation">]</span> <span class="token operator">=</span> j<span class="token punctuation">;</span> j <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token number">1</span> <span class="token operator"><<</span> <span class="token number">2</span> <span class="token operator">*</span> i <span class="token operator">+</span> <span class="token number">4</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">3</span> <span class="token operator">*</span> <span class="token punctuation">(</span><span class="token number">1</span> <span class="token operator"><<</span> i <span class="token operator">+</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>j <span class="token operator"><=</span> n<span class="token punctuation">)</span> tmp<span class="token punctuation">[</span>k<span class="token operator">++</span><span class="token punctuation">]</span> <span class="token operator">=</span> j<span class="token punctuation">;</span> <span class="token keyword">else</span> <span class="token keyword">break</span><span class="token punctuation">;</span> i <span class="token operator">+=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> k<span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token comment">// sedgewick步长</span><span class="token keyword">void</span> <span class="token function">shell_sort</span><span class="token punctuation">(</span><span class="token keyword">int</span> n<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> k <span class="token operator">=</span> <span class="token function">get_sedgewick_arr</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">int</span> i<span class="token punctuation">,</span> j<span class="token punctuation">,</span> t<span class="token punctuation">,</span> step<span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token operator">--</span>k<span class="token punctuation">)</span> <span class="token punctuation">{</span> step <span class="token operator">=</span> tmp<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span>i <span class="token operator">=</span> step<span class="token punctuation">;</span> i <span class="token operator"><</span> n<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span> <span class="token punctuation">{</span> j <span class="token operator">=</span> i<span class="token punctuation">;</span> t <span class="token operator">=</span> a<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>j <span class="token operator">>=</span> step<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>t <span class="token operator"><</span> a<span class="token punctuation">[</span>j <span class="token operator">-</span> step<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> a<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> a<span class="token punctuation">[</span>j <span class="token operator">-</span> step<span class="token punctuation">]</span><span class="token punctuation">;</span> j <span class="token operator">-=</span> step<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> a<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> t<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token keyword">void</span> <span class="token function">bubble_sort</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator"><=</span> n<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> n <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span> j <span class="token operator">>=</span> i<span class="token punctuation">;</span> <span class="token operator">--</span>j<span class="token punctuation">)</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>a<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">></span> a<span class="token punctuation">[</span>j <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token function">swap</span><span class="token punctuation">(</span>a<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">,</span> a<span class="token punctuation">[</span>j <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">void</span> <span class="token function">select_sort</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> min<span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator"><=</span> n<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span> <span class="token punctuation">{</span> min <span class="token operator">=</span> i<span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> i <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span> j <span class="token operator"><=</span> n<span class="token punctuation">;</span> <span class="token operator">++</span>j<span class="token punctuation">)</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>a<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator"><</span> a<span class="token punctuation">[</span>min<span class="token punctuation">]</span><span class="token punctuation">)</span> min <span class="token operator">=</span> j<span class="token punctuation">;</span> <span class="token function">swap</span><span class="token punctuation">(</span>a<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span> a<span class="token punctuation">[</span>min<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> cin <span class="token operator">>></span> n <span class="token operator">>></span> m<span class="token punctuation">;</span> <span class="token comment">//每组n个随机数,共测试m组</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> m<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">int</span> k <span class="token operator">=</span> <span class="token number">6</span><span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>k<span class="token operator">--</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> st <span class="token operator">=</span> <span class="token function">clock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">switch</span> <span class="token punctuation">(</span>k<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">case</span> <span class="token number">0</span><span class="token operator">:</span> <span class="token function">quick_sort</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token keyword">case</span> <span class="token number">1</span><span class="token operator">:</span> <span class="token function">merge_sort</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token keyword">case</span> <span class="token number">2</span><span class="token operator">:</span> <span class="token function">heap_sort</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token keyword">case</span> <span class="token number">3</span><span class="token operator">:</span> <span class="token function">shell_sort</span><span class="token punctuation">(</span>n <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token keyword">case</span> <span class="token number">4</span><span class="token operator">:</span> <span class="token function">bubble_sort</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token keyword">case</span> <span class="token number">5</span><span class="token operator">:</span> <span class="token function">select_sort</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> ed <span class="token operator">=</span> <span class="token function">clock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// display();</span> <span class="token function">judge</span><span class="token punctuation">(</span>k<span class="token punctuation">,</span> i<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token function">puts</span><span class="token punctuation">(</span><span class="token string">"=================================================\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token function">analyze</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><p>输入<code>n m</code>分别代表产生乱序数组长度和测试次数。</p>]]></content>
<categories>
<category>算法</category>
</categories>
<tags>
<tag>排序</tag>
</tags>
</entry>
<entry>
<title>记录一下博客搭建历程</title>
<link href="/posts/e348bc59/"/>
<url>/posts/e348bc59/</url>
<content type="html"><![CDATA[<h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>19年8月份开始部署自己的独立博客到现在已经过去了10个月了,这期间断断续续做了很多修改,也踩了很多坑。这几天换了个域名,换了个主题,完全重新部署了博客,现在用起来感觉还不错,应该会持续用一段时间,所以现在写这篇文章总结一下博客部署的细节过程,避免我这个记性不好的人过段时间就不记得自己是怎么操作的了😂,网上有很多大佬写的Github Pages + hexo部署博客的教程,我这里主要是记录我自己的操作步骤和一些我自己的理解。我不是一个专业的前端程序员,所以如果在看这篇文章的大佬发现了错误,请指出。</p><p>最早部署博客的时候,我还是一个大一的学生,很多东西都不太懂,在网上看了很多关于部署博客文章,明白了部署博客的方法之一就是用现有的框架,然后自己做个性化的调整,找了一圈发现目前比较流行、<u>搭建简单</u>适合我这个蒟蒻的框架是<a href="https://hexo.io/zh-cn/">hexo</a>,看了官方文档和其他大佬的搭建教程,就开始找好看的主题了,去年部署使用了<a href="https://blinkfox.github.io/">闪烁之狐</a>大佬的<a href="https://blinkfox.github.io/2018/09/28/qian-duan/hexo-bo-ke-zhu-ti-zhi-hexo-theme-matery-de-jie-shao/">matery</a>主题,作为一个喜新厌旧的渣男这么几个月过去了有一些视觉疲劳了😂,现在改成了<a href="https://zkqiang.cn/">zkqing</a>大佬的<a href="https://hexo.fluid-dev.com/">Fluid</a>,这个简洁又有设计感的风格,很是喜欢。</p><blockquote><p>hexo是一个基于nodejs的静态站点生成框架,不仅搭建简单,还可以将生成的博客直接上传到<a href="github.com">github</a>、<a href="gitlab.com">gitlab</a>、<a href="gitee.com">gitee</a>这些提供Pages服务的平台(下面会对Pages服务做介绍)服务建立静态站点,而且社区生态活跃,有大量有趣实用的插件可供下载。通过hexo,即使我们不怎么会HTML也可以用markdown写出工整的文章,markdown是一种轻量级的标记语言,文件扩展名是“md”,就是在项目文件里常见的README.md文件使用的格式。markdown的语法超级超级超级简单,没有哪个正常人20分钟还学不会基础语法的吧,不会吧,不会吧。话说回来markdown只是一个语法规范,所以我们还需要一个工具来辅助我们编写markdown文件,强烈推荐<a href="https://typora.io/">Typora</a>,不用像vscode那样分屏预览,所见即所得的书写体验是真的awesome。</p></blockquote><blockquote><p>“You can create a website directly from a GitHub repository.”,这是<a href="https://help.github.com/cn/github/working-with-github-pages/getting-started-with-github-pages">Github Pages官方文档</a>里面的介绍,我们可以直接从Github仓库创建静态网站,通过Github Pages,我们就可以不用自己维护一台服务器来托管博客,省钱又省心。</p></blockquote><blockquote><p>全程使用的是window10环境。</p></blockquote><hr><h1 id="准备工作"><a href="#准备工作" class="headerlink" title="准备工作"></a>准备工作</h1><h2 id="安装nodejs"><a href="#安装nodejs" class="headerlink" title="安装nodejs"></a>安装nodejs</h2><p>在这里下载适合自己的<a href="https://nodejs.org/en/download/">nodejs</a>。</p><p>安装过程一路<code>next</code>就可以了,但是如果你觉得不放心那就自己选择。</p><p>安装完成后打开终端,输入:</p><div class="code-wrapper"><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">node -v<span class="token function">npm</span> -v<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre></div><p>看到输出版本号就可以了。(<code>-v</code>改成<code>--version</code>也是一样的)</p><blockquote><p><a href="https://zh.wikipedia.org/wiki/Npm">npm</a>是nodejs默认的包管理工具,我们需要用npm来安装hexo以及一些插件。</p></blockquote><h3 id="添加国内镜像"><a href="#添加国内镜像" class="headerlink" title="添加国内镜像"></a>添加国内镜像</h3><p>如果没有梯子的话,可以使用阿里的国内镜像下载,这样速度会好很多。</p><div class="code-wrapper"><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token function">npm</span> config <span class="token builtin class-name">set</span> registry https://registry.npm.taobao.org<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></div><p>这条命令会写入<code>https://registry.npm.taobao.org</code>到<code>C:\%USERPROFILE%\</code>目录下的<code>.npmrc</code>文件,也可以直接用文本编辑器编辑<code>.npmrc</code>文件。</p><hr><h2 id="安装Git"><a href="#安装Git" class="headerlink" title="安装Git"></a>安装Git</h2><p>Git是当今最流行也是最好用的版本控制工具之一,我们在这里使用Git是为了把本地生成的静态页面推送到github(也可以使用其他平台,或者同时使用)。</p><p>在这里下载适合自己的<a href="https://git-scm.com/downloads">Git</a>。</p><p>安装过程还是一路<code>next</code>就可以了,但是如果你觉得不放心那就自己看着办。</p><p>安装完成后打开终端,输入:</p><div class="code-wrapper"><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token function">git</span> --verison<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></div><h3 id="添加代理"><a href="#添加代理" class="headerlink" title="添加代理"></a>添加代理</h3><p>git没有国内镜像(这个要是有国内镜像那是真的强/狗头),如果没有梯子的话,那<code>git clone</code>可能会很慢,有梯子可以设置一下代理,如下:</p><h4 id="全局代理"><a href="#全局代理" class="headerlink" title="全局代理"></a>全局代理</h4><div class="code-wrapper"><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># socks5协议,1080端口修改成自己的本地代理端口</span><span class="token function">git</span> config --global http.proxy socks5://127.0.0.1:1080<span class="token function">git</span> config --global https.proxy socks5://127.0.0.1:1080<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre></div><h4 id="仅代理github"><a href="#仅代理github" class="headerlink" title="仅代理github"></a>仅代理github</h4><div class="code-wrapper"><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># socks5协议,1080端口修改成自己的本地代理端口</span><span class="token function">git</span> config --global http.https://github.com.proxy socks5://127.0.0.1:1080<span class="token function">git</span> config --global https.https://github.com.proxy socks5://127.0.0.1:1080<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre></div><p>上面的命令会在<code>C:\%USERPROFILE%\</code>目录下的<code>.gitconfig</code>文件写入</p><div class="code-wrapper"><pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">[</span>http <span class="token string">"https://github.com"</span><span class="token punctuation">]</span>proxy = socks5<span class="token operator">:</span><span class="token comment">//127.0.0.1:1080</span><span class="token punctuation">[</span>https <span class="token string">"https://github.com"</span><span class="token punctuation">]</span>proxy = socks5<span class="token operator">:</span><span class="token comment">//127.0.0.1:1080</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre></div><p>用编辑器写入也是可以的。</p><h3 id="连接Github"><a href="#连接Github" class="headerlink" title="连接Github"></a>连接Github</h3><h4 id="配置个人信息"><a href="#配置个人信息" class="headerlink" title="配置个人信息"></a>配置个人信息</h4><p>打开git bash,输入如下命令,注意替换用户名和邮箱。</p><div class="code-wrapper"><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token function">git</span> config --global user.name <span class="token string">"username"</span><span class="token function">git</span> config --global user.email <span class="token string">"email"</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre></div><p>写入的和前面添加代理的是同一个配置文件,同样也可以用编辑器写入。</p><div class="code-wrapper"><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>user<span class="token punctuation">]</span>name <span class="token operator">=</span> usernameemail <span class="token operator">=</span> email<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre></div><h4 id="生成SSH"><a href="#生成SSH" class="headerlink" title="生成SSH"></a>生成<a href="https://zh.wikipedia.org/wiki/Secure_Shell">SSH</a></h4><p>执行如下命令</p><div class="code-wrapper"><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">ssh-keygen -t rsa -C <span class="token string">"email"</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></div><p>这个命令会在你的用户文件夹下的<code>.ssh</code>文件夹下生成<code>id_rsa</code>和<code>id_rsa.pub</code>文件,前者是你的私钥,后者是公钥,接下来就是把公钥提交到Github上,这样就可以安全的传输了。</p><p>使用命令<code>cat ~/.ssh/id_rsa.pub</code>,或者用编辑器打开<code>id_rsa.pub</code>文件都可以,复制里面的全部内容。</p><p>回到github,点击如下图所示的<code>setting</code>,然后点击<code>SSH and GPG keys</code></p><p><img src="https://cdn.jsdelivr.net/gh/oopstls/PicBed/blog/blog/image-20200611163001280.png" alt="ssh"></p><p>然后点击<code>New SSH Key</code>,名字自己决定,内容就粘贴刚刚复制的公钥。</p><p>最后执行下面的命令查看是否成功</p><div class="code-wrapper"><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token function">ssh</span> -T git@github.com<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></div><p><img src="https://cdn.jsdelivr.net/gh/oopstls/PicBed/blog/blog/image-20200611163517010.png" alt="check"></p><p>如上图所示,输出了<code>Hi username!</code>,那么就成功了。</p><hr><h2 id="创建博客仓库"><a href="#创建博客仓库" class="headerlink" title="创建博客仓库"></a>创建博客仓库</h2><p>不会还有人需要注册github的详细步骤吧,不会吧,不会吧。</p><p>在github首页点击右上角的<code>+</code>号,选择<code>New repository</code></p><p><img src="https://gitee.com/oopstls/PicBed/raw/master/blog/blog/image-20200610215943623.png" alt="新建仓库"></p><p>然后在如下页面中填写仓库名(Repository name)</p><blockquote><p>**一般这里的格式都是且必须是username.github.io (username就是前面<code>Owner</code>里面显示的名字)**,原因如下。</p><p>仓库名命名为<code>username.github.io</code>那么访问通过该仓库创建的博客的默认域名就是<code>username.github.io</code>。</p><p>仓库名命名为<code>xxx.github.io</code>,访问地址就是<code>username.github.io/xxx.github.io</code>。</p></blockquote><p>我的username是oopstls,但是这里我用<code>sameple.github.io</code>作为样例。</p><p><code>Description</code>是对这个仓库的描述。</p><p><strong>后面的<code>README</code>一定要勾选,不然后面不能初始化Github Pages。</strong></p><p><em><code>Imgbot</code>是我用来给我的github图床里面的图片压缩的,没有使用过就没有这个选项,不影响后序操作。</em></p><p>然后点击下面的绿色按钮<code>Create repository</code>。</p><p><img src="https://cdn.jsdelivr.net/gh/oopstls/PicBed/blog/blog/image-20200611143035975.png" alt="创建博客"></p><p>然后点击仓库的<code>setting</code>。</p><p><img src="https://cdn.jsdelivr.net/gh/oopstls/PicBed/blog/blog/image-20200611143317463.png" alt="setting"></p><p>向下翻找到<code>Github Pages</code>,点击<code>choose a theme</code>。</p><p><img src="https://cdn.jsdelivr.net/gh/oopstls/PicBed/blog/blog/image-20200611145834783.png" alt="choose a theme"></p><p>然后直接点击<code>select</code>,不用去选主题了,这里只是通过选主题激活Github Pages</p><p><img src="https://cdn.jsdelivr.net/gh/oopstls/PicBed/blog/blog/image-20200611150027154.png" alt="select"></p><p>然后在刚刚Github Pages设置的位置就会显示你的默认链接。</p><p>点击进去就是Github Pages为你生成的个人页面。</p><hr><h2 id="安装初始化Hexo"><a href="#安装初始化Hexo" class="headerlink" title="安装初始化Hexo"></a>安装初始化Hexo</h2><h3 id="安装Hexo"><a href="#安装Hexo" class="headerlink" title="安装Hexo"></a>安装Hexo</h3><p>打开一个已经配置好可以执行npm命令行终端,可以直接用前面安装的git自带的Git Bash。</p><p>输入如下命令安装<b>hexo命令行工具<b/>。</p><div class="code-wrapper"><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token function">npm</span> i hexo-cli -g<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></div><p><img src="https://cdn.jsdelivr.net/gh/oopstls/PicBed/blog/blog/image-20200611154949704.png" alt="install"></p><p>输入命令后如上图所示就安装成功了,也可以再输入命令<code>hexo -v</code>看看。</p><h3 id="初始化Hexo"><a href="#初始化Hexo" class="headerlink" title="初始化Hexo"></a>初始化Hexo</h3><p>在你想用来存放博客文件的存储路径上新建一个文件夹,这个文件夹用来存储博客文件,比如<code>D:\Blog\</code>。</p><p>然后再这个目录下打开终端,或者<code>cd /d/blog</code>(注意这里是我的目录路径,以当前自己的为准,不能随意复制)</p><p>然后执行下面这条命令初始化你的博客文件。</p><div class="code-wrapper"><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">hexo init sample <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></div><p>sample是生成后存储文件的文件夹</p><p><img src="https://cdn.jsdelivr.net/gh/oopstls/PicBed/blog/blog/image-20200611160633299.png" alt="INFO"></p><p>命令执行完成显示上图所示内容就可以了。</p><p>接着就执行<code>npm install</code>安装必要的组件。</p><div class="code-wrapper"><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token function">npm</span> <span class="token function">install</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></div><p>安装完成没有报错的话,那基本环境就配置好了。</p><p>介绍几个后面会常用的命令,也可以在<a href="https://hexo.io/zh-cn/docs/commands">官方文档</a>查看更多。</p><div class="code-wrapper"><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">hexo clean <span class="token comment">#清除已经生成的静态站点文件,最好每次生成之前都clean一下</span>hexo generate <span class="token comment">#生成静态站点文件</span>hexo g <span class="token comment">#上一个命令的简写</span>hexo server <span class="token comment">#启动服务器。默认情况下,访问网址为: http://localhost:4000/</span>hexo s <span class="token comment">#上一个命令的简写</span>hexo deploy <span class="token comment">#部署网站,需要先generate生成静态文件才能部署</span>hexo clean <span class="token operator">&&</span> hexo g <span class="token operator">&&</span> hexo s <span class="token comment">#本地测试的时候可以这么写</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><p>现在执行<code>hexo g && hexo s</code></p><p>然后打开浏览器,在地址栏输入<code>localhost:4000</code>,就可以看到初始博客页面了。</p><p><img src="https://gitee.com/oopstls/PicBed/raw/master/blog/blog/image-20200611165033743.png" alt="local"></p><h2 id="目录结构"><a href="#目录结构" class="headerlink" title="目录结构"></a>目录结构</h2><h3 id="根目录"><a href="#根目录" class="headerlink" title="根目录"></a>根目录</h3><div class="code-wrapper"><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">├── _config.yml<span class="token comment">#博客配置文件</span>├── node_modules├── package-lock.json├── package.json├── scaffolds<span class="token comment">#草稿、文章模板、页面模板</span>├── <span class="token builtin class-name">source</span><span class="token comment">#资源文件夹</span>└── themes<span class="token comment">#主题文件夹</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h3 id="source目录下"><a href="#source目录下" class="headerlink" title="source目录下"></a>source目录下</h3><p><em>没有的文件夹不是自动生成的,而是推荐使用方式(后面介绍)</em></p><div class="code-wrapper"><pre class="line-numbers language-none"><code class="language-none">├── source│ ├── CNAME#自定义域名│ ├── _data#覆盖主题配置│ │ ├── fluid_config.yml#fluid主题覆盖配置│ │ └── fluid_static_prefix.yml│ ├── _posts#文章存储文件夹│ │ └── helloworld.md│ ├── about#自定义关于页│ │ └── index.md│ └── img#作为主题的图片资源文件夹│ ├── avatar.png│ ├── banner│ ├── favicon.png<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h1 id="个性化"><a href="#个性化" class="headerlink" title="个性化"></a>个性化</h1><h2 id="更换主题"><a href="#更换主题" class="headerlink" title="更换主题"></a>更换主题</h2><p>前面hexo初始化完成之后,默认使用的是<code>landspace</code>主题。除了自带的主题外,<a href="https://hexo.io/themes/">hexo官网</a>还罗列了很多好看的主题可供查看和使用。</p><p><img src="https://cdn.jsdelivr.net/gh/oopstls/PicBed/note/POCC/image-20201014164742396.png" alt="image-20201014164742396"></p><p>选择自己喜欢的主题,下载至<code>/theme</code>文件夹,然后再<code>/_config.yml</code>文件中修改<code>theme: landspace</code>,将landspace改为主题存储的文件夹名。</p><p>在hexo5之后,可以直接使用npm命令下载主题,但是下载的主题是存放在<code>/node_modules</code>文件夹下的,需要自定义修改一些东西的时候不是很方便(只改配置文件可以在<code>/source/_data/</code>下覆盖主题配置文件)。</p><p>不同的主题配置方式各不相同,hexo官网列出的主题大多都有较详细的配置说明,可以在主题的github仓库查看README.md文件。</p><p>我现在(至文章最后一次更新)使用的是<a href="https://github.com/fluid-dev/hexo-theme-fluid">Fluid</a>,主题安装过程以及配置过程就不具体演示了,<a href="https://hexo.fluid-dev.com/docs/guide/">官方说明非常详细</a>。</p><h1 id="生成以及上传"><a href="#生成以及上传" class="headerlink" title="生成以及上传"></a>生成以及上传</h1><p>前面已经介绍过博客的生成命令。</p><p>这里再贴一下。</p><div class="code-wrapper"><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">hexo clean <span class="token comment">#清除已经生成的静态站点文件,最好每次生成之前都clean一下</span>hexo generate <span class="token comment">#生成静态站点文件,生成内容在根目录下的public文件夹内</span>hexo g <span class="token comment">#上一个命令的简写</span>hexo server <span class="token comment">#启动服务器。默认情况下,访问网址为: http://localhost:4000/</span>hexo s <span class="token comment">#上一个命令的简写</span>hexo deploy <span class="token comment">#部署网站,需要先generate生成静态文件才能部署</span>hexo clean <span class="token operator">&&</span> hexo g <span class="token operator">&&</span> hexo s <span class="token comment">#本地测试的时候可以这么写</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><p><code>hexo deploy</code>需要先安装对应平台的deploy插件</p><p>如github pages,需要执行下命令安装插件。</p><div class="code-wrapper"><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token function">npm</span> <span class="token function">install</span> hexo-deployer-git --save<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></div><p>当本地测试完成之后,执行<code>hexo d</code>即可上传至仓库。</p><h1 id="绑定域名"><a href="#绑定域名" class="headerlink" title="绑定域名"></a>绑定域名</h1><p>github pages默认的域名是<code>username.github.io</code>。</p><p>建议改为自己的域名的几个原因:</p><ol><li>不够个性化</li><li>不能做cdn加速(国内访问gituhb.io很慢)</li><li>上一条说访问慢,现在貌似已经被墙了(202008xx),不改域名想访问只能修改自己本机的DNS。</li></ol><h2 id="购买域名"><a href="#购买域名" class="headerlink" title="购买域名"></a>购买域名</h2><p>在国内几个大厂都可以购买,如阿里云、腾讯云、百度云…</p><p>购买域名的详细过程大概就是实名、选域名、买域名。</p><p>无脑操作,具体过程就不赘述了。</p><h2 id="Cloudflare"><a href="#Cloudflare" class="headerlink" title="Cloudflare"></a>Cloudflare</h2><p>我这里以阿里云为例</p><h3 id="注册cloudflare"><a href="#注册cloudflare" class="headerlink" title="注册cloudflare"></a>注册<a href="https://cloudflare.com/">cloudflare</a></h3><p>注册过程就不详细展示了</p><h3 id="添加站点"><a href="#添加站点" class="headerlink" title="添加站点"></a>添加站点</h3><p><img src="https://cdn.jsdelivr.net/gh/oopstls/PicBed/note/POCC/image-20201014174143276.png" alt="image-20201014174143276"></p><p><img src="https://cdn.jsdelivr.net/gh/oopstls/PicBed/note/POCC/image-20201014174633561.png" alt="image-20201014174633561"></p><p>填写购买的域名,然后点击添加站点,然后无脑下一步,直到提示修改DNS服务器。</p><h3 id="修改DNS服务器"><a href="#修改DNS服务器" class="headerlink" title="修改DNS服务器"></a>修改DNS服务器</h3><p>打开阿里云域名控制台,点击准备好的域名。</p><p><img src="https://cdn.jsdelivr.net/gh/oopstls/PicBed/note/POCC/image-20201014175006245.png" alt="image-20201014175006245"></p><p>然后点击DNS修改、修改DNS服务器</p><p><img src="https://cdn.jsdelivr.net/gh/oopstls/PicBed/note/POCC/image-20201014175611777.png" alt="image-20201014175611777"></p><p>将刚才Cloudflare给出的两个域名解析服务器粘贴至这里。</p><p><img src="https://cdn.jsdelivr.net/gh/oopstls/PicBed/note/POCC/image-20201014181139698.png" alt="image-20201014181139698"></p><p>点击确定之后返回Cloudflare点击检查。</p><p>等cloudflare检查完成之后执行下面的操作,这个过程会等几分钟到几个小时不等。</p><h3 id="解析加速"><a href="#解析加速" class="headerlink" title="解析加速"></a>解析加速</h3><p>上面点击DNS,然后添加记录,两个记录都是CNAME,分别填入域名和www。确定后面的代理状态为开就可以了。</p><p><img src="https://cdn.jsdelivr.net/gh/oopstls/PicBed/note/POCC/image-20201014180747647.png" alt="image-20201014180747647"></p>]]></content>
<categories>
<category>日常</category>
<category>博客部署</category>
</categories>
<tags>
<tag>blog</tag>
<tag>Github Pages</tag>
<tag>hexo</tag>
</tags>
</entry>
<entry>
<title>POJ 2387 Til the Cows Come Home</title>
<link href="/posts/abe46ed/"/>
<url>/posts/abe46ed/</url>
<content type="html"><![CDATA[<h1 id="【POJ】2387-Til-the-Cows-Come-Home"><a href="#【POJ】2387-Til-the-Cows-Come-Home" class="headerlink" title="【POJ】2387 Til the Cows Come Home"></a>【POJ】2387 Til the Cows Come Home</h1><blockquote><p>题目链接:<a href="https://vjudge.net/contest/66569#problem">poj-2387</a></p></blockquote><hr><h2 id="Dscription"><a href="#Dscription" class="headerlink" title="Dscription"></a>Dscription</h2><p> Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so she wants to get back as quickly as possible. </p><p>Farmer John’s field has N (2 <= N <= 1000) landmarks in it, uniquely numbered 1..N. Landmark 1 is the barn; the apple tree grove in which Bessie stands all day is landmark N. Cows travel in the field using T (1 <= T <= 2000) bidirectional cow-trails of various lengths between the landmarks. Bessie is not confident of her navigation ability, so she always stays on a trail from its start to its end once she starts it. </p><p>Given the trails between the landmarks, determine the minimum distance Bessie must walk to get back to the barn. It is guaranteed that some such route exists. </p><hr><h2 id="Input"><a href="#Input" class="headerlink" title="Input"></a>Input</h2><p> * Line 1: Two integers: T and N </p><p>* Lines 2..T+1: Each line describes a trail as three space-separated integers. The first two integers are the landmarks between which the trail travels. The third integer is the length of the trail, range 1..100. </p><hr><h2 id="Output"><a href="#Output" class="headerlink" title="Output"></a>Output</h2><p> * Line 1: A single integer, the minimum distance that Bessie must travel to get from landmark N to landmark 1. </p><hr><h2 id="Sample-Input"><a href="#Sample-Input" class="headerlink" title="Sample Input"></a>Sample Input</h2><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token number">5</span> <span class="token number">5</span><span class="token number">1</span> <span class="token number">2</span> <span class="token number">20</span><span class="token number">2</span> <span class="token number">3</span> <span class="token number">30</span><span class="token number">3</span> <span class="token number">4</span> <span class="token number">20</span><span class="token number">4</span> <span class="token number">5</span> <span class="token number">20</span><span class="token number">1</span> <span class="token number">5</span> <span class="token number">100</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><hr><h2 id="Sample-Output"><a href="#Sample-Output" class="headerlink" title="Sample Output"></a>Sample Output</h2><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token number">90</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></div><hr><h2 id="Hint"><a href="#Hint" class="headerlink" title="Hint"></a>Hint</h2><p> INPUT DETAILS: </p><p>There are five landmarks. </p><p>OUTPUT DETAILS: </p><p>Bessie can get home by following trails 4, 3, 2, and 1. </p><hr><h2 id="题目理解"><a href="#题目理解" class="headerlink" title="题目理解"></a>题目理解</h2><p> 给定多个点以及部分点与点之间道路的过程,问从点<code>1</code>到点<code>N</code>的最短距离,保证<code>1</code>到<code>N</code>之间有一条通路。</p><p> Dijkstra最短路问题,需要注意的是,本题可能多次输入两个点之间的距离,输入过程需要判断。</p><hr><h2 id="代码1-Dijkstra"><a href="#代码1-Dijkstra" class="headerlink" title="代码1 Dijkstra"></a><em>代码1 Dijkstra</em></h2><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string"><iostream></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string"><cstring></span> </span><span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span><span class="token keyword">const</span> <span class="token keyword">int</span> INF <span class="token operator">=</span> <span class="token number">0x3f3f3f3f</span><span class="token punctuation">;</span><span class="token comment">//定义距离的最大值 </span><span class="token keyword">const</span> <span class="token keyword">int</span> MAXN <span class="token operator">=</span> <span class="token number">1005</span><span class="token punctuation">;</span><span class="token keyword">int</span> mp<span class="token punctuation">[</span>MAXN<span class="token punctuation">]</span><span class="token punctuation">[</span>MAXN<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//记录两点之间的距离 </span><span class="token keyword">int</span> dis<span class="token punctuation">[</span>MAXN<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//记录源点与其他点的距离 </span><span class="token keyword">bool</span> vis<span class="token punctuation">[</span>MAXN<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//访问记录 </span><span class="token keyword">int</span> T<span class="token punctuation">,</span> N<span class="token punctuation">;</span><span class="token keyword">void</span> <span class="token function">Dijkstra</span><span class="token punctuation">(</span><span class="token keyword">int</span> s<span class="token punctuation">,</span> <span class="token keyword">int</span> e<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token comment">//接受起始点和终点 </span><span class="token function">memset</span><span class="token punctuation">(</span>vis<span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>vis<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token function">memset</span><span class="token punctuation">(</span>dis<span class="token punctuation">,</span> INF<span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>dis<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>dis<span class="token punctuation">[</span>s<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator"><=</span> N<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token keyword">int</span> tmp <span class="token operator">=</span> INF<span class="token punctuation">,</span> k <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span><span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> j <span class="token operator"><=</span> N<span class="token punctuation">;</span> <span class="token operator">++</span>j<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token comment">//找出未访问的点中与源点距离最小的点作为新的起始点 </span><span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>vis<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">&&</span> dis<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator"><</span> tmp<span class="token punctuation">)</span><span class="token punctuation">{</span>k <span class="token operator">=</span> j<span class="token punctuation">;</span>tmp <span class="token operator">=</span> dis<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token punctuation">}</span>vis<span class="token punctuation">[</span>k<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span><span class="token keyword">if</span><span class="token punctuation">(</span>k <span class="token operator">==</span> e<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token keyword">return</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> j <span class="token operator"><=</span> N<span class="token punctuation">;</span> <span class="token operator">++</span>j<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token comment">//更新与上一个循环中找出的起始点相连的点与源点的距离 </span><span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>vis<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">{</span>dis<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">min</span><span class="token punctuation">(</span>dis<span class="token punctuation">[</span>k<span class="token punctuation">]</span> <span class="token operator">+</span> mp<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">,</span> dis<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token keyword">while</span><span class="token punctuation">(</span>cin <span class="token operator">>></span> T <span class="token operator">>></span> N<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token function">memset</span><span class="token punctuation">(</span>mp<span class="token punctuation">,</span> INF<span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>mp<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">int</span> p1<span class="token punctuation">,</span> p2<span class="token punctuation">,</span> d<span class="token punctuation">;</span><span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> T<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span><span class="token punctuation">{</span>cin <span class="token operator">>></span> p1 <span class="token operator">>></span> p2 <span class="token operator">>></span> d<span class="token punctuation">;</span><span class="token keyword">if</span><span class="token punctuation">(</span>mp<span class="token punctuation">[</span>p1<span class="token punctuation">]</span><span class="token punctuation">[</span>p2<span class="token punctuation">]</span> <span class="token operator">></span> d<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token comment">//当重复输入两个点的距离时,判定更新 </span>mp<span class="token punctuation">[</span>p1<span class="token punctuation">]</span><span class="token punctuation">[</span>p2<span class="token punctuation">]</span> <span class="token operator">=</span> d<span class="token punctuation">;</span>mp<span class="token punctuation">[</span>p2<span class="token punctuation">]</span><span class="token punctuation">[</span>p1<span class="token punctuation">]</span> <span class="token operator">=</span> d<span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token function">Dijkstra</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> N<span class="token punctuation">)</span><span class="token punctuation">;</span>cout <span class="token operator"><<</span> dis<span class="token punctuation">[</span>N<span class="token punctuation">]</span> <span class="token operator"><<</span> endl<span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><hr><h2 id="代码2-堆优化"><a href="#代码2-堆优化" class="headerlink" title="代码2 堆优化"></a><em>代码2 堆优化</em></h2><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string"><iostream></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string"><cstring></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string"><queue></span></span><span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span><span class="token keyword">const</span> <span class="token keyword">int</span> INF <span class="token operator">=</span> <span class="token number">0x3f3f3f3f</span><span class="token punctuation">;</span><span class="token keyword">const</span> <span class="token keyword">int</span> MAXN <span class="token operator">=</span> <span class="token number">1005</span><span class="token punctuation">;</span><span class="token keyword">int</span> mp<span class="token punctuation">[</span>MAXN<span class="token punctuation">]</span><span class="token punctuation">[</span>MAXN<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token keyword">bool</span> vis<span class="token punctuation">[</span>MAXN<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token keyword">int</span> dis<span class="token punctuation">[</span>MAXN<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">int</span> T<span class="token punctuation">,</span> N<span class="token punctuation">;</span>priority_queue<span class="token operator"><</span>pair<span class="token operator"><</span><span class="token keyword">int</span><span class="token punctuation">,</span> <span class="token keyword">int</span><span class="token operator">></span><span class="token punctuation">,</span> vector<span class="token operator"><</span>pair<span class="token operator"><</span><span class="token keyword">int</span><span class="token punctuation">,</span> <span class="token keyword">int</span><span class="token operator">></span> <span class="token operator">></span><span class="token punctuation">,</span> greater<span class="token operator"><</span>pair<span class="token operator"><</span><span class="token keyword">int</span><span class="token punctuation">,</span> <span class="token keyword">int</span><span class="token operator">></span> <span class="token operator">></span> <span class="token operator">></span> q<span class="token punctuation">;</span><span class="token keyword">void</span> <span class="token function">Dijkstra</span><span class="token punctuation">(</span><span class="token keyword">int</span> s<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token function">memset</span><span class="token punctuation">(</span>vis<span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>vis<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token function">memset</span><span class="token punctuation">(</span>dis<span class="token punctuation">,</span> INF<span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>dis<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>dis<span class="token punctuation">[</span>s<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>q<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> s<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">while</span><span class="token punctuation">(</span><span class="token operator">!</span>q<span class="token punctuation">.</span><span class="token function">empty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token comment">//BFS思路 </span>pair<span class="token operator"><</span><span class="token keyword">int</span><span class="token punctuation">,</span> <span class="token keyword">int</span><span class="token operator">></span> p<span class="token punctuation">;</span>p <span class="token operator">=</span> q<span class="token punctuation">.</span><span class="token function">top</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>q<span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">if</span><span class="token punctuation">(</span>vis<span class="token punctuation">[</span>p<span class="token punctuation">.</span>second<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token keyword">continue</span><span class="token punctuation">;</span>vis<span class="token punctuation">[</span>p<span class="token punctuation">.</span>second<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span><span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator"><=</span> N<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>vis<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">&&</span> dis<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">></span> dis<span class="token punctuation">[</span>p<span class="token punctuation">.</span>second<span class="token punctuation">]</span> <span class="token operator">+</span> mp<span class="token punctuation">[</span>p<span class="token punctuation">.</span>second<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">{</span>dis<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> dis<span class="token punctuation">[</span>p<span class="token punctuation">.</span>second<span class="token punctuation">]</span> <span class="token operator">+</span> mp<span class="token punctuation">[</span>p<span class="token punctuation">.</span>second<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>q<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token function">make_pair</span><span class="token punctuation">(</span>dis<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span> i<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token keyword">while</span><span class="token punctuation">(</span>cin <span class="token operator">>></span> T <span class="token operator">>></span> N<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token function">memset</span><span class="token punctuation">(</span>mp<span class="token punctuation">,</span> INF<span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>mp<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> T<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token keyword">int</span> p1<span class="token punctuation">,</span> p2<span class="token punctuation">,</span> d<span class="token punctuation">;</span>cin <span class="token operator">>></span> p1 <span class="token operator">>></span> p2 <span class="token operator">>></span> d<span class="token punctuation">;</span><span class="token keyword">if</span><span class="token punctuation">(</span>mp<span class="token punctuation">[</span>p1<span class="token punctuation">]</span><span class="token punctuation">[</span>p2<span class="token punctuation">]</span> <span class="token operator">></span> d<span class="token punctuation">)</span><span class="token punctuation">{</span>mp<span class="token punctuation">[</span>p1<span class="token punctuation">]</span><span class="token punctuation">[</span>p2<span class="token punctuation">]</span> <span class="token operator">=</span> d<span class="token punctuation">;</span>mp<span class="token punctuation">[</span>p2<span class="token punctuation">]</span><span class="token punctuation">[</span>p1<span class="token punctuation">]</span> <span class="token operator">=</span> d<span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token function">Dijkstra</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>cout <span class="token operator"><<</span> dis<span class="token punctuation">[</span>N<span class="token punctuation">]</span> <span class="token operator"><<</span> endl<span class="token punctuation">;</span><span class="token punctuation">}</span> <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>]]></content>
<categories>
<category>算法</category>
</categories>
<tags>
<tag>POJ</tag>
<tag>Dijkstra</tag>
<tag>图论</tag>
<tag>优先队列</tag>
<tag>堆</tag>
</tags>
</entry>
<entry>
<title>POJ 3279 FLiptile</title>
<link href="/posts/fba0a2d1/"/>
<url>/posts/fba0a2d1/</url>
<content type="html"><![CDATA[<h1 id="【POJ】-3279-Fliptile"><a href="#【POJ】-3279-Fliptile" class="headerlink" title="【POJ】 3279 Fliptile"></a>【POJ】 3279 Fliptile</h1><blockquote><p> 题目链接:<a href="https://vjudge.net/contest/65959#problem/D">poj-3279</a></p></blockquote><h2 id="Description"><a href="#Description" class="headerlink" title="Description"></a>Description</h2><p>Farmer John knows that an intellectually satisfied cow is a happy cow who will give more milk. He has arranged a brainy activity for cows in which they manipulate an <em>M</em> × <em>N</em> grid (1 ≤ <em>M</em> ≤ 15; 1 ≤ <em>N</em> ≤ 15) of square tiles, each of which is colored black on one side and white on the other side.</p><p>As one would guess, when a single white tile is flipped, it changes to black; when a single black tile is flipped, it changes to white. The cows are rewarded when they flip the tiles so that each tile has the white side face up. However, the cows have rather large hooves and when they try to flip a certain tile, they also flip all the adjacent tiles (tiles that share a full edge with the flipped tile). Since the flips are tiring, the cows want to minimize the number of flips they have to make.</p><p>Help the cows determine the minimum number of flips required, and the locations to flip to achieve that minimum. If there are multiple ways to achieve the task with the minimum amount of flips, return the one with the least lexicographical ordering in the output when considered as a string. If the task is impossible, print one line with the word “IMPOSSIBLE”.</p><hr><h2 id="Input"><a href="#Input" class="headerlink" title="Input"></a>Input</h2><p> Line 1: Two space-separated integers: <em>M</em> and <em>N</em><br>Lines 2.. <em>M</em>+1: Line <em>i</em>+1 describes the colors (left to right) of row i of the grid with <em>N</em> space-separated integers which are 1 for black and 0 for white </p><hr><h2 id="Output"><a href="#Output" class="headerlink" title="Output"></a>Output</h2><p> Lines 1.. <em>M</em>: Each line contains <em>N</em> space-separated integers, each specifying how many times to flip that particular location. </p><hr><h2 id="Sample-Input"><a href="#Sample-Input" class="headerlink" title="Sample Input"></a>Sample Input</h2><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token number">4</span> <span class="token number">4</span><span class="token number">1</span> <span class="token number">0</span> <span class="token number">0</span> <span class="token number">1</span><span class="token number">0</span> <span class="token number">1</span> <span class="token number">1</span> <span class="token number">0</span><span class="token number">0</span> <span class="token number">1</span> <span class="token number">1</span> <span class="token number">0</span><span class="token number">1</span> <span class="token number">0</span> <span class="token number">0</span> <span class="token number">1</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><hr><h2 id="Sample-Output"><a href="#Sample-Output" class="headerlink" title="Sample Output"></a>Sample Output</h2><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token number">0</span> <span class="token number">0</span> <span class="token number">0</span> <span class="token number">0</span><span class="token number">1</span> <span class="token number">0</span> <span class="token number">0</span> <span class="token number">1</span><span class="token number">1</span> <span class="token number">0</span> <span class="token number">0</span> <span class="token number">1</span><span class="token number">0</span> <span class="token number">0</span> <span class="token number">0</span> <span class="token number">0</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre></div><hr><h2 id="题目理解"><a href="#题目理解" class="headerlink" title="题目理解"></a>题目理解</h2><p> 给定一个<code>M * N</code>的棋盘,上面有<code>M * N</code>个棋子,每个棋子有上下两面,一面为黑色,另一面为白色,用<code>1</code>表示黑色,用<code>0</code>表示黑色,现在FJ的牛用蹄子去翻动棋子,目的是将所有的棋子都翻转为白色,但是牛的蹄子比较大,每次反转中心的一个棋子都会同时反转周围的四个棋子。</p><p> 如果可以将所有棋子都翻转为白色,则输出每个棋子的翻转情况,<code>1</code>代表该棋子要翻转,<code>0</code>代表该棋子不需要翻转,特别的,如果有多个答案,则输出翻转次数最小且字符排序最小的答案。如果不存在全部反转为白色的可能,则输出<code>IMPOSSIBLE</code>。</p><p> 每一个棋子翻转两次就复原了,所以每个棋子只有翻和不翻两种可能,在棋盘较小的情况下可以直接暴力枚举,本题<code>1 <= M, N <= 15</code>显然会TLE。</p><p> 这里我们不妨枚举第一行的所有情况,有<code>2 ^ N</code>种情况,不管第一行是否全为白色,不是白色的我们交给第二行来翻转,这样我们就可以根据第一行的情况来确定第二行的情况,然后根据第二行的情况来确定第三行的情况……最后确定到最后一行,当最后一行确定后,我们查看最后一行的颜色情况,如果最后一行中含有黑色,则继续枚举过程,如果最后一行全为白色,则找到答案。</p><p> 由于每一个棋子只有翻与不翻两种情况,故可以使用二进制来表示,使用二进制来枚举第一行的所有情况,既可以表示翻转情况,又兼顾了字符序,二进制从小到大枚举,先被找到答案字符序小于后被找到的答案。</p><hr><h2 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h2><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string"><iostream></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string"><cstring></span></span><span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span><span class="token keyword">int</span> m<span class="token punctuation">,</span> n<span class="token punctuation">,</span> step<span class="token punctuation">,</span> tmp<span class="token punctuation">;</span><span class="token keyword">int</span> mp<span class="token punctuation">[</span><span class="token number">20</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">20</span><span class="token punctuation">]</span><span class="token punctuation">,</span> vis<span class="token punctuation">[</span><span class="token number">20</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">20</span><span class="token punctuation">]</span><span class="token punctuation">,</span> ans<span class="token punctuation">[</span><span class="token number">20</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">20</span><span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token keyword">int</span> dy<span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">}</span><span class="token punctuation">;</span><span class="token comment">//用来遍历正在判定的棋子四周棋子的情况 </span><span class="token keyword">int</span> dx<span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">}</span><span class="token punctuation">;</span><span class="token keyword">bool</span> <span class="token function">isBlack</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span> <span class="token keyword">int</span> y<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token comment">//判断遍历到的棋子当前的颜色 </span><span class="token keyword">int</span> t <span class="token operator">=</span> mp<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>y<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//t为单数则是黑色,反之为白色,因为翻转两次就还原 </span><span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> <span class="token number">4</span><span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token comment">//遍历该棋子四周棋子是否翻转过 </span><span class="token keyword">if</span><span class="token punctuation">(</span>vis<span class="token punctuation">[</span>x <span class="token operator">+</span> dx<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">[</span>y <span class="token operator">+</span> dy<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">++</span>t<span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">return</span> t <span class="token operator">&</span> <span class="token number">1</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">void</span> <span class="token function">DFS</span><span class="token punctuation">(</span><span class="token keyword">int</span> t<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> m <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator">>=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token operator">--</span>i<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token comment">//该循环遍历自一行的所有情况,使用到位运算 </span><span class="token keyword">if</span><span class="token punctuation">(</span>t <span class="token operator">&</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">{</span>vis<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span><span class="token operator">++</span>step<span class="token punctuation">;</span><span class="token punctuation">}</span>t <span class="token operator">>>=</span> <span class="token number">1</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator"><</span> m<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span><span class="token comment">//该循环遍历第二行到最后一行,确定除第一行外的每一个棋子的翻转情况 </span><span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> j <span class="token operator"><</span> n<span class="token punctuation">;</span> <span class="token operator">++</span>j<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">isBlack</span><span class="token punctuation">(</span>i <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">,</span> j<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token comment">//如果相邻的上一行的棋子是黑色,则该棋子需要翻转 </span>vis<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span><span class="token operator">++</span>step<span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> n<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span><span class="token comment">//遍历最后一行是否全为白色 </span><span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">isBlack</span><span class="token punctuation">(</span>m <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">,</span> i<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token comment">//如果出现黑色则返回,进行下一种情况的搜索 </span><span class="token keyword">return</span><span class="token punctuation">;</span><span class="token keyword">if</span><span class="token punctuation">(</span>step <span class="token operator"><</span> tmp<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token comment">//更新记录翻转次数最小的情况 </span>tmp <span class="token operator">=</span> step<span class="token punctuation">;</span><span class="token function">memcpy</span><span class="token punctuation">(</span>ans<span class="token punctuation">,</span> vis<span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>vis<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">return</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token keyword">while</span><span class="token punctuation">(</span>cin <span class="token operator">>></span> m <span class="token operator">>></span> n<span class="token punctuation">)</span><span class="token punctuation">{</span>tmp <span class="token operator">=</span> <span class="token number">32769</span><span class="token punctuation">;</span><span class="token function">memset</span><span class="token punctuation">(</span>ans<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>vis<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> m<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span><span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> j <span class="token operator"><</span> n<span class="token punctuation">;</span> <span class="token operator">++</span>j<span class="token punctuation">)</span>cin <span class="token operator">>></span> mp<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> <span class="token punctuation">(</span><span class="token number">1</span> <span class="token operator"><<</span> n<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token comment">//遍历第一行的2 ^ n种情况 </span><span class="token function">memset</span><span class="token punctuation">(</span>vis<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>vis<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>step <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token function">DFS</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">if</span><span class="token punctuation">(</span>tmp <span class="token operator">!=</span> <span class="token number">32769</span><span class="token punctuation">)</span> <span class="token comment">//32768 = 2 ^ 15, 总翻转次数必然小于该数值,以此来判断是否有可能 </span><span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> m<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span><span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> j <span class="token operator"><</span> n<span class="token punctuation">;</span> <span class="token operator">++</span>j<span class="token punctuation">)</span>cout <span class="token operator"><<</span> ans<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator"><<</span> <span class="token punctuation">(</span>j <span class="token operator">==</span> n <span class="token operator">-</span> <span class="token number">1</span> <span class="token operator">?</span> <span class="token string">'\n'</span> <span class="token operator">:</span> <span class="token string">' '</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">else</span>cout <span class="token operator"><<</span> <span class="token string">"IMPOSSIBLE"</span> <span class="token operator"><<</span> endl<span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>]]></content>
<categories>
<category>算法</category>
</categories>
<tags>
<tag>DFS</tag>
<tag>POJ</tag>
<tag>状态压缩</tag>
</tags>
</entry>
<entry>
<title>算法竞赛模板</title>
<link href="/posts/aabf0cad/"/>
<url>/posts/aabf0cad/</url>
<content type="html"><![CDATA[<h1 id="一、基础"><a href="#一、基础" class="headerlink" title="一、基础"></a>一、基础</h1><h2 id="1-二分"><a href="#1-二分" class="headerlink" title="1. 二分"></a>1. 二分</h2><p> <em>具有单调性的一组数据</em></p><h3 id="1-1-整数二分查找"><a href="#1-1-整数二分查找" class="headerlink" title="1.1. 整数二分查找"></a>1.1. 整数二分查找</h3><div class="code-wrapper"><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token comment">//有序的两种属性A,B</span><span class="token keyword">while</span><span class="token punctuation">(</span>L <span class="token operator"><</span> R<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token comment">//左边界的右端点</span><span class="token keyword">int</span> mid <span class="token operator">=</span> L <span class="token operator">+</span> R <span class="token operator">+</span> <span class="token number">1</span> <span class="token operator">>></span> <span class="token number">1</span><span class="token punctuation">;</span><span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">check</span><span class="token punctuation">(</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span><span class="token punctuation">)</span> L <span class="token operator">=</span> mid<span class="token punctuation">;</span> <span class="token keyword">else</span> R <span class="token operator">=</span> mid <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">while</span><span class="token punctuation">(</span>L <span class="token operator"><</span> R<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token comment">//右边界的左端点</span> <span class="token keyword">int</span> mid <span class="token operator">=</span> L <span class="token operator">+</span> R <span class="token operator">>></span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">check</span><span class="token punctuation">(</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span><span class="token punctuation">)</span> R <span class="token operator">=</span> mid<span class="token punctuation">;</span> <span class="token keyword">else</span> L <span class="token operator">=</span> mid <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h3 id="1-2-实数二分查找"><a href="#1-2-实数二分查找" class="headerlink" title="1.2. 实数二分查找"></a>1.2. 实数二分查找</h3><div class="code-wrapper"><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token comment">//求根</span><span class="token keyword">while</span><span class="token punctuation">(</span>L <span class="token operator">-</span> R <span class="token operator">></span> <span class="token number">1e-6</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token comment">//精度1e-4(多取两位)</span><span class="token keyword">double</span> mid <span class="token operator">=</span> <span class="token punctuation">(</span>L <span class="token operator">+</span> R<span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">check</span><span class="token punctuation">(</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span><span class="token punctuation">)</span> L <span class="token operator">=</span> mid<span class="token punctuation">;</span> <span class="token keyword">else</span> R <span class="token operator">=</span> mid<span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h3 id="2-离散化"><a href="#2-离散化" class="headerlink" title="2.离散化"></a>2.离散化</h3><div class="code-wrapper"><pre class="line-numbers language-c" data-language="c"><code class="language-c">vector<span class="token operator"><</span><span class="token keyword">int</span><span class="token operator">></span> alls<span class="token punctuation">;</span> <span class="token comment">// 存储所有待离散化的值</span><span class="token function">sort</span><span class="token punctuation">(</span>alls<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> alls<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 将所有值排序</span>alls<span class="token punctuation">.</span><span class="token function">erase</span><span class="token punctuation">(</span><span class="token function">unique</span><span class="token punctuation">(</span>alls<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> alls<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span> alls<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 去掉重复元素</span><span class="token comment">// 二分求出x对应的离散化的值</span><span class="token comment">// 找到第一个大于等于x的位置</span><span class="token keyword">int</span> <span class="token function">find</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> l <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> r <span class="token operator">=</span> alls<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>l <span class="token operator"><</span> r<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> mid <span class="token operator">=</span> l <span class="token operator">+</span> r <span class="token operator">>></span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>alls<span class="token punctuation">[</span>mid<span class="token punctuation">]</span> <span class="token operator">>=</span> x<span class="token punctuation">)</span> r <span class="token operator">=</span> mid<span class="token punctuation">;</span> <span class="token keyword">else</span> l <span class="token operator">=</span> mid <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> r <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token comment">// 映射到1, 2, ...n</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h3 id="3-区间合并"><a href="#3-区间合并" class="headerlink" title="3.区间合并"></a>3.区间合并</h3><div class="code-wrapper"><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token comment">// 将所有存在交集的区间合并</span><span class="token keyword">void</span> <span class="token function">merge</span><span class="token punctuation">(</span>vector<span class="token operator"><</span>PII<span class="token operator">></span> <span class="token operator">&</span>segs<span class="token punctuation">)</span> <span class="token punctuation">{</span> vector<span class="token operator"><</span>PII<span class="token operator">></span> res<span class="token punctuation">;</span> <span class="token function">sort</span><span class="token punctuation">(</span>segs<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> segs<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">int</span> st <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">2e9</span><span class="token punctuation">,</span> ed <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">2e9</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">auto</span> seg <span class="token operator">:</span> segs<span class="token punctuation">)</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>ed <span class="token operator"><</span> seg<span class="token punctuation">.</span>first<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>st <span class="token operator">!=</span> <span class="token operator">-</span><span class="token number">2e9</span><span class="token punctuation">)</span> res<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span><span class="token punctuation">{</span>st<span class="token punctuation">,</span> ed<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> st <span class="token operator">=</span> seg<span class="token punctuation">.</span>first<span class="token punctuation">,</span> ed <span class="token operator">=</span> seg<span class="token punctuation">.</span>second<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> ed <span class="token operator">=</span> <span class="token function">max</span><span class="token punctuation">(</span>ed<span class="token punctuation">,</span> seg<span class="token punctuation">.</span>second<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>st <span class="token operator">!=</span> <span class="token operator">-</span><span class="token number">2e9</span><span class="token punctuation">)</span> res<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span><span class="token punctuation">{</span>st<span class="token punctuation">,</span> ed<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> segs <span class="token operator">=</span> res<span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h2 id="4-排序"><a href="#4-排序" class="headerlink" title="4.排序"></a>4.排序</h2><h3 id="4-1-快速排序"><a href="#4-1-快速排序" class="headerlink" title="4.1 快速排序"></a>4.1 快速排序</h3><div class="code-wrapper"><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token keyword">void</span> <span class="token function">qs</span><span class="token punctuation">(</span><span class="token keyword">int</span> l<span class="token punctuation">,</span> <span class="token keyword">int</span> r<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>l <span class="token operator">>=</span> r<span class="token punctuation">)</span> <span class="token keyword">return</span><span class="token punctuation">;</span> <span class="token keyword">int</span> x <span class="token operator">=</span> a<span class="token punctuation">[</span>l <span class="token operator">+</span> r <span class="token operator">>></span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">int</span> i <span class="token operator">=</span> l <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">,</span> j <span class="token operator">=</span> r <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>i <span class="token operator"><</span> j<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>a<span class="token punctuation">[</span><span class="token operator">++</span>i<span class="token punctuation">]</span> <span class="token operator"><</span> x<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>a<span class="token punctuation">[</span><span class="token operator">--</span>j<span class="token punctuation">]</span> <span class="token operator">></span> x<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>i <span class="token operator"><</span> j<span class="token punctuation">)</span> <span class="token function">swap</span><span class="token punctuation">(</span>a<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span> a<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token function">qs</span><span class="token punctuation">(</span>l<span class="token punctuation">,</span> j<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">qs</span><span class="token punctuation">(</span>j <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> r<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h3 id="4-2-归并排序"><a href="#4-2-归并排序" class="headerlink" title="4.2 归并排序"></a>4.2 归并排序</h3><div class="code-wrapper"><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token keyword">void</span> <span class="token function">ms</span><span class="token punctuation">(</span><span class="token keyword">int</span> l<span class="token punctuation">,</span> <span class="token keyword">int</span> r<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>l <span class="token operator">>=</span> r<span class="token punctuation">)</span> <span class="token keyword">return</span><span class="token punctuation">;</span> <span class="token keyword">int</span> mid <span class="token operator">=</span> l <span class="token operator">+</span> r <span class="token operator">>></span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token function">ms</span><span class="token punctuation">(</span>l<span class="token punctuation">,</span> mid<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">ms</span><span class="token punctuation">(</span>mid <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> r<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">int</span> i <span class="token operator">=</span> l<span class="token punctuation">,</span> j <span class="token operator">=</span> mid <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> k <span class="token operator">=</span> l<span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>i <span class="token operator"><=</span> mid <span class="token operator">&&</span> j <span class="token operator"><=</span> r<span class="token punctuation">)</span> tmp<span class="token punctuation">[</span>k<span class="token operator">++</span><span class="token punctuation">]</span> <span class="token operator">=</span> a<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator"><</span> a<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">?</span> a<span class="token punctuation">[</span>i<span class="token operator">++</span><span class="token punctuation">]</span> <span class="token operator">:</span> a<span class="token punctuation">[</span>j<span class="token operator">++</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>i <span class="token operator"><=</span> mid<span class="token punctuation">)</span> tmp<span class="token punctuation">[</span>k<span class="token operator">++</span><span class="token punctuation">]</span> <span class="token operator">=</span> a<span class="token punctuation">[</span>i<span class="token operator">++</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>j <span class="token operator"><=</span> r<span class="token punctuation">)</span> tmp<span class="token punctuation">[</span>k<span class="token operator">++</span><span class="token punctuation">]</span> <span class="token operator">=</span> a<span class="token punctuation">[</span>j<span class="token operator">++</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> l<span class="token punctuation">;</span> i <span class="token operator"><=</span> r<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span> a<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> tmp<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h3 id="4-3-堆排序"><a href="#4-3-堆排序" class="headerlink" title="4.3 堆排序"></a>4.3 堆排序</h3><div class="code-wrapper"><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><iostream></span></span>using namespace std<span class="token punctuation">;</span><span class="token keyword">const</span> <span class="token keyword">int</span> N <span class="token operator">=</span> <span class="token number">1e5</span> <span class="token operator">+</span> <span class="token number">10</span><span class="token punctuation">;</span><span class="token keyword">int</span> h<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">,</span> cnt<span class="token punctuation">,</span> n<span class="token punctuation">,</span> m<span class="token punctuation">;</span><span class="token keyword">void</span> <span class="token function">down</span><span class="token punctuation">(</span><span class="token keyword">int</span> u<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> t <span class="token operator">=</span> u<span class="token punctuation">,</span> uu <span class="token operator">=</span> u <span class="token operator"><<</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>uu <span class="token operator"><=</span> cnt <span class="token operator">&&</span> h<span class="token punctuation">[</span>uu<span class="token punctuation">]</span> <span class="token operator"><</span> h<span class="token punctuation">[</span>t<span class="token punctuation">]</span><span class="token punctuation">)</span> t <span class="token operator">=</span> uu<span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>uu <span class="token operator">+</span><span class="token number">1</span> <span class="token operator"><=</span> cnt <span class="token operator">&&</span> h<span class="token punctuation">[</span>uu <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator"><</span> h<span class="token punctuation">[</span>t<span class="token punctuation">]</span><span class="token punctuation">)</span> t <span class="token operator">=</span> uu <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>t <span class="token operator">!=</span> u<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token function">swap</span><span class="token punctuation">(</span>h<span class="token punctuation">[</span>t<span class="token punctuation">]</span><span class="token punctuation">,</span> h<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">down</span><span class="token punctuation">(</span>t<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> cin <span class="token operator">>></span> n <span class="token operator">>></span> m<span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator"><=</span> n<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span> <span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">"%d"</span><span class="token punctuation">,</span> <span class="token operator">&</span>h<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> cnt <span class="token operator">=</span> n<span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> n <span class="token operator">>></span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token punctuation">;</span> <span class="token operator">--</span>i<span class="token punctuation">)</span> <span class="token function">down</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>m<span class="token operator">--</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d "</span><span class="token punctuation">,</span> h<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> h<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> h<span class="token punctuation">[</span>cnt<span class="token operator">--</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token function">down</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h2 id="5-高精度"><a href="#5-高精度" class="headerlink" title="5. 高精度"></a>5. 高精度</h2><h3 id="5-1-高精度加法"><a href="#5-1-高精度加法" class="headerlink" title="5.1 高精度加法"></a>5.1 高精度加法</h3><div class="code-wrapper"><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token comment">// C = A + B, A >= 0, B >= 0</span>vector<span class="token operator"><</span><span class="token keyword">int</span><span class="token operator">></span> <span class="token function">add</span><span class="token punctuation">(</span>vector<span class="token operator"><</span><span class="token keyword">int</span><span class="token operator">></span> <span class="token operator">&</span>A<span class="token punctuation">,</span> vector<span class="token operator"><</span><span class="token keyword">int</span><span class="token operator">></span> <span class="token operator">&</span>B<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>A<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator"><</span> B<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token function">add</span><span class="token punctuation">(</span>B<span class="token punctuation">,</span> A<span class="token punctuation">)</span><span class="token punctuation">;</span> vector<span class="token operator"><</span><span class="token keyword">int</span><span class="token operator">></span> C<span class="token punctuation">;</span> <span class="token keyword">int</span> t <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> A<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> i <span class="token operator">++</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> t <span class="token operator">+=</span> A<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>i <span class="token operator"><</span> B<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> t <span class="token operator">+=</span> B<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span> C<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span>t <span class="token operator">%</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span> t <span class="token operator">/=</span> <span class="token number">10</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>t<span class="token punctuation">)</span> C<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span>t<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> C<span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h3 id="5-2-高精度减法"><a href="#5-2-高精度减法" class="headerlink" title="5.2 高精度减法"></a>5.2 高精度减法</h3><div class="code-wrapper"><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token comment">// C = A - B, 满足A >= B, A >= 0, B >= 0</span>vector<span class="token operator"><</span><span class="token keyword">int</span><span class="token operator">></span> <span class="token function">sub</span><span class="token punctuation">(</span>vector<span class="token operator"><</span><span class="token keyword">int</span><span class="token operator">></span> <span class="token operator">&</span>A<span class="token punctuation">,</span> vector<span class="token operator"><</span><span class="token keyword">int</span><span class="token operator">></span> <span class="token operator">&</span>B<span class="token punctuation">)</span><span class="token punctuation">{</span> vector<span class="token operator"><</span><span class="token keyword">int</span><span class="token operator">></span> C<span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> t <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> A<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> i <span class="token operator">++</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> t <span class="token operator">=</span> A<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">-</span> t<span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>i <span class="token operator"><</span> B<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> t <span class="token operator">-=</span> B<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span> C<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span><span class="token punctuation">(</span>t <span class="token operator">+</span> <span class="token number">10</span><span class="token punctuation">)</span> <span class="token operator">%</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>t <span class="token operator"><</span> <span class="token number">0</span><span class="token punctuation">)</span> t <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token keyword">else</span> t <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>C<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">></span> <span class="token number">1</span> <span class="token operator">&&</span> C<span class="token punctuation">.</span><span class="token function">back</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> C<span class="token punctuation">.</span><span class="token function">pop_back</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> C<span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h3 id="5-3-高精度乘低精度"><a href="#5-3-高精度乘低精度" class="headerlink" title="5.3 高精度乘低精度"></a>5.3 高精度乘低精度</h3><div class="code-wrapper"><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token comment">// A / b = C ... r, A >= 0, b > 0</span>vector<span class="token operator"><</span><span class="token keyword">int</span><span class="token operator">></span> <span class="token function">div</span><span class="token punctuation">(</span>vector<span class="token operator"><</span><span class="token keyword">int</span><span class="token operator">></span> <span class="token operator">&</span>A<span class="token punctuation">,</span> <span class="token keyword">int</span> b<span class="token punctuation">,</span> <span class="token keyword">int</span> <span class="token operator">&</span>r<span class="token punctuation">)</span><span class="token punctuation">{</span> vector<span class="token operator"><</span><span class="token keyword">int</span><span class="token operator">></span> C<span class="token punctuation">;</span> r <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> A<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator">>=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">--</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> r <span class="token operator">=</span> r <span class="token operator">*</span> <span class="token number">10</span> <span class="token operator">+</span> A<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span> C<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span>r <span class="token operator">/</span> b<span class="token punctuation">)</span><span class="token punctuation">;</span> r <span class="token operator">%=</span> b<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token function">reverse</span><span class="token punctuation">(</span>C<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> C<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>C<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">></span> <span class="token number">1</span> <span class="token operator">&&</span> C<span class="token punctuation">.</span><span class="token function">back</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> C<span class="token punctuation">.</span><span class="token function">pop_back</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> C<span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h2 id="5-4-高精度乘高精度"><a href="#5-4-高精度乘高精度" class="headerlink" title="5.4 高精度乘高精度"></a>5.4 高精度乘高精度</h2><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp">vector<span class="token operator"><</span><span class="token keyword">int</span><span class="token operator">></span> <span class="token function">mul</span><span class="token punctuation">(</span> vector<span class="token operator"><</span><span class="token keyword">int</span><span class="token operator">></span> <span class="token operator">&</span>A<span class="token punctuation">,</span> vector<span class="token operator"><</span><span class="token keyword">int</span><span class="token operator">></span> <span class="token operator">&</span>B<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> la <span class="token operator">=</span> A<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>lb <span class="token operator">=</span> B<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> vector<span class="token operator"><</span><span class="token keyword">int</span><span class="token operator">></span> <span class="token function">C</span><span class="token punctuation">(</span>la<span class="token operator">+</span>lb<span class="token operator">+</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//提前申请结果所需的空间</span> <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator"><</span>la<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> j<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>j<span class="token operator"><</span>lb<span class="token punctuation">;</span>j<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span> C<span class="token punctuation">[</span>i<span class="token operator">+</span>j<span class="token punctuation">]</span> <span class="token operator">+=</span> A<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">*</span> B<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator"><</span>C<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">if</span><span class="token punctuation">(</span>C<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">>=</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">{</span> C<span class="token punctuation">[</span>i <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">+=</span> C<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">/</span> <span class="token number">10</span><span class="token punctuation">;</span> C<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">%=</span> <span class="token number">10</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token comment">//处理前导0</span> <span class="token keyword">while</span><span class="token punctuation">(</span>C<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">></span> <span class="token number">1</span> <span class="token operator">&&</span> C<span class="token punctuation">.</span><span class="token function">back</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span>C<span class="token punctuation">.</span><span class="token function">pop_back</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> C<span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h2 id="5-5-高精度除高精度"><a href="#5-5-高精度除高精度" class="headerlink" title="5.5 高精度除高精度"></a>5.5 高精度除高精度</h2><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp">vector<span class="token operator"><</span><span class="token keyword">int</span><span class="token operator">></span> <span class="token function">div</span><span class="token punctuation">(</span>vector<span class="token operator"><</span><span class="token keyword">int</span><span class="token operator">></span> A<span class="token punctuation">,</span>vector<span class="token operator"><</span><span class="token keyword">int</span><span class="token operator">></span> B<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">int</span> la <span class="token operator">=</span> A<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>lb <span class="token operator">=</span> B<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">int</span> dv <span class="token operator">=</span> la <span class="token operator">-</span> lb<span class="token punctuation">;</span> <span class="token comment">// 相差位数</span> vector<span class="token operator"><</span><span class="token keyword">int</span><span class="token operator">></span> <span class="token function">C</span><span class="token punctuation">(</span>dv<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//提前申请结果所需空间</span> <span class="token comment">//将除数扩大,使得除数和被除数位数相等</span> <span class="token function">reverse</span><span class="token punctuation">(</span>B<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>B<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator"><</span>dv<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>B<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">reverse</span><span class="token punctuation">(</span>B<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>B<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> lb <span class="token operator">=</span> la<span class="token punctuation">;</span> <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> j<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>j<span class="token operator"><=</span>dv<span class="token punctuation">;</span>j<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">while</span><span class="token punctuation">(</span><span class="token operator">!</span><span class="token function">cmp</span><span class="token punctuation">(</span>A<span class="token punctuation">,</span>B<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token comment">//这里用到一个比较函数,cmp返回的是A是否比B小,此处判断的是A是否大于等于B,该循环当A无法再进行减法时结束</span> A <span class="token operator">=</span> <span class="token function">sub</span><span class="token punctuation">(</span>A<span class="token punctuation">,</span>B<span class="token punctuation">)</span><span class="token punctuation">;</span> C<span class="token punctuation">[</span>dv<span class="token operator">-</span>j<span class="token punctuation">]</span><span class="token operator">++</span><span class="token punctuation">;</span><span class="token comment">//答案里相应的那一位数字++</span> <span class="token punctuation">}</span> B<span class="token punctuation">.</span><span class="token function">erase</span><span class="token punctuation">(</span>B<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//缩小被除数</span> <span class="token punctuation">}</span> <span class="token keyword">while</span><span class="token punctuation">(</span>C<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">></span><span class="token number">1</span> <span class="token operator">&&</span> C<span class="token punctuation">.</span><span class="token function">back</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span>C<span class="token punctuation">.</span><span class="token function">pop_back</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> C<span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><blockquote><p>模板只提供了正整数的运算,对于含有负整数的运算,只需要进行合理的转换即可,见下表</p></blockquote><table><thead><tr><th align="center">A</th><th align="center">B</th><th align="center">+</th><th align="center">-</th><th align="center">*</th><th align="center">/</th></tr></thead><tbody><tr><td align="center">+</td><td align="center">+</td><td align="center">$\left</td><td align="center">A \right</td><td align="center">+\left</td><td align="center">B \right</td></tr><tr><td align="center">+</td><td align="center">-</td><td align="center">$\left</td><td align="center">A \right</td><td align="center">-\left</td><td align="center">B \right</td></tr><tr><td align="center">-</td><td align="center">+</td><td align="center">$\left</td><td align="center">B \right</td><td align="center">-\left</td><td align="center">A \right</td></tr><tr><td align="center">-</td><td align="center">-</td><td align="center">$-(\left</td><td align="center">A \right</td><td align="center">+\left</td><td align="center">B \right</td></tr></tbody></table><h1 id="二、数据结构"><a href="#二、数据结构" class="headerlink" title="二、数据结构"></a>二、数据结构</h1><h2 id="2-1-单链表"><a href="#2-1-单链表" class="headerlink" title="2.1 单链表"></a>2.1 单链表</h2><div class="code-wrapper"><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token comment">// head存储链表头,e[]存储节点的值,ne[]存储节点的next指针,idx表示当前用到了哪个节点</span><span class="token keyword">int</span> head<span class="token punctuation">,</span> e<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">,</span> ne<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">,</span> idx<span class="token punctuation">;</span><span class="token comment">// 初始化</span><span class="token keyword">void</span> <span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> head <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> idx <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token comment">// 在链表头插入一个数a</span><span class="token keyword">void</span> <span class="token function">insert</span><span class="token punctuation">(</span><span class="token keyword">int</span> a<span class="token punctuation">)</span><span class="token punctuation">{</span> e<span class="token punctuation">[</span>idx<span class="token punctuation">]</span> <span class="token operator">=</span> a<span class="token punctuation">,</span> ne<span class="token punctuation">[</span>idx<span class="token punctuation">]</span> <span class="token operator">=</span> head<span class="token punctuation">,</span> head <span class="token operator">=</span> idx <span class="token operator">++</span> <span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token comment">// 将头结点删除,需要保证头结点存在</span><span class="token keyword">void</span> <span class="token function">remove</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> head <span class="token operator">=</span> ne<span class="token punctuation">[</span>head<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h2 id="2-2-双链表"><a href="#2-2-双链表" class="headerlink" title="2.2 双链表"></a>2.2 双链表</h2><div class="code-wrapper"><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token comment">// e[]表示节点的值,l[]表示节点的左指针,r[]表示节点的右指针,idx表示当前用到了哪个节点</span><span class="token keyword">int</span> e<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">,</span> l<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">,</span> r<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">,</span> idx<span class="token punctuation">;</span><span class="token comment">// 初始化</span><span class="token keyword">void</span> <span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token comment">//0是左端点,1是右端点</span> r<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">,</span> l<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> idx <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token comment">// 在节点a的右边插入一个数x</span><span class="token keyword">void</span> <span class="token function">insert</span><span class="token punctuation">(</span><span class="token keyword">int</span> a<span class="token punctuation">,</span> <span class="token keyword">int</span> x<span class="token punctuation">)</span><span class="token punctuation">{</span> e<span class="token punctuation">[</span>idx<span class="token punctuation">]</span> <span class="token operator">=</span> x<span class="token punctuation">;</span> l<span class="token punctuation">[</span>idx<span class="token punctuation">]</span> <span class="token operator">=</span> a<span class="token punctuation">,</span> r<span class="token punctuation">[</span>idx<span class="token punctuation">]</span> <span class="token operator">=</span> r<span class="token punctuation">[</span>a<span class="token punctuation">]</span><span class="token punctuation">;</span> l<span class="token punctuation">[</span>r<span class="token punctuation">[</span>a<span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token operator">=</span> idx<span class="token punctuation">,</span> r<span class="token punctuation">[</span>a<span class="token punctuation">]</span> <span class="token operator">=</span> idx <span class="token operator">++</span> <span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token comment">// 删除节点a</span><span class="token keyword">void</span> <span class="token function">remove</span><span class="token punctuation">(</span><span class="token keyword">int</span> a<span class="token punctuation">)</span><span class="token punctuation">{</span> l<span class="token punctuation">[</span>r<span class="token punctuation">[</span>a<span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token operator">=</span> l<span class="token punctuation">[</span>a<span class="token punctuation">]</span><span class="token punctuation">;</span> r<span class="token punctuation">[</span>l<span class="token punctuation">[</span>a<span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token operator">=</span> r<span class="token punctuation">[</span>a<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h2 id="2-3-栈"><a href="#2-3-栈" class="headerlink" title="2.3 栈"></a>2.3 栈</h2><div class="code-wrapper"><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token comment">// tt表示栈顶</span><span class="token keyword">int</span> stk<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">,</span> tt <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token comment">// 向栈顶插入一个数</span>stk<span class="token punctuation">[</span> <span class="token operator">++</span> tt<span class="token punctuation">]</span> <span class="token operator">=</span> x<span class="token punctuation">;</span><span class="token comment">// 从栈顶弹出一个数</span>tt <span class="token operator">--</span> <span class="token punctuation">;</span><span class="token comment">// 栈顶的值</span>stk<span class="token punctuation">[</span>tt<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">// 判断栈是否为空</span><span class="token keyword">if</span> <span class="token punctuation">(</span>tt <span class="token operator">></span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h2 id="2-4-队列"><a href="#2-4-队列" class="headerlink" title="2.4 队列"></a>2.4 队列</h2><div class="code-wrapper"><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token comment">// hh 表示队头,tt表示队尾</span><span class="token keyword">int</span> q<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">,</span> hh <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> tt <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span><span class="token comment">// 向队尾插入一个数</span>q<span class="token punctuation">[</span> <span class="token operator">++</span> tt<span class="token punctuation">]</span> <span class="token operator">=</span> x<span class="token punctuation">;</span><span class="token comment">// 从队头弹出一个数</span>hh <span class="token operator">++</span> <span class="token punctuation">;</span><span class="token comment">// 队头的值</span>q<span class="token punctuation">[</span>hh<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">// 判断队列是否为空</span><span class="token keyword">if</span> <span class="token punctuation">(</span>hh <span class="token operator"><=</span> tt<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h2 id="2-5-循环队列"><a href="#2-5-循环队列" class="headerlink" title="2.5 循环队列"></a>2.5 循环队列</h2><div class="code-wrapper"><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token comment">// hh 表示队头,tt表示队尾的后一个位置</span><span class="token keyword">int</span> q<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">,</span> hh <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> tt <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token comment">// 向队尾插入一个数</span>q<span class="token punctuation">[</span>tt <span class="token operator">++</span> <span class="token punctuation">]</span> <span class="token operator">=</span> x<span class="token punctuation">;</span><span class="token keyword">if</span> <span class="token punctuation">(</span>tt <span class="token operator">==</span> N<span class="token punctuation">)</span> tt <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token comment">// 从队头弹出一个数</span>hh <span class="token operator">++</span> <span class="token punctuation">;</span><span class="token keyword">if</span> <span class="token punctuation">(</span>hh <span class="token operator">==</span> N<span class="token punctuation">)</span> hh <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token comment">// 队头的值</span>q<span class="token punctuation">[</span>hh<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">// 判断队列是否为空</span><span class="token keyword">if</span> <span class="token punctuation">(</span>hh <span class="token operator">!=</span> tt<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h2 id="2-6-单调栈"><a href="#2-6-单调栈" class="headerlink" title="2.6 单调栈"></a>2.6 单调栈</h2><div class="code-wrapper"><pre class="line-numbers language-c" data-language="c"><code class="language-c">常见模型:找出每个数左边离它最近的比它大<span class="token operator">/</span>小的数<span class="token keyword">int</span> tt <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator"><=</span> n<span class="token punctuation">;</span> i <span class="token operator">++</span> <span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>tt <span class="token operator">&&</span> <span class="token function">check</span><span class="token punctuation">(</span>stk<span class="token punctuation">[</span>tt<span class="token punctuation">]</span><span class="token punctuation">,</span> i<span class="token punctuation">)</span><span class="token punctuation">)</span> tt <span class="token operator">--</span> <span class="token punctuation">;</span> stk<span class="token punctuation">[</span> <span class="token operator">++</span> tt<span class="token punctuation">]</span> <span class="token operator">=</span> i<span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h2 id="2-7-单调队列"><a href="#2-7-单调队列" class="headerlink" title="2.7 单调队列"></a>2.7 单调队列</h2><div class="code-wrapper"><pre class="line-numbers language-c" data-language="c"><code class="language-c">常见模型:找出滑动窗口中的最大值<span class="token operator">/</span>最小值<span class="token keyword">int</span> hh <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> tt <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span><span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> n<span class="token punctuation">;</span> i <span class="token operator">++</span> <span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>hh <span class="token operator"><=</span> tt <span class="token operator">&&</span> <span class="token function">check_out</span><span class="token punctuation">(</span>q<span class="token punctuation">[</span>hh<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span> hh <span class="token operator">++</span> <span class="token punctuation">;</span> <span class="token comment">// 判断队头是否滑出窗口</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>hh <span class="token operator"><=</span> tt <span class="token operator">&&</span> <span class="token function">check</span><span class="token punctuation">(</span>q<span class="token punctuation">[</span>tt<span class="token punctuation">]</span><span class="token punctuation">,</span> i<span class="token punctuation">)</span><span class="token punctuation">)</span> tt <span class="token operator">--</span> <span class="token punctuation">;</span> q<span class="token punctuation">[</span> <span class="token operator">++</span> tt<span class="token punctuation">]</span> <span class="token operator">=</span> i<span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h2 id="2-8-KMP"><a href="#2-8-KMP" class="headerlink" title="2.8 KMP"></a>2.8 KMP</h2><div class="code-wrapper"><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token comment">// s[]是长文本,p[]是模式串,n是s的长度,m是p的长度</span>求模式串的Next数组:<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">,</span> j <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><=</span> m<span class="token punctuation">;</span> i <span class="token operator">++</span> <span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>j <span class="token operator">&&</span> p<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">!=</span> p<span class="token punctuation">[</span>j <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span> j <span class="token operator">=</span> ne<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>p<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">==</span> p<span class="token punctuation">[</span>j <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span> j <span class="token operator">++</span> <span class="token punctuation">;</span> ne<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> j<span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token comment">// 匹配</span><span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">,</span> j <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><=</span> n<span class="token punctuation">;</span> i <span class="token operator">++</span> <span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>j <span class="token operator">&&</span> s<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">!=</span> p<span class="token punctuation">[</span>j <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span> j <span class="token operator">=</span> ne<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>s<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">==</span> p<span class="token punctuation">[</span>j <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span> j <span class="token operator">++</span> <span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>j <span class="token operator">==</span> m<span class="token punctuation">)</span> <span class="token punctuation">{</span> j <span class="token operator">=</span> ne<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// 匹配成功后的逻辑</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h2 id="2-9-Trie树"><a href="#2-9-Trie树" class="headerlink" title="2.9 Trie树"></a>2.9 Trie树</h2><div class="code-wrapper"><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token keyword">int</span> son<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">26</span><span class="token punctuation">]</span><span class="token punctuation">,</span> cnt<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">,</span> idx<span class="token punctuation">;</span><span class="token comment">// 0号点既是根节点,又是空节点</span><span class="token comment">// son[][]存储树中每个节点的子节点</span><span class="token comment">// cnt[]存储以每个节点结尾的单词数量</span><span class="token comment">// 插入一个字符串</span><span class="token keyword">void</span> <span class="token function">insert</span><span class="token punctuation">(</span><span class="token keyword">char</span> <span class="token operator">*</span>str<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">int</span> p <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> str<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span> i <span class="token operator">++</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> u <span class="token operator">=</span> str<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">-</span> <span class="token string">'a'</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>son<span class="token punctuation">[</span>p<span class="token punctuation">]</span><span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">)</span> son<span class="token punctuation">[</span>p<span class="token punctuation">]</span><span class="token punctuation">[</span>u<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token operator">++</span> idx<span class="token punctuation">;</span> p <span class="token operator">=</span> son<span class="token punctuation">[</span>p<span class="token punctuation">]</span><span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> cnt<span class="token punctuation">[</span>p<span class="token punctuation">]</span> <span class="token operator">++</span> <span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token comment">// 查询字符串出现的次数</span><span class="token keyword">int</span> <span class="token function">query</span><span class="token punctuation">(</span><span class="token keyword">char</span> <span class="token operator">*</span>str<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">int</span> p <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> str<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span> i <span class="token operator">++</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> u <span class="token operator">=</span> str<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">-</span> <span class="token string">'a'</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>son<span class="token punctuation">[</span>p<span class="token punctuation">]</span><span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span> p <span class="token operator">=</span> son<span class="token punctuation">[</span>p<span class="token punctuation">]</span><span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> cnt<span class="token punctuation">[</span>p<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h2 id="2-10-并查集"><a href="#2-10-并查集" class="headerlink" title="2.10 并查集"></a>2.10 并查集</h2><div class="code-wrapper"><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>朴素并查集: <span class="token keyword">int</span> p<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">//存储每个点的祖宗节点</span> <span class="token comment">// 返回x的祖宗节点</span> <span class="token keyword">int</span> <span class="token function">find</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>p<span class="token punctuation">[</span>x<span class="token punctuation">]</span> <span class="token operator">!=</span> x<span class="token punctuation">)</span> p<span class="token punctuation">[</span>x<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">find</span><span class="token punctuation">(</span>p<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> p<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">// 初始化,假定节点编号是1~n</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator"><=</span> n<span class="token punctuation">;</span> i <span class="token operator">++</span> <span class="token punctuation">)</span> p<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> i<span class="token punctuation">;</span> <span class="token comment">// 合并a和b所在的两个集合:</span> p<span class="token punctuation">[</span><span class="token function">find</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">find</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span>维护size的并查集: <span class="token keyword">int</span> p<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">,</span> size<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">//p[]存储每个点的祖宗节点, size[]只有祖宗节点的有意义,表示祖宗节点所在集合中的点的数量</span> <span class="token comment">// 返回x的祖宗节点</span> <span class="token keyword">int</span> <span class="token function">find</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>p<span class="token punctuation">[</span>x<span class="token punctuation">]</span> <span class="token operator">!=</span> x<span class="token punctuation">)</span> p<span class="token punctuation">[</span>x<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">find</span><span class="token punctuation">(</span>p<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> p<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">// 初始化,假定节点编号是1~n</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator"><=</span> n<span class="token punctuation">;</span> i <span class="token operator">++</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> p<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> i<span class="token punctuation">;</span> size<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">// 合并a和b所在的两个集合:</span> size<span class="token punctuation">[</span><span class="token function">find</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span><span class="token punctuation">]</span> <span class="token operator">+=</span> size<span class="token punctuation">[</span><span class="token function">find</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">;</span> p<span class="token punctuation">[</span><span class="token function">find</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">find</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span>维护到祖宗节点距离的并查集: <span class="token keyword">int</span> p<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">,</span> d<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">//p[]存储每个点的祖宗节点, d[x]存储x到p[x]的距离</span> <span class="token comment">// 返回x的祖宗节点</span> <span class="token keyword">int</span> <span class="token function">find</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>p<span class="token punctuation">[</span>x<span class="token punctuation">]</span> <span class="token operator">!=</span> x<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> u <span class="token operator">=</span> <span class="token function">find</span><span class="token punctuation">(</span>p<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> d<span class="token punctuation">[</span>x<span class="token punctuation">]</span> <span class="token operator">+=</span> d<span class="token punctuation">[</span>p<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span> p<span class="token punctuation">[</span>x<span class="token punctuation">]</span> <span class="token operator">=</span> u<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> p<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">// 初始化,假定节点编号是1~n</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator"><=</span> n<span class="token punctuation">;</span> i <span class="token operator">++</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> p<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> i<span class="token punctuation">;</span> d<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">// 合并a和b所在的两个集合:</span> p<span class="token punctuation">[</span><span class="token function">find</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">find</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span><span class="token punctuation">;</span> d<span class="token punctuation">[</span><span class="token function">find</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">]</span> <span class="token operator">=</span> distance<span class="token punctuation">;</span> <span class="token comment">// 根据具体问题,初始化find(a)的偏移量</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h2 id="2-11-堆"><a href="#2-11-堆" class="headerlink" title="2.11 堆"></a>2.11 堆</h2><div class="code-wrapper"><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token comment">// h[N]存储堆中的值, h[1]是堆顶,x的左儿子是2x, 右儿子是2x + 1</span><span class="token comment">// ph[k]存储第k个插入的点在堆中的位置</span><span class="token comment">// hp[k]存储堆中下标是k的点是第几个插入的</span><span class="token keyword">int</span> h<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">,</span> ph<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">,</span> hp<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">,</span> size<span class="token punctuation">;</span><span class="token comment">// 交换两个点,及其映射关系</span><span class="token keyword">void</span> <span class="token function">heap_swap</span><span class="token punctuation">(</span><span class="token keyword">int</span> a<span class="token punctuation">,</span> <span class="token keyword">int</span> b<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token function">swap</span><span class="token punctuation">(</span>ph<span class="token punctuation">[</span>hp<span class="token punctuation">[</span>a<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span>ph<span class="token punctuation">[</span>hp<span class="token punctuation">[</span>b<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">swap</span><span class="token punctuation">(</span>hp<span class="token punctuation">[</span>a<span class="token punctuation">]</span><span class="token punctuation">,</span> hp<span class="token punctuation">[</span>b<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">swap</span><span class="token punctuation">(</span>h<span class="token punctuation">[</span>a<span class="token punctuation">]</span><span class="token punctuation">,</span> h<span class="token punctuation">[</span>b<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">void</span> <span class="token function">down</span><span class="token punctuation">(</span><span class="token keyword">int</span> u<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">int</span> t <span class="token operator">=</span> u<span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>u <span class="token operator">*</span> <span class="token number">2</span> <span class="token operator"><=</span> size <span class="token operator">&&</span> h<span class="token punctuation">[</span>u <span class="token operator">*</span> <span class="token number">2</span><span class="token punctuation">]</span> <span class="token operator"><</span> h<span class="token punctuation">[</span>t<span class="token punctuation">]</span><span class="token punctuation">)</span> t <span class="token operator">=</span> u <span class="token operator">*</span> <span class="token number">2</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>u <span class="token operator">*</span> <span class="token number">2</span> <span class="token operator">+</span> <span class="token number">1</span> <span class="token operator"><=</span> size <span class="token operator">&&</span> h<span class="token punctuation">[</span>u <span class="token operator">*</span> <span class="token number">2</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator"><</span> h<span class="token punctuation">[</span>t<span class="token punctuation">]</span><span class="token punctuation">)</span> t <span class="token operator">=</span> u <span class="token operator">*</span> <span class="token number">2</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>u <span class="token operator">!=</span> t<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token function">heap_swap</span><span class="token punctuation">(</span>u<span class="token punctuation">,</span> t<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">down</span><span class="token punctuation">(</span>t<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token keyword">void</span> <span class="token function">up</span><span class="token punctuation">(</span><span class="token keyword">int</span> u<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>u <span class="token operator">/</span> <span class="token number">2</span> <span class="token operator">&&</span> h<span class="token punctuation">[</span>u<span class="token punctuation">]</span> <span class="token operator"><</span> h<span class="token punctuation">[</span>u <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token function">heap_swap</span><span class="token punctuation">(</span>u<span class="token punctuation">,</span> u <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span> u <span class="token operator">>>=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token comment">// O(n)建堆</span><span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> n <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">;</span> i<span class="token punctuation">;</span> i <span class="token operator">--</span> <span class="token punctuation">)</span> <span class="token function">down</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h2 id="2-12-STL"><a href="#2-12-STL" class="headerlink" title="2.12 STL"></a>2.12 STL</h2><div class="code-wrapper"><pre class="line-numbers language-c" data-language="c"><code class="language-c">vector<span class="token punctuation">,</span> 变长数组,倍增的思想 <span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> 返回元素个数 <span class="token function">empty</span><span class="token punctuation">(</span><span class="token punctuation">)</span> 返回是否为空 <span class="token function">clear</span><span class="token punctuation">(</span><span class="token punctuation">)</span> 清空 <span class="token function">front</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">/</span><span class="token function">back</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token function">push_back</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">/</span><span class="token function">pop_back</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">/</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">[</span><span class="token punctuation">]</span> 支持比较运算,按字典序pair<span class="token operator"><</span><span class="token keyword">int</span><span class="token punctuation">,</span> <span class="token keyword">int</span><span class="token operator">></span> first<span class="token punctuation">,</span> 第一个元素 second<span class="token punctuation">,</span> 第二个元素 支持比较运算,以first为第一关键字,以second为第二关键字(字典序)string,字符串 <span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">/</span><span class="token function">length</span><span class="token punctuation">(</span><span class="token punctuation">)</span> 返回字符串长度 <span class="token function">empty</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token function">clear</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token function">substr</span><span class="token punctuation">(</span>起始下标,<span class="token punctuation">(</span>子串长度<span class="token punctuation">)</span><span class="token punctuation">)</span> 返回子串 <span class="token function">c_str</span><span class="token punctuation">(</span><span class="token punctuation">)</span> 返回字符串所在字符数组的起始地址queue<span class="token punctuation">,</span> 队列 <span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token function">empty</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token function">push</span><span class="token punctuation">(</span><span class="token punctuation">)</span> 向队尾插入一个元素 <span class="token function">front</span><span class="token punctuation">(</span><span class="token punctuation">)</span> 返回队头元素 <span class="token function">back</span><span class="token punctuation">(</span><span class="token punctuation">)</span> 返回队尾元素 <span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span> 弹出队头元素priority_queue<span class="token punctuation">,</span> 优先队列,默认是大根堆 <span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token function">empty</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token function">push</span><span class="token punctuation">(</span><span class="token punctuation">)</span> 插入一个元素 <span class="token function">top</span><span class="token punctuation">(</span><span class="token punctuation">)</span> 返回堆顶元素 <span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span> 弹出堆顶元素 定义成小根堆的方式:priority_queue<span class="token operator"><</span><span class="token keyword">int</span><span class="token punctuation">,</span> vector<span class="token operator"><</span><span class="token keyword">int</span><span class="token operator">></span><span class="token punctuation">,</span> greater<span class="token operator"><</span><span class="token keyword">int</span><span class="token operator">>></span> q<span class="token punctuation">;</span>stack<span class="token punctuation">,</span> 栈 <span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token function">empty</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token function">push</span><span class="token punctuation">(</span><span class="token punctuation">)</span> 向栈顶插入一个元素 <span class="token function">top</span><span class="token punctuation">(</span><span class="token punctuation">)</span> 返回栈顶元素 <span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span> 弹出栈顶元素deque<span class="token punctuation">,</span> 双端队列 <span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token function">empty</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token function">clear</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token function">front</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">/</span><span class="token function">back</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token function">push_back</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">/</span><span class="token function">pop_back</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token function">push_front</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">/</span><span class="token function">pop_front</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">/</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>set<span class="token punctuation">,</span> map<span class="token punctuation">,</span> multiset<span class="token punctuation">,</span> multimap<span class="token punctuation">,</span> 基于平衡二叉树(红黑树),动态维护有序序列 <span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token function">empty</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token function">clear</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">/</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">++</span><span class="token punctuation">,</span> <span class="token operator">--</span> 返回前驱和后继,时间复杂度 <span class="token function">O</span><span class="token punctuation">(</span>logn<span class="token punctuation">)</span> set<span class="token operator">/</span>multiset <span class="token function">insert</span><span class="token punctuation">(</span><span class="token punctuation">)</span> 插入一个数 <span class="token function">find</span><span class="token punctuation">(</span><span class="token punctuation">)</span> 查找一个数 <span class="token function">count</span><span class="token punctuation">(</span><span class="token punctuation">)</span> 返回某一个数的个数 <span class="token function">erase</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span> 输入是一个数x,删除所有x <span class="token function">O</span><span class="token punctuation">(</span>k <span class="token operator">+</span> logn<span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span> 输入一个迭代器,删除这个迭代器 <span class="token function">lower_bound</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">/</span><span class="token function">upper_bound</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token function">lower_bound</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span> 返回大于等于x的最小的数的迭代器 <span class="token function">upper_bound</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span> 返回大于x的最小的数的迭代器 map<span class="token operator">/</span>multimap <span class="token function">insert</span><span class="token punctuation">(</span><span class="token punctuation">)</span> 插入的数是一个pair <span class="token function">erase</span><span class="token punctuation">(</span><span class="token punctuation">)</span> 输入的参数是pair或者迭代器 <span class="token function">find</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">[</span><span class="token punctuation">]</span> 注意multimap不支持此操作。 时间复杂度是 <span class="token function">O</span><span class="token punctuation">(</span>logn<span class="token punctuation">)</span> <span class="token function">lower_bound</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">/</span><span class="token function">upper_bound</span><span class="token punctuation">(</span><span class="token punctuation">)</span>unordered_set<span class="token punctuation">,</span> unordered_map<span class="token punctuation">,</span> unordered_multiset<span class="token punctuation">,</span> unordered_multimap<span class="token punctuation">,</span> 哈希表 和上面类似,增删改查的时间复杂度是 <span class="token function">O</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span> 不支持 <span class="token function">lower_bound</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">/</span><span class="token function">upper_bound</span><span class="token punctuation">(</span><span class="token punctuation">)</span>, 迭代器的<span class="token operator">++</span>,<span class="token operator">--</span>bitset<span class="token punctuation">,</span> 圧位 bitset<span class="token operator"><</span><span class="token number">10000</span><span class="token operator">></span> s<span class="token punctuation">;</span> <span class="token operator">~</span><span class="token punctuation">,</span> <span class="token operator">&</span><span class="token punctuation">,</span> <span class="token operator">|</span><span class="token punctuation">,</span> <span class="token operator">^</span> <span class="token operator">>></span><span class="token punctuation">,</span> <span class="token operator"><<</span> <span class="token operator">==</span><span class="token punctuation">,</span> <span class="token operator">!=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token function">count</span><span class="token punctuation">(</span><span class="token punctuation">)</span> 返回有多少个<span class="token number">1</span> <span class="token function">any</span><span class="token punctuation">(</span><span class="token punctuation">)</span> 判断是否至少有一个<span class="token number">1</span> <span class="token function">none</span><span class="token punctuation">(</span><span class="token punctuation">)</span> 判断是否全为<span class="token number">0</span> <span class="token function">set</span><span class="token punctuation">(</span><span class="token punctuation">)</span> 把所有位置成<span class="token number">1</span> <span class="token function">set</span><span class="token punctuation">(</span>k<span class="token punctuation">,</span> v<span class="token punctuation">)</span> 将第k位变成v <span class="token function">reset</span><span class="token punctuation">(</span><span class="token punctuation">)</span> 把所有位变成<span class="token number">0</span> <span class="token function">flip</span><span class="token punctuation">(</span><span class="token punctuation">)</span> 等价于<span class="token operator">~</span> <span class="token function">flip</span><span class="token punctuation">(</span>k<span class="token punctuation">)</span> 把第k位取反<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h1 id="三、数论"><a href="#三、数论" class="headerlink" title="三、数论"></a>三、数论</h1><h2 id="1-素数打表"><a href="#1-素数打表" class="headerlink" title="1. 素数打表"></a>1. 素数打表</h2><h3 id="埃氏筛"><a href="#埃氏筛" class="headerlink" title="埃氏筛"></a>埃氏筛</h3><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token comment">//在primes中值为true的是合数 </span><span class="token keyword">bool</span> primes<span class="token punctuation">[</span>MAXN<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">}</span><span class="token punctuation">;</span><span class="token keyword">void</span> <span class="token function">eraSieve</span><span class="token punctuation">(</span><span class="token keyword">int</span> n<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span> i <span class="token operator">*</span> i <span class="token operator"><</span> n<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span><span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>primes<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment">//i为素数</span><span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> i <span class="token operator">*</span> i<span class="token punctuation">;</span> j <span class="token operator"><=</span> n<span class="token punctuation">;</span> j <span class="token operator">+=</span> i<span class="token punctuation">)</span><span class="token comment">//标记i的倍数为合数</span>primes<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span><span class="token keyword">int</span> primes<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">,</span> cnt<span class="token punctuation">;</span> <span class="token comment">// primes[]存储所有素数</span><span class="token keyword">bool</span> st<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// st[x]存储x是否被筛掉</span><span class="token keyword">void</span> <span class="token function">get_primes</span><span class="token punctuation">(</span><span class="token keyword">int</span> n<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span> i <span class="token operator"><=</span> n<span class="token punctuation">;</span> i <span class="token operator">++</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>st<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token keyword">continue</span><span class="token punctuation">;</span> primes<span class="token punctuation">[</span>cnt <span class="token operator">++</span> <span class="token punctuation">]</span> <span class="token operator">=</span> i<span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> i <span class="token operator">+</span> i<span class="token punctuation">;</span> j <span class="token operator"><=</span> n<span class="token punctuation">;</span> j <span class="token operator">+=</span> i<span class="token punctuation">)</span> st<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h3 id="欧拉筛"><a href="#欧拉筛" class="headerlink" title="欧拉筛"></a>欧拉筛</h3><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">int</span> primes<span class="token punctuation">[</span>MAXN<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//0~N内的素数集合 </span><span class="token keyword">void</span> <span class="token function">eulerSieve</span><span class="token punctuation">(</span><span class="token keyword">int</span> n<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token keyword">int</span> sum <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token comment">//已经找到的素数的数量 </span><span class="token keyword">bool</span> flag<span class="token punctuation">[</span>MAXN<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token boolean">false</span><span class="token punctuation">}</span><span class="token punctuation">;</span><span class="token comment">//标记是否为合数 </span><span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span> i <span class="token operator"><=</span> n<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>flag<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span>primes<span class="token punctuation">[</span>sum<span class="token operator">++</span><span class="token punctuation">]</span> <span class="token operator">=</span> i<span class="token punctuation">;</span><span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">*</span> primes<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator"><=</span> n<span class="token punctuation">;</span> <span class="token operator">++</span>j<span class="token punctuation">)</span><span class="token punctuation">{</span>flag<span class="token punctuation">[</span>i <span class="token operator">*</span> primes<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span><span class="token comment">//标记素数的倍数为合数 </span><span class="token keyword">if</span><span class="token punctuation">(</span>i <span class="token operator">%</span> primes<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token keyword">break</span><span class="token punctuation">;</span><span class="token comment">//primes[j]同时是i和i*primes[j]的最小质因数 </span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token keyword">int</span> primes<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">,</span> cnt<span class="token punctuation">;</span> <span class="token comment">// primes[]存储所有素数</span><span class="token keyword">bool</span> st<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// st[x]存储x是否被筛掉</span><span class="token keyword">void</span> <span class="token function">get_primes</span><span class="token punctuation">(</span><span class="token keyword">int</span> n<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span> i <span class="token operator"><=</span> n<span class="token punctuation">;</span> i <span class="token operator">++</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>st<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span> primes<span class="token punctuation">[</span>cnt <span class="token operator">++</span> <span class="token punctuation">]</span> <span class="token operator">=</span> i<span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> primes<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator"><=</span> n <span class="token operator">/</span> i<span class="token punctuation">;</span> j <span class="token operator">++</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> st<span class="token punctuation">[</span>primes<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">*</span> i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>i <span class="token operator">%</span> primes<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h2 id="2-试除法分解质因数"><a href="#2-试除法分解质因数" class="headerlink" title="2. 试除法分解质因数"></a>2. 试除法分解质因数</h2><div class="code-wrapper"><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token keyword">void</span> <span class="token function">divide</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span> i <span class="token operator"><=</span> x <span class="token operator">/</span> i<span class="token punctuation">;</span> i <span class="token operator">++</span> <span class="token punctuation">)</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>x <span class="token operator">%</span> i <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> s <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>x <span class="token operator">%</span> i <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> x <span class="token operator">/=</span> i<span class="token punctuation">,</span> s <span class="token operator">++</span> <span class="token punctuation">;</span> cout <span class="token operator"><<</span> i <span class="token operator"><<</span> <span class="token string">' '</span> <span class="token operator"><<</span> s <span class="token operator"><<</span> endl<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>x <span class="token operator">></span> <span class="token number">1</span><span class="token punctuation">)</span> cout <span class="token operator"><<</span> x <span class="token operator"><<</span> <span class="token string">' '</span> <span class="token operator"><<</span> <span class="token number">1</span> <span class="token operator"><<</span> endl<span class="token punctuation">;</span> cout <span class="token operator"><<</span> endl<span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h2 id="3-试除法求所有约数"><a href="#3-试除法求所有约数" class="headerlink" title="3.试除法求所有约数"></a>3.试除法求所有约数</h2><div class="code-wrapper"><pre class="line-numbers language-c" data-language="c"><code class="language-c">vector<span class="token operator"><</span><span class="token keyword">int</span><span class="token operator">></span> <span class="token function">get_divisors</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">)</span><span class="token punctuation">{</span> vector<span class="token operator"><</span><span class="token keyword">int</span><span class="token operator">></span> res<span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator"><=</span> x <span class="token operator">/</span> i<span class="token punctuation">;</span> i <span class="token operator">++</span> <span class="token punctuation">)</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>x <span class="token operator">%</span> i <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> res<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>i <span class="token operator">!=</span> x <span class="token operator">/</span> i<span class="token punctuation">)</span> res<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span>x <span class="token operator">/</span> i<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token function">sort</span><span class="token punctuation">(</span>res<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> res<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> res<span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h2 id="4-约数个数、约束之和"><a href="#4-约数个数、约束之和" class="headerlink" title="4. 约数个数、约束之和"></a>4. 约数个数、约束之和</h2><div class="code-wrapper"><pre class="line-numbers language-none"><code class="language-none">如果 N = p1^c1 * p2^c2 * ... *pk^ck约数个数: (c1 + 1) * (c2 + 1) * ... * (ck + 1)约数之和: (p1^0 + p1^1 + ... + p1^c1) * ... * (pk^0 + pk^1 + ... + pk^ck)<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre></div><h2 id="5-欧几里得"><a href="#5-欧几里得" class="headerlink" title="5. 欧几里得"></a>5. 欧几里得</h2><div class="code-wrapper"><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token keyword">int</span> <span class="token function">gcd</span><span class="token punctuation">(</span><span class="token keyword">int</span> a<span class="token punctuation">,</span> <span class="token keyword">int</span> b<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">return</span> b <span class="token operator">?</span> <span class="token function">gcd</span><span class="token punctuation">(</span>b<span class="token punctuation">,</span> a <span class="token operator">%</span> b<span class="token punctuation">)</span> <span class="token operator">:</span> a<span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">int</span> <span class="token function">lcm</span><span class="token punctuation">(</span><span class="token keyword">int</span> a<span class="token punctuation">,</span> <span class="token keyword">int</span> b<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">return</span> a <span class="token operator">*</span> b <span class="token operator">/</span> <span class="token function">gcd</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h2 id="6-欧拉函数"><a href="#6-欧拉函数" class="headerlink" title="6. 欧拉函数"></a>6. 欧拉函数</h2><h3 id="6-1-公式法"><a href="#6-1-公式法" class="headerlink" title="6.1 公式法"></a>6.1 公式法</h3><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token comment">//欧拉函数公式:euler(x) = x * (1 - 1 / p1)...(1 - 1 / pn) p为x的质因数 </span><span class="token keyword">int</span> <span class="token function">Euler</span><span class="token punctuation">(</span><span class="token keyword">int</span> n<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">int</span> ans <span class="token operator">=</span> n<span class="token punctuation">;</span> <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span> i <span class="token operator">*</span> i <span class="token operator"><=</span> n<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">if</span><span class="token punctuation">(</span>n <span class="token operator">%</span> i <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">{</span> ans <span class="token operator">=</span> ans <span class="token operator">/</span> i <span class="token operator">*</span> <span class="token punctuation">(</span>i <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//先进行除法是为了防止中间数据的溢出 </span> <span class="token keyword">while</span><span class="token punctuation">(</span>n <span class="token operator">%</span> i <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> n <span class="token operator">/=</span> i<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">if</span><span class="token punctuation">(</span>n <span class="token operator">></span> <span class="token number">1</span><span class="token punctuation">)</span> ans <span class="token operator">=</span> ans <span class="token operator">/</span> n <span class="token operator">*</span> <span class="token punctuation">(</span>n <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> ans<span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span> <span class="token keyword">int</span> <span class="token function">phi</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">int</span> res <span class="token operator">=</span> x<span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span> i <span class="token operator"><=</span> x <span class="token operator">/</span> i<span class="token punctuation">;</span> i <span class="token operator">++</span> <span class="token punctuation">)</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>x <span class="token operator">%</span> i <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> res <span class="token operator">=</span> res <span class="token operator">/</span> i <span class="token operator">*</span> <span class="token punctuation">(</span>i <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>x <span class="token operator">%</span> i <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> x <span class="token operator">/=</span> i<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>x <span class="token operator">></span> <span class="token number">1</span><span class="token punctuation">)</span> res <span class="token operator">=</span> res <span class="token operator">/</span> x <span class="token operator">*</span> <span class="token punctuation">(</span>x <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> res<span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h3 id="6-2-打表法"><a href="#6-2-打表法" class="headerlink" title="6.2 打表法"></a>6.2 打表法</h3><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">void</span> <span class="token function">Euler</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>euler<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span><span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span> i <span class="token operator"><</span> MAXN<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span>euler<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> i<span class="token punctuation">;</span><span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span> i <span class="token operator"><</span> MAXN<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span><span class="token keyword">if</span><span class="token punctuation">(</span>euler<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">==</span> i<span class="token punctuation">)</span><span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> i<span class="token punctuation">;</span> j <span class="token operator"><</span> MAXN<span class="token punctuation">;</span> j <span class="token operator">+=</span> i<span class="token punctuation">)</span>euler<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> euler<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">/</span> i <span class="token operator">*</span> <span class="token punctuation">(</span>i <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span> <span class="token keyword">int</span> primes<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">,</span> cnt<span class="token punctuation">;</span> <span class="token comment">// primes[]存储所有素数</span><span class="token keyword">int</span> euler<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// 存储每个数的欧拉函数</span><span class="token keyword">bool</span> st<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// st[x]存储x是否被筛掉</span><span class="token keyword">void</span> <span class="token function">get_eulers</span><span class="token punctuation">(</span><span class="token keyword">int</span> n<span class="token punctuation">)</span><span class="token punctuation">{</span> euler<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span> i <span class="token operator"><=</span> n<span class="token punctuation">;</span> i <span class="token operator">++</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>st<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> primes<span class="token punctuation">[</span>cnt <span class="token operator">++</span> <span class="token punctuation">]</span> <span class="token operator">=</span> i<span class="token punctuation">;</span> euler<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> i <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> primes<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator"><=</span> n <span class="token operator">/</span> i<span class="token punctuation">;</span> j <span class="token operator">++</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> t <span class="token operator">=</span> primes<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">*</span> i<span class="token punctuation">;</span> st<span class="token punctuation">[</span>t<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>i <span class="token operator">%</span> primes<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> euler<span class="token punctuation">[</span>t<span class="token punctuation">]</span> <span class="token operator">=</span> euler<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">*</span> primes<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> euler<span class="token punctuation">[</span>t<span class="token punctuation">]</span> <span class="token operator">=</span> euler<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">*</span> <span class="token punctuation">(</span>primes<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h2 id="7-快速幂"><a href="#7-快速幂" class="headerlink" title="7. 快速幂"></a>7. 快速幂</h2><div class="code-wrapper"><pre class="line-numbers language-c" data-language="c"><code class="language-c">求 m<span class="token operator">^</span>k mod p,时间复杂度 <span class="token function">O</span><span class="token punctuation">(</span>logk<span class="token punctuation">)</span>。<span class="token keyword">int</span> <span class="token function">qmi</span><span class="token punctuation">(</span><span class="token keyword">int</span> m<span class="token punctuation">,</span> <span class="token keyword">int</span> k<span class="token punctuation">,</span> <span class="token keyword">int</span> p<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">int</span> res <span class="token operator">=</span> <span class="token number">1</span> <span class="token operator">%</span> p<span class="token punctuation">,</span> t <span class="token operator">=</span> m<span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>k<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>k<span class="token operator">&</span><span class="token number">1</span><span class="token punctuation">)</span> res <span class="token operator">=</span> res <span class="token operator">*</span> t <span class="token operator">%</span> p<span class="token punctuation">;</span> t <span class="token operator">=</span> t <span class="token operator">*</span> t <span class="token operator">%</span> p<span class="token punctuation">;</span> k <span class="token operator">>>=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> res<span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h2 id="8-扩展欧几里得"><a href="#8-扩展欧几里得" class="headerlink" title="8. 扩展欧几里得"></a>8. 扩展欧几里得</h2><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token comment">// 求x, y,使得ax + by = gcd(a, b)</span><span class="token keyword">int</span> <span class="token function">exgcd</span><span class="token punctuation">(</span><span class="token keyword">int</span> a<span class="token punctuation">,</span> <span class="token keyword">int</span> b<span class="token punctuation">,</span> <span class="token keyword">int</span> <span class="token operator">&</span>x<span class="token punctuation">,</span> <span class="token keyword">int</span> <span class="token operator">&</span>y<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>b<span class="token punctuation">)</span> <span class="token punctuation">{</span> x <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> y <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token keyword">return</span> a<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">int</span> d <span class="token operator">=</span> <span class="token function">exgcd</span><span class="token punctuation">(</span>b<span class="token punctuation">,</span> a <span class="token operator">%</span> b<span class="token punctuation">,</span> y<span class="token punctuation">,</span> x<span class="token punctuation">)</span><span class="token punctuation">;</span> y <span class="token operator">-=</span> <span class="token punctuation">(</span>a<span class="token operator">/</span>b<span class="token punctuation">)</span> <span class="token operator">*</span> x<span class="token punctuation">;</span> <span class="token keyword">return</span> d<span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h2 id="9-高斯消元"><a href="#9-高斯消元" class="headerlink" title="9. 高斯消元"></a>9. 高斯消元</h2><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token comment">// a[N][N]是增广矩阵</span><span class="token keyword">int</span> <span class="token function">gauss</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">int</span> c<span class="token punctuation">,</span> r<span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span>c <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> r <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> c <span class="token operator"><</span> n<span class="token punctuation">;</span> c <span class="token operator">++</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> t <span class="token operator">=</span> r<span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> r<span class="token punctuation">;</span> i <span class="token operator"><</span> n<span class="token punctuation">;</span> i <span class="token operator">++</span> <span class="token punctuation">)</span> <span class="token comment">// 找到绝对值最大的行</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">fabs</span><span class="token punctuation">(</span>a<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>c<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">></span> <span class="token function">fabs</span><span class="token punctuation">(</span>a<span class="token punctuation">[</span>t<span class="token punctuation">]</span><span class="token punctuation">[</span>c<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span> t <span class="token operator">=</span> i<span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">fabs</span><span class="token punctuation">(</span>a<span class="token punctuation">[</span>t<span class="token punctuation">]</span><span class="token punctuation">[</span>c<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator"><</span> eps<span class="token punctuation">)</span> <span class="token keyword">continue</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> c<span class="token punctuation">;</span> i <span class="token operator"><=</span> n<span class="token punctuation">;</span> i <span class="token operator">++</span> <span class="token punctuation">)</span> <span class="token function">swap</span><span class="token punctuation">(</span>a<span class="token punctuation">[</span>t<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span> a<span class="token punctuation">[</span>r<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 将绝对值最大的行换到最顶端</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> n<span class="token punctuation">;</span> i <span class="token operator">>=</span> c<span class="token punctuation">;</span> i <span class="token operator">--</span> <span class="token punctuation">)</span> a<span class="token punctuation">[</span>r<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">/=</span> a<span class="token punctuation">[</span>r<span class="token punctuation">]</span><span class="token punctuation">[</span>c<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// 将当前行的首位变成1</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> r <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator"><</span> n<span class="token punctuation">;</span> i <span class="token operator">++</span> <span class="token punctuation">)</span> <span class="token comment">// 用当前行将下面所有的列消成0</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">fabs</span><span class="token punctuation">(</span>a<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>c<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">></span> eps<span class="token punctuation">)</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> n<span class="token punctuation">;</span> j <span class="token operator">>=</span> c<span class="token punctuation">;</span> j <span class="token operator">--</span> <span class="token punctuation">)</span> a<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">-=</span> a<span class="token punctuation">[</span>r<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">*</span> a<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>c<span class="token punctuation">]</span><span class="token punctuation">;</span> r <span class="token operator">++</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>r <span class="token operator"><</span> n<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> r<span class="token punctuation">;</span> i <span class="token operator"><</span> n<span class="token punctuation">;</span> i <span class="token operator">++</span> <span class="token punctuation">)</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">fabs</span><span class="token punctuation">(</span>a<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>n<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">></span> eps<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token number">2</span><span class="token punctuation">;</span> <span class="token comment">// 无解</span> <span class="token keyword">return</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token comment">// 有无穷多组解</span> <span class="token punctuation">}</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> n <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator">>=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">--</span> <span class="token punctuation">)</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> i <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span> j <span class="token operator"><</span> n<span class="token punctuation">;</span> j <span class="token operator">++</span> <span class="token punctuation">)</span> a<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>n<span class="token punctuation">]</span> <span class="token operator">-=</span> a<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">*</span> a<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">[</span>n<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token comment">// 有唯一解</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h2 id="10-递归求组合数"><a href="#10-递归求组合数" class="headerlink" title="10. 递归求组合数"></a>10. 递归求组合数</h2><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token comment">// c[a][b] 表示从a个苹果中选b个的方案数</span><span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> N<span class="token punctuation">;</span> i <span class="token operator">++</span> <span class="token punctuation">)</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> j <span class="token operator"><=</span> i<span class="token punctuation">;</span> j <span class="token operator">++</span> <span class="token punctuation">)</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>j<span class="token punctuation">)</span> c<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token keyword">else</span> c<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">(</span>c<span class="token punctuation">[</span>i <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">+</span> c<span class="token punctuation">[</span>i <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span>j <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">%</span> mod<span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h2 id="11-通过预处理逆元的方式求组合数"><a href="#11-通过预处理逆元的方式求组合数" class="headerlink" title="11. 通过预处理逆元的方式求组合数"></a>11. 通过预处理逆元的方式求组合数</h2><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp">首先预处理出所有阶乘取模的余数fact<span class="token punctuation">[</span>N<span class="token punctuation">]</span>,以及所有阶乘取模的逆元infact<span class="token punctuation">[</span>N<span class="token punctuation">]</span>如果取模的数是质数,可以用费马小定理求逆元<span class="token keyword">int</span> <span class="token function">qmi</span><span class="token punctuation">(</span><span class="token keyword">int</span> a<span class="token punctuation">,</span> <span class="token keyword">int</span> k<span class="token punctuation">,</span> <span class="token keyword">int</span> p<span class="token punctuation">)</span> <span class="token comment">// 快速幂模板</span><span class="token punctuation">{</span> <span class="token keyword">int</span> res <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>k<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>k <span class="token operator">&</span> <span class="token number">1</span><span class="token punctuation">)</span> res <span class="token operator">=</span> <span class="token punctuation">(</span>LL<span class="token punctuation">)</span>res <span class="token operator">*</span> a <span class="token operator">%</span> p<span class="token punctuation">;</span> a <span class="token operator">=</span> <span class="token punctuation">(</span>LL<span class="token punctuation">)</span>a <span class="token operator">*</span> a <span class="token operator">%</span> p<span class="token punctuation">;</span> k <span class="token operator">>>=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> res<span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token comment">// 预处理阶乘的余数和阶乘逆元的余数</span>fact<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> infact<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span><span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator"><</span> N<span class="token punctuation">;</span> i <span class="token operator">++</span> <span class="token punctuation">)</span><span class="token punctuation">{</span> fact<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">(</span>LL<span class="token punctuation">)</span>fact<span class="token punctuation">[</span>i <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">*</span> i <span class="token operator">%</span> mod<span class="token punctuation">;</span> infact<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">(</span>LL<span class="token punctuation">)</span>infact<span class="token punctuation">[</span>i <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">*</span> <span class="token function">qmi</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> mod <span class="token operator">-</span> <span class="token number">2</span><span class="token punctuation">,</span> mod<span class="token punctuation">)</span> <span class="token operator">%</span> mod<span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h2 id="12-Lucas定理"><a href="#12-Lucas定理" class="headerlink" title="12. Lucas定理"></a>12. Lucas定理</h2><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp">若p是质数,则对于任意整数 <span class="token number">1</span> <span class="token operator"><=</span> m <span class="token operator"><=</span> n,有: <span class="token function">C</span><span class="token punctuation">(</span>n<span class="token punctuation">,</span> m<span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token function">C</span><span class="token punctuation">(</span>n <span class="token operator">%</span> p<span class="token punctuation">,</span> m <span class="token operator">%</span> p<span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token function">C</span><span class="token punctuation">(</span>n <span class="token operator">/</span> p<span class="token punctuation">,</span> m <span class="token operator">/</span> p<span class="token punctuation">)</span> <span class="token punctuation">(</span>mod p<span class="token punctuation">)</span><span class="token keyword">int</span> <span class="token function">qmi</span><span class="token punctuation">(</span><span class="token keyword">int</span> a<span class="token punctuation">,</span> <span class="token keyword">int</span> k<span class="token punctuation">,</span> <span class="token keyword">int</span> p<span class="token punctuation">)</span> <span class="token comment">// 快速幂模板</span><span class="token punctuation">{</span> <span class="token keyword">int</span> res <span class="token operator">=</span> <span class="token number">1</span> <span class="token operator">%</span> p<span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>k<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>k <span class="token operator">&</span> <span class="token number">1</span><span class="token punctuation">)</span> res <span class="token operator">=</span> <span class="token punctuation">(</span>LL<span class="token punctuation">)</span>res <span class="token operator">*</span> a <span class="token operator">%</span> p<span class="token punctuation">;</span> a <span class="token operator">=</span> <span class="token punctuation">(</span>LL<span class="token punctuation">)</span>a <span class="token operator">*</span> a <span class="token operator">%</span> p<span class="token punctuation">;</span> k <span class="token operator">>>=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> res<span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">int</span> <span class="token function">C</span><span class="token punctuation">(</span><span class="token keyword">int</span> a<span class="token punctuation">,</span> <span class="token keyword">int</span> b<span class="token punctuation">,</span> <span class="token keyword">int</span> p<span class="token punctuation">)</span> <span class="token comment">// 通过定理求组合数C(a, b)</span><span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>a <span class="token operator"><</span> b<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span> LL x <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">,</span> y <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token comment">// x是分子,y是分母</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> a<span class="token punctuation">,</span> j <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> j <span class="token operator"><=</span> b<span class="token punctuation">;</span> i <span class="token operator">--</span><span class="token punctuation">,</span> j <span class="token operator">++</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> x <span class="token operator">=</span> <span class="token punctuation">(</span>LL<span class="token punctuation">)</span>x <span class="token operator">*</span> i <span class="token operator">%</span> p<span class="token punctuation">;</span> y <span class="token operator">=</span> <span class="token punctuation">(</span>LL<span class="token punctuation">)</span> y <span class="token operator">*</span> j <span class="token operator">%</span> p<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> x <span class="token operator">*</span> <span class="token punctuation">(</span>LL<span class="token punctuation">)</span><span class="token function">qmi</span><span class="token punctuation">(</span>y<span class="token punctuation">,</span> p <span class="token operator">-</span> <span class="token number">2</span><span class="token punctuation">,</span> p<span class="token punctuation">)</span> <span class="token operator">%</span> p<span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">int</span> <span class="token function">lucas</span><span class="token punctuation">(</span>LL a<span class="token punctuation">,</span> LL b<span class="token punctuation">,</span> <span class="token keyword">int</span> p<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>a <span class="token operator"><</span> p <span class="token operator">&&</span> b <span class="token operator"><</span> p<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token function">C</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">,</span> p<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token punctuation">(</span>LL<span class="token punctuation">)</span><span class="token function">C</span><span class="token punctuation">(</span>a <span class="token operator">%</span> p<span class="token punctuation">,</span> b <span class="token operator">%</span> p<span class="token punctuation">,</span> p<span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token function">lucas</span><span class="token punctuation">(</span>a <span class="token operator">/</span> p<span class="token punctuation">,</span> b <span class="token operator">/</span> p<span class="token punctuation">,</span> p<span class="token punctuation">)</span> <span class="token operator">%</span> p<span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h2 id="13-分解质因数求组合数"><a href="#13-分解质因数求组合数" class="headerlink" title="13. 分解质因数求组合数"></a>13. 分解质因数求组合数</h2><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp">当我们需要求出组合数的真实值,而非对某个数的余数时,分解质因数的方式比较好用: <span class="token number">1.</span> 筛法求出范围内的所有质数 <span class="token number">2.</span> 通过 <span class="token function">C</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span> <span class="token operator">=</span> a<span class="token operator">!</span> <span class="token operator">/</span> b<span class="token operator">!</span> <span class="token operator">/</span> <span class="token punctuation">(</span>a <span class="token operator">-</span> b<span class="token punctuation">)</span><span class="token operator">!</span> 这个公式求出每个质因子的次数。 n<span class="token operator">!</span> 中p的次数是 n <span class="token operator">/</span> p <span class="token operator">+</span> n <span class="token operator">/</span> p<span class="token operator">^</span><span class="token number">2</span> <span class="token operator">+</span> n <span class="token operator">/</span> p<span class="token operator">^</span><span class="token number">3</span> <span class="token operator">+</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> <span class="token number">3.</span> 用高精度乘法将所有质因子相乘<span class="token keyword">int</span> primes<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">,</span> cnt<span class="token punctuation">;</span> <span class="token comment">// 存储所有质数</span><span class="token keyword">int</span> sum<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// 存储每个质数的次数</span><span class="token keyword">bool</span> st<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// 存储每个数是否已被筛掉</span><span class="token keyword">void</span> <span class="token function">get_primes</span><span class="token punctuation">(</span><span class="token keyword">int</span> n<span class="token punctuation">)</span> <span class="token comment">// 线性筛法求素数</span><span class="token punctuation">{</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span> i <span class="token operator"><=</span> n<span class="token punctuation">;</span> i <span class="token operator">++</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>st<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span> primes<span class="token punctuation">[</span>cnt <span class="token operator">++</span> <span class="token punctuation">]</span> <span class="token operator">=</span> i<span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> primes<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator"><=</span> n <span class="token operator">/</span> i<span class="token punctuation">;</span> j <span class="token operator">++</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> st<span class="token punctuation">[</span>primes<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">*</span> i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>i <span class="token operator">%</span> primes<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token keyword">int</span> <span class="token function">get</span><span class="token punctuation">(</span><span class="token keyword">int</span> n<span class="token punctuation">,</span> <span class="token keyword">int</span> p<span class="token punctuation">)</span> <span class="token comment">// 求n!中的次数</span><span class="token punctuation">{</span> <span class="token keyword">int</span> res <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>n<span class="token punctuation">)</span> <span class="token punctuation">{</span> res <span class="token operator">+=</span> n <span class="token operator">/</span> p<span class="token punctuation">;</span> n <span class="token operator">/=</span> p<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> res<span class="token punctuation">;</span><span class="token punctuation">}</span>vector<span class="token operator"><</span><span class="token keyword">int</span><span class="token operator">></span> <span class="token function">mul</span><span class="token punctuation">(</span>vector<span class="token operator"><</span><span class="token keyword">int</span><span class="token operator">></span> a<span class="token punctuation">,</span> <span class="token keyword">int</span> b<span class="token punctuation">)</span> <span class="token comment">// 高精度乘低精度模板</span><span class="token punctuation">{</span> vector<span class="token operator"><</span><span class="token keyword">int</span><span class="token operator">></span> c<span class="token punctuation">;</span> <span class="token keyword">int</span> t <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> a<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> i <span class="token operator">++</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> t <span class="token operator">+=</span> a<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">*</span> b<span class="token punctuation">;</span> c<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span>t <span class="token operator">%</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span> t <span class="token operator">/=</span> <span class="token number">10</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>t<span class="token punctuation">)</span> <span class="token punctuation">{</span> c<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span>t <span class="token operator">%</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span> t <span class="token operator">/=</span> <span class="token number">10</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> c<span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token function">get_primes</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 预处理范围内的所有质数</span><span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> cnt<span class="token punctuation">;</span> i <span class="token operator">++</span> <span class="token punctuation">)</span> <span class="token comment">// 求每个质因数的次数</span><span class="token punctuation">{</span> <span class="token keyword">int</span> p <span class="token operator">=</span> primes<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span> sum<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">get</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> p<span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token function">get</span><span class="token punctuation">(</span>b<span class="token punctuation">,</span> p<span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token function">get</span><span class="token punctuation">(</span>a <span class="token operator">-</span> b<span class="token punctuation">,</span> p<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span>vector<span class="token operator"><</span><span class="token keyword">int</span><span class="token operator">></span> res<span class="token punctuation">;</span>res<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> cnt<span class="token punctuation">;</span> i <span class="token operator">++</span> <span class="token punctuation">)</span> <span class="token comment">// 用高精度乘法将所有质因子相乘</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> j <span class="token operator"><</span> sum<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span> j <span class="token operator">++</span> <span class="token punctuation">)</span> res <span class="token operator">=</span> <span class="token function">mul</span><span class="token punctuation">(</span>res<span class="token punctuation">,</span> primes<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h2 id="14-卡特兰数"><a href="#14-卡特兰数" class="headerlink" title="14. 卡特兰数"></a>14. 卡特兰数</h2><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp">给定n个<span class="token number">0</span>和n个<span class="token number">1</span>,它们按照某种顺序排成长度为<span class="token number">2</span>n的序列,满足任意前缀中<span class="token number">0</span>的个数都不少于<span class="token number">1</span>的个数的序列的数量为: <span class="token function">Cat</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token function">C</span><span class="token punctuation">(</span><span class="token number">2</span>n<span class="token punctuation">,</span> n<span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token punctuation">(</span>n <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></div><h2 id="15-NIM游戏"><a href="#15-NIM游戏" class="headerlink" title="15. NIM游戏"></a>15. NIM游戏</h2><p>给定N堆物品,第i堆物品有Ai个。两名玩家轮流行动,每次可以任选一堆,取走任意多个物品,可把一堆取光,但不能不取。取走最后一件物品者获胜。两人都采取最优策略,问先手是否必胜。</p><p>我们把这种游戏称为NIM博弈。把游戏过程中面临的状态称为局面。整局游戏第一个行动的称为先手,第二个行动的称为后手。若在某一局面下无论采取何种行动,都会输掉游戏,则称该局面必败。<br>所谓采取最优策略是指,若在某一局面下存在某种行动,使得行动后对面面临必败局面,则优先采取该行动。同时,这样的局面被称为必胜。我们讨论的博弈问题一般都只考虑理想情况,即两人均无失误,都采取最优策略行动时游戏的结果。<br>NIM博弈不存在平局,只有先手必胜和先手必败两种情况。</p><p>定理: NIM博弈先手必胜,当且仅当 A1 ^ A2 ^ … ^ An != 0</p><hr><h3 id="公平组合游戏ICG"><a href="#公平组合游戏ICG" class="headerlink" title="公平组合游戏ICG"></a>公平组合游戏ICG</h3><p>若一个游戏满足:</p><ol><li>由两名玩家交替行动;</li><li>在游戏进程的任意时刻,可以执行的合法行动与轮到哪名玩家无关;</li><li>不能行动的玩家判负;</li></ol><p>则称该游戏为一个公平组合游戏。<br>NIM博弈属于公平组合游戏,但城建的棋类游戏,比如围棋,就不是公平组合游戏。因为围棋交战双方分别只能落黑子和白子,胜负判定也比较复杂,不满足条件2和条件3。</p><hr><h3 id="有向图游戏"><a href="#有向图游戏" class="headerlink" title="有向图游戏"></a>有向图游戏</h3><p>给定一个有向无环图,图中有一个唯一的起点,在起点上放有一枚棋子。两名玩家交替地把这枚棋子沿有向边进行移动,每次可以移动一步,无法移动者判负。该游戏被称为有向图游戏。<br>任何一个公平组合游戏都可以转化为有向图游戏。具体方法是,把每个局面看成图中的一个节点,并且从每个局面向沿着合法行动能够到达的下一个局面连有向边。</p><hr><h3 id="Mex运算"><a href="#Mex运算" class="headerlink" title="Mex运算"></a>Mex运算</h3><p>设S表示一个非负整数集合。定义mex(S)为求出不属于集合S的最小非负整数的运算,即:<br>mex(S) = min{x}, x属于自然数,且x不属于S</p><hr><h3 id="SG函数"><a href="#SG函数" class="headerlink" title="SG函数"></a>SG函数</h3><p>在有向图游戏中,对于每个节点x,设从x出发共有k条有向边,分别到达节点y1, y2, …, yk,定义SG(x)为x的后继节点y1, y2, …, yk 的SG函数值构成的集合再执行mex(S)运算的结果,即:<br>SG(x) = mex({SG(y1), SG(y2), …, SG(yk)})<br>特别地,整个有向图游戏G的SG函数值被定义为有向图游戏起点s的SG函数值,即SG(G) = SG(s)。</p><hr><h3 id="有向图游戏的和"><a href="#有向图游戏的和" class="headerlink" title="有向图游戏的和"></a>有向图游戏的和</h3><p>在有向图游戏中,对于每个节点x,设从x出发共有k条有向边,分别到达节点y1, y2, …, yk,定义SG(x)为x的后继节点y1, y2, …, yk 的SG函数值构成的集合再执行mex(S)运算的结果,即:<br>SG(x) = mex({SG(y1), SG(y2), …, SG(yk)})<br>特别地,整个有向图游戏G的SG函数值被定义为有向图游戏起点s的SG函数值,即SG(G) = SG(s)。</p><hr><h3 id="定理"><a href="#定理" class="headerlink" title="定理"></a>定理</h3><p>有向图游戏的某个局面必胜,当且仅当该局面对应节点的SG函数值大于0。<br>有向图游戏的某个局面必败,当且仅当该局面对应节点的SG函数值等于0。</p><h1 id="四、图论"><a href="#四、图论" class="headerlink" title="四、图论"></a>四、图论</h1><h3 id="1-链式前向星"><a href="#1-链式前向星" class="headerlink" title="1. 链式前向星"></a>1. 链式前向星</h3><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token comment">//存储结构 </span><span class="token keyword">struct</span> <span class="token class-name">Edge</span><span class="token punctuation">{</span><span class="token keyword">int</span> to<span class="token punctuation">;</span><span class="token comment">//边的终点 </span><span class="token keyword">int</span> w<span class="token punctuation">;</span><span class="token comment">//边的起点 </span><span class="token keyword">int</span> next<span class="token punctuation">;</span><span class="token comment">//同起点的下一条边 </span><span class="token punctuation">}</span>edge<span class="token punctuation">[</span>M<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">//M为边的最大数,N为点的 </span><span class="token keyword">int</span> cnt<span class="token punctuation">;</span><span class="token comment">//使用cnt来计数 </span><span class="token keyword">int</span> head<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//存储点,用来索引每个点下边的分布 </span><span class="token comment">//初始化 </span>cnt <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token comment">//计数0 </span><span class="token function">memset</span><span class="token punctuation">(</span>head<span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>head<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//head为存储点,将head的值设为-1 </span><span class="token comment">//添加边 </span><span class="token keyword">void</span> <span class="token function">addEdge</span><span class="token punctuation">(</span><span class="token keyword">int</span> u<span class="token punctuation">,</span> <span class="token keyword">int</span> v<span class="token punctuation">,</span> <span class="token keyword">int</span> w<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token comment">//u v w 分别表示起点 终点 权值 </span>edge<span class="token punctuation">[</span>cnt<span class="token punctuation">]</span><span class="token punctuation">.</span>to <span class="token operator">=</span> v<span class="token punctuation">;</span><span class="token comment">//记录终点 </span>edge<span class="token punctuation">[</span>cnt<span class="token punctuation">]</span><span class="token punctuation">.</span>w <span class="token operator">=</span> w<span class="token punctuation">;</span><span class="token comment">//记录权值 </span>edge<span class="token punctuation">[</span>cnt<span class="token punctuation">]</span><span class="token punctuation">.</span>next <span class="token operator">=</span> head<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//将构造好的边放进对应的head </span>head<span class="token punctuation">[</span>u<span class="token punctuation">]</span> <span class="token operator">=</span> cnt<span class="token operator">++</span><span class="token punctuation">;</span><span class="token comment">//更新head和cnt </span><span class="token punctuation">}</span> <span class="token comment">//遍历以u为起点的每一条边 </span><span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> head<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token operator">~</span>i<span class="token punctuation">;</span> i <span class="token operator">=</span> edge<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>next<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token keyword">int</span> to <span class="token operator">=</span> edge<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>to<span class="token punctuation">;</span><span class="token keyword">int</span> w <span class="token operator">=</span> edge<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>w<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span> <span class="token comment">// 对于每个点k,开一个单链表,存储k所有可以走到的点。h[k]存储这个单链表的头结点</span><span class="token keyword">int</span> h<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">,</span> e<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">,</span> ne<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">,</span> idx<span class="token punctuation">;</span><span class="token comment">// 添加一条边a->b</span><span class="token keyword">void</span> <span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">int</span> a<span class="token punctuation">,</span> <span class="token keyword">int</span> b<span class="token punctuation">)</span><span class="token punctuation">{</span> e<span class="token punctuation">[</span>idx<span class="token punctuation">]</span> <span class="token operator">=</span> b<span class="token punctuation">,</span> ne<span class="token punctuation">[</span>idx<span class="token punctuation">]</span> <span class="token operator">=</span> h<span class="token punctuation">[</span>a<span class="token punctuation">]</span><span class="token punctuation">,</span> h<span class="token punctuation">[</span>a<span class="token punctuation">]</span> <span class="token operator">=</span> idx <span class="token operator">++</span> <span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token comment">// 初始化</span>idx <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token function">memset</span><span class="token punctuation">(</span>h<span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token keyword">sizeof</span> h<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h2 id="2-dfs"><a href="#2-dfs" class="headerlink" title="2. dfs"></a>2. dfs</h2><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">int</span> <span class="token function">dfs</span><span class="token punctuation">(</span><span class="token keyword">int</span> u<span class="token punctuation">)</span><span class="token punctuation">{</span> st<span class="token punctuation">[</span>u<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span> <span class="token comment">// st[u] 表示点u已经被遍历过</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> h<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">;</span> i <span class="token operator">!=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator">=</span> ne<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> j <span class="token operator">=</span> e<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>st<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token function">dfs</span><span class="token punctuation">(</span>j<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h2 id="3-bfs"><a href="#3-bfs" class="headerlink" title="3. bfs"></a>3. bfs</h2><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp">queue<span class="token operator"><</span><span class="token keyword">int</span><span class="token operator">></span> q<span class="token punctuation">;</span>st<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span> <span class="token comment">// 表示1号点已经被遍历过</span>q<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">while</span> <span class="token punctuation">(</span>q<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">int</span> t <span class="token operator">=</span> q<span class="token punctuation">.</span><span class="token function">front</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> q<span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> h<span class="token punctuation">[</span>t<span class="token punctuation">]</span><span class="token punctuation">;</span> i <span class="token operator">!=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator">=</span> ne<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> j <span class="token operator">=</span> e<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>st<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> st<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span> <span class="token comment">// 表示点j已经被遍历过</span> q<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>j<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h2 id="4-拓扑排序"><a href="#4-拓扑排序" class="headerlink" title="4. 拓扑排序"></a>4. 拓扑排序</h2><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">bool</span> <span class="token function">topsort</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">int</span> hh <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> tt <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> <span class="token comment">// d[i] 存储点i的入度</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator"><=</span> n<span class="token punctuation">;</span> i <span class="token operator">++</span> <span class="token punctuation">)</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>d<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span> q<span class="token punctuation">[</span> <span class="token operator">++</span> tt<span class="token punctuation">]</span> <span class="token operator">=</span> i<span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>hh <span class="token operator"><=</span> tt<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> t <span class="token operator">=</span> q<span class="token punctuation">[</span>hh <span class="token operator">++</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> h<span class="token punctuation">[</span>t<span class="token punctuation">]</span><span class="token punctuation">;</span> i <span class="token operator">!=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator">=</span> ne<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> j <span class="token operator">=</span> e<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">--</span> d<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> q<span class="token punctuation">[</span> <span class="token operator">++</span> tt<span class="token punctuation">]</span> <span class="token operator">=</span> j<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token comment">// 如果所有点都入队了,说明存在拓扑序列;否则不存在拓扑序列。</span> <span class="token keyword">return</span> tt <span class="token operator">==</span> n <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h2 id="5-最短路"><a href="#5-最短路" class="headerlink" title="5.最短路"></a>5.最短路</h2><h3 id="5-1-Dijkstra-朴素"><a href="#5-1-Dijkstra-朴素" class="headerlink" title="5.1 Dijkstra(朴素)"></a>5.1 Dijkstra(朴素)</h3><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">int</span> g<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// 存储每条边</span><span class="token keyword">int</span> dist<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// 存储1号点到每个点的最短距离</span><span class="token keyword">bool</span> st<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// 存储每个点的最短路是否已经确定</span><span class="token comment">// 求1号点到n号点的最短路,如果不存在则返回-1</span><span class="token keyword">int</span> <span class="token function">dijkstra</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token function">memset</span><span class="token punctuation">(</span>dist<span class="token punctuation">,</span> <span class="token number">0x3f</span><span class="token punctuation">,</span> <span class="token keyword">sizeof</span> dist<span class="token punctuation">)</span><span class="token punctuation">;</span> dist<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> n <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator">++</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> t <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> <span class="token comment">// 在还未确定最短路的点中,寻找距离最小的点</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> j <span class="token operator"><=</span> n<span class="token punctuation">;</span> j <span class="token operator">++</span> <span class="token punctuation">)</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>st<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">&&</span> <span class="token punctuation">(</span>t <span class="token operator">==</span> <span class="token operator">-</span><span class="token number">1</span> <span class="token operator">||</span> dist<span class="token punctuation">[</span>t<span class="token punctuation">]</span> <span class="token operator">></span> dist<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span> t <span class="token operator">=</span> j<span class="token punctuation">;</span> <span class="token comment">// 用t更新其他点的距离</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> j <span class="token operator"><=</span> n<span class="token punctuation">;</span> j <span class="token operator">++</span> <span class="token punctuation">)</span> dist<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">min</span><span class="token punctuation">(</span>dist<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">,</span> dist<span class="token punctuation">[</span>t<span class="token punctuation">]</span> <span class="token operator">+</span> g<span class="token punctuation">[</span>t<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> st<span class="token punctuation">[</span>t<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>dist<span class="token punctuation">[</span>n<span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token number">0x3f3f3f3f</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> <span class="token keyword">return</span> dist<span class="token punctuation">[</span>n<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h2 id="5-2-Dijkstra-堆优化"><a href="#5-2-Dijkstra-堆优化" class="headerlink" title="5.2 Dijkstra(堆优化)"></a>5.2 Dijkstra(堆优化)</h2><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">typedef</span> pair<span class="token operator"><</span><span class="token keyword">int</span><span class="token punctuation">,</span> <span class="token keyword">int</span><span class="token operator">></span> PII<span class="token punctuation">;</span><span class="token keyword">int</span> n<span class="token punctuation">;</span> <span class="token comment">// 点的数量</span><span class="token keyword">int</span> h<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">,</span> w<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">,</span> e<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">,</span> ne<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">,</span> idx<span class="token punctuation">;</span> <span class="token comment">// 邻接表存储所有边</span><span class="token keyword">int</span> dist<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// 存储所有点到1号点的距离</span><span class="token keyword">bool</span> st<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// 存储每个点的最短距离是否已确定</span><span class="token comment">// 求1号点到n号点的最短距离,如果不存在,则返回-1</span><span class="token keyword">int</span> <span class="token function">dijkstra</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token function">memset</span><span class="token punctuation">(</span>dist<span class="token punctuation">,</span> <span class="token number">0x3f</span><span class="token punctuation">,</span> <span class="token keyword">sizeof</span> dist<span class="token punctuation">)</span><span class="token punctuation">;</span> dist<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> priority_queue<span class="token operator"><</span>PII<span class="token punctuation">,</span> vector<span class="token operator"><</span>PII<span class="token operator">></span><span class="token punctuation">,</span> greater<span class="token operator"><</span>PII<span class="token operator">>></span> heap<span class="token punctuation">;</span> heap<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// first存储距离,second存储节点编号</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>heap<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">auto</span> t <span class="token operator">=</span> heap<span class="token punctuation">.</span><span class="token function">top</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> heap<span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">int</span> ver <span class="token operator">=</span> t<span class="token punctuation">.</span>second<span class="token punctuation">,</span> distance <span class="token operator">=</span> t<span class="token punctuation">.</span>first<span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>st<span class="token punctuation">[</span>ver<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token keyword">continue</span><span class="token punctuation">;</span> st<span class="token punctuation">[</span>ver<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> h<span class="token punctuation">[</span>ver<span class="token punctuation">]</span><span class="token punctuation">;</span> i <span class="token operator">!=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator">=</span> ne<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> j <span class="token operator">=</span> e<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>dist<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">></span> distance <span class="token operator">+</span> w<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> dist<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> distance <span class="token operator">+</span> w<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span> heap<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token punctuation">{</span>dist<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">,</span> j<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>dist<span class="token punctuation">[</span>n<span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token number">0x3f3f3f3f</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> <span class="token keyword">return</span> dist<span class="token punctuation">[</span>n<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h2 id="5-3-Bellman-Ford"><a href="#5-3-Bellman-Ford" class="headerlink" title="5.3 Bellman-Ford"></a>5.3 Bellman-Ford</h2><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">int</span> n<span class="token punctuation">,</span> m<span class="token punctuation">;</span> <span class="token comment">// n表示点数,m表示边数</span><span class="token keyword">int</span> dist<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// dist[x]存储1到x的最短路距离</span><span class="token keyword">struct</span> <span class="token class-name">Edge</span> <span class="token comment">// 边,a表示出点,b表示入点,w表示边的权重</span><span class="token punctuation">{</span> <span class="token keyword">int</span> a<span class="token punctuation">,</span> b<span class="token punctuation">,</span> w<span class="token punctuation">;</span><span class="token punctuation">}</span>edges<span class="token punctuation">[</span>M<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">// 求1到n的最短路距离,如果无法从1走到n,则返回-1。</span><span class="token keyword">int</span> <span class="token function">bellman_ford</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token function">memset</span><span class="token punctuation">(</span>dist<span class="token punctuation">,</span> <span class="token number">0x3f</span><span class="token punctuation">,</span> <span class="token keyword">sizeof</span> dist<span class="token punctuation">)</span><span class="token punctuation">;</span> dist<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token comment">// 如果第n次迭代仍然会松弛三角不等式,就说明存在一条长度是n+1的最短路径,由抽屉原理,路径中至少存在两个相同的点,说明图中存在负权回路。</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> n<span class="token punctuation">;</span> i <span class="token operator">++</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> j <span class="token operator"><</span> m<span class="token punctuation">;</span> j <span class="token operator">++</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> a <span class="token operator">=</span> edges<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">.</span>a<span class="token punctuation">,</span> b <span class="token operator">=</span> edges<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">.</span>b<span class="token punctuation">,</span> w <span class="token operator">=</span> edges<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">.</span>w<span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>dist<span class="token punctuation">[</span>b<span class="token punctuation">]</span> <span class="token operator">></span> dist<span class="token punctuation">[</span>a<span class="token punctuation">]</span> <span class="token operator">+</span> w<span class="token punctuation">)</span> dist<span class="token punctuation">[</span>b<span class="token punctuation">]</span> <span class="token operator">=</span> dist<span class="token punctuation">[</span>a<span class="token punctuation">]</span> <span class="token operator">+</span> w<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>dist<span class="token punctuation">[</span>n<span class="token punctuation">]</span> <span class="token operator">></span> <span class="token number">0x3f3f3f3f</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> <span class="token keyword">return</span> dist<span class="token punctuation">[</span>n<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h2 id="5-4-SPFA"><a href="#5-4-SPFA" class="headerlink" title="5.4 SPFA"></a>5.4 SPFA</h2><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">int</span> n<span class="token punctuation">;</span> <span class="token comment">// 总点数</span><span class="token keyword">int</span> h<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">,</span> w<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">,</span> e<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">,</span> ne<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">,</span> idx<span class="token punctuation">;</span> <span class="token comment">// 邻接表存储所有边</span><span class="token keyword">int</span> dist<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// 存储每个点到1号点的最短距离</span><span class="token keyword">bool</span> st<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// 存储每个点是否在队列中</span><span class="token comment">// 求1号点到n号点的最短路距离,如果从1号点无法走到n号点则返回-1</span><span class="token keyword">int</span> <span class="token function">spfa</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token function">memset</span><span class="token punctuation">(</span>dist<span class="token punctuation">,</span> <span class="token number">0x3f</span><span class="token punctuation">,</span> <span class="token keyword">sizeof</span> dist<span class="token punctuation">)</span><span class="token punctuation">;</span> dist<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> queue<span class="token operator"><</span><span class="token keyword">int</span><span class="token operator">></span> q<span class="token punctuation">;</span> q<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span> st<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>q<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">auto</span> t <span class="token operator">=</span> q<span class="token punctuation">.</span><span class="token function">front</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> q<span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> st<span class="token punctuation">[</span>t<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> h<span class="token punctuation">[</span>t<span class="token punctuation">]</span><span class="token punctuation">;</span> i <span class="token operator">!=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator">=</span> ne<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> j <span class="token operator">=</span> e<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>dist<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">></span> dist<span class="token punctuation">[</span>t<span class="token punctuation">]</span> <span class="token operator">+</span> w<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> dist<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> dist<span class="token punctuation">[</span>t<span class="token punctuation">]</span> <span class="token operator">+</span> w<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>st<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment">// 如果队列中已存在j,则不需要将j重复插入</span> <span class="token punctuation">{</span> q<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>j<span class="token punctuation">)</span><span class="token punctuation">;</span> st<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>dist<span class="token punctuation">[</span>n<span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token number">0x3f3f3f3f</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> <span class="token keyword">return</span> dist<span class="token punctuation">[</span>n<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h2 id="5-5-SPFA判断负环"><a href="#5-5-SPFA判断负环" class="headerlink" title="5.5 SPFA判断负环"></a>5.5 SPFA判断负环</h2><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">int</span> n<span class="token punctuation">;</span> <span class="token comment">// 总点数</span><span class="token keyword">int</span> h<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">,</span> w<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">,</span> e<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">,</span> ne<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">,</span> idx<span class="token punctuation">;</span> <span class="token comment">// 邻接表存储所有边</span><span class="token keyword">int</span> dist<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">,</span> cnt<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// dist[x]存储1号点到x的最短距离,cnt[x]存储1到x的最短路中经过的点数</span><span class="token keyword">bool</span> st<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// 存储每个点是否在队列中</span><span class="token comment">// 如果存在负环,则返回true,否则返回false。</span><span class="token keyword">bool</span> <span class="token function">spfa</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token comment">// 不需要初始化dist数组</span> <span class="token comment">// 原理:如果某条最短路径上有n个点(除了自己),那么加上自己之后一共有n+1个点,由抽屉原理一定有两个点相同,所以存在环。</span> queue<span class="token operator"><</span><span class="token keyword">int</span><span class="token operator">></span> q<span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator"><=</span> n<span class="token punctuation">;</span> i <span class="token operator">++</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> q<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span> st<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>q<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">auto</span> t <span class="token operator">=</span> q<span class="token punctuation">.</span><span class="token function">front</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> q<span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> st<span class="token punctuation">[</span>t<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> h<span class="token punctuation">[</span>t<span class="token punctuation">]</span><span class="token punctuation">;</span> i <span class="token operator">!=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator">=</span> ne<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> j <span class="token operator">=</span> e<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>dist<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">></span> dist<span class="token punctuation">[</span>t<span class="token punctuation">]</span> <span class="token operator">+</span> w<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> dist<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> dist<span class="token punctuation">[</span>t<span class="token punctuation">]</span> <span class="token operator">+</span> w<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span> cnt<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> cnt<span class="token punctuation">[</span>t<span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>cnt<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">>=</span> n<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span> <span class="token comment">// 如果从1号点到x的最短路中包含至少n个点(不包括自己),则说明存在环</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>st<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> q<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>j<span class="token punctuation">)</span><span class="token punctuation">;</span> st<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h2 id="5-6-Floyd"><a href="#5-6-Floyd" class="headerlink" title="5.6 Floyd"></a>5.6 Floyd</h2><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">int</span> n<span class="token punctuation">;</span> <span class="token comment">// 总点数</span><span class="token keyword">int</span> h<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">,</span> w<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">,</span> e<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">,</span> ne<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">,</span> idx<span class="token punctuation">;</span> <span class="token comment">// 邻接表存储所有边</span><span class="token keyword">int</span> dist<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">,</span> cnt<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// dist[x]存储1号点到x的最短距离,cnt[x]存储1到x的最短路中经过的点数</span><span class="token keyword">bool</span> st<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// 存储每个点是否在队列中</span><span class="token comment">// 如果存在负环,则返回true,否则返回false。</span><span class="token keyword">bool</span> <span class="token function">spfa</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token comment">// 不需要初始化dist数组</span> <span class="token comment">// 原理:如果某条最短路径上有n个点(除了自己),那么加上自己之后一共有n+1个点,由抽屉原理一定有两个点相同,所以存在环。</span> queue<span class="token operator"><</span><span class="token keyword">int</span><span class="token operator">></span> q<span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator"><=</span> n<span class="token punctuation">;</span> i <span class="token operator">++</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> q<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span> st<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>q<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">auto</span> t <span class="token operator">=</span> q<span class="token punctuation">.</span><span class="token function">front</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> q<span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> st<span class="token punctuation">[</span>t<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> h<span class="token punctuation">[</span>t<span class="token punctuation">]</span><span class="token punctuation">;</span> i <span class="token operator">!=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator">=</span> ne<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> j <span class="token operator">=</span> e<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>dist<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">></span> dist<span class="token punctuation">[</span>t<span class="token punctuation">]</span> <span class="token operator">+</span> w<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> dist<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> dist<span class="token punctuation">[</span>t<span class="token punctuation">]</span> <span class="token operator">+</span> w<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span> cnt<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> cnt<span class="token punctuation">[</span>t<span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>cnt<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">>=</span> n<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span> <span class="token comment">// 如果从1号点到x的最短路中包含至少n个点(不包括自己),则说明存在环</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>st<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> q<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>j<span class="token punctuation">)</span><span class="token punctuation">;</span> st<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h2 id="5-7-朴素prim"><a href="#5-7-朴素prim" class="headerlink" title="5.7 朴素prim"></a>5.7 朴素prim</h2><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">int</span> n<span class="token punctuation">;</span> <span class="token comment">// n表示点数</span><span class="token keyword">int</span> g<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// 邻接矩阵,存储所有边</span><span class="token keyword">int</span> dist<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// 存储其他点到当前最小生成树的距离</span><span class="token keyword">bool</span> st<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// 存储每个点是否已经在生成树中</span><span class="token comment">// 如果图不连通,则返回INF(值是0x3f3f3f3f), 否则返回最小生成树的树边权重之和</span><span class="token keyword">int</span> <span class="token function">prim</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token function">memset</span><span class="token punctuation">(</span>dist<span class="token punctuation">,</span> <span class="token number">0x3f</span><span class="token punctuation">,</span> <span class="token keyword">sizeof</span> dist<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">int</span> res <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> n<span class="token punctuation">;</span> i <span class="token operator">++</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> t <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> j <span class="token operator"><=</span> n<span class="token punctuation">;</span> j <span class="token operator">++</span> <span class="token punctuation">)</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>st<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">&&</span> <span class="token punctuation">(</span>t <span class="token operator">==</span> <span class="token operator">-</span><span class="token number">1</span> <span class="token operator">||</span> dist<span class="token punctuation">[</span>t<span class="token punctuation">]</span> <span class="token operator">></span> dist<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span> t <span class="token operator">=</span> j<span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>i <span class="token operator">&&</span> dist<span class="token punctuation">[</span>t<span class="token punctuation">]</span> <span class="token operator">==</span> INF<span class="token punctuation">)</span> <span class="token keyword">return</span> INF<span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>i<span class="token punctuation">)</span> res <span class="token operator">+=</span> dist<span class="token punctuation">[</span>t<span class="token punctuation">]</span><span class="token punctuation">;</span> st<span class="token punctuation">[</span>t<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> j <span class="token operator"><=</span> n<span class="token punctuation">;</span> j <span class="token operator">++</span> <span class="token punctuation">)</span> dist<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">min</span><span class="token punctuation">(</span>dist<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">,</span> g<span class="token punctuation">[</span>t<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> res<span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h2 id="5-8-Kruskal"><a href="#5-8-Kruskal" class="headerlink" title="5.8 Kruskal"></a>5.8 Kruskal</h2><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">int</span> n<span class="token punctuation">,</span> m<span class="token punctuation">;</span> <span class="token comment">// n是点数,m是边数</span><span class="token keyword">int</span> p<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// 并查集的父节点数组</span><span class="token keyword">struct</span> <span class="token class-name">Edge</span> <span class="token comment">// 存储边</span><span class="token punctuation">{</span> <span class="token keyword">int</span> a<span class="token punctuation">,</span> b<span class="token punctuation">,</span> w<span class="token punctuation">;</span> <span class="token keyword">bool</span> <span class="token keyword">operator</span><span class="token operator"><</span> <span class="token punctuation">(</span><span class="token keyword">const</span> Edge <span class="token operator">&</span>W<span class="token punctuation">)</span><span class="token keyword">const</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> w <span class="token operator"><</span> W<span class="token punctuation">.</span>w<span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span>edges<span class="token punctuation">[</span>M<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token keyword">int</span> <span class="token function">find</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">)</span> <span class="token comment">// 并查集核心操作</span><span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>p<span class="token punctuation">[</span>x<span class="token punctuation">]</span> <span class="token operator">!=</span> x<span class="token punctuation">)</span> p<span class="token punctuation">[</span>x<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">find</span><span class="token punctuation">(</span>p<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> p<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">int</span> <span class="token function">kruskal</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token function">sort</span><span class="token punctuation">(</span>edges<span class="token punctuation">,</span> edges <span class="token operator">+</span> m<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator"><=</span> n<span class="token punctuation">;</span> i <span class="token operator">++</span> <span class="token punctuation">)</span> p<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> i<span class="token punctuation">;</span> <span class="token comment">// 初始化并查集</span> <span class="token keyword">int</span> res <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> cnt <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> m<span class="token punctuation">;</span> i <span class="token operator">++</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> a <span class="token operator">=</span> edges<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>a<span class="token punctuation">,</span> b <span class="token operator">=</span> edges<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>b<span class="token punctuation">,</span> w <span class="token operator">=</span> edges<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>w<span class="token punctuation">;</span> a <span class="token operator">=</span> <span class="token function">find</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">,</span> b <span class="token operator">=</span> <span class="token function">find</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>a <span class="token operator">!=</span> b<span class="token punctuation">)</span> <span class="token comment">// 如果两个连通块不连通,则将这两个连通块合并</span> <span class="token punctuation">{</span> p<span class="token punctuation">[</span>a<span class="token punctuation">]</span> <span class="token operator">=</span> b<span class="token punctuation">;</span> res <span class="token operator">+=</span> w<span class="token punctuation">;</span> cnt <span class="token operator">++</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>cnt <span class="token operator"><</span> n <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token keyword">return</span> INF<span class="token punctuation">;</span> <span class="token keyword">return</span> res<span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h2 id="5-9-染色法判别二分图"><a href="#5-9-染色法判别二分图" class="headerlink" title="5.9 染色法判别二分图"></a>5.9 染色法判别二分图</h2><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">int</span> n<span class="token punctuation">;</span> <span class="token comment">// n表示点数</span><span class="token keyword">int</span> h<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">,</span> e<span class="token punctuation">[</span>M<span class="token punctuation">]</span><span class="token punctuation">,</span> ne<span class="token punctuation">[</span>M<span class="token punctuation">]</span><span class="token punctuation">,</span> idx<span class="token punctuation">;</span> <span class="token comment">// 邻接表存储图</span><span class="token keyword">int</span> color<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// 表示每个点的颜色,-1表示未染色,0表示白色,1表示黑色</span><span class="token comment">// 参数:u表示当前节点,c表示当前点的颜色</span><span class="token keyword">bool</span> <span class="token function">dfs</span><span class="token punctuation">(</span><span class="token keyword">int</span> u<span class="token punctuation">,</span> <span class="token keyword">int</span> c<span class="token punctuation">)</span><span class="token punctuation">{</span> color<span class="token punctuation">[</span>u<span class="token punctuation">]</span> <span class="token operator">=</span> c<span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> h<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">;</span> i <span class="token operator">!=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator">=</span> ne<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> j <span class="token operator">=</span> e<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>color<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token function">dfs</span><span class="token punctuation">(</span>j<span class="token punctuation">,</span> <span class="token operator">!</span>c<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>color<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">==</span> c<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">bool</span> <span class="token function">check</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token function">memset</span><span class="token punctuation">(</span>color<span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token keyword">sizeof</span> color<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">bool</span> flag <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator"><=</span> n<span class="token punctuation">;</span> i <span class="token operator">++</span> <span class="token punctuation">)</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>color<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token function">dfs</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> flag <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> flag<span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h2 id="5-10-匈牙利算法"><a href="#5-10-匈牙利算法" class="headerlink" title="5.10 匈牙利算法"></a>5.10 匈牙利算法</h2><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">int</span> n1<span class="token punctuation">,</span> n2<span class="token punctuation">;</span> <span class="token comment">// n1表示第一个集合中的点数,n2表示第二个集合中的点数</span><span class="token keyword">int</span> h<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">,</span> e<span class="token punctuation">[</span>M<span class="token punctuation">]</span><span class="token punctuation">,</span> ne<span class="token punctuation">[</span>M<span class="token punctuation">]</span><span class="token punctuation">,</span> idx<span class="token punctuation">;</span> <span class="token comment">// 邻接表存储所有边,匈牙利算法中只会用到从第一个集合指向第二个集合的边,所以这里只用存一个方向的边</span><span class="token keyword">int</span> match<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// 存储第二个集合中的每个点当前匹配的第一个集合中的点是哪个</span><span class="token keyword">bool</span> st<span class="token punctuation">[</span>N<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// 表示第二个集合中的每个点是否已经被遍历过</span><span class="token keyword">bool</span> <span class="token function">find</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> h<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">;</span> i <span class="token operator">!=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator">=</span> ne<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> j <span class="token operator">=</span> e<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>st<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> st<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>match<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token number">0</span> <span class="token operator">||</span> <span class="token function">find</span><span class="token punctuation">(</span>match<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> match<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> x<span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token comment">// 求最大匹配数,依次枚举第一个集合中的每个点能否匹配第二个集合中的点</span><span class="token keyword">int</span> res <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator"><=</span> n1<span class="token punctuation">;</span> i <span class="token operator">++</span> <span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token function">memset</span><span class="token punctuation">(</span>st<span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">,</span> <span class="token keyword">sizeof</span> st<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">find</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">)</span> res <span class="token operator">++</span> <span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>]]></content>
<categories>
<category>算法</category>
</categories>
<tags>
<tag>数据结构</tag>
<tag>算法模板</tag>
</tags>
</entry>
<entry>
<title>POJ 3126 Prime Path</title>
<link href="/posts/101590c6/"/>
<url>/posts/101590c6/</url>
<content type="html"><![CDATA[<h1 id="【POJ】-3126-Prime-Path"><a href="#【POJ】-3126-Prime-Path" class="headerlink" title="【POJ】 3126 Prime Path"></a>【POJ】 3126 Prime Path</h1><blockquote><p>题目链接:<a href="https://vjudge.net/contest/65959#problem/F">poj-3126</a></p></blockquote><h2 id="Description"><a href="#Description" class="headerlink" title="Description"></a>Description</h2><p>The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change the four-digit room numbers on their offices.<br>Now, the minister of finance, who had been eavesdropping, intervened. — No unnecessary expenditure, please! I happen to know that the price of a digit is one pound. — Hmm, in that case I need a computer program to minimize the cost. You don’t know some very cheap software gurus, do you? — In fact, I do. You see, there is this programming contest going on… Help the prime minister to find the cheapest prime path between any two given four-digit primes! The first digit must be nonzero, of course. Here is a solution in the case above. </p><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token number">1033</span> <span class="token number">1733</span> <span class="token number">3733</span> <span class="token number">3739</span> <span class="token number">3779</span> <span class="token number">8779</span> <span class="token number">8179</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><p>The cost of this solution is 6 pounds. Note that the digit 1 which got pasted over in step 2 can not be reused in the last step – a new 1 must be purchased.</p><hr><h2 id="Input"><a href="#Input" class="headerlink" title="Input"></a>Input</h2><p> One line with a positive number: the number of test cases (at most 100). Then for each test case, one line with two numbers separated by a blank. Both numbers are four-digit primes (without leading zeros). </p><hr><h2 id="Output"><a href="#Output" class="headerlink" title="Output"></a>Output</h2><p> One line for each case, either with a number stating the minimal cost or containing the word Impossible. </p><hr><h2 id="Sample-Input"><a href="#Sample-Input" class="headerlink" title="Sample Input"></a>Sample Input</h2><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token number">3</span><span class="token number">1033</span> <span class="token number">8179</span><span class="token number">1373</span> <span class="token number">8017</span><span class="token number">1033</span> <span class="token number">1033</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre></div><hr><h2 id="Sample-Output"><a href="#Sample-Output" class="headerlink" title="Sample Output"></a>Sample Output</h2><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token number">6</span><span class="token number">7</span><span class="token number">0</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre></div><hr><h2 id="题目理解"><a href="#题目理解" class="headerlink" title="题目理解"></a>题目理解</h2><p> 给定两个四位素数,找出从第一个数到第二个数的最短路径长度,规则是:</p><ol><li>每次只能更改一个数字</li><li>每次更改之后的数字还是素数</li></ol><p> 本体需要判定大量四位数字是否是素数,所以可以先用素数筛将范围内的素数筛选出来,这样在变更数字后查询该数字是否为素数是就可以做到O(1)的时间复杂度。</p><p> 寻找最短路径长度,使用BFS。</p><hr><h2 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h2><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string"><iostream></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string"><cstring></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string"><queue></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string"><cmath></span></span><span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span><span class="token keyword">const</span> <span class="token keyword">int</span> MAXN <span class="token operator">=</span> <span class="token number">10001</span><span class="token punctuation">;</span><span class="token keyword">struct</span> <span class="token class-name">primeth</span><span class="token punctuation">{</span><span class="token comment">//答案要求的是路径长度,都BFS过程中需要记录素数数值(num)以及路径长度(ans) </span><span class="token keyword">int</span> num<span class="token punctuation">;</span><span class="token keyword">int</span> ans<span class="token punctuation">;</span><span class="token function">primeth</span><span class="token punctuation">(</span><span class="token keyword">int</span> num<span class="token punctuation">,</span> <span class="token keyword">int</span> ans<span class="token punctuation">)</span> <span class="token operator">:</span> <span class="token function">num</span><span class="token punctuation">(</span>num<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">ans</span><span class="token punctuation">(</span>ans<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">;</span><span class="token keyword">int</span> a<span class="token punctuation">,</span> b<span class="token punctuation">,</span> cnt<span class="token punctuation">,</span> n<span class="token punctuation">;</span><span class="token keyword">int</span> primes<span class="token punctuation">[</span>MAXN<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token keyword">bool</span> flag<span class="token punctuation">[</span>MAXN<span class="token punctuation">]</span><span class="token punctuation">,</span> vis<span class="token punctuation">[</span>MAXN<span class="token punctuation">]</span><span class="token punctuation">;</span>queue<span class="token operator"><</span>primeth<span class="token operator">></span> q<span class="token punctuation">;</span><span class="token comment">//欧拉筛 (详细解释见上篇博客)</span><span class="token keyword">void</span> <span class="token function">prime</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>cnt <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token function">memset</span><span class="token punctuation">(</span>flag<span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">,</span> MAXN<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span> i <span class="token operator"><</span> MAXN<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>flag<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span>primes<span class="token punctuation">[</span>cnt<span class="token operator">++</span><span class="token punctuation">]</span> <span class="token operator">=</span> i<span class="token punctuation">;</span><span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> j <span class="token operator"><=</span> cnt<span class="token punctuation">;</span> <span class="token operator">++</span>j<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token keyword">if</span><span class="token punctuation">(</span>i <span class="token operator">*</span> primes<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">></span> MAXN<span class="token punctuation">)</span> <span class="token keyword">break</span><span class="token punctuation">;</span>flag<span class="token punctuation">[</span>i <span class="token operator">*</span> primes<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span><span class="token keyword">if</span><span class="token punctuation">(</span>i <span class="token operator">%</span> primes<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token keyword">break</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">}</span> <span class="token keyword">int</span> <span class="token function">BFS</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>q<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token function">primeth</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>primeth tmp <span class="token operator">=</span> q<span class="token punctuation">.</span><span class="token function">front</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">while</span><span class="token punctuation">(</span><span class="token operator">!</span>q<span class="token punctuation">.</span><span class="token function">empty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token keyword">if</span><span class="token punctuation">(</span>tmp<span class="token punctuation">.</span>num <span class="token operator">==</span> b<span class="token punctuation">)</span> <span class="token keyword">return</span> tmp<span class="token punctuation">.</span>ans<span class="token punctuation">;</span>primeth front <span class="token operator">=</span> q<span class="token punctuation">.</span><span class="token function">front</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>q<span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>tmp<span class="token punctuation">.</span>ans <span class="token operator">=</span> front<span class="token punctuation">.</span>ans <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span><span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><=</span> <span class="token number">9</span><span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token comment">//这个循环是用来改变素数的各个位的数值的,写得比较麻烦,有更好的写法欢迎评论 </span><span class="token keyword">if</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">{</span>tmp<span class="token punctuation">.</span>num <span class="token operator">=</span> front<span class="token punctuation">.</span>num <span class="token operator">%</span> <span class="token number">1000</span> <span class="token operator">+</span> <span class="token number">1000</span> <span class="token operator">*</span> i<span class="token punctuation">;</span><span class="token keyword">if</span><span class="token punctuation">(</span>vis<span class="token punctuation">[</span>tmp<span class="token punctuation">.</span>num<span class="token punctuation">]</span> <span class="token operator">&&</span> <span class="token operator">!</span>flag<span class="token punctuation">[</span>tmp<span class="token punctuation">.</span>num<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token keyword">if</span><span class="token punctuation">(</span>tmp<span class="token punctuation">.</span>num <span class="token operator">==</span> b<span class="token punctuation">)</span> <span class="token keyword">return</span> tmp<span class="token punctuation">.</span>ans<span class="token punctuation">;</span>q<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>tmp<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span>vis<span class="token punctuation">[</span>tmp<span class="token punctuation">.</span>num<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> j <span class="token operator"><=</span> <span class="token number">3</span><span class="token punctuation">;</span> <span class="token operator">++</span>j<span class="token punctuation">)</span><span class="token punctuation">{</span>tmp<span class="token punctuation">.</span>num <span class="token operator">=</span> front<span class="token punctuation">.</span>num <span class="token operator">-</span> <span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">)</span><span class="token punctuation">(</span>front<span class="token punctuation">.</span>num <span class="token operator">%</span> <span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">)</span><span class="token function">pow</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span> j<span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token function">pow</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span> j <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token function">pow</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span> j <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token function">pow</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span> j <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">*</span> i<span class="token punctuation">;</span><span class="token keyword">if</span><span class="token punctuation">(</span>vis<span class="token punctuation">[</span>tmp<span class="token punctuation">.</span>num<span class="token punctuation">]</span> <span class="token operator">&&</span> <span class="token operator">!</span>flag<span class="token punctuation">[</span>tmp<span class="token punctuation">.</span>num<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token keyword">if</span><span class="token punctuation">(</span>tmp<span class="token punctuation">.</span>num <span class="token operator">==</span> b<span class="token punctuation">)</span> <span class="token keyword">return</span> tmp<span class="token punctuation">.</span>ans<span class="token punctuation">;</span>q<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>tmp<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span>vis<span class="token punctuation">[</span>tmp<span class="token punctuation">.</span>num<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token function">prime</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>cin <span class="token operator">>></span> n<span class="token punctuation">;</span><span class="token keyword">while</span><span class="token punctuation">(</span>n<span class="token operator">--</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token function">memset</span><span class="token punctuation">(</span>vis<span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">,</span> MAXN<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">while</span><span class="token punctuation">(</span><span class="token operator">!</span>q<span class="token punctuation">.</span><span class="token function">empty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> q<span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> cin <span class="token operator">>></span> a <span class="token operator">>></span> b<span class="token punctuation">;</span>vis<span class="token punctuation">[</span>a<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span>cout <span class="token operator"><<</span> <span class="token function">BFS</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator"><<</span> endl<span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>]]></content>
<categories>
<category>算法</category>
</categories>
<tags>
<tag>POJ</tag>
<tag>BFS</tag>
<tag>素数筛</tag>
<tag>欧拉筛</tag>
<tag>埃氏筛</tag>
<tag>数论</tag>
</tags>
</entry>
<entry>
<title>素数 埃氏筛 欧拉筛(线性筛)</title>
<link href="/posts/6597126a/"/>
<url>/posts/6597126a/</url>
<content type="html"><![CDATA[<h1 id="【素数筛】-埃氏筛-欧拉筛(线性筛)"><a href="#【素数筛】-埃氏筛-欧拉筛(线性筛)" class="headerlink" title="【素数筛】 埃氏筛 欧拉筛(线性筛)"></a>【素数筛】 埃氏筛 欧拉筛(线性筛)</h1><p> 素数筛是一种用来筛选自然数n以内全部素数的算法。</p><h2 id="埃氏筛-Sieve-of-Eratosthenes"><a href="#埃氏筛-Sieve-of-Eratosthenes" class="headerlink" title="埃氏筛 (Sieve of Eratosthenes)"></a>埃氏筛 (Sieve of Eratosthenes)</h2><p><img src="https://s2.ax1x.com/2019/11/06/Mioeu4.gif" alt="Era"></p><p> 埃氏筛的原理很容易理解,任意一个合数都可以表示成一个自然数i和一个素数的乘积,因此,如上图,筛出素数的倍数,剩下的就是素数。</p><h3 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h3><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token comment">//在primes中值为true的是合数 </span><span class="token keyword">bool</span> primes<span class="token punctuation">[</span>MAXN<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">}</span><span class="token punctuation">;</span><span class="token keyword">void</span> <span class="token function">eraSieve</span><span class="token punctuation">(</span><span class="token keyword">int</span> n<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span> i <span class="token operator">*</span> i <span class="token operator"><</span> n<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span><span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>primes<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment">//i为素数</span><span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> i <span class="token operator">*</span> i<span class="token punctuation">;</span> j <span class="token operator"><=</span> n<span class="token punctuation">;</span> j <span class="token operator">+=</span> i<span class="token punctuation">)</span><span class="token comment">//标记i的倍数为合数</span>primes<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><hr><h4 id="代码注意点:"><a href="#代码注意点:" class="headerlink" title="代码注意点:"></a>代码注意点:</h4><ol><li>外层循环的终止条件是<code>i * i < N</code>,因为只需要把不大于根号n的所有素数的倍数剔除就可以了。</li><li>内层循环,起始<code>j = i * i</code>,小于<code>i * i</code>的之前已经被去除了。</li></ol><h2 id="欧拉筛"><a href="#欧拉筛" class="headerlink" title="欧拉筛"></a>欧拉筛</h2><p> 欧拉筛是一个线性筛法,在埃氏筛中有的合数有多组因子,比如36,这样的合数就会被多次标记,不妨规定每个合数只用最小的一个质因数去筛,这就是欧拉筛了。使用到的定理 </p><p> ==<strong>n = Factory<del>max</del> * P</strong>==</p><p> 上式中,==<strong>n</strong>==是一个合数,每一个合数可以唯一的表示成如上形式。</p><p>其中Factory是除了==<strong>n</strong>==以外的最大的==<strong>n</strong>==的因数。而P满足如下两点:</p><blockquote><p><em>1. P是一个素数</em></p><p><em>2. P小于等于Factory的所有因数</em></p></blockquote><p><em><a href="https://www.cnblogs.com/woxiaosade/p/10638818.html">证明详见阳离子博客</a></em></p><h3 id="代码-1"><a href="#代码-1" class="headerlink" title="代码"></a>代码</h3><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">int</span> primes<span class="token punctuation">[</span>MAXN<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//0~N内的素数集合 </span><span class="token keyword">void</span> <span class="token function">eulerSieve</span><span class="token punctuation">(</span><span class="token keyword">int</span> n<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token keyword">int</span> sum <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token comment">//已经找到的素数的数量 </span><span class="token keyword">bool</span> flag<span class="token punctuation">[</span>MAXN<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token boolean">false</span><span class="token punctuation">}</span><span class="token punctuation">;</span><span class="token comment">//标记是否为合数 </span><span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span> i <span class="token operator"><=</span> n<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>flag<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span>primes<span class="token punctuation">[</span>sum<span class="token operator">++</span><span class="token punctuation">]</span> <span class="token operator">=</span> i<span class="token punctuation">;</span><span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">*</span> primes<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator"><=</span> n<span class="token punctuation">;</span> <span class="token operator">++</span>j<span class="token punctuation">)</span><span class="token punctuation">{</span>flag<span class="token punctuation">[</span>i <span class="token operator">*</span> primes<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span><span class="token comment">//标记素数的倍数为合数 </span><span class="token keyword">if</span><span class="token punctuation">(</span>i <span class="token operator">%</span> primes<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token keyword">break</span><span class="token punctuation">;</span><span class="token comment">//primes[j]同时是i和i*primes[j]的最小质因数 </span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>]]></content>
<categories>
<category>算法</category>
</categories>
<tags>
<tag>素数筛</tag>
<tag>欧拉筛</tag>
<tag>埃氏筛</tag>
<tag>数论</tag>
</tags>
</entry>
<entry>
<title>POJ 1426 Find The Multiple</title>
<link href="/posts/e49c5017/"/>
<url>/posts/e49c5017/</url>
<content type="html"><![CDATA[<h1 id="【POJ】1426-Find-The-Multiple-BFS-DFS"><a href="#【POJ】1426-Find-The-Multiple-BFS-DFS" class="headerlink" title="【POJ】1426 Find The Multiple (BFS/DFS)"></a>【POJ】1426 Find The Multiple (BFS/DFS)</h1><blockquote><p>题目链接:<a href="https://vjudge.net/contest/65959#problem/E">poj-1426</a></p></blockquote><h2 id="Description"><a href="#Description" class="headerlink" title="Description"></a>Description</h2><p> Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You may assume that n is not greater than 200 and there is a corresponding m containing no more than 100 decimal digits. </p><hr><h2 id="Input"><a href="#Input" class="headerlink" title="Input"></a>Input</h2><p> The input file may contain multiple test cases. Each line contains a value of n (1 <= n <= 200). A line containing a zero terminates the input. </p><hr><h2 id="Output"><a href="#Output" class="headerlink" title="Output"></a>Output</h2><p> For each value of n in the input print a line containing the corresponding value of m. The decimal representation of m must not contain more than 100 digits. If there are multiple solutions for a given value of n, any one of them is acceptable. </p><h2 id="Sample-Input"><a href="#Sample-Input" class="headerlink" title="Sample Input"></a>Sample Input</h2><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token number">2</span><span class="token number">6</span><span class="token number">19</span><span class="token number">0</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre></div><hr><h2 id="Sample-Ouput"><a href="#Sample-Ouput" class="headerlink" title="Sample Ouput"></a>Sample Ouput</h2><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token number">10</span><span class="token number">100100100100100100</span><span class="token number">111111111111111111</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre></div><hr><h2 id="题目理解"><a href="#题目理解" class="headerlink" title="题目理解"></a>题目理解</h2><p> 给定一个整数<code>n</code>,范围在<code>[1,200]</code>,要求输出<code>n</code>的一个倍数,该倍数只能由<code>0</code>和<code>1</code>组成,输出任意一个即可。</p><p> 只能由<code>0</code>和<code>1</code>组成,那第一位肯定是<code>1</code>,后面的每一位都有两种可能。这就是在一个二叉树里面找都一个符合题意的数,如果使用<code>BFS</code>,那么找到的答案是最短的<code>n</code>的倍数,实测<code>long long</code>可以容纳,如果使用<code>DFS</code>,找到的答案必然比较长,需要使用数组了储存答案。</p><h2 id="代码1-BFS"><a href="#代码1-BFS" class="headerlink" title="代码1 (BFS)"></a>代码<em>1</em> (BFS)</h2><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string"><iostream></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string"><cstring></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string"><queue></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token expression">LL <span class="token keyword">long</span> <span class="token keyword">long</span></span></span><span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span><span class="token keyword">int</span> n<span class="token punctuation">;</span>queue<span class="token operator"><</span>LL<span class="token operator">></span> q<span class="token punctuation">;</span>LL <span class="token function">BFS</span><span class="token punctuation">(</span><span class="token keyword">int</span> n<span class="token punctuation">)</span><span class="token punctuation">{</span>q<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">while</span><span class="token punctuation">(</span><span class="token operator">!</span>q<span class="token punctuation">.</span><span class="token function">empty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>LL front <span class="token operator">=</span> q<span class="token punctuation">.</span><span class="token function">front</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>q<span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">if</span><span class="token punctuation">(</span>front <span class="token operator">%</span> n <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token keyword">return</span> front<span class="token punctuation">;</span>q<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>front <span class="token operator">*</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//在答案后面添加0</span>q<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>front <span class="token operator">*</span> <span class="token number">10</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//在答案后面添加1</span><span class="token punctuation">}</span><span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token keyword">while</span><span class="token punctuation">(</span>cin <span class="token operator">>></span> n <span class="token operator">&&</span> n<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token keyword">while</span><span class="token punctuation">(</span><span class="token operator">!</span>q<span class="token punctuation">.</span><span class="token function">empty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>q<span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>cout <span class="token operator"><<</span> <span class="token function">BFS</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span> <span class="token operator"><<</span> endl<span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token comment">//测试数据是否会越界</span><span class="token comment">//for(int i = 1; i <= 200; ++i){</span><span class="token comment">//while(!q.empty())</span><span class="token comment">//q.pop();</span><span class="token comment">//cout << BFS(i) << endl;</span><span class="token comment">//}</span><span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><hr><h2 id="代码2-DFS"><a href="#代码2-DFS" class="headerlink" title="代码2 (DFS)"></a>代码<em>2</em> (DFS)</h2><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string"><iostream></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string"><cstring></span></span><span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span><span class="token keyword">int</span> n<span class="token punctuation">;</span><span class="token keyword">char</span> ans<span class="token punctuation">[</span><span class="token number">110</span><span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token keyword">bool</span> flag<span class="token punctuation">;</span><span class="token keyword">void</span> <span class="token function">DFS</span><span class="token punctuation">(</span><span class="token keyword">int</span> divisor<span class="token punctuation">,</span> <span class="token keyword">int</span> digit<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token keyword">if</span><span class="token punctuation">(</span>digit <span class="token operator">></span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token keyword">return</span><span class="token punctuation">;</span><span class="token keyword">if</span><span class="token punctuation">(</span>divisor <span class="token operator">%</span> n <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">{</span>flag <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>ans<span class="token punctuation">[</span>digit<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'\0'</span><span class="token punctuation">;</span><span class="token keyword">return</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>flag<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token comment">//flag用来判断是否已经找到答案</span>ans<span class="token punctuation">[</span>digit<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'0'</span><span class="token punctuation">;</span><span class="token function">DFS</span><span class="token punctuation">(</span><span class="token punctuation">(</span>divisor <span class="token operator">*</span> <span class="token number">10</span><span class="token punctuation">)</span> <span class="token operator">%</span> n<span class="token punctuation">,</span> digit <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>flag<span class="token punctuation">)</span><span class="token punctuation">{</span>ans<span class="token punctuation">[</span>digit<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'1'</span><span class="token punctuation">;</span><span class="token function">DFS</span><span class="token punctuation">(</span><span class="token punctuation">(</span>divisor <span class="token operator">*</span> <span class="token number">10</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">%</span> n<span class="token punctuation">,</span> digit <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token keyword">while</span><span class="token punctuation">(</span>cin <span class="token operator">>></span> n <span class="token operator">&&</span> n<span class="token punctuation">)</span><span class="token punctuation">{</span>flag <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span><span class="token function">memset</span><span class="token punctuation">(</span>ans<span class="token punctuation">,</span> <span class="token string">'0'</span><span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>ans<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>ans<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'1'</span><span class="token punctuation">;</span> <span class="token function">DFS</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>cout <span class="token operator"><<</span> ans <span class="token operator"><<</span> endl<span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>]]></content>
<categories>
<category>算法</category>
</categories>
<tags>
<tag>DFS</tag>
<tag>POJ</tag>
<tag>BFS</tag>
</tags>
</entry>
<entry>
<title>POJ 3278 Catch That Cow</title>
<link href="/posts/a4cbd5d6/"/>
<url>/posts/a4cbd5d6/</url>
<content type="html"><![CDATA[<h1 id="【POJ】-3278-Catch-That-Cow-BFS-剪枝"><a href="#【POJ】-3278-Catch-That-Cow-BFS-剪枝" class="headerlink" title="【POJ】 3278 Catch That Cow (BFS + 剪枝)"></a>【POJ】 3278 Catch That Cow (BFS + 剪枝)</h1><blockquote><p>题目链接:<a href="https://vjudge.net/contest/65959#problem/C">poj 3278</a></p></blockquote><h2 id="Description"><a href="#Description" class="headerlink" title="Description"></a>Description</h2><p>Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point <em>N</em> (0 ≤ <em>N</em> ≤ 100,000) on a number line and the cow is at a point <em>K</em> (0 ≤ <em>K</em> ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting.</p><p>* Walking: FJ can move from any point <em>X</em> to the points <em>X</em> - 1 or <em>X</em> + 1 in a single minute<br>* Teleporting: FJ can move from any point <em>X</em> to the point 2 × <em>X</em> in a single minute.</p><p>If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?</p><hr><h2 id="Input"><a href="#Input" class="headerlink" title="Input"></a>Input</h2><p> Line 1: Two space-separated integers: <em>N</em> and <em>K</em> </p><hr><h2 id="Output"><a href="#Output" class="headerlink" title="Output"></a>Output</h2><p> Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow. </p><hr><h2 id="Sample-Input"><a href="#Sample-Input" class="headerlink" title="Sample Input"></a>Sample Input</h2><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token number">5</span> <span class="token number">17</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></div><hr><h2 id="Sample-Output"><a href="#Sample-Output" class="headerlink" title="Sample Output"></a>Sample Output</h2><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token number">4</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></div><hr><h2 id="Hint"><a href="#Hint" class="headerlink" title="Hint"></a>Hint</h2><p> The fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes. </p><hr><h2 id="题目理解"><a href="#题目理解" class="headerlink" title="题目理解"></a>题目理解</h2><p> 在一个数轴上有一个人<code>FJ</code>,有一头牛<code>Cow</code>,牛始终保持不动,人可以动,移动规则如下</p><p> <strong>左移或右移一步:耗时1minute</strong></p><p> <strong>传送至当前坐标的2倍处:耗时1minute</strong></p><p> 问人找到牛需要的最短时间。</p><p> 找最短路径,广度优先搜索(BFS) + 剪枝</p><hr><h2 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h2><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string"><cstdio></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string"><queue></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string"><cstring></span></span><span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span><span class="token keyword">const</span> <span class="token keyword">int</span> MAXN <span class="token operator">=</span> <span class="token number">1e5</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span><span class="token keyword">int</span> N<span class="token punctuation">,</span> K<span class="token punctuation">,</span> next<span class="token punctuation">;</span>queue<span class="token operator"><</span><span class="token keyword">int</span><span class="token operator">></span> q<span class="token punctuation">;</span><span class="token keyword">int</span> vis<span class="token punctuation">[</span>MAXN<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token keyword">int</span> <span class="token function">BFS</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>q<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>N<span class="token punctuation">)</span><span class="token punctuation">;</span>vis<span class="token punctuation">[</span>N<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token comment">//标记FJ初始位置已被访问 </span><span class="token keyword">while</span><span class="token punctuation">(</span><span class="token operator">!</span>q<span class="token punctuation">.</span><span class="token function">empty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator"><=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token comment">//尝试用三种方式走</span><span class="token keyword">if</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span>next <span class="token operator">=</span> q<span class="token punctuation">.</span><span class="token function">front</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> i<span class="token punctuation">;</span><span class="token keyword">else</span>next <span class="token operator">=</span> q<span class="token punctuation">.</span><span class="token function">front</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token number">2</span><span class="token punctuation">;</span><span class="token keyword">if</span><span class="token punctuation">(</span>next <span class="token operator">></span> <span class="token number">0</span> <span class="token operator">&&</span> next <span class="token operator"><</span> MAXN <span class="token operator">&&</span> <span class="token operator">!</span>vis<span class="token punctuation">[</span>next<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token comment">//这里要注意要后判断vis[next],否则可能数组越界!!!</span>q<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>next<span class="token punctuation">)</span><span class="token punctuation">;</span>vis<span class="token punctuation">[</span>next<span class="token punctuation">]</span> <span class="token operator">=</span> vis<span class="token punctuation">[</span>q<span class="token punctuation">.</span><span class="token function">front</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">if</span><span class="token punctuation">(</span>next <span class="token operator">==</span> K<span class="token punctuation">)</span><span class="token keyword">return</span> vis<span class="token punctuation">[</span>K<span class="token punctuation">]</span> <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span><span class="token punctuation">}</span>q<span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token punctuation">}</span> <span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token keyword">while</span><span class="token punctuation">(</span><span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">"%d %d"</span><span class="token punctuation">,</span> <span class="token operator">&</span>N<span class="token punctuation">,</span> <span class="token operator">&</span>K<span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">EOF</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token function">memset</span><span class="token punctuation">(</span>vis<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>vis<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">while</span><span class="token punctuation">(</span><span class="token operator">!</span>q<span class="token punctuation">.</span><span class="token function">empty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>q<span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">if</span><span class="token punctuation">(</span>N <span class="token operator">>=</span> K<span class="token punctuation">)</span><span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d\n"</span><span class="token punctuation">,</span> N <span class="token operator">-</span> K<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">else</span><span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d\n"</span><span class="token punctuation">,</span> <span class="token function">BFS</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>]]></content>
<categories>
<category>算法</category>
</categories>
<tags>
<tag>POJ</tag>
<tag>BFS</tag>
<tag>剪枝</tag>
</tags>
</entry>
<entry>
<title>POJ 2251 Dungeon Master</title>
<link href="/posts/a07a2a/"/>
<url>/posts/a07a2a/</url>
<content type="html"><![CDATA[<h1 id="【POJ】-2251-Dungeon-Master-BFS"><a href="#【POJ】-2251-Dungeon-Master-BFS" class="headerlink" title="【POJ】 2251 Dungeon Master (BFS)"></a>【POJ】 2251 Dungeon Master (BFS)</h1><blockquote><p>题目链接:<a href="https://vjudge.net/contest/65959#problem/B">poj-2251</a></p></blockquote><h2 id="Description"><a href="#Description" class="headerlink" title="Description"></a>Description</h2><p> You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides. </p><p>Is an escape possible? If yes, how long will it take? </p><hr><h2 id="Input"><a href="#Input" class="headerlink" title="Input"></a>Input</h2><p> The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size).<br>L is the number of levels making up the dungeon.<br>R and C are the number of rows and columns making up the plan of each level.<br>Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a ‘#’ and empty cells are represented by a ‘.’. Your starting position is indicated by ‘S’ and the exit by the letter ‘E’. There’s a single blank line after each level. Input is terminated by three zeroes for L, R and C. </p><hr><h2 id="Output"><a href="#Output" class="headerlink" title="Output"></a>Output</h2><p>Each maze generates one line of output. If it is possible to reach the exit, print a line of the form </p><p> Escaped in x minute(s).</p><p> where x is replaced by the shortest time it takes to escape.<br>If it is not possible to escape, print the line </p><p> Trapped!</p><hr><h2 id="Sample-Input"><a href="#Sample-Input" class="headerlink" title="Sample Input"></a>Sample Input</h2><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token number">3</span> <span class="token number">4</span> <span class="token number">5</span>S<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>###<span class="token punctuation">.</span><span class="token punctuation">.</span>##<span class="token punctuation">.</span><span class="token punctuation">.</span>###<span class="token punctuation">.</span>#############<span class="token punctuation">.</span>####<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>###########<span class="token punctuation">.</span>#######E<span class="token number">1</span> <span class="token number">3</span> <span class="token number">3</span>S##<span class="token macro property"><span class="token directive-hash">#</span><span class="token expression">E#</span></span>###<span class="token number">0</span> <span class="token number">0</span> <span class="token number">0</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><hr><h2 id="Sample-Output"><a href="#Sample-Output" class="headerlink" title="Sample Output"></a>Sample Output</h2><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp">Escaped in <span class="token number">11</span> <span class="token function">minute</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span><span class="token punctuation">.</span>Trapped<span class="token operator">!</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre></div><hr><h2 id="题目理解"><a href="#题目理解" class="headerlink" title="题目理解"></a>题目理解</h2><p> 给定一个三维的迷宫,障碍物用<code>#</code>表示,迷宫中给定起点<code>S</code>终点<code>E</code>,判断是否能够从<code>S</code>走到<code>E</code>,如果能,返回所需最短路径长度。使用BFS。</p><hr><h2 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h2><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string"><iostream></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string"><cstring></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string"><queue></span></span><span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span><span class="token keyword">struct</span> <span class="token class-name">node</span><span class="token punctuation">{</span> <span class="token keyword">int</span> x<span class="token punctuation">,</span> y<span class="token punctuation">,</span> z<span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token punctuation">;</span>string map3D<span class="token punctuation">[</span><span class="token number">32</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">32</span><span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token keyword">int</span> vis<span class="token punctuation">[</span><span class="token number">32</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">32</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">32</span><span class="token punctuation">]</span><span class="token punctuation">;</span>node Begin<span class="token punctuation">,</span> End<span class="token punctuation">;</span>queue<span class="token operator"><</span>node<span class="token operator">></span> q<span class="token punctuation">;</span><span class="token keyword">int</span> L<span class="token punctuation">,</span> R<span class="token punctuation">,</span> C<span class="token punctuation">,</span> ans<span class="token punctuation">;</span><span class="token keyword">int</span> dx<span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">}</span><span class="token punctuation">;</span><span class="token keyword">int</span> dy<span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">}</span><span class="token punctuation">;</span><span class="token keyword">int</span> dz<span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">;</span><span class="token keyword">int</span> <span class="token function">BFS</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>q<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>Begin<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">while</span><span class="token punctuation">(</span><span class="token operator">!</span>q<span class="token punctuation">.</span><span class="token function">empty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>node cur <span class="token operator">=</span> q<span class="token punctuation">.</span><span class="token function">front</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>q<span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">if</span><span class="token punctuation">(</span>cur<span class="token punctuation">.</span>x <span class="token operator">==</span> End<span class="token punctuation">.</span>x <span class="token operator">&&</span> cur<span class="token punctuation">.</span>y <span class="token operator">==</span> End<span class="token punctuation">.</span>y <span class="token operator">&&</span> cur<span class="token punctuation">.</span>z <span class="token operator">==</span> End<span class="token punctuation">.</span>z<span class="token punctuation">)</span><span class="token keyword">return</span> vis<span class="token punctuation">[</span>End<span class="token punctuation">.</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>End<span class="token punctuation">.</span>y<span class="token punctuation">]</span><span class="token punctuation">[</span>End<span class="token punctuation">.</span>z<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> <span class="token number">6</span><span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span><span class="token punctuation">{</span>node temp<span class="token punctuation">;</span>temp<span class="token punctuation">.</span>x <span class="token operator">=</span> cur<span class="token punctuation">.</span>x <span class="token operator">+</span> dx<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>temp<span class="token punctuation">.</span>y <span class="token operator">=</span> cur<span class="token punctuation">.</span>y <span class="token operator">+</span> dy<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>temp<span class="token punctuation">.</span>z <span class="token operator">=</span> cur<span class="token punctuation">.</span>z <span class="token operator">+</span> dz<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token keyword">if</span><span class="token punctuation">(</span>temp<span class="token punctuation">.</span>x <span class="token operator">>=</span> <span class="token number">0</span> <span class="token operator">&&</span> temp<span class="token punctuation">.</span>y <span class="token operator">>=</span><span class="token number">0</span> <span class="token operator">&&</span> temp<span class="token punctuation">.</span>z <span class="token operator">>=</span> <span class="token number">0</span> <span class="token operator">&&</span> temp<span class="token punctuation">.</span>x <span class="token operator"><</span> L <span class="token operator">&&</span> temp<span class="token punctuation">.</span>y <span class="token operator"><</span> R <span class="token operator">&&</span> temp<span class="token punctuation">.</span>z <span class="token operator"><</span> C <span class="token operator">&&</span> map3D<span class="token punctuation">[</span>temp<span class="token punctuation">.</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>temp<span class="token punctuation">.</span>y<span class="token punctuation">]</span><span class="token punctuation">[</span>temp<span class="token punctuation">.</span>z<span class="token punctuation">]</span> <span class="token operator">!=</span> <span class="token string">'#'</span> <span class="token operator">&&</span> <span class="token operator">!</span>vis<span class="token punctuation">[</span>temp<span class="token punctuation">.</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>temp<span class="token punctuation">.</span>y<span class="token punctuation">]</span><span class="token punctuation">[</span>temp<span class="token punctuation">.</span>z<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">{</span>q<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>temp<span class="token punctuation">)</span><span class="token punctuation">;</span>vis<span class="token punctuation">[</span>temp<span class="token punctuation">.</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>temp<span class="token punctuation">.</span>y<span class="token punctuation">]</span><span class="token punctuation">[</span>temp<span class="token punctuation">.</span>z<span class="token punctuation">]</span> <span class="token operator">=</span> vis<span class="token punctuation">[</span>cur<span class="token punctuation">.</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>cur<span class="token punctuation">.</span>y<span class="token punctuation">]</span><span class="token punctuation">[</span>cur<span class="token punctuation">.</span>z<span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token keyword">while</span><span class="token punctuation">(</span>cin <span class="token operator">>></span> L <span class="token operator">>></span> R <span class="token operator">>></span> C <span class="token operator">&&</span> L <span class="token operator">!=</span> <span class="token number">0</span> <span class="token operator">&&</span> R <span class="token operator">!=</span> <span class="token number">0</span> <span class="token operator">&&</span> C <span class="token operator">!=</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token function">memset</span><span class="token punctuation">(</span>vis<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>vis<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">while</span><span class="token punctuation">(</span><span class="token operator">!</span>q<span class="token punctuation">.</span><span class="token function">empty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>q<span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> L<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span><span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> j <span class="token operator"><</span> R<span class="token punctuation">;</span> <span class="token operator">++</span>j<span class="token punctuation">)</span><span class="token punctuation">{</span>cin <span class="token operator">>></span> map3D<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> k <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> k <span class="token operator"><</span> C<span class="token punctuation">;</span> <span class="token operator">++</span>k<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token keyword">if</span><span class="token punctuation">(</span>map3D<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">[</span>k<span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token string">'S'</span><span class="token punctuation">)</span>Begin<span class="token punctuation">.</span>x <span class="token operator">=</span> i<span class="token punctuation">,</span> Begin<span class="token punctuation">.</span>y <span class="token operator">=</span> j<span class="token punctuation">,</span> Begin<span class="token punctuation">.</span>z <span class="token operator">=</span> k<span class="token punctuation">;</span><span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span>map3D<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">[</span>k<span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token string">'E'</span><span class="token punctuation">)</span>End<span class="token punctuation">.</span>x <span class="token operator">=</span> i<span class="token punctuation">,</span> End<span class="token punctuation">.</span>y <span class="token operator">=</span> j<span class="token punctuation">,</span> End<span class="token punctuation">.</span>z <span class="token operator">=</span> k<span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token punctuation">}</span>ans <span class="token operator">=</span> <span class="token function">BFS</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">if</span><span class="token punctuation">(</span>ans<span class="token punctuation">)</span>cout <span class="token operator"><<</span> <span class="token string">"Escaped in "</span> <span class="token operator"><<</span> ans <span class="token operator"><<</span> <span class="token string">" minute(s)."</span> <span class="token operator"><<</span> endl<span class="token punctuation">;</span><span class="token keyword">else</span>cout <span class="token operator"><<</span> <span class="token string">"Trapped!"</span> <span class="token operator"><<</span> endl<span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token punctuation">}</span> <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>]]></content>
<categories>
<category>算法</category>
</categories>
<tags>
<tag>POJ</tag>
<tag>BFS</tag>
</tags>
</entry>
<entry>
<title>POJ 1321 棋盘问题</title>
<link href="/posts/47786f48/"/>
<url>/posts/47786f48/</url>
<content type="html"><![CDATA[<h1 id="【POJ】1321-棋盘问题-DFS"><a href="#【POJ】1321-棋盘问题-DFS" class="headerlink" title="【POJ】1321 棋盘问题 (DFS)"></a>【POJ】1321 棋盘问题 (DFS)</h1><blockquote><p> 题目链接:<a href="https://vjudge.net/contest/65959#problem/A">poj-1321</a></p></blockquote><h2 id="Description"><a href="#Description" class="headerlink" title="Description"></a>Description</h2><p> 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别。要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C。 </p><hr><h2 id="Input"><a href="#Input" class="headerlink" title="Input"></a>Input</h2><p> 输入含有多组测试数据。<br>每组数据的第一行是两个正整数,n k,用一个空格隔开,表示了将在一个n*n的矩阵内描述棋盘,以及摆放棋子的数目。 n <= 8 , k <= n<br>当为-1 -1时表示输入结束。<br>随后的n行描述了棋盘的形状:每行有n个字符,其中 # 表示棋盘区域, . 表示空白区域(数据保证不出现多余的空白行或者空白列)。 </p><hr><h2 id="Output"><a href="#Output" class="headerlink" title="Output"></a>Output</h2><p> 对于每一组数据,给出一行输出,输出摆放的方案数目C (数据保证C<2^31)。 </p><hr><h2 id="Sample-Input"><a href="#Sample-Input" class="headerlink" title="Sample Input"></a>Sample Input</h2><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token number">2</span> <span class="token number">1</span>#<span class="token punctuation">.</span><span class="token punctuation">.</span>#<span class="token number">4</span> <span class="token number">4</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>#<span class="token punctuation">.</span><span class="token punctuation">.</span>#<span class="token punctuation">.</span><span class="token punctuation">.</span>#<span class="token punctuation">.</span><span class="token punctuation">.</span>#<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token operator">-</span><span class="token number">1</span> <span class="token operator">-</span><span class="token number">1</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><hr><h2 id="Sample-Output"><a href="#Sample-Output" class="headerlink" title="Sample Output"></a>Sample Output</h2><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token number">2</span><span class="token number">1</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre></div><hr><h2 id="题目理解"><a href="#题目理解" class="headerlink" title="题目理解"></a>题目理解</h2><p> 给一个<code>n * n</code>的矩阵,上面是<code>#</code>的可以放置棋子,是<code>.</code>的不能放置棋子,而且同一行同一列只能最多有一个棋子,问放k个棋子有多少种可能性。</p><p> 和<code>n皇后问题</code>很像,方法相同,都可以用递归来解决,本题更简单,不用判断斜向的情况,这里使用DFS解题。</p><hr><h2 id="代码1"><a href="#代码1" class="headerlink" title="代码1"></a>代码<em>1</em></h2><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><iostream></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><algorithm></span></span><span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span><span class="token keyword">int</span> n<span class="token punctuation">,</span> k<span class="token punctuation">,</span> ans<span class="token punctuation">;</span><span class="token keyword">char</span> board<span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token keyword">bool</span> check<span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token keyword">void</span> <span class="token function">DFS</span><span class="token punctuation">(</span><span class="token keyword">int</span> cur<span class="token punctuation">,</span> <span class="token keyword">int</span> used<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token comment">//判断棋子是否用完,用完了可以确定找出了一种放置方式</span><span class="token keyword">if</span><span class="token punctuation">(</span>used <span class="token operator">==</span> k<span class="token punctuation">)</span><span class="token punctuation">{</span>ans<span class="token operator">++</span><span class="token punctuation">;</span><span class="token keyword">return</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> cur<span class="token punctuation">;</span> i <span class="token operator"><</span> n<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token comment">//此循环判断是否超出棋盘最大行数 </span><span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> j <span class="token operator"><</span> n<span class="token punctuation">;</span> <span class="token operator">++</span>j<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token comment">//此循环查找一行里面可以放置的位置 </span><span class="token keyword">if</span><span class="token punctuation">(</span>board<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token string">'#'</span> <span class="token operator">&&</span> <span class="token operator">!</span>check<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">{</span>check<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span><span class="token function">DFS</span><span class="token punctuation">(</span>i <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> used <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>check<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token keyword">while</span><span class="token punctuation">(</span>cin <span class="token operator">>></span> n <span class="token operator">>></span> k <span class="token operator">&&</span> n <span class="token operator">!=</span> <span class="token operator">-</span><span class="token number">1</span> <span class="token operator">&&</span> k <span class="token operator">!=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">{</span>ans <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token function">fill</span><span class="token punctuation">(</span>check<span class="token punctuation">,</span> check <span class="token operator">+</span> n<span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> n<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span>cin <span class="token operator">>></span> board<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token function">DFS</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>cout <span class="token operator"><<</span> ans <span class="token operator"><<</span> endl<span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token punctuation">}</span> <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h2 id="代码2"><a href="#代码2" class="headerlink" title="代码2"></a>代码<em>2</em></h2><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><iostream></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><algorithm></span></span><span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span><span class="token keyword">int</span> n<span class="token punctuation">,</span> k<span class="token punctuation">,</span> used<span class="token punctuation">,</span> ans<span class="token punctuation">;</span><span class="token keyword">char</span> board<span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token keyword">bool</span> check<span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token keyword">void</span> <span class="token function">DFS</span><span class="token punctuation">(</span><span class="token keyword">int</span> cur<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token comment">//判断棋子是否已经用完了,用完了可以确定找出了一种放置方式 </span><span class="token keyword">if</span><span class="token punctuation">(</span>used <span class="token operator">==</span> k<span class="token punctuation">)</span><span class="token punctuation">{</span>ans<span class="token operator">++</span><span class="token punctuation">;</span><span class="token keyword">return</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token comment">//判断是否已经超出了棋盘最大行数,超出就回退 </span><span class="token keyword">if</span><span class="token punctuation">(</span>cur <span class="token operator">==</span> n<span class="token punctuation">)</span><span class="token keyword">return</span><span class="token punctuation">;</span><span class="token comment">//查找一行里面可以放置的位置 </span><span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> n<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token keyword">if</span><span class="token punctuation">(</span>board<span class="token punctuation">[</span>cur<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token string">'#'</span> <span class="token operator">&&</span> <span class="token operator">!</span>check<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">{</span>check<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>used<span class="token operator">++</span><span class="token punctuation">;</span><span class="token function">DFS</span><span class="token punctuation">(</span>cur <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//放置完当前行,进入下一行 </span>check<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span><span class="token comment">//取消放置当前位置 </span>used<span class="token operator">--</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token comment">//当前行不放置棋子,进入下一行 </span><span class="token function">DFS</span><span class="token punctuation">(</span>cur <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token keyword">while</span><span class="token punctuation">(</span>cin <span class="token operator">>></span> n <span class="token operator">>></span> k <span class="token operator">&&</span> n <span class="token operator">!=</span> <span class="token operator">-</span><span class="token number">1</span> <span class="token operator">&&</span> k <span class="token operator">!=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">{</span>used <span class="token operator">=</span> ans <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token function">fill</span><span class="token punctuation">(</span>check<span class="token punctuation">,</span> check <span class="token operator">+</span> n<span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> n<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span><span class="token punctuation">{</span>cin <span class="token operator">>></span> board<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token function">DFS</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>cout <span class="token operator"><<</span> ans <span class="token operator"><<</span>endl<span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token punctuation">}</span> <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>]]></content>
<categories>
<category>算法</category>
</categories>
<tags>
<tag>DFS</tag>
<tag>POJ</tag>
</tags>
</entry>
<entry>
<title>leetcode单向链表[206, 24, 141, 142, 25]</title>
<link href="/posts/928b7e4b/"/>
<url>/posts/928b7e4b/</url>
<content type="html"><![CDATA[<h1 id="【leetcode】链表题目训练-206-24-141-142-25"><a href="#【leetcode】链表题目训练-206-24-141-142-25" class="headerlink" title="【leetcode】链表题目训练[206, 24, 141, 142, 25]"></a>【leetcode】链表题目训练[206, 24, 141, 142, 25]</h1><h2 id="题目一:206-Reverse-Linked-List"><a href="#题目一:206-Reverse-Linked-List" class="headerlink" title="题目一:206. Reverse Linked List"></a>题目一:<a href="https://leetcode.com/problems/reverse-linked-list/">206. Reverse Linked List</a></h2><p> <strong>题目:Reverse a singly linked list.</strong></p><ul><li>理解:反转一个单项链表</li><li>难度:easy</li></ul><h3 id="解法:"><a href="#解法:" class="headerlink" title="解法:"></a>解法:</h3><ul><li>思路:头节点开始依次往后遍历链表,并更改每一个节点的next指针为前一个结点的地址</li><li>代码</li></ul><div class="code-wrapper"><pre class="line-numbers language-c" data-language="c"><code class="language-c">class Solution <span class="token punctuation">{</span>public<span class="token operator">:</span> ListNode<span class="token operator">*</span> <span class="token function">reverseList</span><span class="token punctuation">(</span>ListNode<span class="token operator">*</span> head<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span><span class="token punctuation">(</span>head <span class="token operator">==</span> <span class="token constant">NULL</span><span class="token punctuation">)</span> <span class="token keyword">return</span> head<span class="token punctuation">;</span> ListNode <span class="token operator">*</span>cur <span class="token operator">=</span> head<span class="token punctuation">,</span> <span class="token operator">*</span>prev <span class="token operator">=</span> <span class="token constant">NULL</span><span class="token punctuation">,</span> <span class="token operator">*</span>lat <span class="token operator">=</span> head<span class="token operator">-></span>next<span class="token punctuation">;</span> <span class="token keyword">while</span><span class="token punctuation">(</span>cur<span class="token punctuation">)</span><span class="token punctuation">{</span> cur<span class="token operator">-></span>next <span class="token operator">=</span> prev<span class="token punctuation">;</span> prev <span class="token operator">=</span> cur<span class="token punctuation">;</span> cur <span class="token operator">=</span> lat<span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>lat <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span> lat <span class="token operator">=</span> lat<span class="token operator">-></span>next<span class="token punctuation">;</span><span class="token punctuation">}</span> <span class="token keyword">return</span> prev<span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h2 id="题目二:24-Swap-Nodes-in-Pairs"><a href="#题目二:24-Swap-Nodes-in-Pairs" class="headerlink" title="题目二:24. Swap Nodes in Pairs"></a>题目二:<a href="https://leetcode.com/problems/swap-nodes-in-pairs/">24. Swap Nodes in Pairs</a></h2><p> <strong>题目:Given a linked list, swap every two adjacent nodes and return its head.You may not modify the values in the list’s nodes, only nodes itself may be changed.</strong></p><ul><li>理解:从第一个节点开始,依次反转两个相邻的节点</li><li>难度:medium</li></ul><h3 id="解法:-1"><a href="#解法:-1" class="headerlink" title="解法:"></a>解法:</h3><ul><li>思路:两个节点一组,将第二个指向第一个,然后将第一个指向下一组的第二个,如此循环,如下图</li></ul><p><img src="http://pxjvmix9e.bkt.clouddn.com/blog/20191002/dRgjm4jJFw5N.png?imageslim" alt="改变顺序"></p><ul><li>代码</li></ul><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">class</span> <span class="token class-name">Solution</span> <span class="token punctuation">{</span><span class="token keyword">public</span><span class="token operator">:</span> ListNode<span class="token operator">*</span> <span class="token function">swapPairs</span><span class="token punctuation">(</span>ListNode<span class="token operator">*</span> head<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span><span class="token punctuation">(</span>head <span class="token operator">==</span> <span class="token constant">NULL</span> <span class="token operator">||</span> head<span class="token operator">-></span>next <span class="token operator">==</span> <span class="token constant">NULL</span><span class="token punctuation">)</span> <span class="token keyword">return</span> head<span class="token punctuation">;</span> ListNode <span class="token operator">*</span>cur <span class="token operator">=</span> head<span class="token operator">-></span>next<span class="token punctuation">,</span> <span class="token operator">*</span>temp <span class="token operator">=</span> cur<span class="token operator">-></span>next<span class="token punctuation">,</span> <span class="token operator">*</span>prev <span class="token operator">=</span> head<span class="token punctuation">,</span> <span class="token operator">*</span>res <span class="token operator">=</span> head<span class="token operator">-></span>next<span class="token punctuation">;</span> <span class="token keyword">while</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">{</span> temp <span class="token operator">=</span> cur<span class="token operator">-></span>next<span class="token punctuation">;</span> cur<span class="token operator">-></span>next <span class="token operator">=</span> prev<span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>temp <span class="token operator">==</span> <span class="token constant">NULL</span> <span class="token operator">||</span> temp<span class="token operator">-></span>next <span class="token operator">==</span> <span class="token constant">NULL</span><span class="token punctuation">)</span><span class="token punctuation">{</span> prev<span class="token operator">-></span>next <span class="token operator">=</span> temp<span class="token punctuation">;</span> <span class="token keyword">return</span> res<span class="token punctuation">;</span> <span class="token punctuation">}</span> cur <span class="token operator">=</span> temp<span class="token operator">-></span>next<span class="token punctuation">;</span> prev<span class="token operator">-></span>next <span class="token operator">=</span> cur<span class="token punctuation">;</span> prev <span class="token operator">=</span> temp<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h2 id="题目三:141-Linked-List-Cycle"><a href="#题目三:141-Linked-List-Cycle" class="headerlink" title="题目三:141. Linked List Cycle"></a>题目三:<a href="https://leetcode.com/problems/linked-list-cycle/">141. Linked List Cycle</a></h2><p> <strong>题目:Given a linked list, determine if it has a cycle in it.To represent a cycle in the given linked list, we use an integer <code>pos</code> which represents the position (0-indexed) in the linked list where tail connects to. If <code>pos</code> is <code>-1</code>,then there is no cycle in the linked list.</strong></p><ul><li>理解:判断一个链表是否有环</li><li>难度:easy</li></ul><h3 id="解法一:"><a href="#解法一:" class="headerlink" title="解法一:"></a>解法一:</h3><ul><li>思路:快慢指针,创建两个指针,一个指针每次遍历两个节点,一个每次遍历一个节点,当遇到NULL则返回无环,当两个指针相遇则返回有环<ul><li>缺点:时间复杂度大于O(n)</li></ul></li></ul><img src="http://pxjvmix9e.bkt.clouddn.com/blog/20191002/wJQBfmcaxCCR.png?imageslim" alt="改变顺序" style="zoom:50%;" /><ul><li>代码:</li></ul><div class="code-wrapper"><pre class="line-numbers language-c" data-language="c"><code class="language-c">class Solution <span class="token punctuation">{</span>public<span class="token operator">:</span> bool <span class="token function">hasCycle</span><span class="token punctuation">(</span>ListNode <span class="token operator">*</span>head<span class="token punctuation">)</span> <span class="token punctuation">{</span> ListNode <span class="token operator">*</span>cur <span class="token operator">=</span> head<span class="token punctuation">,</span> <span class="token operator">*</span>prev <span class="token operator">=</span> head<span class="token punctuation">,</span> <span class="token operator">*</span>temp <span class="token operator">=</span> head<span class="token punctuation">;</span> <span class="token keyword">while</span><span class="token punctuation">(</span>cur<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>cur<span class="token operator">-></span>next <span class="token operator">||</span> <span class="token operator">!</span><span class="token punctuation">(</span>cur<span class="token operator">-></span>next<span class="token punctuation">)</span><span class="token operator">-></span>next<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">return</span> false<span class="token punctuation">;</span> <span class="token punctuation">}</span> prev <span class="token operator">=</span> prev<span class="token operator">-></span>next<span class="token punctuation">;</span> cur <span class="token operator">=</span> <span class="token punctuation">(</span>cur<span class="token operator">-></span>next<span class="token punctuation">)</span><span class="token operator">-></span>next<span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>cur <span class="token operator">==</span> prev<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">return</span> true<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> false<span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h3 id="解法二:"><a href="#解法二:" class="headerlink" title="解法二:"></a>解法二:</h3><ul><li>思路:遍历一遍,将遍历到的节点指向head,遍历过程中遇到NULL则返回无环,遍历过程中遇到head则返回有环<ul><li>优点:时间复杂度O(n)</li><li>缺点:更改了原有的链表</li></ul></li></ul><p><img src="http://pxjvmix9e.bkt.clouddn.com/blog/20191002/TnUTrvF0pA4D.png?imageslim" alt="mark"></p><ul><li>代码:</li></ul><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">class</span> <span class="token class-name">Solution</span> <span class="token punctuation">{</span><span class="token keyword">public</span><span class="token operator">:</span> <span class="token keyword">bool</span> <span class="token function">hasCycle</span><span class="token punctuation">(</span>ListNode <span class="token operator">*</span>head<span class="token punctuation">)</span> <span class="token punctuation">{</span> ListNode <span class="token operator">*</span>cur <span class="token operator">=</span> head<span class="token punctuation">,</span> <span class="token operator">*</span>prev <span class="token operator">=</span> head<span class="token punctuation">;</span> <span class="token keyword">while</span><span class="token punctuation">(</span>cur<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">if</span><span class="token punctuation">(</span>cur<span class="token operator">-></span>next <span class="token operator">==</span> head<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> prev <span class="token operator">=</span> cur<span class="token operator">-></span>next<span class="token punctuation">;</span> cur<span class="token operator">-></span>next <span class="token operator">=</span> head<span class="token punctuation">;</span> cur <span class="token operator">=</span> prev<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h2 id="题目四:142-Linked-List-Cycle-II"><a href="#题目四:142-Linked-List-Cycle-II" class="headerlink" title="题目四:142. Linked List Cycle II"></a>题目四:<a href="https://leetcode.com/problems/linked-list-cycle-ii/">142. Linked List Cycle II</a></h2><p> <strong>题目:Given a linked list, return the node where the cycle begins. If there is no cycle, return <code>null</code>.To representa cycle in the given linked list, we use an integer <code>pos</code> which represents the position (0-indexed) in thelinked list where tail connects to. If <code>pos</code> is <code>-1</code>, then there is no cycle in the linked list.</strong></p><p> <strong>Note: Do not modify the linked list.</strong></p><ul><li>理解:141题的hard,在判定有环的基础上返回尾部所指向的节点</li><li>难度:medium</li></ul><h3 id="解法:-2"><a href="#解法:-2" class="headerlink" title="解法:"></a>解法:</h3><ul><li>思路:创建一个unordered_set,然后依次遍历,将遍历到的节点的地址存放到unordered_set,然后判断当前节点的next存放的地址是否在unordered_set中存在,存在的话,则返回该地址。</li><li>代码:</li></ul><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">class</span> <span class="token class-name">Solution</span> <span class="token punctuation">{</span><span class="token keyword">public</span><span class="token operator">:</span> ListNode <span class="token operator">*</span><span class="token function">detectCycle</span><span class="token punctuation">(</span>ListNode <span class="token operator">*</span>head<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span><span class="token punctuation">(</span>head <span class="token operator">==</span> <span class="token constant">NULL</span> <span class="token operator">||</span> head<span class="token operator">-></span>next <span class="token operator">==</span> <span class="token constant">NULL</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token constant">NULL</span><span class="token punctuation">;</span> ListNode <span class="token operator">*</span>cur <span class="token operator">=</span> head<span class="token punctuation">;</span> unordered_set<span class="token operator"><</span>ListNode<span class="token operator">*</span><span class="token operator">></span> se<span class="token punctuation">;</span> <span class="token keyword">while</span><span class="token punctuation">(</span>cur<span class="token operator">-></span>next<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">if</span><span class="token punctuation">(</span>se<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span>cur<span class="token punctuation">)</span> <span class="token operator">==</span> se<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> se<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span>cur<span class="token punctuation">)</span><span class="token punctuation">;</span> cur <span class="token operator">=</span> cur<span class="token operator">-></span>next<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span><span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token operator">*</span>se<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span>cur<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> <span class="token constant">NULL</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div><h2 id="题目五:25-Reverse-Nodes-in-k-Group"><a href="#题目五:25-Reverse-Nodes-in-k-Group" class="headerlink" title="题目五:25. Reverse Nodes in k-Group"></a>题目五:<a href="https://leetcode.com/problems/reverse-nodes-in-k-group/">25. Reverse Nodes in k-Group</a></h2><p> <strong>题目:Given a linked list, reverse the nodes of a linked list <em>k</em> at a time and return its modified list.<em>k</em> is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of <em>k</em> thenleft-out nodes in the end should remain as it is.</strong></p><ul><li>理解:24题的hard,原先是反转相邻两个节点的基础上,现在是反转k个节点</li><li>难度:hard</li></ul><h3 id="解法"><a href="#解法" class="headerlink" title="解法"></a>解法</h3><ul><li>思路:基本思路和24题相同</li><li>代码:</li></ul><div class="code-wrapper"><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">class</span> <span class="token class-name">Solution</span> <span class="token punctuation">{</span><span class="token keyword">public</span><span class="token operator">:</span> ListNode<span class="token operator">*</span> <span class="token function">reverseKGroup</span><span class="token punctuation">(</span>ListNode<span class="token operator">*</span> head<span class="token punctuation">,</span> <span class="token keyword">int</span> k<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span><span class="token punctuation">(</span>head <span class="token operator">==</span> <span class="token constant">NULL</span> <span class="token operator">||</span> head<span class="token operator">-></span>next <span class="token operator">==</span> <span class="token constant">NULL</span> <span class="token operator">||</span> k <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token keyword">return</span> head<span class="token punctuation">;</span> ListNode <span class="token operator">*</span>test <span class="token operator">=</span> head<span class="token punctuation">,</span> <span class="token operator">*</span>res <span class="token operator">=</span> <span class="token constant">NULL</span><span class="token punctuation">,</span> <span class="token operator">*</span>cur <span class="token operator">=</span> head<span class="token punctuation">,</span> <span class="token operator">*</span>prev <span class="token operator">=</span> <span class="token constant">NULL</span><span class="token punctuation">,</span> <span class="token operator">*</span>lat <span class="token operator">=</span> head<span class="token punctuation">,</span> <span class="token operator">*</span>temp <span class="token operator">=</span> head<span class="token punctuation">;</span> <span class="token keyword">int</span> n <span class="token operator">=</span> k<span class="token punctuation">,</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token keyword">while</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">{</span> n <span class="token operator">=</span> k<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">;</span> <span class="token keyword">while</span><span class="token punctuation">(</span>test <span class="token operator">&&</span> n<span class="token punctuation">)</span><span class="token punctuation">{</span> n <span class="token operator">-=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>n <span class="token operator">+</span> <span class="token number">1</span> <span class="token operator">==</span> k<span class="token punctuation">)</span> test <span class="token operator">=</span> cur<span class="token punctuation">;</span> <span class="token keyword">else</span> test <span class="token operator">=</span> test<span class="token operator">-></span>next<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>n <span class="token operator">&&</span> test<span class="token punctuation">)</span><span class="token punctuation">{</span> n <span class="token operator">=</span> k<span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>i <span class="token operator">>=</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">{</span> temp<span class="token operator">-></span>next <span class="token operator">=</span> test<span class="token punctuation">;</span> temp <span class="token operator">=</span> cur<span class="token punctuation">;</span><span class="token punctuation">}</span> <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>res<span class="token punctuation">)</span> res <span class="token operator">=</span> test<span class="token punctuation">;</span> <span class="token keyword">while</span><span class="token punctuation">(</span>n<span class="token operator">--</span><span class="token punctuation">)</span><span class="token punctuation">{</span> lat <span class="token operator">=</span> lat<span class="token operator">-></span>next<span class="token punctuation">;</span> cur<span class="token operator">-></span>next <span class="token operator">=</span> prev<span class="token punctuation">;</span> prev <span class="token operator">=</span> cur<span class="token punctuation">;</span> cur <span class="token operator">=</span> lat<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span>i <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">return</span> head<span class="token punctuation">;</span><span class="token punctuation">}</span> <span class="token keyword">else</span><span class="token punctuation">{</span> temp<span class="token operator">-></span>next <span class="token operator">=</span> cur<span class="token punctuation">;</span> <span class="token keyword">return</span> res<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>]]></content>
<categories>
<category>数据结构</category>
</categories>
<tags>
<tag>单向链表</tag>
<tag>数据结构</tag>
<tag>Leetcode</tag>
</tags>
</entry>
</search>