-
Notifications
You must be signed in to change notification settings - Fork 1
/
search.xml
1942 lines (1937 loc) · 775 KB
/
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="utf-8"?>
<search>
<entry>
<title>数据结构(1)</title>
<url>/2021/12/19/22/</url>
<content><![CDATA[<h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><span id="more"></span>
<p>数据结构可以说是学习算法的前提,我将在寒假前后更新完数据结构相关的内容,同时会更新一些简单的算法,我学习的是mooc上浙大的<a href="https://www.icourse163.org/learn/ZJU-93001?tid=1465570445#/learn/announce">数据结构</a></p>
<h1 id="线性表及其实现"><a href="#线性表及其实现" class="headerlink" title="线性表及其实现"></a>线性表及其实现</h1><h2 id="线性表定义"><a href="#线性表定义" class="headerlink" title="线性表定义"></a>线性表定义</h2><p><strong>“线性表(Linear List)”</strong>:由同类型<strong>数据元素</strong>构成<strong>有序序列</strong>的线性结构</p>
<p>表中元素个数称为线性表的<strong>长度</strong>;线性表没有元素时,称为<strong>空表</strong>;表起始位置称<strong>表头</strong>,表结束位置称<strong>表尾</strong></p>
<h2 id="对线性表的操作"><a href="#对线性表的操作" class="headerlink" title="对线性表的操作"></a>对线性表的操作</h2><p>1、<strong>List MakeEmpty()</strong> 初始化一个空线性表L</p>
<p>2、<strong>ElementType FindKth( int K, List L )</strong> 根据位序K,返回相应元素</p>
<p>3、<strong>int Find( ElementType X, List L )</strong> 在线性表L中查找X的第一次出现位置</p>
<p>4、<strong>void Insert( ElementType X, int i, List L)</strong> 在位序i前插入一个新元素X</p>
<p>5、<strong>void Delete( int i, List L )</strong> 删除指定位序i的元素;</p>
<p>6、<strong>int Length( List L )</strong> 返回线性表L的长度n</p>
<h2 id="线性表的顺序存储实现"><a href="#线性表的顺序存储实现" class="headerlink" title="线性表的顺序存储实现"></a>线性表的顺序存储实现</h2><h3 id="建立空的顺序表"><a href="#建立空的顺序表" class="headerlink" title="建立空的顺序表"></a>建立空的顺序表</h3><figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="function">List <span class="title">MakeEmpty</span><span class="params">( )</span></span>{</span><br><span class="line"> List PtrL;</span><br><span class="line"> PtrL = (List )<span class="built_in">malloc</span>( <span class="built_in"><span class="keyword">sizeof</span></span>(struct LNode) );</span><br><span class="line"> PtrL->Last = <span class="number">-1</span>; </span><br><span class="line"> <span class="keyword">return</span> PtrL;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h3 id="查找"><a href="#查找" class="headerlink" title="查找"></a>查找</h3><figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">Find</span><span class="params">( ElementType X, List PtrL )</span></span>{</span><br><span class="line"> <span class="keyword">int</span> i = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span>( i <= PtrL->Last && PtrL->Data[i]!= X )</span><br><span class="line"> i++;</span><br><span class="line"> <span class="keyword">if</span> (i > PtrL->Last) <span class="keyword">return</span> <span class="number">-1</span>; <span class="comment">/* 如果没找到,返回-1 */</span></span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">return</span> i; <span class="comment">/* 找到后返回的是存储位置 */</span> </span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h3 id="插入"><a href="#插入" class="headerlink" title="插入"></a>插入</h3><figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Insert</span><span class="params">( ElementType X, <span class="keyword">int</span> i, List PtrL )</span></span>{</span><br><span class="line"> <span class="keyword">int</span> j;</span><br><span class="line"> <span class="keyword">if</span> ( PtrL->Last == MAXSIZE<span class="number">-1</span> ){ <span class="comment">/* 表空间已满,不能插入*/</span></span><br><span class="line"> <span class="built_in">printf</span>("表满"); </span><br><span class="line"> <span class="keyword">return</span>; </span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span> ( i < <span class="number">1</span> || i > PtrL->Last+<span class="number">2</span>) { <span class="comment">/*检查插入位置的合法性*/</span></span><br><span class="line"> <span class="built_in">printf</span>("位置不合法");</span><br><span class="line"> <span class="keyword">return</span>; </span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span> ( j = PtrL->Last; j >= i<span class="number">-1</span>; j-- )</span><br><span class="line"> PtrL->Data[j+<span class="number">1</span>] = PtrL->Data[j]; <span class="comment">/*将 ai~ an倒序向后移动*/</span></span><br><span class="line"> PtrL->Data[i<span class="number">-1</span>] = X; <span class="comment">/*新元素插入*/</span></span><br><span class="line"> PtrL->Last++; <span class="comment">/*Last仍指向最后元素*/</span></span><br><span class="line"> <span class="keyword">return</span>; </span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h3 id="删除"><a href="#删除" class="headerlink" title="删除"></a>删除</h3><figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Delete</span><span class="params">( <span class="keyword">int</span> i, List PtrL )</span></span>{ </span><br><span class="line"> <span class="keyword">int</span> j;</span><br><span class="line"> <span class="keyword">if</span>( i < <span class="number">1</span> || i > PtrL->Last+<span class="number">1</span> ) { <span class="comment">/*检查空表及删除位置的合法性*/</span></span><br><span class="line"> <span class="built_in">printf</span> (“不存在第%d个元素”, i ); </span><br><span class="line"> <span class="keyword">return</span> ; </span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span> ( j = i; j <= PtrL->Last; j++ )</span><br><span class="line"> PtrL->Data[j<span class="number">-1</span>] = PtrL->Data[j]; <span class="comment">/*将 ai+1~ an顺序向前移动*/</span></span><br><span class="line"> PtrL->Last--; <span class="comment">/*Last仍指向最后元素*/</span></span><br><span class="line"> <span class="keyword">return</span>; </span><br><span class="line">} </span><br></pre></td></tr></tbody></table></figure>
<h2 id="线性表的链式存储实现"><a href="#线性表的链式存储实现" class="headerlink" title="线性表的链式存储实现"></a>线性表的链式存储实现</h2><h3 id="定义结构类型"><a href="#定义结构类型" class="headerlink" title="定义结构类型"></a>定义结构类型</h3><figure class="highlight c"><table><tbody><tr><td class="code"><pre><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span> <span class="title">LNode</span> *<span class="title">List</span>;</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">LNode</span>{</span> </span><br><span class="line"> ElementType Data; </span><br><span class="line"> List Next;</span><br><span class="line">}; </span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Lnode</span> <span class="title">L</span>;</span></span><br><span class="line">List PtrL;</span><br></pre></td></tr></tbody></table></figure>
<h3 id="求表长"><a href="#求表长" class="headerlink" title="求表长"></a>求表长</h3><figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">Length</span> <span class="params">( List PtrL )</span></span>{ </span><br><span class="line"> List p = PtrL; <span class="comment">/* p指向表的第一个结点*/</span></span><br><span class="line"> <span class="keyword">int</span> j = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span> ( p ) { </span><br><span class="line"> p = p->Next; </span><br><span class="line"> j++; <span class="comment">/* 当前p指向的是第 j 个结点*/</span></span><br><span class="line"> } </span><br><span class="line"> <span class="keyword">return</span> j;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h3 id="查找-1"><a href="#查找-1" class="headerlink" title="查找"></a>查找</h3><h4 id="(1)按序号查找-FindKth"><a href="#(1)按序号查找-FindKth" class="headerlink" title="(1)按序号查找: FindKth"></a>(1)按序号查找: FindKth</h4><figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="function">List <span class="title">FindKth</span><span class="params">( <span class="keyword">int</span> K, List PtrL )</span></span>{ </span><br><span class="line"> List p = PtrL;</span><br><span class="line"> <span class="keyword">int</span> i = <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">while</span> (p !=<span class="literal">NULL</span> && i < K ){</span><br><span class="line"> p = p->Next;</span><br><span class="line"> i++; </span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span> ( i == K ) <span class="keyword">return</span> p; </span><br><span class="line"> <span class="comment">/* 找到第K个,返回指针 */</span></span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">return</span> <span class="literal">NULL</span>; </span><br><span class="line"> <span class="comment">/* 否则返回空 */</span></span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h4 id="(2)按值查找-Find-更常用"><a href="#(2)按值查找-Find-更常用" class="headerlink" title="(2)按值查找: Find(更常用)"></a>(2)按值查找: Find(更常用)</h4><figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="function">List <span class="title">Find</span><span class="params">( ElementType X, List PtrL )</span></span>{ </span><br><span class="line"> List p = PtrL;</span><br><span class="line"> <span class="keyword">while</span> ( p!=<span class="literal">NULL</span> && p->Data != X )</span><br><span class="line"> p = p->Next; </span><br><span class="line"> <span class="keyword">return</span> p;</span><br><span class="line">} </span><br></pre></td></tr></tbody></table></figure>
<h3 id="插入-1"><a href="#插入-1" class="headerlink" title="插入"></a>插入</h3><p>先做s->Next = p->Next,后做p->Next = s,不要写反了</p>
<img src="/2021/12/19/22/1.jpg" class="" title="blog">
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="function">List <span class="title">Insert</span><span class="params">( ElementType X, <span class="keyword">int</span> i, List PtrL )</span></span>{ </span><br><span class="line"> List p, s;</span><br><span class="line"> <span class="keyword">if</span> ( i == <span class="number">1</span> ) { <span class="comment">/* 新结点插入在表头 */</span></span><br><span class="line"> s = (List)<span class="built_in">malloc</span>(<span class="built_in"><span class="keyword">sizeof</span></span>(struct LNode)); <span class="comment">/*申请、填装结点*/</span></span><br><span class="line"> s->Data = X; </span><br><span class="line"> s->Next = PtrL;</span><br><span class="line"> <span class="keyword">return</span> s; <span class="comment">/*返回新表头指针*/</span></span><br><span class="line"> }</span><br><span class="line"> p = <span class="built_in">FindKth</span>( i<span class="number">-1</span>, PtrL ); <span class="comment">/* 查找第i-1个结点 */</span></span><br><span class="line"> <span class="keyword">if</span> ( p == <span class="literal">NULL</span> ) { <span class="comment">/* 第i-1个不存在,不能插入 */</span></span><br><span class="line"> <span class="built_in">printf</span>("参数i错");</span><br><span class="line"> <span class="keyword">return</span> <span class="literal">NULL</span>; </span><br><span class="line"> }<span class="keyword">else</span> {</span><br><span class="line"> s = (List)<span class="built_in">malloc</span>(<span class="built_in"><span class="keyword">sizeof</span></span>(struct LNode)); <span class="comment">/*申请、填装结点*/</span></span><br><span class="line"> s->Data = X; </span><br><span class="line"> s->Next = p->Next; <span class="comment">/*新结点插入在第i-1个结点的后面*/</span></span><br><span class="line"> p->Next = s; </span><br><span class="line"> <span class="keyword">return</span> PtrL;</span><br><span class="line"> }</span><br></pre></td></tr></tbody></table></figure>
<h3 id="删除-1"><a href="#删除-1" class="headerlink" title="删除"></a>删除</h3><p>malloc后肯定要free了,下面三个步骤最关键</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line">s = p->Next; <span class="comment">/*s指向第i个结点*/</span></span><br><span class="line">p->Next = s->Next; <span class="comment">/*从链表中删除*/</span></span><br><span class="line"><span class="built_in">free</span>(s); <span class="comment">/*释放被删除结点 */</span></span><br></pre></td></tr></tbody></table></figure>
<img src="/2021/12/19/22/2.jpg" class="" title="blog">
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="function">List <span class="title">Delete</span><span class="params">( <span class="keyword">int</span> i, List PtrL )</span></span>{ </span><br><span class="line"> List p, s;</span><br><span class="line"> <span class="keyword">if</span> ( i == <span class="number">1</span> ) { <span class="comment">/* 若要删除的是表的第一个结点 */</span></span><br><span class="line"> s = PtrL; <span class="comment">/*s指向第1个结点*/</span></span><br><span class="line"> <span class="keyword">if</span> (PtrL!=<span class="literal">NULL</span>) PtrL = PtrL->Next; <span class="comment">/*从链表中删除*/</span></span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">return</span> <span class="literal">NULL</span>;</span><br><span class="line"> <span class="built_in">free</span>(s); <span class="comment">/*释放被删除结点 */</span></span><br><span class="line"> <span class="keyword">return</span> PtrL;</span><br><span class="line"> }</span><br><span class="line"> p = <span class="built_in">FindKth</span>( i<span class="number">-1</span>, PtrL ); <span class="comment">/*查找第i-1个结点*/</span></span><br><span class="line"> <span class="keyword">if</span> ( p == <span class="literal">NULL</span> ) { </span><br><span class="line"> <span class="built_in">printf</span>(“第%d个结点不存在”, i<span class="number">-1</span>); <span class="keyword">return</span> <span class="literal">NULL</span>; </span><br><span class="line"> } <span class="keyword">else</span> <span class="keyword">if</span> ( p->Next == <span class="literal">NULL</span> ){ </span><br><span class="line"> <span class="built_in">printf</span>(“第%d个结点不存在”, i); <span class="keyword">return</span> <span class="literal">NULL</span>; </span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> s = p->Next; <span class="comment">/*s指向第i个结点*/</span></span><br><span class="line"> p->Next = s->Next; <span class="comment">/*从链表中删除*/</span></span><br><span class="line"> <span class="built_in">free</span>(s); <span class="comment">/*释放被删除结点 */</span></span><br><span class="line"> <span class="keyword">return</span> PtrL;</span><br><span class="line"> }</span><br></pre></td></tr></tbody></table></figure>
<h2 id="广义表"><a href="#广义表" class="headerlink" title="广义表"></a>广义表</h2><p>广义表是线性表的推广;对于线性表而言, n个元素都是基本的单元素;广义表中,这些元素不仅可以是单元素也可以是<strong>另一个广义表</strong></p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span> <span class="title">GNode</span> *<span class="title">GList</span>;</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">GNode</span>{</span></span><br><span class="line"> <span class="keyword">int</span> Tag; <span class="comment">/*标志域:0表示结点是单元素,1表示结点是广义表 */</span></span><br><span class="line"> <span class="class"><span class="keyword">union</span> {</span> <span class="comment">/*子表指针域Sublist与单元素数据域Data复用(union类型),即共用存储空间*/</span></span><br><span class="line"> ElementType Data;</span><br><span class="line"> GList SubList; </span><br><span class="line"> } URegion;</span><br></pre></td></tr></tbody></table></figure>
<h2 id="多重链表"><a href="#多重链表" class="headerlink" title="多重链表"></a>多重链表</h2><p>广义表就是多重链表的一种</p>
<p>多重链表中结点的指针域会有多个,如前面例子包含了Next和SubList两个指针域</p>
<p>但包含两个指针域的链表并不一定是多重链表,比如在双向链表不是多重链表</p>
<p>多重链表有广泛的用途: 基本上如树、图这样相对复杂的数据结构都可以采 用多重链表方式实现存储</p>
]]></content>
<tags>
<tag>Data Structure</tag>
</tags>
</entry>
<entry>
<title>主题由nexT更换到了Butterfly</title>
<url>/2021/12/29/23/</url>
<content><![CDATA[<h2 id="无法挽回的错误"><a href="#无法挽回的错误" class="headerlink" title="无法挽回的错误"></a>无法挽回的错误</h2><p>昨天夜里想给博客增添一点新功能,没有备份主题配置文件,发布时出了问题,导致页面不显示了,无奈之下更换了Butterfly主题</p>
<p>令我意外的是这个主题的代码块自带mac风格</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"hello world"</span>);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<p>只不过gittalk要重新设置了,我也真是爱折腾。。。</p>
]]></content>
<tags>
<tag>theme</tag>
</tags>
</entry>
<entry>
<title>琐事记(1)</title>
<url>/2021/12/31/24/</url>
<content><![CDATA[<h3 id="题记"><a href="#题记" class="headerlink" title="题记"></a>题记</h3><p><strong>世间好物不坚牢,彩云易散琉璃脆。</strong></p>
<p><strong>—白居易《简简吟》</strong></p>
<h3 id="旧忆"><a href="#旧忆" class="headerlink" title="旧忆"></a>旧忆</h3><p>常常梦到春天里断了线的风筝,夏日里碧绿池塘边的蛙鸣,秋天里晒了满院的玉米,冬日里煤球炉中温暖的火苗……</p>
<p>我那思绪也像断了线的风筝,乘着自由的风,邂逅一幕幕回忆。</p>
<p>一切故事仿佛都是从冬天开始的,冬天对我而言象征着温暖的相聚,而春天则是怀着希望的分别,不论结果如何,年底一家人总会相聚一团,和和气气,畅叙往事。</p>
<p>从故乡到第一次走出家门,我们家就已经做好了用勤劳的双手换取幸福生活的准备。在西辛庄,我母亲挣得第一笔金,她说,那个加班的夜晚她永远也不会忘记;在昆山,母亲仅用几天时间就赶上了工厂机器运转的速度,母亲说了一句让我永远忘不了的话:“别人能做,我也可以!”在濮阳,母亲服务员的工作一干就是七年,生活越来月可观了,家里盖起了二层楼,往日的负债一一还清,仿佛什么样的累活都难不倒我的母亲,直到我第一次听到母亲说脚疼的只能慢慢得走路,第一次看到母亲滑倒差点再也没起来,第一次看到母亲失眠……我方才发现,那个无比坚强的母亲,老了。常年陪在母亲身边,却不知母亲常去染头是为了掩盖岁月的痕迹,脚疼去医院被检查出来是骨质增生,母亲又独自消化了多少疼痛和孤独?</p>
<p>父亲常年打工在外,记得高中放假每次都是母亲接我,不曾料到有一次父亲专门赶回家来接我,我在校门口等了两个小时,愣是没认出来父亲。“那是我爸吗,我真没认出来!”“你不知道你爸额头上添了多少皱纹和黑斑……”</p>
<img src="/2021/12/31/24/2.jpg" class="" title="blog">
<p>如今的年,也失色了不少。以往的除夕夜的天空,是烟花的舞台:每一次轰鸣,每一簇火花,每一次绽放,携卷着人们一年的烦恼与不幸,冲向穹顶,化作星光万丈。手里捧一碗热乎的饺子,便会震起层层涟漪。那些过往的人和事,兴许仍记得,兴许早已忘记了,都融进这新年的梦里。梦醒时,天朗气清,一切如初。</p>
]]></content>
<tags>
<tag>memories</tag>
</tags>
</entry>
<entry>
<title>算法学习(1)</title>
<url>/2021/12/11/21/</url>
<content><![CDATA[<h1 id="暴力求解法"><a href="#暴力求解法" class="headerlink" title="暴力求解法"></a>暴力求解法</h1><p><strong>有错还请指正</strong></p>
<span id="more"></span>
<h2 id="简单枚举"><a href="#简单枚举" class="headerlink" title="简单枚举"></a>简单枚举</h2><h3 id="例题7-1-除法(Division-UVa-725)"><a href="#例题7-1-除法(Division-UVa-725)" class="headerlink" title="例题7-1 除法(Division, UVa 725)"></a>例题7-1 除法(Division, UVa 725)</h3><p>输入正整数n,按从小到大的顺序输出所有形如abcde/fghij = n的表达式,其中a~j恰好 为数字0~9的一个排列(可以有前导0),2≤n≤79。 </p>
<p>样例输入: </p>
<p>62 </p>
<p>样例输出: </p>
<p>79546 / 01283 = 62 </p>
<p>94736 / 01528 = 62</p>
<figure class="highlight c"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><stdio.h></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><string.h></span></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> i,j,n,s1,s2,flag[<span class="number">10</span>];</span><br><span class="line"> <span class="keyword">while</span>(~<span class="built_in">scanf</span>(<span class="string">"%d"</span>,&n))</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">for</span>(i=<span class="number">1234</span>;i<<span class="number">50000</span>;i++)</span><br><span class="line"> {</span><br><span class="line"> <span class="built_in">memset</span>(flag,<span class="number">0</span>,<span class="keyword">sizeof</span>(flag));</span><br><span class="line"> s1=i;</span><br><span class="line"> s2=i*n;</span><br><span class="line"> <span class="keyword">while</span>(s1||s2)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(!flag[s1%<span class="number">10</span>])</span><br><span class="line"> {</span><br><span class="line"> flag[s1%<span class="number">10</span>]=<span class="number">1</span>;</span><br><span class="line"> s1/=<span class="number">10</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">if</span>(!flag[s2%<span class="number">10</span>])</span><br><span class="line"> {</span><br><span class="line"> flag[s2%<span class="number">10</span>]=<span class="number">1</span>;</span><br><span class="line"> s2/=<span class="number">10</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(j=<span class="number">0</span>;j<<span class="number">10</span>;j++)</span><br><span class="line"> <span class="keyword">if</span>(!flag[j])</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">if</span>(j==<span class="number">10</span>&&i*n<=<span class="number">98765</span>)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(i<<span class="number">10000</span>)</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d / 0%d = %d\n"</span>,i*n,i,n);</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d / %d = %d\n"</span>,i*n,i,n);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h3 id="例题7-2-最大乘积(Maximum-Product-UVa-11059)"><a href="#例题7-2-最大乘积(Maximum-Product-UVa-11059)" class="headerlink" title="例题7-2 最大乘积(Maximum Product, UVa 11059)"></a>例题7-2 最大乘积(Maximum Product, UVa 11059)</h3><p>输入n个元素组成的序列S,你需要找出一个乘积最大的连续子序列。如果这个最大的乘 积不是正数,应输出0(表示无解)。1≤n≤18,-10≤Si≤10。 </p>
<p>样例输入: </p>
<p>3 </p>
<p>2 4 -3 </p>
<p>5 </p>
<p>2 5 -1 2 -1 </p>
<p>样例输出: </p>
<p>8 20</p>
<figure class="highlight c"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><stdio.h></span></span></span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> max=<span class="number">-1e19</span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> t;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%lld"</span>,&t);</span><br><span class="line"> <span class="keyword">while</span>(t--){</span><br><span class="line"> <span class="keyword">long</span> <span class="keyword">long</span> <span class="keyword">int</span> n,a[<span class="number">20</span>]={<span class="number">0</span>},ret=<span class="number">1</span>;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%lld"</span>,&n);</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=n;i++){</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%lld"</span>,&a[i]);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=n<span class="number">-1</span>;i++){</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j=i+<span class="number">1</span>;j<=n;j++){</span><br><span class="line"> ret=<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> k=i;k<=j;k++){</span><br><span class="line"> ret=ret*a[k];</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(ret>max)</span><br><span class="line"> max=ret;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(max<<span class="number">0</span>)</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"0\n"</span>);</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%lld\n"</span>,max);</span><br><span class="line">}</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h3 id="例题7-3-分数拆分(Fractions-Again-UVa-10976)"><a href="#例题7-3-分数拆分(Fractions-Again-UVa-10976)" class="headerlink" title="例题7-3 分数拆分(Fractions Again?!, UVa 10976)"></a>例题7-3 分数拆分(Fractions Again?!, UVa 10976)</h3><p>输入正整数k,找到所有的正整数x>=y,使得1/k=1/x + 1/y;</p>
<p>样例输入:</p>
<p>2 </p>
<p>12 </p>
<p>样例输出:</p>
<p>2 </p>
<p>1/2 = 1/6 + 1/3; </p>
<p>1/2 = 1/4 + 1/4;</p>
<figure class="highlight c"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><stdio.h></span></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> k;</span><br><span class="line"> <span class="keyword">while</span>(~<span class="built_in">scanf</span>(<span class="string">"%d"</span>,&k)){</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> y=k+<span class="number">1</span>;y<=<span class="number">2</span>*k;y++){</span><br><span class="line"> <span class="keyword">if</span>(k*y%(y-k)==<span class="number">0</span>){</span><br><span class="line"> <span class="keyword">int</span> x = k*y/(y-k);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"1/%d = 1/%d + 1/%d\n"</span>,k,x,y);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<p>从1/12=1/156+1/13可以看出,x可以比y大很多。由于x>=y,有1/x<=1/y,因此1/k-1/y<=1/y,即<strong>y<=2 * k</strong>.这样,只需要在2 * k范围之内枚举y,然后根据y尝试计算出x即可</p>
<h2 id="枚举排列"><a href="#枚举排列" class="headerlink" title="枚举排列"></a>枚举排列</h2><h3 id="7-2-1-生成1~n的排列"><a href="#7-2-1-生成1~n的排列" class="headerlink" title="7.2.1 生成1~n的排列"></a>7.2.1 生成1~n的排列</h3><figure class="highlight c"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><stdio.h></span></span></span><br><span class="line"><span class="keyword">int</span> a[<span class="number">1000</span>];</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">print_permutation</span><span class="params">(<span class="keyword">int</span> n,<span class="keyword">int</span> *A,<span class="keyword">int</span> cur)</span></span>{</span><br><span class="line"> <span class="keyword">if</span>(cur==n){</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i<n;i++) </span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d "</span>,A[i]);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"\n"</span>);</span><br><span class="line">}</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=n;i++){</span><br><span class="line"> <span class="keyword">int</span> ok=<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">0</span>;j<cur;j++)</span><br><span class="line"> <span class="keyword">if</span>(A[j]==i)</span><br><span class="line"> ok=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">if</span>(ok){</span><br><span class="line"> A[cur]=i;</span><br><span class="line"> print_permutation(n,A,cur+<span class="number">1</span>);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> n,cur=<span class="number">0</span>;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>,&n);</span><br><span class="line"> print_permutation(n,a,<span class="number">0</span>);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<p>当然我知道c++STL里有next_permutation函数,这里是函数的一种实现方式(递归)</p>
<figure class="highlight c"><table><tbody><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">next_pre</span><span class="params">(<span class="keyword">int</span> n,<span class="keyword">int</span> x)</span></span>{</span><br><span class="line"> <span class="keyword">do</span></span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i<n;i++){</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d"</span>,p[i]);</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"\n"</span>);</span><br><span class="line"> }<span class="keyword">while</span>(next_permutation(p,p+n));</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h3 id="7-2-2-生成可重集的排列"><a href="#7-2-2-生成可重集的排列" class="headerlink" title="7.2.2 生成可重集的排列"></a>7.2.2 生成可重集的排列</h3><figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span> </span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">int</span> ans[<span class="number">30</span>],a[<span class="number">30</span>]; </span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">print_permutation</span><span class="params">(<span class="keyword">int</span> n,<span class="keyword">int</span>*x,<span class="keyword">int</span> pos)</span></span>{</span><br><span class="line"> <span class="keyword">if</span> (pos==n) {</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i=<span class="number">0</span>;i<n;i++) </span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d "</span>,x[i]); </span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"\n"</span>);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">for</span> (<span class="keyword">int</span> i=<span class="number">0</span>;i<n;i++){</span><br><span class="line"> <span class="keyword">if</span> (!i||(a[i]!=a[i<span class="number">-1</span>])){</span><br><span class="line"> <span class="keyword">int</span> c1=<span class="number">0</span>,c2=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> j=<span class="number">0</span>;j<pos;j++) </span><br><span class="line"> <span class="keyword">if</span> (x[j]==a[i]) c1++;</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> j=<span class="number">0</span>;j<n;j++) </span><br><span class="line"> <span class="keyword">if</span> (a[i]==a[j]) c2++;</span><br><span class="line"> <span class="keyword">if</span> (c1<c2) {</span><br><span class="line"> x[pos]=a[i];</span><br><span class="line"> <span class="built_in">print_permutation</span>(n,x,pos+<span class="number">1</span>);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="keyword">int</span> n; </span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>,&n); </span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i<n;i++) </span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>,&a[i]); </span><br><span class="line"> <span class="built_in">sort</span>(a,a+n); </span><br><span class="line"> <span class="built_in">print_permutation</span>(n,ans,<span class="number">0</span>); </span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">} </span><br></pre></td></tr></tbody></table></figure>
<h3 id="7-2-4-下一个排列"><a href="#7-2-4-下一个排列" class="headerlink" title="7.2.4 下一个排列"></a>7.2.4 下一个排列</h3><figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>{</span><br><span class="line"><span class="keyword">int</span> n,p[<span class="number">10</span>];</span><br><span class="line"><span class="built_in">scanf</span>(<span class="string">"%d"</span>,&n);</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i < n; i++) </span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &p[i]);</span><br><span class="line"> <span class="built_in">sort</span>(p, p+n);</span><br><span class="line"><span class="keyword">do</span> {</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i < n; i++) </span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d "</span>, p[i]);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"\n"</span>);</span><br><span class="line">}<span class="keyword">while</span>(<span class="built_in">next_permutation</span>(p, p+n));</span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<p>待更新。。。</p>
]]></content>
<tags>
<tag>algorithm</tag>
</tags>
</entry>
<entry>
<title>数据结构(2)</title>
<url>/2022/01/12/26/</url>
<content><![CDATA[<h1 id="栈"><a href="#栈" class="headerlink" title="栈"></a>栈</h1><h2 id="定义"><a href="#定义" class="headerlink" title="定义"></a>定义</h2><p>堆栈(Stack):</p>
<p>具有一定操作约束的线性表,只在一端(栈顶,Top)做插入、删除</p>
<p>特点:<strong>后入先出</strong></p>
<p>栈的操作:</p>
<p> 1、Stack CreateStack( int MaxSize ): 生成空堆栈,其最大长度为MaxSize;</p>
<p> 2、int IsFull( Stack S, int MaxSize ):判断堆栈S是否已满; </p>
<p> 3、void Push( Stack S, ElementType item ):将元素item压入堆栈;</p>
<p> 4、int IsEmpty ( Stack S ):判断堆栈S是否为空;</p>
<p> 5、ElementType Pop( Stack S ):删除并返回栈顶元素;</p>
<h2 id="栈的顺序存储实现"><a href="#栈的顺序存储实现" class="headerlink" title="栈的顺序存储实现"></a>栈的顺序存储实现</h2><figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">define</span> MaxSize <span class="comment">/*储存数据元素的最大个数*/</span></span></span><br><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span> <span class="title">SNode</span> *<span class="title">Stack</span>;</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">SNode</span>{</span></span><br><span class="line"> ElementType Data[MaxSize];</span><br><span class="line"> <span class="keyword">int</span> Top;</span><br><span class="line">};</span><br></pre></td></tr></tbody></table></figure>
<h3 id="入栈"><a href="#入栈" class="headerlink" title="入栈"></a>入栈</h3><figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Push</span><span class="params">( Stack PtrS, ElementType item )</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">if</span> ( PtrS->Top == MaxSize<span class="number">-1</span> ) {</span><br><span class="line"> <span class="built_in">printf</span>(“堆栈满”); <span class="keyword">return</span>;</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> PtrS->Data[++(PtrS->Top)] = item;</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h3 id="出栈"><a href="#出栈" class="headerlink" title="出栈"></a>出栈</h3><figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="function">ElementType <span class="title">Pop</span><span class="params">( Stack PtrS )</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">if</span> ( PtrS->Top == <span class="number">-1</span> ) {</span><br><span class="line"> <span class="built_in">printf</span>(“堆栈空”);</span><br><span class="line"> <span class="keyword">return</span> ERROR; <span class="comment">/* ERROR是ElementType的特殊值,标志错误*/</span></span><br><span class="line"> } <span class="keyword">else</span> </span><br><span class="line"> <span class="built_in"><span class="keyword">return</span></span> ( PtrS->Data[(PtrS->Top)--] );</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="堆栈的链式存储实现"><a href="#堆栈的链式存储实现" class="headerlink" title="堆栈的链式存储实现"></a>堆栈的链式存储实现</h2><figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span> <span class="title">SNode</span> *<span class="title">Stack</span>;</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">SNode</span>{</span></span><br><span class="line"> ElementType Data;</span><br><span class="line"> <span class="class"><span class="keyword">struct</span> <span class="title">SNode</span> *<span class="title">Next</span>;</span></span><br><span class="line">} ;</span><br></pre></td></tr></tbody></table></figure>
<h3 id="建栈"><a href="#建栈" class="headerlink" title="建栈"></a>建栈</h3><figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="function">Stack <span class="title">CreateStack</span><span class="params">()</span> </span></span><br><span class="line"><span class="function"></span>{ <span class="comment">/* 构建一个堆栈的头结点,返回指针 */</span></span><br><span class="line"> Stack S;</span><br><span class="line"> S =(Stack)<span class="built_in">malloc</span>(<span class="built_in"><span class="keyword">sizeof</span></span>(struct SNode));</span><br><span class="line"> S->Next = <span class="literal">NULL</span>;</span><br><span class="line"> <span class="keyword">return</span> S;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">IsEmpty</span><span class="params">(Stack S)</span> </span></span><br><span class="line"><span class="function"></span>{ <span class="comment">/*判断堆栈S是否为空,若为空函数返回整数1,否则返回0 */</span></span><br><span class="line"> <span class="keyword">return</span> ( S->Next == <span class="literal">NULL</span> );</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h3 id="入栈-1"><a href="#入栈-1" class="headerlink" title="入栈"></a>入栈</h3><figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Push</span><span class="params">( ElementType item, Stack S)</span> </span></span><br><span class="line"><span class="function"></span>{ <span class="comment">/* 将元素item压入堆栈S */</span></span><br><span class="line"> <span class="class"><span class="keyword">struct</span> <span class="title">SNode</span> *<span class="title">TmpCell</span>;</span></span><br><span class="line"> TmpCell=(struct SNode *)<span class="built_in">malloc</span>(<span class="built_in"><span class="keyword">sizeof</span></span>(struct SNode));</span><br><span class="line"> TmpCell->Element = item;</span><br><span class="line"> TmpCell->Next = S->Next;</span><br><span class="line"> S->Next = TmpCell;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h3 id="出栈-1"><a href="#出栈-1" class="headerlink" title="出栈"></a>出栈</h3><figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="function">ElementType <span class="title">Pop</span><span class="params">(Stack S)</span></span></span><br><span class="line"><span class="function"></span>{ <span class="comment">/* 删除并返回堆栈S的栈顶元素 */</span></span><br><span class="line"> <span class="class"><span class="keyword">struct</span> <span class="title">SNode</span> *<span class="title">FirstCell</span>;</span></span><br><span class="line"> ElementType TopElem;</span><br><span class="line"> <span class="keyword">if</span>( <span class="built_in">IsEmpty</span>( S ) ) {</span><br><span class="line"> <span class="built_in">printf</span>(“堆栈空”); <span class="keyword">return</span> <span class="literal">NULL</span>;</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> FirstCell = S->Next; </span><br><span class="line"> S->Next = FirstCell->Next;</span><br><span class="line"> TopElem = FirstCell ->Element;</span><br><span class="line"> <span class="built_in">free</span>(FirstCell);</span><br><span class="line"> <span class="keyword">return</span> TopElem;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h1 id="队列"><a href="#队列" class="headerlink" title="队列"></a>队列</h1><h2 id="定义-1"><a href="#定义-1" class="headerlink" title="定义"></a>定义</h2><p>队列(Queue):具有一定操作约束的线性表,只能在一端插入,而在另一端删除</p>
<p>特点:<strong>先进先出</strong></p>
<p>队列的操作:</p>
<p> 1、Queue CreatQueue( int MaxSize ):生成长度为MaxSize的空队列;</p>
<p> 2、int IsFullQ( Queue Q, int MaxSize ):判断队列Q是否已满;</p>
<p> 3、void AddQ( Queue Q, ElementType item ): 将数据元素item插入队列Q中;</p>
<p> 4、int IsEmptyQ( Queue Q ): 判断队列Q是否为空;</p>
<p> 5、ElementType DeleteQ( Queue Q ):将队头数据元素从队列中删除并返回。</p>
<h2 id="队列的顺序存储实现"><a href="#队列的顺序存储实现" class="headerlink" title="队列的顺序存储实现"></a>队列的顺序存储实现</h2><figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">define</span> MaxSize <span class="comment">/*储存数据元素的最大个数*/</span></span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">QNode</span> {</span></span><br><span class="line"> ElementType Data[ MaxSize ];</span><br><span class="line"> <span class="keyword">int</span> rear;</span><br><span class="line"> <span class="keyword">int</span> front;</span><br><span class="line">}; </span><br><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span> <span class="title">QNode</span> *<span class="title">Queue</span>;</span></span><br></pre></td></tr></tbody></table></figure>
<h3 id="入队列"><a href="#入队列" class="headerlink" title="入队列"></a>入队列</h3><figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">AddQ</span><span class="params">( Queue PtrQ, ElementType item)</span></span></span><br><span class="line"><span class="function"></span>{ </span><br><span class="line"> <span class="keyword">if</span> ( (PtrQ->rear+<span class="number">1</span>) % MaxSize == PtrQ->front ) {</span><br><span class="line"> <span class="built_in">printf</span>(“队列满”); </span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> PtrQ->rear = (PtrQ->rear+<span class="number">1</span>)% MaxSize;</span><br><span class="line"> PtrQ->Data[PtrQ->rear] = item;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h3 id="出队列"><a href="#出队列" class="headerlink" title="出队列"></a>出队列</h3><figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="function">ElementType <span class="title">DeleteQ</span> <span class="params">( Queue PtrQ )</span></span></span><br><span class="line"><span class="function"></span>{ </span><br><span class="line"> <span class="keyword">if</span> ( PtrQ->front == PtrQ->rear ) { </span><br><span class="line"> <span class="built_in">printf</span>(“队列空”);</span><br><span class="line"> <span class="keyword">return</span> ERROR;</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> PtrQ->front = (PtrQ->front+<span class="number">1</span>)% MaxSize;</span><br><span class="line"> <span class="keyword">return</span> PtrQ->Data[PtrQ->front];</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="队列的链式存储实现"><a href="#队列的链式存储实现" class="headerlink" title="队列的链式存储实现"></a>队列的链式存储实现</h2><figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Node</span>{</span></span><br><span class="line"> ElementType Data;</span><br><span class="line"> <span class="class"><span class="keyword">struct</span> <span class="title">Node</span> *<span class="title">Next</span>;</span></span><br><span class="line">}; </span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">QNode</span>{</span> <span class="comment">/* 链队列结构 */</span></span><br><span class="line"> <span class="class"><span class="keyword">struct</span> <span class="title">Node</span> *<span class="title">rear</span>;</span> <span class="comment">/* 指向队尾结点 */</span></span><br><span class="line"> <span class="class"><span class="keyword">struct</span> <span class="title">Node</span> *<span class="title">front</span>;</span> <span class="comment">/* 指向队头结点 */</span></span><br><span class="line">}; </span><br><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span> <span class="title">QNode</span> *<span class="title">Queue</span>;</span></span><br><span class="line">Queue PtrQ;</span><br></pre></td></tr></tbody></table></figure>
<h3 id="出队列-1"><a href="#出队列-1" class="headerlink" title="出队列"></a>出队列</h3><figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="function">ElementType <span class="title">DeleteQ</span> <span class="params">( Queue PtrQ )</span></span></span><br><span class="line"><span class="function"></span>{ <span class="class"><span class="keyword">struct</span> <span class="title">Node</span> *<span class="title">FrontCell</span>;</span> </span><br><span class="line"> ElementType FrontElem;</span><br><span class="line"> <span class="keyword">if</span> ( PtrQ->front == <span class="literal">NULL</span>) {</span><br><span class="line"> <span class="built_in">printf</span>(“队列空”); <span class="keyword">return</span> ERROR;</span><br><span class="line"> } </span><br><span class="line"> FrontCell = PtrQ->front;</span><br><span class="line"> <span class="keyword">if</span> ( PtrQ->front == PtrQ->rear) <span class="comment">/* 若队列只有一个元素 */</span></span><br><span class="line"> PtrQ->front = PtrQ->rear = <span class="literal">NULL</span>; <span class="comment">/* 删除后队列置为空 */</span></span><br><span class="line"> <span class="keyword">else</span> </span><br><span class="line"> PtrQ->front = PtrQ->front->Next;</span><br><span class="line"> FrontElem = FrontCell->Data;</span><br><span class="line"> <span class="built_in">free</span>( FrontCell ); <span class="comment">/* 释放被删除结点空间 */</span></span><br><span class="line"> <span class="keyword">return</span> FrontElem;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
]]></content>
<tags>
<tag>Data Structure</tag>
</tags>
</entry>
<entry>
<title>寒假冬训营字符串专题题解</title>
<url>/2022/01/08/25/</url>
<content><![CDATA[<h1 id="字符串专题第22组题解"><a href="#字符串专题第22组题解" class="headerlink" title="字符串专题第22组题解"></a>字符串专题第22组题解</h1><h2 id="A-雷同检测"><a href="#A-雷同检测" class="headerlink" title="A - 雷同检测"></a>A - 雷同检测</h2><p>考试的时候老师最讨厌有人抄袭了。自从有了电子评卷,老师要查找雷同卷,就容易多了,只要将两个人的答案输入计算机,进行逐个字符的比对,把相同的位置都找出来,就一目了然了。</p>
<h3 id="输入格式"><a href="#输入格式" class="headerlink" title="输入格式"></a>输入格式</h3><p>2 行,每行包含一串字符(长度不超过 200)。</p>
<h3 id="输出格式"><a href="#输出格式" class="headerlink" title="输出格式"></a>输出格式</h3><p>1 行,包含若干个以空格分隔的数字,表示出现相同字符的位置。</p>
<p>Sample Input</p>
<figure class="highlight plaintext"><table><tbody><tr><td class="code"><pre><span class="line">I am suantoujun.</span><br><span class="line">I am huayemei.</span><br></pre></td></tr></tbody></table></figure>
<p>Sample Output</p>
<figure class="highlight plaintext"><table><tbody><tr><td class="code"><pre><span class="line">1 2 3 4 5 6 8 9</span><br></pre></td></tr></tbody></table></figure>
<figure class="highlight c"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><stdio.h></span></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">char</span> a[<span class="number">210</span>],b[<span class="number">210</span>];</span><br><span class="line"> gets(a),gets(b);</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;a[i]!=<span class="string">'\0'</span>;i++)</span><br><span class="line"> <span class="keyword">if</span>(a[i]==b[i])</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d "</span>,i+<span class="number">1</span>);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="B-首字母大写"><a href="#B-首字母大写" class="headerlink" title="B - 首字母大写"></a>B - 首字母大写</h2><p>对一个字符串中的所有单词,如果单词的首字母不是大写字母,则把单词的首字母变成大写字母。在字符串中,单词之间通过空白符分隔,空白符包括:空格(‘ ‘)、制表符(‘\t’)、回车符(‘\r’)、换行符(‘\n’)。</p>
<p>Input</p>
<p>输入一行:待处理的字符串(长度小于80)。</p>
<p>Output</p>
<p>输出一行:转换后的字符串。</p>
<p>Sample Input</p>
<figure class="highlight plaintext"><table><tbody><tr><td class="code"><pre><span class="line">if so, you already have a google account. you can sign in on the right.</span><br></pre></td></tr></tbody></table></figure>
<p>Sample Output</p>
<figure class="highlight plaintext"><table><tbody><tr><td class="code"><pre><span class="line">If So, You Already Have A Google Account. You Can Sign In On The Right.</span><br></pre></td></tr></tbody></table></figure>
<figure class="highlight c"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><stdio.h></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><string.h></span></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">char</span> a[<span class="number">90</span>];</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%[^\n]"</span>,a);</span><br><span class="line"> <span class="keyword">if</span>(a[<span class="number">0</span>]>=<span class="string">'a'</span>&&a[<span class="number">0</span>]<=<span class="string">'z'</span>)</span><br><span class="line"> a[<span class="number">0</span>]-=<span class="number">32</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<<span class="built_in">strlen</span>(a)<span class="number">-1</span>;i++)</span><br><span class="line"> <span class="keyword">if</span>(a[i]==<span class="string">' '</span>&&a[i+<span class="number">1</span>]>=<span class="string">'a'</span>&&a[i+<span class="number">1</span>]<=<span class="string">'z'</span>)</span><br><span class="line"> a[i+<span class="number">1</span>]-=<span class="number">32</span>;</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%s"</span>,a);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="C-大小写转换"><a href="#C-大小写转换" class="headerlink" title="C - 大小写转换"></a>C - 大小写转换</h2><p> 读入一些字符串,将其中的小写字母转成大写字母(其他字符不变)。</p>
<h2 id="输入"><a href="#输入" class="headerlink" title="输入"></a>输入</h2><p> 输入为多行,每行为一个字符串,字符串只由字母和数字组成,长度不超过80。输入以“End of file”结束。</p>
<h2 id="输出"><a href="#输出" class="headerlink" title="输出"></a>输出</h2><p> 对于每行输入,输出转换后的字符串。</p>
<h2 id="输入示例"><a href="#输入示例" class="headerlink" title="输入示例"></a>输入示例</h2><figure class="highlight plaintext"><table><tbody><tr><td class="code"><pre><span class="line">Hello</span><br><span class="line">ICPC2004</span><br><span class="line">12345abcde</span><br></pre></td></tr></tbody></table></figure>
<h2 id="输出示例"><a href="#输出示例" class="headerlink" title="输出示例"></a>输出示例</h2><figure class="highlight plaintext"><table><tbody><tr><td class="code"><pre><span class="line">HELLO</span><br><span class="line">ICPC2004</span><br><span class="line">12345ABCDE</span><br></pre></td></tr></tbody></table></figure>
<figure class="highlight c"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><stdio.h></span></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">char</span> a[<span class="number">90</span>];</span><br><span class="line"> <span class="keyword">while</span>(<span class="built_in">scanf</span>(<span class="string">"%s"</span>,a)==<span class="number">1</span>){</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;a[i]!=<span class="string">'\0'</span>;i++)</span><br><span class="line"> <span class="keyword">if</span>(a[i]>=<span class="string">'a'</span>&&a[i]<=<span class="string">'z'</span>)</span><br><span class="line"> a[i]-=<span class="number">32</span>;</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%s\n"</span>,a);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="D-数字反转"><a href="#D-数字反转" class="headerlink" title="D - 数字反转"></a>D - 数字反转</h2><p>给定一个整数,请将该数各个位上数字反转得到一个新数。新数也应满足整数的常见形式,即除非给定的原数为零,否则反转后得到的新数的最高位数字不应为零(参见样例 2)。</p>
<h4 id="输入格式-1"><a href="#输入格式-1" class="headerlink" title="输入格式"></a>输入格式</h4><p>输入共 1 行,一个整数 N。</p>
<h4 id="输出格式-1"><a href="#输出格式-1" class="headerlink" title="输出格式"></a>输出格式</h4><p>输出共 1 行,一个整数,表示反转后的新数。</p>
<h4 id="数据范围"><a href="#数据范围" class="headerlink" title="数据范围"></a>数据范围</h4><p>-1,000,000,000 <= N <= 1,000,000,000−1,000,000,000 <= N <= 1,000,000,000。</p>
<p>Sample Input</p>
<figure class="highlight plaintext"><table><tbody><tr><td class="code"><pre><span class="line">123</span><br></pre></td></tr></tbody></table></figure>
<p>Sample Output</p>
<figure class="highlight plaintext"><table><tbody><tr><td class="code"><pre><span class="line">321</span><br></pre></td></tr></tbody></table></figure>
<p>Sample Input 2</p>
<figure class="highlight plaintext"><table><tbody><tr><td class="code"><pre><span class="line">-380</span><br></pre></td></tr></tbody></table></figure>
<p>Sample Output 2</p>
<figure class="highlight plaintext"><table><tbody><tr><td class="code"><pre><span class="line">-83</span><br></pre></td></tr></tbody></table></figure>
<figure class="highlight c"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><stdio.h></span></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> n,sum=<span class="number">0</span>;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>,&n);</span><br><span class="line"> <span class="keyword">while</span>(n){</span><br><span class="line"> sum=sum*<span class="number">10</span>+n%<span class="number">10</span>;</span><br><span class="line"> n/=<span class="number">10</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d"</span>,sum);</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="E-删除单词后缀"><a href="#E-删除单词后缀" class="headerlink" title="E - 删除单词后缀"></a>E - 删除单词后缀</h2><p>给定一个单词,如果该单词以<code>er</code>、<code>ly</code>或者<code>ing</code>后缀结尾, 则删除该后缀(题目保证删除后缀后的单词长度不为 0),否则不进行任何操作。</p>
<h4 id="输入格式-2"><a href="#输入格式-2" class="headerlink" title="输入格式"></a>输入格式</h4><p>输入一行,包含一个单词(单词中间没有空格,每个单词最大长度为 32)。</p>
<h4 id="输出格式-2"><a href="#输出格式-2" class="headerlink" title="输出格式"></a>输出格式</h4><p>输出按照题目要求处理后的单词。</p>
<p>Sample Input</p>
<figure class="highlight plaintext"><table><tbody><tr><td class="code"><pre><span class="line">referer</span><br></pre></td></tr></tbody></table></figure>
<p>Sample Output</p>
<figure class="highlight plaintext"><table><tbody><tr><td class="code"><pre><span class="line">refer</span><br></pre></td></tr></tbody></table></figure>
<figure class="highlight c"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><stdio.h></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><string.h></span></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">char</span> a[<span class="number">40</span>];</span><br><span class="line"> <span class="keyword">int</span> l;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%s"</span>,a);</span><br><span class="line"> l=<span class="built_in">strlen</span>(a)<span class="number">-1</span>;</span><br><span class="line"> <span class="keyword">if</span>(a[l]==<span class="string">'r'</span>&&a[l<span class="number">-1</span>]==<span class="string">'e'</span>||a[l]==<span class="string">'y'</span>&&a[l<span class="number">-1</span>]==<span class="string">'l'</span>)</span><br><span class="line"> a[l<span class="number">-1</span>]=<span class="string">'\0'</span>;</span><br><span class="line"> <span class="keyword">if</span>(a[l<span class="number">-2</span>]==<span class="string">'i'</span>&&a[l<span class="number">-1</span>]==<span class="string">'n'</span>&&a[l]==<span class="string">'g'</span>)</span><br><span class="line"> a[l<span class="number">-2</span>]=<span class="string">'\0'</span>;</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%s"</span>,a);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="F-判断字符串是否为回文"><a href="#F-判断字符串是否为回文" class="headerlink" title="F - 判断字符串是否为回文"></a>F - 判断字符串是否为回文</h2><p>输入一个字符串,输出该字符串是否回文。回文是指顺读和倒读都一样的字符串。</p>
<h4 id="输入格式-3"><a href="#输入格式-3" class="headerlink" title="输入格式"></a>输入格式</h4><p>输入为一行字符串(字符串中没有空白字符,字符串长度不超过 100)。</p>
<h4 id="输出格式-3"><a href="#输出格式-3" class="headerlink" title="输出格式"></a>输出格式</h4><p>如果字符串是回文,输出<code>"yes"</code>;否则,输出<code>"no"</code>。</p>
<p>Sample Input</p>
<figure class="highlight plaintext"><table><tbody><tr><td class="code"><pre><span class="line">abcdedcba</span><br></pre></td></tr></tbody></table></figure>
<p>Sample Output</p>
<figure class="highlight plaintext"><table><tbody><tr><td class="code"><pre><span class="line">yes</span><br></pre></td></tr></tbody></table></figure>
<figure class="highlight c"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><stdio.h></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><string.h></span></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">char</span> a[<span class="number">110</span>];</span><br><span class="line"> <span class="keyword">int</span> len,i;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%s"</span>,a);</span><br><span class="line"> len=<span class="built_in">strlen</span>(a)<span class="number">-1</span>;</span><br><span class="line"> <span class="keyword">for</span>(i=<span class="number">0</span>;i<=len/<span class="number">2</span>;i++)</span><br><span class="line"> <span class="keyword">if</span>(a[len-i]!=a[i])</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">if</span>(i<=len/<span class="number">2</span>)</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"no"</span>);</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"yes"</span>);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="G-基础数据结构——栈(1)"><a href="#G-基础数据结构——栈(1)" class="headerlink" title="G - 基础数据结构——栈(1)"></a>G - 基础数据结构——栈(1)</h2><p>给你一串字符,不超过50个字符,可能包括括号、数字、字母、标点符号、空格,你的任务是检查这一串字符中的( ) ,[ ],{ }是否匹配。</p>
<h4 id="输入格式-4"><a href="#输入格式-4" class="headerlink" title="输入格式"></a>输入格式</h4><p>输入数据有多组,每组数据不超过100个字符并含有( ,) ,[, ],{, }一个或多个。处理到文件结束。</p>
<h4 id="输出格式-4"><a href="#输出格式-4" class="headerlink" title="输出格式"></a>输出格式</h4><p>如果匹配就输出<code>"yes"</code>,不匹配输出<code>"no"</code></p>
<p>Sample Input</p>
<figure class="highlight plaintext"><table><tbody><tr><td class="code"><pre><span class="line">sin(20+10)</span><br><span class="line">{[}]</span><br></pre></td></tr></tbody></table></figure>
<p>Sample Output</p>
<figure class="highlight plaintext"><table><tbody><tr><td class="code"><pre><span class="line">yes</span><br><span class="line">no</span><br></pre></td></tr></tbody></table></figure>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">char</span> s[<span class="number">60</span>],c[<span class="number">60</span>];</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">while</span>(<span class="built_in">gets</span>(s)<span class="comment">/*!=NULL*/</span>){</span><br><span class="line"> <span class="keyword">int</span> i,top=<span class="number">0</span>,l=<span class="built_in">strlen</span>(s);<span class="comment">//top代表栈顶</span></span><br><span class="line"> <span class="keyword">for</span>(i=<span class="number">0</span>;i<l;i++){</span><br><span class="line"> <span class="keyword">if</span>(s[i]==<span class="string">'('</span>||s[i]==<span class="string">'['</span>||s[i]==<span class="string">'{'</span>){</span><br><span class="line"> c[top]=s[i];</span><br><span class="line"> top++;<span class="comment">//遇到前括号就入栈</span></span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">if</span>(s[i]==<span class="string">')'</span>||s[i]==<span class="string">']'</span>||s[i]==<span class="string">'}'</span>){</span><br><span class="line"> <span class="keyword">if</span>(s[i]==c[top<span class="number">-1</span>]+<span class="number">1</span>||s[i]==c[top<span class="number">-1</span>]+<span class="number">2</span>){<span class="comment">//根据ascll码值找关系(40,41,91,93,123,125)</span></span><br><span class="line"> top--;<span class="comment">//匹配成功就一块出栈</span></span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(i==l&&!top)<span class="comment">//最后栈空就说明全匹配成功了</span></span><br><span class="line"> cout<<<span class="string">"yes"</span><<endl;</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> cout<<<span class="string">"no"</span><<endl;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="H-字典序"><a href="#H-字典序" class="headerlink" title="H - 字典序"></a>H - 字典序</h2><p>给你两个不同的字符串,如果第一个字符串的字典序小于第二个字符串,则输出YES,如果第一个字符串的字典序大于第二个字符串,则输出NO。</p>
<p>Input</p>
<p>两行。第一行一个字符串,第二行一个字符串。保证字符串的长度不超过10000。保证两个字符串不完全相等。</p>
<p>Output</p>
<p>如果第一个字符串的字典序小于第二个字符串,则输出YES,如果第一个字符串的字典序大于第二个字符串,则输出NO。</p>
<p>Sample Input</p>
<figure class="highlight plaintext"><table><tbody><tr><td class="code"><pre><span class="line">abc</span><br><span class="line">abe</span><br></pre></td></tr></tbody></table></figure>
<p>Sample Output</p>
<figure class="highlight plaintext"><table><tbody><tr><td class="code"><pre><span class="line">YES</span><br></pre></td></tr></tbody></table></figure>
<figure class="highlight c"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><stdio.h></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><string.h></span></span></span><br><span class="line"><span class="keyword">char</span> a[<span class="number">10010</span>],b[<span class="number">10010</span>];</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%s%s"</span>,a,b);</span><br><span class="line"> <span class="keyword">if</span>(<span class="built_in">strcmp</span>(a,b)><span class="number">0</span>)</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"NO"</span>);</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"YES"</span>);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="I-验证子串"><a href="#I-验证子串" class="headerlink" title="I - 验证子串"></a>I - 验证子串</h2><p>输入两个字符串,验证其中一个串是否为另一个串的子串。</p>
<h4 id="输入格式-5"><a href="#输入格式-5" class="headerlink" title="输入格式"></a>输入格式</h4><p>输入两个字符串, 每个字符串占一行,长度不超过 200 且不含空格。</p>
<h4 id="输出格式-5"><a href="#输出格式-5" class="headerlink" title="输出格式"></a>输出格式</h4><p>若第一个串 <em>s</em>1 是第二个串 <em>s</em>2 的子串,则输出<code>"(s1) is substring of (s2)"</code>;</p>
<p>否则,若第二个串 s2是第一个串s1的子串,输出<code>"(s2) is substring of (s1)"</code>;</p>
<p>否则,输出<code>"No substring"</code>。</p>
<p>Sample Input</p>
<figure class="highlight plaintext"><table><tbody><tr><td class="code"><pre><span class="line">abc</span><br><span class="line">dddncabca</span><br></pre></td></tr></tbody></table></figure>
<p>Sample Output</p>
<figure class="highlight plaintext"><table><tbody><tr><td class="code"><pre><span class="line">abc is substring of dddncabca</span><br></pre></td></tr></tbody></table></figure>
<figure class="highlight c"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><stdio.h></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><string.h></span></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> l1,l2;</span><br><span class="line"> <span class="keyword">char</span> s1[<span class="number">210</span>],s2[<span class="number">210</span>];</span><br><span class="line"> gets(s1),gets(s2);</span><br><span class="line"> l1=<span class="built_in">strlen</span>(s1),l2=<span class="built_in">strlen</span>(s2);</span><br><span class="line"> <span class="keyword">if</span>(l1<l2&&<span class="built_in">strstr</span>(s2,s1))</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%s is substring of %s"</span>,s1,s2);</span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">if</span>(l1>l2&&<span class="built_in">strstr</span>(s1,s2))</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%s is substring of %s"</span>,s2,s1);</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"No substring"</span>);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="J-子串查找"><a href="#J-子串查找" class="headerlink" title="J - 子串查找"></a>J - 子串查找</h2><h4 id="题目描述"><a href="#题目描述" class="headerlink" title="题目描述"></a>题目描述</h4><p>这是一道模板题。</p>
<p>给定一个字符串 A 和一个字符串 <em>B</em>,求 B 在 A 中的出现次数。<em>A</em> 和 <em>B</em> 中的字符均为英语大写字母或小写字母。</p>
<p><em>A</em> 中不同位置出现的 <em>B</em> 可重叠。</p>
<h4 id="输入格式-6"><a href="#输入格式-6" class="headerlink" title="输入格式"></a>输入格式</h4><p>输入共两行,分别是字符串 A 和字符串 B。</p>
<h4 id="输出格式-6"><a href="#输出格式-6" class="headerlink" title="输出格式"></a>输出格式</h4><p>输出一个整数,表示 B 在 A 中的出现次数。</p>
<h4 id="样例"><a href="#样例" class="headerlink" title="样例"></a>样例</h4><div class="table-container">
<table>
<thead>
<tr>
<th>Input</th>
<th>Output</th>
</tr>
</thead>
<tbody>
<tr>
<td>zyzyzyz zyz</td>
<td>3</td>
</tr>
</tbody>
</table>
</div>
<h4 id="数据范围与提示"><a href="#数据范围与提示" class="headerlink" title="数据范围与提示"></a>数据范围与提示</h4><p>1 <= A, B的长度 <=10^6,A、B 仅包含大小写字母。</p>
<h3 id="kmp"><a href="#kmp" class="headerlink" title="kmp"></a>kmp</h3><figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> M 1000000</span></span><br><span class="line"><span class="keyword">char</span> s[M],p[M];</span><br><span class="line"><span class="keyword">int</span> nxt[M];</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">kmp</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="keyword">int</span> len=<span class="built_in">strlen</span>(p);</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>,j=<span class="number">0</span>;p[i];i++){</span><br><span class="line"> <span class="keyword">while</span>(j&&p[j]!=p[i])</span><br><span class="line"> j=nxt[j];</span><br><span class="line"> nxt[i+<span class="number">1</span>]=p[j]==p[i]?++j:<span class="number">0</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">int</span> tot=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>,j=<span class="number">0</span>;s[i];i++){</span><br><span class="line"> <span class="keyword">while</span>(j&&p[j]!=s[i])</span><br><span class="line"> j=nxt[j];</span><br><span class="line"> <span class="keyword">if</span> (p[j]==s[i]&&++j==len){</span><br><span class="line"> tot++;</span><br><span class="line"> j=nxt[j];</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d"</span>,tot);</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%s %s"</span>,s,p);</span><br><span class="line"> <span class="built_in">kmp</span>();</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="K-剪花布条"><a href="#K-剪花布条" class="headerlink" title="K - 剪花布条"></a>K - 剪花布条</h2><h4 id="题目描述-1"><a href="#题目描述-1" class="headerlink" title="题目描述"></a>题目描述</h4><p>一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案。对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢?</p>
<h4 id="输入格式-7"><a href="#输入格式-7" class="headerlink" title="输入格式"></a>输入格式</h4><p>输入数据为多组数据,读取到 <code>#</code> 字符时结束。每组数据仅有一行,为由空格分开的花布条和小饰条。花布条和小饰条都是用可见 ASCII 字符表示的,不会超过 1000 个字符。</p>
<p><strong>注意:这个 <code>#</code> 应为单个字符。若某字符串开头有 <code>#</code>,不意味着读入结束!</strong></p>
<h4 id="输出格式-7"><a href="#输出格式-7" class="headerlink" title="输出格式"></a>输出格式</h4><p>对于每组数据,输出一行一个整数,表示能从花纹布中剪出的最多小饰条个数。</p>
<h4 id="样例-1"><a href="#样例-1" class="headerlink" title="样例"></a>样例</h4><div class="table-container">
<table>
<thead>
<tr>
<th>Input</th>
<th>Output</th>
</tr>
</thead>
<tbody>
<tr>
<td>abcde a3 aaaaaa aa #</td>
<td>0 3</td>
</tr>
</tbody>
</table>
</div>
<h4 id="数据范围与提示-1"><a href="#数据范围与提示-1" class="headerlink" title="数据范围与提示"></a>数据范围与提示</h4><p>对于全部数据,字符串长度 ≤1000。</p>
<h3 id="1-用string代替char"><a href="#1-用string代替char" class="headerlink" title="1.用string代替char"></a>1.用string代替char</h3><figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> string s,t;</span><br><span class="line"> <span class="keyword">while</span>(cin>>s){</span><br><span class="line"> <span class="keyword">if</span>(s[<span class="number">0</span>]==<span class="string">'#'</span>&&s[<span class="number">1</span>]==<span class="string">'\0'</span>)</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> cin>>t;</span><br><span class="line"> <span class="keyword">int</span> cnt=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i<s.<span class="built_in">size</span>();i++){</span><br><span class="line"> <span class="keyword">int</span> j=<span class="number">0</span>,k=i;</span><br><span class="line"> <span class="keyword">while</span>(s[k]==t[j]&&k<s.<span class="built_in">size</span>()){</span><br><span class="line"> ++k;++j;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(j==t.<span class="built_in">size</span>()){</span><br><span class="line"> cnt++;</span><br><span class="line"> i=k<span class="number">-1</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> cout<<cnt<<endl;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h3 id="2-STL里的find函数"><a href="#2-STL里的find函数" class="headerlink" title="2.STL里的find函数"></a>2.STL里的find函数</h3><figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> string s,t;</span><br><span class="line"> <span class="keyword">while</span>(cin>>s&&!(s[<span class="number">0</span>]==<span class="string">'#'</span>&&s[<span class="number">1</span>]==<span class="string">'\0'</span>)){</span><br><span class="line"> cin>>t;</span><br><span class="line"> <span class="keyword">int</span> cnt=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span> (s.<span class="built_in">find</span>(t)!=string::npos){ <span class="comment">//s.find(t)找不到会返回string::npos</span></span><br><span class="line"> cnt++;</span><br><span class="line"> s.<span class="built_in">erase</span>(s.<span class="built_in">find</span>(t),t.<span class="built_in">size</span>()); <span class="comment">//s.erase将已经找到的花布条剪掉</span></span><br><span class="line"> }</span><br><span class="line"> cout<<cnt<<endl;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="L-最长回文子串"><a href="#L-最长回文子串" class="headerlink" title="L - 最长回文子串"></a>L - 最长回文子串</h2><p>输入一个字符串Str,输出Str里最长回文子串的长度。</p>
<p>回文串:指aba、abba、cccbccc、aaaa这种左右对称的字符串。</p>
<p>串的子串:一个串的子串指此(字符)串中<strong>连续</strong>的一部分字符构成的子(字符)串<br>例如 abc 这个串的子串:空串、a、b、c、ab、bc、abc</p>
<h4 id="输入格式-8"><a href="#输入格式-8" class="headerlink" title="输入格式"></a>输入格式</h4><p>输入Str(Str的长度 <= 1000)</p>
<h4 id="输出格式-8"><a href="#输出格式-8" class="headerlink" title="输出格式"></a>输出格式</h4><p>输出最长回文子串的长度L。</p>
<p>Sample Input</p>
<figure class="highlight plaintext"><table><tbody><tr><td class="code"><pre><span class="line">daabaac</span><br></pre></td></tr></tbody></table></figure>
<p>Sample Output</p>
<figure class="highlight plaintext"><table><tbody><tr><td class="code"><pre><span class="line">5</span><br></pre></td></tr></tbody></table></figure>
<h3 id="中点扩散"><a href="#中点扩散" class="headerlink" title="中点扩散"></a>中点扩散</h3><p>O(n^2),为了代码简洁好看些doge,O(n)还是用哈希吧</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> string str;</span><br><span class="line"> <span class="built_in">getline</span>(cin,str);</span><br><span class="line"> <span class="keyword">int</span> res=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i<str.<span class="built_in">length</span>();i++){</span><br><span class="line"> <span class="keyword">int</span> l=i<span class="number">-1</span>,r=i+<span class="number">1</span>;<span class="comment">//这里要考虑到回文子串长度可能是偶数或奇数</span></span><br><span class="line"> <span class="keyword">while</span>(l>=<span class="number">0</span>&&r<str.<span class="built_in">length</span>()&&str[l]==str[r]){</span><br><span class="line"> l--;</span><br><span class="line"> r++;</span><br><span class="line"> }</span><br><span class="line"> res=<span class="built_in">max</span>(res,r-l<span class="number">-1</span>);</span><br><span class="line"> l=i,r=i+<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">while</span>(l>=<span class="number">0</span>&&r<str.<span class="built_in">length</span>()&&str[l]==str[r]){</span><br><span class="line"> l--;</span><br><span class="line"> r++;</span><br><span class="line"> }</span><br><span class="line"> res=<span class="built_in">max</span>(res,r-l<span class="number">-1</span>);</span><br><span class="line"> }</span><br><span class="line"> cout<<res<<endl;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
]]></content>
<tags>
<tag>contest</tag>
</tags>
</entry>
<entry>
<title>寒假冬训营搜索专题题解</title>
<url>/2022/01/13/27/</url>
<content><![CDATA[<h1 id="搜索专题第22组题解"><a href="#搜索专题第22组题解" class="headerlink" title="搜索专题第22组题解"></a>搜索专题第22组题解</h1><p>写题之前你至少要搞懂<strong>深搜,广搜</strong>的原理,<strong>队列</strong>(先进先出)和<strong>栈</strong>(后进先出)的特点</p>
<p>深搜用堆栈存储,深搜的路径像面条一样,牺牲时间换空间,适用于求<strong>全部解</strong>的题等</p>
<p>广搜用队列存储,广搜的路径像水波一样,是往四周蔓延的,牺牲空间换时间,适用于求<strong>最短解</strong>的题等</p>
<h2 id="A-棋盘问题"><a href="#A-棋盘问题" class="headerlink" title="A - 棋盘问题"></a><a href="https://vjudge.csgrandeur.cn/contest/476110#problem/A">A - 棋盘问题</a></h2><font color="green">建议先看全排列</font>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">int</span> vis[<span class="number">30</span>],n,k,sum,m;</span><br><span class="line"><span class="keyword">char</span> a[<span class="number">30</span>][<span class="number">30</span>];</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">dfs</span><span class="params">(<span class="keyword">int</span> p)</span></span>{</span><br><span class="line"> <span class="keyword">if</span>(k==m){</span><br><span class="line"> sum++;</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(p>=n)</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">0</span>;j<n;j++){</span><br><span class="line"> <span class="keyword">if</span>(vis[j]==<span class="number">0</span>&&a[p][j]==<span class="string">'#'</span>){</span><br><span class="line"> vis[j]=<span class="number">1</span>;<span class="comment">//依旧先标记后复原</span></span><br><span class="line"> m++;</span><br><span class="line"> <span class="built_in">dfs</span>(p+<span class="number">1</span>);</span><br><span class="line"> vis[j]=<span class="number">0</span>;</span><br><span class="line"> m--;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">dfs</span>(p+<span class="number">1</span>);</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">while</span>(<span class="built_in">scanf</span>(<span class="string">"%d%d"</span>,&n,&k)&&n!=<span class="number">-1</span>&&k!=<span class="number">-1</span>){</span><br><span class="line"> sum=<span class="number">0</span>;</span><br><span class="line"> m=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i<n;i++)</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%s"</span>,&a[i]);</span><br><span class="line"> <span class="built_in">dfs</span>(<span class="number">0</span>);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>,sum);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="B-Perket"><a href="#B-Perket" class="headerlink" title="B - Perket"></a><a href="https://vjudge.csgrandeur.cn/contest/476110#problem/B">B - Perket</a></h2><figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">int</span> flag[<span class="number">11</span>]={<span class="number">0</span>};</span><br><span class="line"><span class="keyword">int</span> a[<span class="number">11</span>],b[<span class="number">11</span>];</span><br><span class="line"><span class="keyword">int</span> c=<span class="number">1</span>,y=<span class="number">0</span>;</span><br><span class="line"><span class="keyword">int</span> N;</span><br><span class="line"><span class="keyword">int</span> ans=<span class="number">1e11</span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">dfs</span><span class="params">(<span class="keyword">int</span> x)</span></span>{</span><br><span class="line"> <span class="keyword">if</span>(x>=N) </span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> <span class="keyword">else</span>{</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i<N;i++){</span><br><span class="line"> <span class="keyword">if</span>(flag[i]==<span class="number">0</span>){</span><br><span class="line"> c*=a[i];<span class="comment">//依旧先标记后复原</span></span><br><span class="line"> y+=b[i];</span><br><span class="line"> flag[i]=<span class="number">1</span>;</span><br><span class="line"> ans=<span class="built_in">min</span>(ans,<span class="built_in">abs</span>(c-y));</span><br><span class="line"> <span class="built_in">dfs</span>(i+<span class="number">1</span>);</span><br><span class="line"> c/=a[i];</span><br><span class="line"> y-=b[i];</span><br><span class="line"> flag[i]=<span class="number">0</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>{</span><br><span class="line"> cin>>N;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i<N;i++){</span><br><span class="line"> cin>>a[i]>>b[i];</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">dfs</span>(<span class="number">0</span>);</span><br><span class="line"> cout<<ans;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="C-全排列"><a href="#C-全排列" class="headerlink" title="C - 全排列"></a><a href="https://vjudge.csgrandeur.cn/contest/476110#problem/C">C - 全排列</a></h2><p>这里是字符全排列,不过和数字全排列没什么区别,用深搜遍历出每一种可能就行了,如果想省事,用c++里的next_permutation()函数就行了。</p>
<font color="blue">像这种经典题如果是第一次见,搞懂然后记住就完了</font>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">long</span> ll;</span><br><span class="line"><span class="keyword">char</span> a[<span class="number">25</span>],b[<span class="number">25</span>];<span class="comment">//用a数组读入字符串,b数组记下路径</span></span><br><span class="line"><span class="keyword">int</span> n,vis[<span class="number">25</span>];<span class="comment">//标记数组,搜索过的数就标记好,防止重复</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">dfs</span><span class="params">(<span class="keyword">int</span> p)</span></span>{</span><br><span class="line"> <span class="keyword">if</span>(p==n){</span><br><span class="line"> b[n]=<span class="string">'\0'</span>;</span><br><span class="line"> cout<<b<<endl;<span class="comment">//可以这样输出字符串</span></span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i<n;i++)<span class="comment">//如果函数调用到第m层,for循环实际上也在dfs前进入到了第m轮,那剩下的(n-m)轮呢,就是用来产生分支的</span></span><br><span class="line"> <span class="keyword">if</span>(!vis[i]){</span><br><span class="line"> vis[i]=<span class="number">1</span>;<span class="comment">//dfs前做好标记(记住)</span></span><br><span class="line"> b[p]=a[i];</span><br><span class="line"> <span class="built_in">dfs</span>(p+<span class="number">1</span>);</span><br><span class="line"> vis[i]=<span class="number">0</span>;<span class="comment">//dfs后删除标记,不同排列有相同数字,走的时候要取消标记(记住)</span></span><br><span class="line"> }</span><br><span class="line">} </span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%s"</span>,a);</span><br><span class="line"> n=<span class="built_in">strlen</span>(a);</span><br><span class="line"> <span class="built_in">dfs</span>(<span class="number">0</span>);<span class="comment">//习惯上可以从0/1开始,和for循环里i的初值保持一致</span></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="D-自然数拆分"><a href="#D-自然数拆分" class="headerlink" title="D - 自然数拆分"></a><a href="https://vjudge.csgrandeur.cn/contest/476110#problem/D">D - 自然数拆分</a></h2><figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">int</span> a[<span class="number">41</span>],s,m;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">pr</span><span class="params">(<span class="keyword">int</span> s)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> cout<<m<<<span class="string">'='</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=s<span class="number">-2</span>;i++)</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d+"</span>,a[i]);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>,a[s<span class="number">-1</span>]);</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">dfs</span><span class="params">(<span class="keyword">int</span> n,<span class="keyword">int</span> s,<span class="keyword">int</span> pre)</span><span class="comment">//pre确保要减的数是递增的</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">if</span>(n==<span class="number">0</span>&&s><span class="number">1</span>){<span class="comment">//结束条件:进行至少一轮并且减完为零</span></span><br><span class="line"> <span class="built_in">pr</span>(s);</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=pre;i<=n;i++){</span><br><span class="line"> a[s]=i;</span><br><span class="line"> <span class="built_in">dfs</span>(n-i,s+<span class="number">1</span>,i);</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> cin>>m;</span><br><span class="line"> <span class="built_in">dfs</span>(m,<span class="number">1</span>,<span class="number">1</span>);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="E-Prime-Ring-Problem"><a href="#E-Prime-Ring-Problem" class="headerlink" title="E - Prime Ring Problem"></a><a href="https://vjudge.csgrandeur.cn/contest/476110#problem/E">E - Prime Ring Problem</a></h2><figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">int</span> n,a[<span class="number">17</span>],id=<span class="number">0</span>;</span><br><span class="line"><span class="keyword">bool</span> vis[<span class="number">17</span>];</span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">prime</span><span class="params">(<span class="keyword">int</span> n)</span></span>{</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">2</span>;i<= <span class="built_in">sqrt</span>(n);i++){</span><br><span class="line"> <span class="keyword">if</span>(n % i == <span class="number">0</span>) </span><br><span class="line"> <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">dfs</span><span class="params">(<span class="keyword">int</span> x)</span></span>{</span><br><span class="line"> <span class="keyword">if</span> (x>n&&<span class="built_in">prime</span>(a[x<span class="number">-1</span>]+a[<span class="number">1</span>])){<span class="comment">//要判断第一个数和最后一个数的和是否为素数</span></span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=n;i++){</span><br><span class="line"> cout<<a[i];</span><br><span class="line"> <span class="keyword">if</span> (i!=n)</span><br><span class="line"> cout<<<span class="string">" "</span>;</span><br><span class="line"> }</span><br><span class="line"> cout<<endl;</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=n;i++){</span><br><span class="line"> <span class="keyword">if</span>(<span class="built_in">prime</span>(i+a[x<span class="number">-1</span>])&&!vis[i]){</span><br><span class="line"> vis[i]=<span class="literal">true</span>;</span><br><span class="line"> a[x]=i;</span><br><span class="line"> <span class="built_in">dfs</span>(x+<span class="number">1</span>);</span><br><span class="line"> vis[i]=<span class="literal">false</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> t=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span>(cin>>n){</span><br><span class="line"> t++;</span><br><span class="line"> <span class="keyword">if</span>(t>=<span class="number">2</span>)</span><br><span class="line"> cout<<endl;</span><br><span class="line"> cout<<<span class="string">"Case "</span><<++id<<<span class="string">":\n"</span>;</span><br><span class="line"> <span class="built_in">memset</span>(vis,<span class="literal">false</span>,<span class="built_in"><span class="keyword">sizeof</span></span>(vis));</span><br><span class="line"> a[<span class="number">1</span>]=<span class="number">1</span>;</span><br><span class="line"> vis[<span class="number">1</span>]=<span class="literal">true</span>;</span><br><span class="line"> <span class="built_in">dfs</span>(<span class="number">2</span>);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="F-Red-and-Black"><a href="#F-Red-and-Black" class="headerlink" title="F - Red and Black"></a><a href="https://vjudge.csgrandeur.cn/contest/476110#problem/F">F - Red and Black</a></h2><figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">int</span> W,H;</span><br><span class="line"><span class="keyword">char</span> a[<span class="number">21</span>][<span class="number">21</span>];</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">f</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> y)</span></span>{</span><br><span class="line"> <span class="keyword">if</span> (x<<span class="number">0</span>||x>=W||y<<span class="number">0</span>||y>=H)</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">if</span> (a[x][y]==<span class="string">'#'</span>){</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"> }<span class="keyword">else</span>{</span><br><span class="line"> a[x][y]=<span class="string">'#'</span>;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">1</span> + <span class="built_in">f</span>(x<span class="number">-1</span>,y)+<span class="built_in">f</span>(x+<span class="number">1</span>,y)+<span class="built_in">f</span>(x,y<span class="number">-1</span>)+<span class="built_in">f</span>(x,y+<span class="number">1</span>);<span class="comment">//求所以可走的路径,将和相加</span></span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> i,j,num;</span><br><span class="line"> <span class="keyword">while</span>(<span class="built_in">scanf</span>(<span class="string">"%d%d"</span>,&H,&W)&&W!=<span class="number">0</span>&&H!=<span class="number">0</span>){</span><br><span class="line"> <span class="keyword">for</span>(i=<span class="number">0</span>;i<W;i++)</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%s"</span>,a[i]);</span><br><span class="line"> <span class="keyword">for</span>(i=<span class="number">0</span>;i<W;i++)</span><br><span class="line"> <span class="keyword">for</span>(j=<span class="number">0</span>;j<H;j++)</span><br><span class="line"> <span class="keyword">if</span>(a[i][j]==<span class="string">'@'</span>) </span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>,<span class="built_in">f</span>(i, j));</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="G-Knight-Moves"><a href="#G-Knight-Moves" class="headerlink" title="G - Knight Moves"></a><a href="https://vjudge.csgrandeur.cn/contest/476110#problem/G">G - Knight Moves</a></h2><p><strong>提醒一点,万能头虽好用,但里面有很多关键字可能会和你的变量名冲突,比如list,visit</strong></p>
<p>这道题应该用广搜,不知道c++队列操作的自行了解一下</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> len=<span class="number">302</span>;</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">horse</span>{</span></span><br><span class="line"> <span class="keyword">int</span> x;</span><br><span class="line"> <span class="keyword">int</span> y;</span><br><span class="line"> <span class="keyword">int</span> step;</span><br><span class="line">};</span><br><span class="line"><span class="keyword">bool</span> Visit[len][len];</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> way[<span class="number">8</span>][<span class="number">2</span>]={{<span class="number">-2</span>,<span class="number">-1</span>},{<span class="number">-1</span>,<span class="number">-2</span>},{<span class="number">1</span>,<span class="number">-2</span>},{<span class="number">2</span>,<span class="number">-1</span>},{<span class="number">2</span>,<span class="number">1</span>},{<span class="number">1</span>,<span class="number">2</span>},{<span class="number">-1</span>,<span class="number">2</span>},{<span class="number">-2</span>,<span class="number">1</span>}};</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">bfs</span><span class="params">(<span class="keyword">int</span> s1,<span class="keyword">int</span> e1,<span class="keyword">int</span> s2,<span class="keyword">int</span> e2,<span class="keyword">int</span> lenth)</span></span>{</span><br><span class="line"> queue<horse> q;</span><br><span class="line"> horse start,now,next;</span><br><span class="line"> start.x=s1;</span><br><span class="line"> start.y=e1;</span><br><span class="line"> start.step=<span class="number">0</span>;</span><br><span class="line"> Visit[start.x][start.y]=<span class="literal">true</span>;</span><br><span class="line"> q.<span class="built_in">push</span>(start);</span><br><span class="line"> <span class="keyword">while</span>(!q.<span class="built_in">empty</span>()){</span><br><span class="line"> now=q.<span class="built_in">front</span>();</span><br><span class="line"> q.<span class="built_in">pop</span>();</span><br><span class="line"> <span class="keyword">if</span>(now.x==s2&&now.y==e2){</span><br><span class="line"> cout<<now.step<<endl;</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i<<span class="number">8</span>;i++){</span><br><span class="line"> next.x=now.x+way[i][<span class="number">0</span>];</span><br><span class="line"> next.y=now.y+way[i][<span class="number">1</span>];</span><br><span class="line"> <span class="keyword">if</span>(next.x>=<span class="number">0</span>&&next.x<lenth&&next.y>=<span class="number">0</span>&&next.y<lenth&&!Visit[next.x][next.y]){</span><br><span class="line"> next.step=now.step+<span class="number">1</span>;</span><br><span class="line"> q.<span class="built_in">push</span>(next);</span><br><span class="line"> Visit[next.x][next.y]=<span class="literal">true</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> n;</span><br><span class="line"> cin>>n;</span><br><span class="line"> <span class="keyword">int</span> l;</span><br><span class="line"> <span class="keyword">int</span> x1,x2,y1,y2;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i<n;i++){</span><br><span class="line"> cin>>l;</span><br><span class="line"> cin>>x1>>y1;</span><br><span class="line"> cin>>x2>>y2;</span><br><span class="line"> <span class="built_in">memset</span>(Visit,<span class="literal">false</span>,<span class="built_in"><span class="keyword">sizeof</span></span>(Visit));</span><br><span class="line"> <span class="built_in">bfs</span>(x1,y1,x2,y2,l);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="H-Oil-Deposits"><a href="#H-Oil-Deposits" class="headerlink" title="H - Oil Deposits"></a><a href="https://vjudge.csgrandeur.cn/contest/476110#problem/H">H - Oil Deposits</a></h2><p>这个题就是求矩阵中有多少个“分开”的区域,还可以变形为求最大面积等等</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">char</span> a[<span class="number">1000</span>][<span class="number">1000</span>];</span><br><span class="line"><span class="keyword">int</span> n,m;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">dfs</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> y)</span></span>{</span><br><span class="line"> <span class="keyword">int</span> i,j,b,c;</span><br><span class="line"> <span class="keyword">for</span>(i=<span class="number">-1</span>;i<=<span class="number">1</span>;i++){<span class="comment">//这个写法爱了,这就是用循环表示8个方向</span></span><br><span class="line"> <span class="keyword">for</span>(j=<span class="number">-1</span>;j<=<span class="number">1</span>;j++){</span><br><span class="line"> b=i+x;</span><br><span class="line"> c=y+j;</span><br><span class="line"> <span class="keyword">if</span>(b>=<span class="number">0</span>&&b<n&&y>=<span class="number">0</span>&&y<m&&a[b][c]==<span class="string">'@'</span>){<span class="comment">//如果遇到@就开始搜索并且把@变为*</span></span><br><span class="line"> a[b][c]=<span class="string">'*'</span>;<span class="comment">//这也算是一种标记</span></span><br><span class="line"> <span class="built_in">dfs</span>(b,c);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> i,j,cnt=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span>(<span class="built_in">scanf</span>(<span class="string">"%d %d"</span>,&n,&m)!=EOF&&n!=<span class="number">0</span>&&m!=<span class="number">0</span>){</span><br><span class="line"> <span class="built_in">memset</span>(a,<span class="string">'0'</span>,<span class="built_in"><span class="keyword">sizeof</span></span>(a));<span class="comment">//学会用memset初始化数组,因为是字符数组,全初始化为字符0,防止上一轮残留数据的干扰</span></span><br><span class="line"> cnt=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(i=<span class="number">0</span>;i<=n<span class="number">-1</span>;i++)</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%s"</span>,a[i]);</span><br><span class="line"> <span class="keyword">for</span>(i=<span class="number">0</span>;i<=n<span class="number">-1</span>;i++){</span><br><span class="line"> <span class="keyword">for</span>(j=<span class="number">0</span>;j<=m<span class="number">-1</span>;j++){</span><br><span class="line"> <span class="keyword">if</span>(a[i][j]==<span class="string">'@'</span>){</span><br><span class="line"> cnt++;<span class="comment">//遇到油层就要计数</span></span><br><span class="line"> <span class="built_in">dfs</span>(i,j);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>,cnt);</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="I-Lake-Counting"><a href="#I-Lake-Counting" class="headerlink" title="I - Lake Counting"></a><a href="https://vjudge.csgrandeur.cn/contest/476110#problem/I">I - Lake Counting</a></h2><p>跟上一题一模一样,就是把@换成了w,*换成了.,并且无输入结束标志。</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">char</span> a[<span class="number">1000</span>][<span class="number">1000</span>];</span><br><span class="line"><span class="keyword">int</span> n,m;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">dfs</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> y)</span></span>{</span><br><span class="line"> <span class="keyword">int</span> i,j,b,c;</span><br><span class="line"> <span class="keyword">for</span>(i=<span class="number">-1</span>;i<=<span class="number">1</span>;i++){</span><br><span class="line"> <span class="keyword">for</span>(j=<span class="number">-1</span>;j<=<span class="number">1</span>;j++){</span><br><span class="line"> b=i+x;</span><br><span class="line"> c=y+j;</span><br><span class="line"> <span class="keyword">if</span>(b>=<span class="number">0</span>&&b<n&&y>=<span class="number">0</span>&&y<m&&a[b][c]==<span class="string">'W'</span>){</span><br><span class="line"> a[b][c]=<span class="string">'.'</span>;</span><br><span class="line"> <span class="built_in">dfs</span>(b,c);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> i,j,cnt=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span>(<span class="built_in">scanf</span>(<span class="string">"%d %d"</span>,&n,&m)!=EOF){</span><br><span class="line"> <span class="built_in">memset</span>(a,<span class="string">'0'</span>,<span class="built_in"><span class="keyword">sizeof</span></span>(a));</span><br><span class="line"> cnt=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(i=<span class="number">0</span>;i<=n<span class="number">-1</span>;i++)</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%s"</span>,a[i]);</span><br><span class="line"> <span class="keyword">for</span>(i=<span class="number">0</span>;i<=n<span class="number">-1</span>;i++){</span><br><span class="line"> <span class="keyword">for</span>(j=<span class="number">0</span>;j<=m<span class="number">-1</span>;j++){</span><br><span class="line"> <span class="keyword">if</span>(a[i][j]==<span class="string">'W'</span>){</span><br><span class="line"> cnt++;</span><br><span class="line"> <span class="built_in">dfs</span>(i,j);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>,cnt);</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="J-二叉树先序遍历"><a href="#J-二叉树先序遍历" class="headerlink" title="J - 二叉树先序遍历"></a><a href="https://vjudge.csgrandeur.cn/contest/476110#problem/J">J - 二叉树先序遍历</a></h2><p>不知道什么是二叉数和先序遍历的先去查查,已经在计导书里学过了,思路很简单,定义一个包含左右节点的结构体,如果树非空,就做三个操作:</p>
<p>1:<strong>输出节点;</strong></p>
<p>2:<strong>先遍历左子树;</strong></p>
<p>3:<strong>之后遍历右子树,如果为空就返回上一层;</strong></p>
<font color="blue">像这种经典题如果是第一次见,搞懂然后记住就完了</font>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">p</span>{</span></span><br><span class="line"> <span class="keyword">int</span> l;</span><br><span class="line"> <span class="keyword">int</span> r;</span><br><span class="line">}t[<span class="number">100010</span>];</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">dfs</span><span class="params">(<span class="keyword">int</span> x)</span></span>{</span><br><span class="line"> <span class="keyword">if</span>(x==<span class="number">0</span>)</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> cout<<x<<endl;<span class="comment">//第1步</span></span><br><span class="line"> <span class="built_in">dfs</span>(t[x].l);<span class="comment">//第2步</span></span><br><span class="line"> <span class="built_in">dfs</span>(t[x].r);<span class="comment">//第3步</span></span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> n;</span><br><span class="line"> cin>>n;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=n;i++)</span><br><span class="line"> cin>>t[i].l>>t[i].r;<span class="comment">//依次读入左右节点</span></span><br><span class="line"> <span class="built_in">dfs</span>(<span class="number">1</span>);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<p>之后在学数据结构的时候还有中序遍历、后序遍历,实现思路差不多</p>
<h2 id="K-迷宫(一)"><a href="#K-迷宫(一)" class="headerlink" title="K - 迷宫(一)"></a><a href="https://vjudge.csgrandeur.cn/contest/476110#problem/K">K - 迷宫(一)</a></h2><p>学会用数组表示方向,不一定要二维数组,两个一维的也行,甚至可以用循环表示</p>
<font color="blue">像这种经典题如果是第一次见,搞懂然后记住就完了</font>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">int</span> n,m;</span><br><span class="line"><span class="keyword">bool</span> f;</span><br><span class="line"><span class="keyword">char</span> mp[<span class="number">15</span>][<span class="number">15</span>];</span><br><span class="line"><span class="keyword">int</span> vis[<span class="number">15</span>][<span class="number">15</span>];</span><br><span class="line"><span class="keyword">int</span> dir[<span class="number">4</span>][<span class="number">2</span>] = {{<span class="number">-1</span>,<span class="number">0</span>},{<span class="number">0</span>,<span class="number">1</span>},{<span class="number">1</span>,<span class="number">0</span>},{<span class="number">0</span>,<span class="number">-1</span>}};<span class="comment">//定义一个二维数组存方向,比如说(x-1,y+0)就是向上一个单位,使用方便</span></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">in</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> y)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span><=x&&x<n&&<span class="number">0</span><=y&&y<m;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">dfs</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> y)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">if</span> (mp[x][y]==<span class="string">'T'</span>){<span class="comment">//结束标志</span></span><br><span class="line"> f=<span class="literal">true</span>;</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span> (!<span class="built_in">in</span>(x,y)||mp[x][y]==<span class="string">'*'</span>||vis[x][y]){</span><br><span class="line"> <span class="keyword">return</span>;<span class="comment">//看看有没有越界,撞墙,走过</span></span><br><span class="line"> }</span><br><span class="line"> vis[x][y]=<span class="number">1</span>;<span class="comment">//dfs前做好标记,不能再走以前的路,所以不用再消除标记</span></span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < <span class="number">4</span>; i++){</span><br><span class="line"> <span class="keyword">int</span> tx=x+dir[i][<span class="number">0</span>];<span class="comment">//移动坐标</span></span><br><span class="line"> <span class="keyword">int</span> ty=y+dir[i][<span class="number">1</span>];</span><br><span class="line"> <span class="built_in">dfs</span>(tx,ty);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d%d"</span>,&n,&m);</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i<n;i++){</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%s"</span>,mp[i]);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">int</span> x,y;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i<n;i++){</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">0</span>;j<m;j++){</span><br><span class="line"> <span class="keyword">if</span>(mp[i][j]==<span class="string">'S'</span>){</span><br><span class="line"> x=i;<span class="comment">//先找到开始搜索的点</span></span><br><span class="line"> y=j;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">dfs</span>(x,y);</span><br><span class="line"> <span class="keyword">if</span>(f){</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"yes\n"</span>);</span><br><span class="line"> }<span class="keyword">else</span>{</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"no\n"</span>);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="L-马走日"><a href="#L-马走日" class="headerlink" title="L - 马走日"></a><a href="https://vjudge.csgrandeur.cn/contest/476110#problem/L">L - 马走日</a></h2><figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">int</span> f[<span class="number">21</span>][<span class="number">22</span>],n,m,t;</span><br><span class="line"><span class="keyword">int</span> xx[<span class="number">10</span>]={<span class="number">-2</span>,<span class="number">-2</span>,<span class="number">-1</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">2</span>,<span class="number">1</span>,<span class="number">-1</span>};<span class="comment">//用两个数组表示方向,也就是坐标变化</span></span><br><span class="line"><span class="keyword">int</span> yy[<span class="number">10</span>]={<span class="number">-1</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">2</span>,<span class="number">1</span>,<span class="number">-1</span>,<span class="number">-2</span>,<span class="number">-2</span>};</span><br><span class="line"><span class="keyword">int</span> x,y,ans;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">dfs</span><span class="params">(<span class="keyword">int</span> a,<span class="keyword">int</span> b)</span></span>{</span><br><span class="line"> <span class="keyword">bool</span> p=<span class="literal">true</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=n;i++){<span class="comment">//判断是不是走过了所有点</span></span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">1</span>;j<=m;j++){</span><br><span class="line"> <span class="keyword">if</span>(f[i][j]==<span class="number">0</span>){</span><br><span class="line"> p=<span class="literal">false</span>;</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(p==<span class="literal">true</span>){</span><br><span class="line"> ans++;</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i<=<span class="number">7</span>;i++){</span><br><span class="line"> <span class="keyword">int</span> l=a+xx[i];</span><br><span class="line"> <span class="keyword">int</span> k=b+yy[i];</span><br><span class="line"> <span class="keyword">if</span>(f[l][k]==<span class="number">1</span>)</span><br><span class="line"> <span class="keyword">continue</span>;</span><br><span class="line"> <span class="keyword">if</span>(l><span class="number">0</span>&&l<=n&&k><span class="number">0</span>&&k<=m){ </span><br><span class="line"> f[l][k]=<span class="number">1</span>;</span><br><span class="line"> <span class="built_in">dfs</span>(l,k);</span><br><span class="line"> f[l][k]=<span class="number">0</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>,&t);</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=t;i++){</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d %d %d %d"</span>,&n,&m,&x,&y);</span><br><span class="line"> x++;</span><br><span class="line"> y++;</span><br><span class="line"> <span class="built_in">memset</span>(f,<span class="number">0</span>,<span class="built_in"><span class="keyword">sizeof</span></span>(f));</span><br><span class="line"> f[x][y]=<span class="number">1</span>;</span><br><span class="line"> ans=<span class="number">0</span>;</span><br><span class="line"> <span class="built_in">dfs</span>(x,y);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>,ans);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="M-八皇后问题"><a href="#M-八皇后问题" class="headerlink" title="M - 八皇后问题"></a><a href="https://vjudge.csgrandeur.cn/contest/476110#problem/M">M - 八皇后问题</a></h2><figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> n = <span class="number">8</span>;</span><br><span class="line"><span class="keyword">int</span> tmp, ans;</span><br><span class="line"><span class="keyword">int</span> x[<span class="number">10</span>];</span><br><span class="line"><span class="keyword">int</span> chess[<span class="number">10</span>][<span class="number">10</span>];</span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">no</span><span class="params">(<span class="keyword">int</span> r, <span class="keyword">int</span> c)</span> </span>{<span class="comment">//判断两个皇后的坐标是否冲突</span></span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < r; i++) {</span><br><span class="line"> <span class="keyword">if</span> (x[i] == c || <span class="built_in">abs</span>(r - i) == <span class="built_in">abs</span>(c - x[i])) </span><br><span class="line"> <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">queen</span><span class="params">(<span class="keyword">int</span> k)</span> </span>{</span><br><span class="line"> <span class="keyword">if</span> (k == n) { </span><br><span class="line"> ans = <span class="built_in">max</span>(ans, tmp);<span class="comment">//找所有情况的八皇后的最大权值和</span></span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < n; i++) {</span><br><span class="line"> <span class="keyword">if</span> (<span class="built_in">no</span>(k, i)) {</span><br><span class="line"> x[k] = i;<span class="comment">//dfs前做好标记</span></span><br><span class="line"> tmp += chess[k][i];</span><br><span class="line"> <span class="built_in">queen</span>(k + <span class="number">1</span>);</span><br><span class="line"> x[k] = <span class="number">0</span>;<span class="comment">//dfs后删除标记</span></span><br><span class="line"> tmp -= chess[k][i];</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> t;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &t);</span><br><span class="line"> <span class="keyword">while</span> (t--) {</span><br><span class="line"> tmp = ans = <span class="number">0</span>;</span><br><span class="line"> <span class="built_in">memset</span>(x, <span class="number">0</span>, <span class="built_in"><span class="keyword">sizeof</span></span>(x));</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < n; i++) {</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j < n; j++)</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &chess[i][j]);</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">queen</span>(<span class="number">0</span>);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, ans);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="N-选数"><a href="#N-选数" class="headerlink" title="N - 选数"></a><a href="https://vjudge.csgrandeur.cn/contest/476110#problem/N">N - 选数</a></h2><figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">int</span> ans,n,k,a[<span class="number">50</span>],i;</span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">check</span><span class="params">(<span class="keyword">int</span> a)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> i;</span><br><span class="line"> <span class="keyword">if</span>(a<<span class="number">2</span>)</span><br><span class="line"> <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line"> <span class="keyword">for</span>(i=<span class="number">2</span>;i<=<span class="built_in">sqrt</span>(a);i++)</span><br><span class="line"> <span class="keyword">if</span>(a%i==<span class="number">0</span>)</span><br><span class="line"> <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line"> <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">dfs</span><span class="params">(<span class="keyword">int</span> num,<span class="keyword">int</span> i,<span class="keyword">int</span> sum)</span></span>{</span><br><span class="line"> <span class="keyword">if</span>(num==k){<span class="comment">//输入的k个数之和是素数</span></span><br><span class="line"> <span class="keyword">if</span>(<span class="built_in">check</span>(sum))</span><br><span class="line"> ++ans;</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(i;i<=n;i++)</span><br><span class="line"> <span class="built_in">dfs</span>(num+<span class="number">1</span>,i+<span class="number">1</span>,sum+a[i]);<span class="comment">//向下一个数搜索</span></span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> cin>>n>>k;</span><br><span class="line"> <span class="keyword">for</span>(i=<span class="number">1</span>;i<=n;i++)</span><br><span class="line"> cin>>a[i];</span><br><span class="line"> <span class="built_in">dfs</span>(<span class="number">0</span>,<span class="number">1</span>,<span class="number">0</span>);</span><br><span class="line"> cout<<ans;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="O-打开灯泡-Switch-the-Lamp-On"><a href="#O-打开灯泡-Switch-the-Lamp-On" class="headerlink" title="O - 打开灯泡 Switch the Lamp On"></a><a href="https://vjudge.csgrandeur.cn/contest/476110#problem/O">O - 打开灯泡 Switch the Lamp On</a></h2><p>想搞懂的自行了解c++<a href="https://blog.csdn.net/jay_zzs/article/details/106534034#:~:text=%E5%8F%8C%E7%AB%AF%E9%98%9F%E5%88%97%EF%BC%88deque%EF%BC%89%20%E6%98%AF%E9%98%9F%E5%88%97%E7%9A%84%E4%B8%80%E7%A7%8D%E5%8F%98%E5%BD%A2%EF%BC%8C%20%E4%B8%80%E8%88%AC%E9%98%9F%E5%88%97%E5%8F%AA%E8%83%BD%E5%9C%A8%E9%98%9F%E5%B0%BE%E6%B7%BB%E5%8A%A0%E5%85%83%E7%B4%A0%EF%BC%88push%EF%BC%89%EF%BC%8C%E5%9C%A8%E9%98%9F%E9%A6%96%E5%88%A0%E9%99%A4%E5%85%83%E7%B4%A0%EF%BC%88pop%EF%BC%89%20%EF%BC%8C%20%E5%8F%8C%E7%AB%AF%E9%98%9F%E5%88%97%20%E5%88%99%20%E5%90%8C%E6%97%B6%E5%9C%A8%E9%98%9F%E9%A6%96%E6%88%96%E8%80%85%E9%98%9F%E5%B0%BE%E6%89%A7%E8%A1%8C%E6%B7%BB%E5%8A%A0%E5%92%8C%E5%88%A0%E9%99%A4%E5%B7%A5%E4%BD%9C,%E3%80%82%20C%2B%2B%20%E4%B8%AD%EF%BC%8C%E4%BD%BF%E7%94%A8%20%E5%8F%8C%E7%AB%AF%E9%98%9F%E5%88%97%20%E9%9C%80%E8%A6%81%E5%8C%85%E5%90%AB%E5%A4%B4%E6%96%87%E4%BB%B6%20%3Cdeque%3E%20%E3%80%82">双端队列(deque)</a>的基本操作</p>
<p>搬运的题解:</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> fir(i,a,b) for(int i=a;i<=b;i++)</span></span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N=<span class="number">510</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> dxy1[<span class="number">4</span>][<span class="number">2</span>]= {{<span class="number">1</span>,<span class="number">1</span>},{<span class="number">-1</span>,<span class="number">-1</span>},{<span class="number">1</span>,<span class="number">-1</span>},{<span class="number">-1</span>,<span class="number">1</span>}},dxy2[<span class="number">4</span>][<span class="number">2</span>]= {{<span class="number">1</span>,<span class="number">1</span>},{<span class="number">0</span>,<span class="number">0</span>},{<span class="number">1</span>,<span class="number">0</span>},{<span class="number">0</span>,<span class="number">1</span>}};</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">node</span></span></span><br><span class="line"><span class="class">{</span></span><br><span class="line"> <span class="keyword">int</span> x,y;</span><br><span class="line">};</span><br><span class="line"><span class="keyword">int</span> t,n,m,ans=<span class="number">1e8</span>,dis[N][N];</span><br><span class="line"><span class="keyword">char</span> s[N][N];</span><br><span class="line"><span class="keyword">bool</span> vis[N][N];</span><br><span class="line">deque<node> q;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">check</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> y)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">return</span> x>=<span class="number">0</span> && x<=n && y>=<span class="number">0</span> && y<=m;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">bfs</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> vis[<span class="number">0</span>][<span class="number">0</span>]=<span class="number">1</span>;</span><br><span class="line"> <span class="built_in">memset</span>(vis,<span class="number">0</span>,<span class="built_in"><span class="keyword">sizeof</span></span>(vis));</span><br><span class="line"> <span class="built_in">memset</span>(dis,<span class="number">0x3f</span>,<span class="built_in"><span class="keyword">sizeof</span></span>(dis));</span><br><span class="line"> q.<span class="built_in">push_front</span>(node {<span class="number">0</span>,<span class="number">0</span>});</span><br><span class="line"> dis[<span class="number">0</span>][<span class="number">0</span>]=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span>(q.<span class="built_in">size</span>())</span><br><span class="line"> {</span><br><span class="line"> node now=q.<span class="built_in">front</span>();</span><br><span class="line"> q.<span class="built_in">pop_front</span>();</span><br><span class="line"> <span class="built_in">fir</span>(i,<span class="number">0</span>,<span class="number">3</span>)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">int</span> tx=now.x+dxy1[i][<span class="number">0</span>],t1=now.x+dxy2[i][<span class="number">0</span>];</span><br><span class="line"> <span class="keyword">int</span> ty=now.y+dxy1[i][<span class="number">1</span>],t2=now.y+dxy2[i][<span class="number">1</span>];</span><br><span class="line"> <span class="keyword">int</span> tt=(s[t1][t2] != (i<=<span class="number">1</span>? <span class="string">'\\'</span>:<span class="string">'/'</span>));</span><br><span class="line"> <span class="keyword">if</span>(<span class="built_in">check</span>(tx,ty) && dis[tx][ty]>dis[now.x][now.y]+tt)</span><br><span class="line"> {</span><br><span class="line"> dis[tx][ty]=dis[now.x][now.y]+tt;</span><br><span class="line"> <span class="keyword">if</span>(tt)</span><br><span class="line"> q.<span class="built_in">push_back</span>(node {tx,ty});</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> q.<span class="built_in">push_front</span>(node {tx,ty});</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> cin>>n>>m;</span><br><span class="line"> <span class="built_in">fir</span>(i,<span class="number">1</span>,n)</span><br><span class="line"> <span class="built_in">fir</span>(j,<span class="number">1</span>,m)</span><br><span class="line"> cin>>s[i][j];</span><br><span class="line"> <span class="built_in">bfs</span>();</span><br><span class="line"> <span class="keyword">if</span>(dis[n][m]<<span class="number">1e8</span>)</span><br><span class="line"> cout<<dis[n][m]<<endl;</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> cout<<<span class="string">"NO SOLUTION"</span><<endl;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<p>可算肝完了。。。</p>
]]></content>
<tags>
<tag>contest</tag>
</tags>
</entry>
<entry>
<title>寒假冬训营数学专题题解</title>
<url>/2022/01/17/28/</url>
<content><![CDATA[<h1 id="第22组数学专题题解"><a href="#第22组数学专题题解" class="headerlink" title="第22组数学专题题解"></a>第22组数学专题题解</h1><h2 id="A-A-B-Mod-C"><a href="#A-A-B-Mod-C" class="headerlink" title="A - A^B Mod C"></a>A - A^B Mod C</h2><p><a href="https://vjudge.csgrandeur.cn/contest/476638#problem/">传送门</a></p>
<p>思路:快速幂</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">long</span> ll;</span><br><span class="line"><span class="function">ll <span class="title">qm</span><span class="params">(ll a,ll b,ll mod)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> ll res=<span class="number">1</span>;</span><br><span class="line"> a%=mod;<span class="comment">//a不能太大 </span></span><br><span class="line"> <span class="keyword">while</span>(b){</span><br><span class="line"> <span class="keyword">if</span>(b&<span class="number">1</span>)<span class="comment">//如果b为奇数</span></span><br><span class="line"> res=(res*a)%mod;</span><br><span class="line"> a=(a*a)%mod;</span><br><span class="line"> b>>=<span class="number">1</span>;<span class="comment">//位运算每轮b除二 </span></span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> res;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> ll a,b,mod,res;</span><br><span class="line"> cin>>a>>b>>mod;</span><br><span class="line"> res=<span class="built_in">qm</span>(a,b,mod);</span><br><span class="line"> cout<<res;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="B-逆元"><a href="#B-逆元" class="headerlink" title="B - 逆元"></a>B - 逆元</h2><p>思路:输出前(p-1)个数的逆元,但用常规方法会超时,但我们发现前(p-1)个数的逆元中,1~p-1各出现了一遍,于是<strong>等差数列</strong>求和得出了答案,当然<strong>i和p不互质就不存在逆元</strong>,只要<strong>p是素数</strong>就能和前面每一个数互质</p>
<p>要观察规律也得是知道怎么求一个数的逆元,下面我介绍三种方法:</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="number">1</span>:扩展欧几里得算法(<span class="built_in">o</span>(logmod),适用于所有模)</span><br><span class="line"> <span class="function">ll <span class="title">exgcd</span><span class="params">(ll a,ll b,ll &x,ll &y)</span></span>{</span><br><span class="line"> <span class="keyword">if</span>(b==<span class="number">0</span>)</span><br><span class="line"> {</span><br><span class="line"> x=<span class="number">1</span>,y=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">return</span> a;</span><br><span class="line"> }</span><br><span class="line"> ll ret=<span class="built_in">exgcd</span>(b,a%b,y,x);</span><br><span class="line"> y-=a/b*x;</span><br><span class="line"> <span class="keyword">return</span> ret;</span><br><span class="line">}</span><br><span class="line"><span class="function">ll <span class="title">inv</span><span class="params">(<span class="keyword">int</span> a,<span class="keyword">int</span> mod)</span></span>{</span><br><span class="line"> ll x,y;</span><br><span class="line"> ll d=<span class="built_in">exgcd</span>(a,mod,x,y);</span><br><span class="line"> <span class="keyword">return</span> d==<span class="number">1</span>?(x%mod+mod)%mod:<span class="number">-1</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">——————————————————————————————我是一条可爱的分割线———————————————————————————————————</span><br><span class="line"> </span><br><span class="line"><span class="number">2</span>:费马小定理/欧拉定理(<span class="built_in">o</span>(logmod),就是求a^mod<span class="number">-2</span>,只适用于mod为素数)</span><br><span class="line"><span class="function">ll <span class="title">qkpow</span><span class="params">(ll a,ll p,ll mod)</span></span>{</span><br><span class="line"> ll t=<span class="number">1</span>,tt=a%mod;</span><br><span class="line"> <span class="keyword">while</span>(p)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(p&<span class="number">1</span>)t=t*tt%mod;</span><br><span class="line"> tt=tt*tt%mod;</span><br><span class="line"> p>>=<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> t;</span><br><span class="line">}</span><br><span class="line"><span class="function">ll <span class="title">inv</span><span class="params">(ll a,ll mod)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">return</span> <span class="built_in">qkpow</span>(a,mod<span class="number">-2</span>,mod);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">——————————————————————————————我是一条可爱的分割线———————————————————————————————————</span><br><span class="line"> </span><br><span class="line"><span class="number">3</span>:递归求逆元(<span class="built_in">o</span>(logmod),思路和线性求逆元差不多,记住公式就完了,同样只适用于mod为素数)</span><br><span class="line"><span class="function">ll <span class="title">inv</span><span class="params">(ll a,ll m)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">return</span> a==<span class="number">1</span>?<span class="number">1</span>:(m-m/a)*<span class="built_in">inv</span>(m%a,m)%m;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">——————————————————————————————我是一条可爱的分割线———————————————————————————————————</span><br><span class="line"> </span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> ll a,ret;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%lld"</span>,&a);</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<a;i++){</span><br><span class="line"> ret=<span class="built_in">inv</span>(i,a);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%lld "</span>,ret);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<p>题解:</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ll long long</span></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">isprime</span><span class="params">(ll x)</span></span>{<span class="comment">//p为1时没请求,这里不需要考虑1</span></span><br><span class="line"> <span class="keyword">for</span>(ll i=<span class="number">2</span>;i*i<=x;i++){</span><br><span class="line"> <span class="keyword">if</span>(x%i==<span class="number">0</span>)</span><br><span class="line"> <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> ll p;</span><br><span class="line"> <span class="keyword">while</span>(cin>>p){</span><br><span class="line"> <span class="keyword">if</span>(!<span class="built_in">isprime</span>(p)){</span><br><span class="line"> cout<<<span class="string">"AKCniubi"</span><<endl;</span><br><span class="line"> }<span class="keyword">else</span>{</span><br><span class="line"> cout<<(p<span class="number">-1</span>)*p/<span class="number">2</span><<endl;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="C-判决素数个数"><a href="#C-判决素数个数" class="headerlink" title="C - 判决素数个数"></a>C - 判决素数个数</h2><figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">isprime</span><span class="params">(<span class="keyword">int</span> x)</span></span>{</span><br><span class="line"> <span class="keyword">if</span>(x==<span class="number">1</span>) </span><br><span class="line"> <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">2</span>;i*i<=x;i++){</span><br><span class="line"> <span class="keyword">if</span>(x%i==<span class="number">0</span>)</span><br><span class="line"> <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> a,b,i,t,sum=<span class="number">0</span>;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d %d"</span>,&a,&b);</span><br><span class="line"> <span class="keyword">if</span>(a>b){<span class="comment">//题目并没有说a和b的大小关系</span></span><br><span class="line"> t=a;</span><br><span class="line"> a=b;</span><br><span class="line"> b=t;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(i=a;i<=b;i++){</span><br><span class="line"> <span class="keyword">if</span>(<span class="built_in">isprime</span>(i)){</span><br><span class="line"> sum++;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d"</span>,sum);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="D-矩阵乘法"><a href="#D-矩阵乘法" class="headerlink" title="D - 矩阵乘法"></a>D - 矩阵乘法</h2><p>虽然咱新生还没开线性代数,但这道题咱们已经遇到不止一次了</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> a[<span class="number">110</span>][<span class="number">110</span>],b[<span class="number">110</span>][<span class="number">110</span>],c[<span class="number">110</span>][<span class="number">110</span>];</span><br><span class="line"> <span class="keyword">int</span> m,p,n;</span><br><span class="line"> <span class="keyword">int</span> i,j,k;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d%d%d"</span>,&m,&p,&n);</span><br><span class="line"> <span class="keyword">for</span>(i=<span class="number">0</span>;i<m;i++)</span><br><span class="line"> <span class="keyword">for</span>(j=<span class="number">0</span>;j<p;j++)</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>,&a[i][j]);</span><br><span class="line"> <span class="keyword">for</span>(i=<span class="number">0</span>;i<p;i++)</span><br><span class="line"> <span class="keyword">for</span>(j=<span class="number">0</span>;j<n;j++)</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>,&b[i][j]);</span><br><span class="line"> <span class="keyword">for</span>(i=<span class="number">0</span>;i<m;i++)<span class="comment">//核心</span></span><br><span class="line"> <span class="keyword">for</span>(j=<span class="number">0</span>;j<n;j++){</span><br><span class="line"> c[i][j]=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(k=<span class="number">0</span>;k<p;k++){</span><br><span class="line"> c[i][j]+=a[i][k]*b[k][j];</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(i=<span class="number">0</span>;i<m;i++){</span><br><span class="line"> <span class="keyword">for</span>(j=<span class="number">0</span>;j<n;j++){</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d "</span>,c[i][j]);</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"\n"</span>);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="E-Bash游戏"><a href="#E-Bash游戏" class="headerlink" title="E - Bash游戏"></a>E - Bash游戏</h2><p>思路:博弈论,还剩(k+1)个石子时,先手怎么拿都是输</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><stdio.h></span></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> t,n,k;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>,&t);</span><br><span class="line"> <span class="keyword">while</span>(t--){</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d%d"</span>,&n,&k);</span><br><span class="line"> <span class="keyword">if</span>(n%(k+<span class="number">1</span>)==<span class="number">0</span>)</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"B\n"</span>);</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"A\n"</span>);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="F-取石子游戏"><a href="#F-取石子游戏" class="headerlink" title="F - 取石子游戏"></a>F - 取石子游戏</h2><p>思路:<a href="https://blog.csdn.net/qq_41311604/article/details/79980882">威佐夫博奕</a>(讲的可清楚)</p>
<p>先手必输局势(a,b)近似满足(b-a)*1.618=a,因此只需判断两堆石子差值是否满足公式</p>
<p>floor是向下取整,如floor(3.9)=3,0.618是黄金比例,即(sqrt(5)-1.0)/2.0</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> a,b,k,t;</span><br><span class="line"> <span class="keyword">while</span>(<span class="built_in">scanf</span>(<span class="string">"%d%d"</span>,&a,&b)!=EOF){</span><br><span class="line"> <span class="keyword">if</span>(a>b){</span><br><span class="line"> t=b;</span><br><span class="line"> b=a;</span><br><span class="line"> a=t;</span><br><span class="line"> }</span><br><span class="line"> k=b-a;</span><br><span class="line"> t=(floor)(k*(<span class="number">1.0</span>+<span class="built_in">sqrt</span>(<span class="number">5</span>))/<span class="number">2.0</span>);<span class="comment">//核心</span></span><br><span class="line"> <span class="keyword">if</span>(t==a)</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"0\n"</span>);</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"1\n"</span>);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="G-Matches-Game"><a href="#G-Matches-Game" class="headerlink" title="G - Matches Game"></a>G - Matches Game</h2><p>思路:<a href="https://blog.csdn.net/cuijy/article/details/23692469">尼姆博弈</a></p>
<p>取每一堆中火柴的情况可分为四种:</p>
<p>(1)如果轮到某人取火柴的时候,火柴已经没有了,那么此人输,设为P-格局</p>
<p>(2)如果轮到某人取火柴的时候,他能够取完火柴,那么此人赢,设为N-格局</p>
<p>(3)如果轮到某人取火柴的时候,他能够将当前格局转化为某个P格局,那么此格局为N-格局</p>
<p>(4)如果轮到某人取火柴的时候,他不能将当前格局转化为某个P格局,那么此格局为P-格局</p>
<p>有这样一个定理:</p>
<p>一个格局记为(x1,x2,…,xn),且次序无关,此格局为 P-格局 当且仅当 x1^x2^…^xn = 0</p>
<p>其中^是按位异或运算,即二进制中两个位相同为0,相异为1,我们只用判断最终状态</p>
<p>因此不断异或,如果结果不为0,则先手胜</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><stdio.h></span></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> n,ret;</span><br><span class="line"> <span class="keyword">int</span> a[<span class="number">30</span>];</span><br><span class="line"> <span class="keyword">while</span>(<span class="built_in">scanf</span>(<span class="string">"%d"</span>,&n)!=EOF){</span><br><span class="line"> ret=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=n;i++){</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>,&a[i]);</span><br><span class="line"> ret=ret^a[i];<span class="comment">//核心</span></span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(ret!=<span class="number">0</span>)</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"Yes\n"</span>);</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"No\n"</span>);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"> }</span><br></pre></td></tr></tbody></table></figure>
<h2 id="H-互质数的个数(一)"><a href="#H-互质数的个数(一)" class="headerlink" title="H - 互质数的个数(一)"></a>H - 互质数的个数(一)</h2><p>思路:<a href="https://blog.csdn.net/liuzibujian/article/details/81086324">欧拉函数</a></p>
<p>pi是x分解成的质因数,如20=2 <em> 2 </em> 5,φ(20)=20*(1-1/2)(1-1/5)=8</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">long</span> ll;</span><br><span class="line"><span class="function">ll <span class="title">oula</span><span class="params">(ll n)</span></span>{</span><br><span class="line"> ll rea=n;</span><br><span class="line"> <span class="keyword">for</span>(ll i=<span class="number">2</span>;i*i<=n;i++)</span><br><span class="line"> <span class="keyword">if</span>(n%i==<span class="number">0</span>){</span><br><span class="line"> rea=rea-rea/i;<span class="comment">//遇到质因数就乘(1-1/pi)</span></span><br><span class="line"> <span class="keyword">do</span></span><br><span class="line"> n/=i;</span><br><span class="line"> <span class="keyword">while</span>(n%i==<span class="number">0</span>);<span class="comment">//将重复的质因数筛去</span></span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(n><span class="number">1</span>)</span><br><span class="line"> rea=rea-rea/n;<span class="comment">//对1特殊处理</span></span><br><span class="line"> <span class="keyword">return</span> rea;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> ll t,ret;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%lld"</span>,&t);</span><br><span class="line"> <span class="keyword">while</span>(t--){</span><br><span class="line"> ll v;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%lld"</span>,&v);</span><br><span class="line"> ret=<span class="built_in">oula</span>(v);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%lld\n"</span>,ret);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="I-Sumdiv"><a href="#I-Sumdiv" class="headerlink" title="I - Sumdiv"></a>I - Sumdiv</h2><p>思路:<a href="https://blog.csdn.net/chabeizip/article/details/111568047">逆元+快速幂+约数和定理</a></p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ll long long</span></span><br><span class="line">ll a,b,m;</span><br><span class="line">ll prime[<span class="number">500000</span>],c[<span class="number">500000</span>];</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">divide</span><span class="params">(ll n)</span></span>{</span><br><span class="line"> <span class="keyword">for</span> (ll i=<span class="number">2</span>;i<=n/i;i++){</span><br><span class="line"> <span class="keyword">if</span> (n % i==<span class="number">0</span>){</span><br><span class="line"> prime[++m]=i;</span><br><span class="line"> <span class="keyword">while</span>(n % i==<span class="number">0</span>){</span><br><span class="line"> n/=i;</span><br><span class="line"> c[m]++;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span> (n><span class="number">1</span>){</span><br><span class="line"> prime[++m]=n;</span><br><span class="line"> c[m]=<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"><span class="function">ll <span class="title">pow</span><span class="params">(ll a,ll b,ll p)</span></span>{</span><br><span class="line"> ll res=<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">while</span>(b){</span><br><span class="line"> <span class="keyword">if</span>(b&<span class="number">1</span>){</span><br><span class="line"> res=res*a%p;</span><br><span class="line"> }</span><br><span class="line"> a=a*a%p;</span><br><span class="line"> b>>=<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> res;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> cin>>a>>b;</span><br><span class="line"> ll ans=<span class="number">1</span>;</span><br><span class="line"> <span class="built_in">divide</span>(a);</span><br><span class="line"> <span class="keyword">for</span>(ll i=<span class="number">1</span>;i<=m;i++){</span><br><span class="line"> <span class="keyword">if</span>((prime[i]<span class="number">-1</span>)%<span class="number">9901</span>==<span class="number">0</span>){</span><br><span class="line"> ans=(b%<span class="number">9901</span>*c[i]%<span class="number">9901</span>+<span class="number">1</span>)*ans%<span class="number">9901</span>;</span><br><span class="line"> <span class="keyword">continue</span>;</span><br><span class="line"> }</span><br><span class="line"> ans=(<span class="built_in">pow</span>(prime[i],b*c[i]+<span class="number">1</span>,<span class="number">9901</span>)<span class="number">-1</span>+<span class="number">9901</span>)%<span class="number">9901</span>*ans%<span class="number">9901</span>;</span><br><span class="line"> ll y=<span class="built_in">pow</span>(prime[i]<span class="number">-1</span>,<span class="number">9901</span><span class="number">-2</span>,<span class="number">9901</span>);</span><br><span class="line"> ans*=y;</span><br><span class="line"> ans%=<span class="number">9901</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%lld"</span>,ans);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="J-The-Lottery"><a href="#J-The-Lottery" class="headerlink" title="J - The Lottery"></a>J - The Lottery</h2><p>思路:容斥原理</p>
<p>我一开始想用素数筛,没看到2^31,数组开大直接编译错误</p>
<p>搬运的题解:</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> EPS 1e-9</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> PI acos(-1.0)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> INF 0x3f3f3f3f</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> LL long long</span></span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> MOD = <span class="number">1E9</span>+<span class="number">7</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N = <span class="number">2000</span>+<span class="number">5</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> dx[] = {<span class="number">0</span>,<span class="number">0</span>,<span class="number">-1</span>,<span class="number">1</span>,<span class="number">-1</span>,<span class="number">-1</span>,<span class="number">1</span>,<span class="number">1</span>};</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> dy[] = {<span class="number">-1</span>,<span class="number">1</span>,<span class="number">0</span>,<span class="number">0</span>,<span class="number">-1</span>,<span class="number">1</span>,<span class="number">-1</span>,<span class="number">1</span>};</span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line">LL a[N];</span><br><span class="line"><span class="function">LL <span class="title">GCD</span><span class="params">(LL a,LL b)</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> b==<span class="number">0</span>?a:<span class="built_in">GCD</span>(b,a%b);</span><br><span class="line">}</span><br><span class="line"><span class="function">LL <span class="title">LCM</span><span class="params">(LL a,LL b)</span></span>{</span><br><span class="line"> <span class="keyword">return</span> a/<span class="built_in">GCD</span>(a,b)*b;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> LL n,m;</span><br><span class="line"> <span class="keyword">while</span>(<span class="built_in">scanf</span>(<span class="string">"%lld%lld"</span>,&n,&m)!=EOF&&(n+m)) {</span><br><span class="line"> <span class="keyword">int</span> tot=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(LL i=<span class="number">0</span>; i<m; i++){</span><br><span class="line"> LL val;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%lld"</span>,&val);</span><br><span class="line"> <span class="keyword">if</span>(val><span class="number">0</span>&&val<n)</span><br><span class="line"> a[tot++]=val;</span><br><span class="line"> }</span><br><span class="line"> LL sum=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(LL i=<span class="number">1</span>; i<(<span class="number">1</span><<tot); i++) {</span><br><span class="line"> LL lcm=<span class="number">1</span>;</span><br><span class="line"> LL cnt=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(LL j=<span class="number">0</span>; j<tot; j++) {</span><br><span class="line"> <span class="keyword">if</span>(i&(<span class="number">1</span><<j)) {</span><br><span class="line"> lcm=<span class="built_in">LCM</span>(lcm,a[j]);</span><br><span class="line"> cnt++;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(cnt!=<span class="number">0</span>){</span><br><span class="line"> <span class="keyword">if</span>(cnt&<span class="number">1</span>)</span><br><span class="line"> sum+=(n)/lcm;</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> sum-=(n)/lcm;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(sum<<span class="number">0</span>)</span><br><span class="line"> sum=<span class="number">0</span>;</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%lld\n"</span>,n-sum);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="K-组合数问题"><a href="#K-组合数问题" class="headerlink" title="K - 组合数问题"></a>K - 组合数问题</h2><p>思路:杨辉三角与组合数的关系</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">int</span> c[<span class="number">2100</span>][<span class="number">2100</span>],f[<span class="number">2100</span>][<span class="number">2100</span>];</span><br><span class="line"><span class="keyword">int</span> t,k;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d%d"</span>,&t,&k);</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=<span class="number">2000</span>;i++){</span><br><span class="line"> c[i][i]=<span class="number">1</span>;</span><br><span class="line"> c[i][<span class="number">0</span>]=<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=<span class="number">2000</span>;i++)</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">1</span>;j<i;j++)</span><br><span class="line"> c[i][j]=(c[i<span class="number">-1</span>][j<span class="number">-1</span>]+c[i<span class="number">-1</span>][j])%k;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=<span class="number">2000</span>;i++)</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">1</span>;j<=<span class="number">2000</span>;j++){</span><br><span class="line"> f[i][j]=f[i][j<span class="number">-1</span>]+f[i<span class="number">-1</span>][j]-f[i<span class="number">-1</span>][j<span class="number">-1</span>];</span><br><span class="line"> <span class="keyword">if</span>(c[i][j]==<span class="number">0</span>&&j<=i)</span><br><span class="line"> f[i][j]++;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">while</span>(t--){</span><br><span class="line"> <span class="keyword">int</span> n,m;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d%d"</span>,&n,&m);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>,f[n][m]);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="L-同余方程"><a href="#L-同余方程" class="headerlink" title="L - 同余方程"></a>L - 同余方程</h2><p>思路:扩展欧几里得</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">int</span> a,q;</span><br><span class="line"><span class="keyword">int</span> x,y;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">exgcd</span><span class="params">(<span class="keyword">int</span> a,<span class="keyword">int</span> b)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">if</span>(b==<span class="number">0</span>){<span class="comment">//到达边界 </span></span><br><span class="line"> x=<span class="number">1</span>;</span><br><span class="line"> y=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }<span class="keyword">else</span>{</span><br><span class="line"> <span class="built_in">exgcd</span>(b,a%b);<span class="comment">//递归调用倒回去求最小解 </span></span><br><span class="line"> <span class="keyword">int</span> k=x;</span><br><span class="line"> x=y;</span><br><span class="line"> y=k-(a/b)*y;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d%d"</span>,&a,&q); </span><br><span class="line"> <span class="built_in">exgcd</span>(a,q);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%lld"</span>,(x+q)%q);<span class="comment">//+q以防x为负数 </span></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
]]></content>
<tags>
<tag>contest</tag>
</tags>
</entry>
<entry>
<title>算法学习(2)</title>
<url>/2022/01/24/29/</url>
<content><![CDATA[<h1 id="STL-初步"><a href="#STL-初步" class="headerlink" title="STL 初步"></a>STL 初步</h1><h2 id="sort用法"><a href="#sort用法" class="headerlink" title="sort用法"></a>sort用法</h2><p>sort排序区间为下标范围为<strong>[n1,n2)</strong>的元素</p>
<p>从小到大排序:</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="built_in">sort</span>(数组名+n1,数组名+n2);</span><br></pre></td></tr></tbody></table></figure>
<p>从大到小排序:</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="built_in">sort</span>(数组名+n1,数组名+n2,greater<type>());</span><br></pre></td></tr></tbody></table></figure>
<p>自定义排序规则:</p>
<p>sort函数的第三个参数可以是<strong>函数对象</strong>也可以是<strong>函数指针</strong>;</p>
<p>1.cmp</p>
<p>如:</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"> <span class="function"><span class="keyword">bool</span> <span class="title">cmp</span> <span class="params">(<span class="keyword">int</span> a,<span class="keyword">int</span> b)</span></span>{</span><br><span class="line"> <span class="keyword">return</span> a > b;</span><br><span class="line">}</span><br><span class="line"> <span class="function"><span class="keyword">bool</span> <span class="title">cmp</span> <span class="params">(stu a,stu b)</span></span>{</span><br><span class="line"> <span class="keyword">return</span> a.score > b.score;</span><br><span class="line">}</span><br><span class="line"> <span class="built_in">sort</span>(a,a+len;cmp);</span><br></pre></td></tr></tbody></table></figure>
<p>2.重载</p>
<p>如:</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">rule</span>{</span></span><br><span class="line"> <span class="function"><span class="keyword">bool</span> <span class="title">operator</span><span class="params">()</span> <span class="params">( <span class="keyword">const</span> T & name1,<span class="keyword">const</span> T & name2)</span> <span class="keyword">const</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> name1 < name2;</span><br><span class="line"> }</span><br><span class="line">};</span><br><span class="line"> <span class="class"><span class="keyword">struct</span> <span class="title">rule</span> {</span></span><br><span class="line"> <span class="function"><span class="keyword">bool</span> <span class="title">operator</span><span class="params">()</span> <span class="params">(<span class="keyword">const</span> Student & s1,<span class="keyword">const</span> Student & s2)</span> <span class="keyword">const</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> s1.id < s2.id;</span><br><span class="line"> }</span><br><span class="line">};</span><br><span class="line"> <span class="built_in">sort</span>(a,a+len;<span class="built_in">rule</span>());</span><br></pre></td></tr></tbody></table></figure>
<h2 id="stl里的二分查找算法"><a href="#stl里的二分查找算法" class="headerlink" title="stl里的二分查找算法"></a>stl里的二分查找算法</h2><p>二分查找(一种写法):</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span> <span class="title">x</span> <span class="title">List</span>;</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">x</span>{</span></span><br><span class="line"> <span class="keyword">int</span> a[<span class="number">10000</span>];</span><br><span class="line"> <span class="keyword">int</span> len;</span><br><span class="line">}p;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">ef</span><span class="params">(List p,<span class="keyword">int</span> find)</span></span>{</span><br><span class="line"> <span class="keyword">int</span> left,right,mid,no=<span class="number">-1</span>;</span><br><span class="line"> left=<span class="number">1</span>;</span><br><span class="line"> right=p.len;</span><br><span class="line"> <span class="keyword">while</span>(left<=right){</span><br><span class="line"> mid=(left+right)/<span class="number">2</span>;</span><br><span class="line"> <span class="keyword">if</span>(find<p.a[mid])</span><br><span class="line"> right=mid<span class="number">-1</span>;</span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">if</span>(find>p.a[mid])</span><br><span class="line"> left=mid+<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="keyword">return</span> mid;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> no;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> num,find;</span><br><span class="line"> List p;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>,&p.len);</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=p.len;i++){</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>,&p.a[i]);</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>,&find);</span><br><span class="line"> num=<span class="built_in">ef</span>(p,find);</span><br><span class="line"> <span class="keyword">if</span>(num==<span class="number">-1</span>)</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"Not found!"</span>);</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d"</span>,p.a[num]);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<p>stlL提供在排好序的数组上进行二分查找的算法,查找区间为下标范围为[n1,n2)的元素</p>
<p>包括binary_search,lower_bound和upper_bound</p>
<p><font color="red">注意</font>:这里查找的规则是依据sort排序的规则而定的,所以不一定是值相等就找到</p>
<h3 id="binary-search"><a href="#binary-search" class="headerlink" title="binary_search"></a>binary_search</h3><figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="built_in">binary_search</span>(数组名+n1,数组名+n2,值,(排序规则)); </span><br></pre></td></tr></tbody></table></figure>
<h3 id="用upper-bound二分查找上界"><a href="#用upper-bound二分查找上界" class="headerlink" title="用upper_bound二分查找上界"></a>用upper_bound二分查找上界</h3><figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="function">type * <span class="title">upper_bound</span><span class="params">(数组名+n1,数组名+n2,值,(排序规则))</span></span>;</span><br></pre></td></tr></tbody></table></figure>
<p>返回一个指针 type * p,表示在值后面的第一个相邻元素;</p>
<h3 id="用lower-bound二分查找下界"><a href="#用lower-bound二分查找下界" class="headerlink" title="用lower_bound二分查找下界"></a>用lower_bound二分查找下界</h3><figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="function">type * <span class="title">lower_bound</span><span class="params">(数组名+n1,数组名+n2,(排序规则))</span></span>;</span><br></pre></td></tr></tbody></table></figure>
<p>也返回一个指针 type* p (p可换),表示在值前面的第一个相邻元素;</p>
<p>应用举例:</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Rule</span> {</span></span><br><span class="line"> <span class="function"><span class="keyword">bool</span> <span class="title">operator</span><span class="params">()</span><span class="params">( <span class="keyword">const</span> <span class="keyword">int</span> & a1,<span class="keyword">const</span> <span class="keyword">int</span> & a2)</span> <span class="keyword">const</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> a1%<span class="number">10</span> < a2%<span class="number">10</span>;</span><br><span class="line">}</span><br><span class="line">};</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Print</span><span class="params">(<span class="keyword">int</span> a[],<span class="keyword">int</span> size)</span> </span>{</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>;i < size;++i) {</span><br><span class="line"> cout << a[i] << <span class="string">","</span> ;</span><br><span class="line">}</span><br><span class="line"> cout << endl;</span><br><span class="line">}</span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> NUM 7 </span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> a[NUM] = { <span class="number">12</span>,<span class="number">5</span>,<span class="number">3</span>,<span class="number">5</span>,<span class="number">98</span>,<span class="number">21</span>,<span class="number">7</span>};</span><br><span class="line"> <span class="built_in">sort</span>(a,a+NUM);</span><br><span class="line"> <span class="built_in">Print</span>(a,NUM); <span class="comment">// => 3,5,5,7,12,21,98,</span></span><br><span class="line"> <span class="keyword">int</span> * p = <span class="built_in">lower_bound</span>(a,a+NUM,<span class="number">5</span>);</span><br><span class="line"> cout << *p << <span class="string">","</span> << p-a << endl; <span class="comment">//=> 5,1</span></span><br><span class="line"> p = <span class="built_in">upper_bound</span>(a,a+NUM,<span class="number">5</span>);</span><br><span class="line"> cout << *p << endl; <span class="comment">//=>7</span></span><br><span class="line"> cout << * <span class="built_in">upper_bound</span>(a,a+NUM,<span class="number">13</span>) << endl; <span class="comment">//=>21</span></span><br><span class="line"> <span class="built_in">sort</span>(a,a+NUM,<span class="built_in">Rule</span>());</span><br><span class="line"> <span class="built_in">Print</span>(a,NUM); <span class="comment">//=>21,12,3,5,5,7,98,</span></span><br><span class="line"> cout << * <span class="built_in">lower_bound</span>(a,a+NUM,<span class="number">16</span>,<span class="built_in">Rule</span>()) << endl; <span class="comment">// => 7</span></span><br><span class="line"> cout << <span class="built_in">lower_bound</span>(a,a+NUM,<span class="number">25</span>,<span class="built_in">Rule</span>()) - a<< endl; <span class="comment">// => 3</span></span><br><span class="line"> cout << <span class="built_in">upper_bound</span>(a,a+NUM,<span class="number">18</span>,<span class="built_in">Rule</span>()) - a << endl; <span class="comment">// => 7</span></span><br><span class="line"> <span class="keyword">if</span>( <span class="built_in">upper_bound</span>(a,a+NUM,<span class="number">18</span>,<span class="built_in">Rule</span>()) == a+NUM)</span><br><span class="line"> cout << <span class="string">"not found"</span> << endl; <span class="comment">//=> not found</span></span><br><span class="line"> cout << * <span class="built_in">upper_bound</span>(a,a+NUM,<span class="number">5</span>,<span class="built_in">Rule</span>()) << endl; <span class="comment">// =>7</span></span><br><span class="line"> cout << * <span class="built_in">upper_bound</span>(a,a+NUM,<span class="number">4</span>,<span class="built_in">Rule</span>()) << endl; <span class="comment">// =>5</span></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="STL中的平衡二叉树数据结构"><a href="#STL中的平衡二叉树数据结构" class="headerlink" title="STL中的平衡二叉树数据结构"></a>STL中的平衡二叉树数据结构</h2><p>目的是使增加数据、删除数据、查找数据都能在 log(n)复杂度完成</p>
<p>用到下面四种“排序容器”:multiset,set,multimap,map;</p>
<h3 id="multiset"><a href="#multiset" class="headerlink" title="multiset"></a>multiset</h3><p>multiset用法:</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line">multiset<T> st; </span><br></pre></td></tr></tbody></table></figure>
<p>定义了一个multiset变量st,st里面可以存放T类型的数据,并且能自动排序(默认a<b为true)。开始时st为空</p>
<p>可用 st.insert添加元素,st.find查找元素,st.erase删除元素,复杂度 都是 log(n)</p>
<p>举例:</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><set></span> <span class="comment">//使用multiset和set需要此头文件</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> multiset<<span class="keyword">int</span>> st; </span><br><span class="line"> <span class="keyword">int</span> a[<span class="number">10</span>]={<span class="number">1</span>,<span class="number">14</span>,<span class="number">12</span>,<span class="number">13</span>,<span class="number">7</span>,<span class="number">13</span>,<span class="number">21</span>,<span class="number">19</span>,<span class="number">8</span>,<span class="number">8</span> };</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>;i < <span class="number">10</span>; ++i)</span><br><span class="line"> st.<span class="built_in">insert</span>(a[i]); <span class="comment">//插入的是a [i]的复制品</span></span><br><span class="line"> multiset<<span class="keyword">int</span>>::iterator i; <span class="comment">//迭代器,近似于指针</span></span><br><span class="line"> <span class="keyword">for</span>(i = st.<span class="built_in">begin</span>(); i != st.<span class="built_in">end</span>(); ++i) </span><br><span class="line"> cout << * i << <span class="string">","</span>; </span><br><span class="line"> cout << endl;</span><br><span class="line"> </span><br><span class="line">输出:<span class="number">1</span>,<span class="number">7</span>,<span class="number">8</span>,<span class="number">8</span>,<span class="number">12</span>,<span class="number">13</span>,<span class="number">13</span>,<span class="number">14</span>,<span class="number">19</span>,<span class="number">21</span></span><br><span class="line"> </span><br><span class="line"> i = st.<span class="built_in">find</span>(<span class="number">22</span>); <span class="comment">//查找22,返回值是迭代器</span></span><br><span class="line"> <span class="keyword">if</span>( i == st.<span class="built_in">end</span>()) <span class="comment">//找不到则返回值为 end()</span></span><br><span class="line"> cout << <span class="string">"not found"</span> << endl;</span><br><span class="line"> st.<span class="built_in">insert</span>(<span class="number">22</span>); <span class="comment">//插入 22</span></span><br><span class="line"> i = st.<span class="built_in">find</span>(<span class="number">22</span>);</span><br><span class="line"> <span class="keyword">if</span>( i == st.<span class="built_in">end</span>())</span><br><span class="line"> cout << <span class="string">"not found"</span> << endl;</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> cout << <span class="string">"found:"</span> << *i <<endl;</span><br><span class="line"> <span class="comment">//找到则返回指向找到的元素的迭代器</span></span><br><span class="line"></span><br><span class="line">输出:</span><br><span class="line"><span class="keyword">not</span> found</span><br><span class="line">found:<span class="number">22</span></span><br><span class="line"> i = st.<span class="built_in">lower_bound</span>(<span class="number">13</span>);</span><br><span class="line"> <span class="comment">//返回最靠后的迭代器 it,使得[begin(),it)中的元素</span></span><br><span class="line"> <span class="comment">//都在 13 前面 ,复杂度 log(n)</span></span><br><span class="line"> cout << * i << endl;</span><br><span class="line"> i = st.<span class="built_in">upper_bound</span>(<span class="number">8</span>);</span><br><span class="line"> <span class="comment">//返回最靠前的迭代器 it,使得[it,end())中的元素</span></span><br><span class="line"> <span class="comment">//都在 8 后面,复杂度 log(n)</span></span><br><span class="line"> cout << * i << endl;</span><br><span class="line"> st.<span class="built_in">erase</span>(i); <span class="comment">//删除迭代器 i 指向的元素,即12</span></span><br><span class="line"> <span class="keyword">for</span>(i = st.<span class="built_in">begin</span>(); i != st.<span class="built_in">end</span>(); ++i) </span><br><span class="line"> cout << * i << <span class="string">","</span>;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">输出:</span><br><span class="line"><span class="number">13</span></span><br><span class="line"><span class="number">12</span></span><br><span class="line"><span class="number">1</span>,<span class="number">7</span>,<span class="number">8</span>,<span class="number">8</span>,<span class="number">13</span>,<span class="number">13</span>,<span class="number">14</span>,<span class="number">19</span>,<span class="number">21</span>,<span class="number">22</span></span><br></pre></td></tr></tbody></table></figure>
<p><strong>multiset 上的迭代器</strong>:</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line">multiset<T>::iterator p; </span><br></pre></td></tr></tbody></table></figure>
<p>p是迭代器,相当于指针,可用于指向multiset中的元素。访问multiset中的元素要通过迭代器</p>
<p>与指针的不同:</p>
<p>multiset上的迭代器可 ++ ,—, 用 != 和 == 比较,不可比大小,不可加减整数,不可相减</p>
<p>st.begin() 返回值类型为 multiset::iterator, 是指向st中的头一个元素的迭代器</p>
<p>st.end() 返回值类型为 multiset::iterator, 是指向st中的最后一个元素后面的迭代器</p>
<p>对迭代器 ++ ,其就指向容器中下一个元素,— 则令其指向上一个元素</p>
<p>自定义排序规则的multiset 用法:</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line">multiset<<span class="keyword">int</span>,greater<<span class="keyword">int</span>> > st;<span class="comment">//排序规则为从大到小</span></span><br><span class="line">multiset<<span class="keyword">int</span>,Rule1> st;</span><br><span class="line">multiset<Student,Rule1>::iterator p;<span class="comment">//对应迭代器写法</span></span><br></pre></td></tr></tbody></table></figure>
<h3 id="set"><a href="#set" class="headerlink" title="set"></a>set</h3><p>学set前先了解一下pair模板的用法:</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line">pair<T1,T2></span><br><span class="line">等价于:</span><br><span class="line"><span class="class"><span class="keyword">struct</span> {</span></span><br><span class="line"> T1 first;</span><br><span class="line"> T2 second;</span><br><span class="line">};</span><br><span class="line">例如:pair<<span class="keyword">int</span>, <span class="keyword">double</span>> a;</span><br><span class="line">等价于:</span><br><span class="line"><span class="class"><span class="keyword">struct</span> {</span></span><br><span class="line"><span class="keyword">int</span> first;</span><br><span class="line"><span class="keyword">double</span> second;</span><br><span class="line">} a;</span><br><span class="line">a.first = <span class="number">1</span>;</span><br><span class="line">a.second = <span class="number">93.93</span>;</span><br></pre></td></tr></tbody></table></figure>
<p><font color="red">注意</font>:first和second不能更换</p>
<p>set和multiset的区别在于容器里不能有重复元素</p>
<p>这里的重复也是依据预先定义的规则才有意义</p>
<p>set插入元素可能不成功(重复)</p>
<p>set在插入时返回的result.second如果为零,表示因重复而无法插入</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line">pair<set<<span class="keyword">int</span>>::iterator, <span class="keyword">bool</span>> result = st.<span class="built_in">insert</span>(<span class="number">2</span>);</span><br></pre></td></tr></tbody></table></figure>
<h3 id="multimap"><a href="#multimap" class="headerlink" title="multimap"></a>multimap</h3><p>multimap容器里的元素,都是pair形式的</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line">multimap<T1,T2> mp</span><br><span class="line"><span class="comment">//即都是如下类型:</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> {</span></span><br><span class="line"> T1 first; <span class="comment">//关键字</span></span><br><span class="line"> T2 second; <span class="comment">//值</span></span><br><span class="line">};</span><br></pre></td></tr></tbody></table></figure>
<p>multimap中的元素按照first排序,并可以按first进行查找,默认”a.first < b.first” 为true</p>
<p>应用举例:</p>
<p>一个学生成绩录入和查询系统,接受以下两种输入:</p>
<p> Add name id score </p>
<p>Query score</p>
<p>name是个不超过16字符的字符串,中间没有空格,代表学生姓名。id 是个整数,代表学号。score是个整数,表示分数。学号不会重复,分数 和姓名都可能重复。 </p>
<p>两种输入交替出现。第一种输入表示要添加一个学生的信息,碰到这 种输入,就记下学生的姓名、id和分数。第二种输入表示要查询,碰到这 种输入,就输出已有记录中分数比score低的最高分获得者的姓名、学号 和分数。如果有多个学生都满足条件,就输出学号最大的那个学生的信 息。如果找不到满足条件的学生,则输出“Nobody” </p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line">输入样例:</span><br><span class="line">Add Jack <span class="number">12</span> <span class="number">78</span></span><br><span class="line">Query <span class="number">78</span></span><br><span class="line">Query <span class="number">81</span></span><br><span class="line">Add Percy <span class="number">9</span> <span class="number">81</span></span><br><span class="line">Add Marry <span class="number">8</span> <span class="number">81</span></span><br><span class="line">Query <span class="number">82</span></span><br><span class="line">Add Tom <span class="number">11</span> <span class="number">79</span></span><br><span class="line">Query <span class="number">80</span></span><br><span class="line">Query <span class="number">81</span></span><br><span class="line">输出样例:</span><br><span class="line">Nobody</span><br><span class="line">Jack <span class="number">12</span> <span class="number">78</span></span><br><span class="line">Percy <span class="number">9</span> <span class="number">81</span></span><br><span class="line">Tom <span class="number">11</span> <span class="number">79</span></span><br><span class="line">Tom <span class="number">11</span> <span class="number">79</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><map></span> <span class="comment">//使用multimap和map需要包含此头文件</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"> <span class="class"><span class="keyword">struct</span> <span class="title">StudentInfo</span> {</span></span><br><span class="line"> <span class="keyword">int</span> id;</span><br><span class="line"> <span class="keyword">char</span> name[<span class="number">20</span>];</span><br><span class="line">};</span><br><span class="line"> <span class="class"><span class="keyword">struct</span> <span class="title">Student</span> {</span></span><br><span class="line"> <span class="keyword">int</span> score;</span><br><span class="line"> StudentInfo info; </span><br><span class="line">};</span><br><span class="line"><span class="keyword">typedef</span> multimap<<span class="keyword">int</span>,StudentInfo> MAP_STD;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> MAP_STD mp;</span><br><span class="line"> Student st;</span><br><span class="line"> <span class="keyword">char</span> cmd[<span class="number">20</span>];</span><br><span class="line"> <span class="keyword">while</span>( cin >> cmd ) {</span><br><span class="line"> <span class="keyword">if</span>( cmd[<span class="number">0</span>] == <span class="string">'A'</span>) {</span><br><span class="line"> cin >> st.info.name >> st.info.id >> st.score ;</span><br><span class="line"> mp.<span class="built_in">insert</span>(<span class="built_in">make_pair</span>(st.score,st.info ));</span><br><span class="line"> } <span class="comment">//make_pair生成一个 pair<int,StudentInfo>变量</span></span><br><span class="line"> <span class="comment">//其first 等于 st.score, second 等于 st.info</span></span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">if</span>( cmd[<span class="number">0</span>] == <span class="string">'Q'</span> ){</span><br><span class="line"> <span class="keyword">int</span> score;</span><br><span class="line"> cin >> score;</span><br><span class="line"> MAP_STD::iterator p = mp.<span class="built_in">lower_bound</span> (score);</span><br><span class="line"> <span class="keyword">if</span>( p!= mp.<span class="built_in">begin</span>()) { </span><br><span class="line"> --p;</span><br><span class="line"> score = p->first; <span class="comment">//比要查询分数低的最高分</span></span><br><span class="line"> MAP_STD::iterator maxp = p; </span><br><span class="line"> <span class="keyword">int</span> maxId = p->second.id; </span><br><span class="line"> <span class="keyword">for</span>(; p != mp.<span class="built_in">begin</span>() && p->first == score; --p) {</span><br><span class="line"> <span class="comment">//遍历所有成绩和score相等的学生</span></span><br><span class="line"> <span class="keyword">if</span>( p->second.id > maxId ) {</span><br><span class="line"> maxp = p;</span><br><span class="line"> maxId = p->second.id ;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>( p->first == score) { </span><br><span class="line"> <span class="comment">//如果上面循环是因为 p == mp.begin() 而终止,则p指向的元素还要处理</span></span><br><span class="line"> <span class="keyword">if</span>( p->second.id > maxId ) {</span><br><span class="line"> maxp = p;</span><br><span class="line"> maxId = p->second.id ;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> cout << maxp->second.name << <span class="string">" "</span> << maxp->second.id << <span class="string">" "</span> << maxp->first << endl;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//lower_bound的结果就是 begin,说明没人分数比查询分数低</span></span><br><span class="line"> <span class="keyword">else</span> cout << <span class="string">"Nobody"</span> << endl;</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h3 id="map"><a href="#map" class="headerlink" title="map"></a>map</h3><p>和multimap区别在于:</p>
<p>不能有关键字重复的元素</p>
<p>可以使用 [] ,下标为关键字,返回值为first和关键字相同的元素的second</p>
<p>当然插入元素也可能失败<br>[]的用法如下:</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Student</span> {</span></span><br><span class="line"> string name;</span><br><span class="line"> <span class="keyword">int</span> score;</span><br><span class="line">};</span><br><span class="line"><span class="keyword">typedef</span> map<string,<span class="keyword">int</span>> MP;</span><br><span class="line">......</span><br><span class="line">Student students[<span class="number">5</span>] = {{<span class="string">"Jack"</span>,<span class="number">89</span>},{<span class="string">"Tom"</span>,<span class="number">74</span>},{<span class="string">"Cindy"</span>,<span class="number">87</span>},{<span class="string">"Alysa"</span>,<span class="number">87</span>},{<span class="string">"Micheal"</span>,<span class="number">98</span>}};</span><br><span class="line">......</span><br><span class="line">cout << mp[<span class="string">"Jack"</span>] << endl; <span class="comment">// 输出 89</span></span><br><span class="line">mp[<span class="string">"Jack"</span>] = <span class="number">60</span>; <span class="comment">//修改名为"Jack"的元素的second</span></span><br></pre></td></tr></tbody></table></figure>
<p><strong>很实用的例子</strong>:</p>
<p>单词词频统计程序</p>
<p>输入<font color="red">大量</font>单词,每个单词,一行,不超过20字符,没有空格。 按出现次数从多到少输出这些单词及其出现次数 。出现次数相同的,字典序靠前的在前面</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line">输入样例:</span><br><span class="line"><span class="keyword">this</span></span><br><span class="line">is</span><br><span class="line">ok</span><br><span class="line"><span class="keyword">this</span></span><br><span class="line">plus</span><br><span class="line">that</span><br><span class="line">is</span><br><span class="line">plus</span><br><span class="line">plus</span><br><span class="line">输出样例:</span><br><span class="line">plus <span class="number">3</span></span><br><span class="line">is <span class="number">2</span></span><br><span class="line"><span class="keyword">this</span> <span class="number">2</span></span><br><span class="line">ok <span class="number">1</span></span><br><span class="line">that <span class="number">1</span></span><br></pre></td></tr></tbody></table></figure>
<p> 首先你要知道,大量意味着常规的二分加排序的方法必然超时,而用自定义排序规则的set和方便存储的map就可以高效解决问题</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><set></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><map></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><string></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Word</span> {</span></span><br><span class="line"> <span class="keyword">int</span> times;</span><br><span class="line"> string wd;</span><br><span class="line">};</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Rule</span> {</span></span><br><span class="line"> <span class="function"><span class="keyword">bool</span> <span class="title">operator</span> <span class="params">()</span> <span class="params">( <span class="keyword">const</span> Word & w1,<span class="keyword">const</span> Word & w2)</span> <span class="keyword">const</span> </span>{</span><br><span class="line"> <span class="keyword">if</span>( w1.times != w2.times)</span><br><span class="line"> <span class="keyword">return</span> w1.times > w2.times;</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="keyword">return</span> w1.wd < w2.wd;</span><br><span class="line"> }</span><br><span class="line">};</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> string s;</span><br><span class="line"> set<Word,Rule> st;</span><br><span class="line"> map<string,<span class="keyword">int</span>> mp;</span><br><span class="line"> <span class="keyword">while</span>( cin >> s ) </span><br><span class="line"> ++ mp[s] ;</span><br><span class="line"> <span class="keyword">for</span>( map<string,<span class="keyword">int</span>>::iterator i = mp.<span class="built_in">begin</span>(); </span><br><span class="line"> i != mp.<span class="built_in">end</span>(); ++i) {</span><br><span class="line"> Word tmp;</span><br><span class="line"> tmp.wd = i->first;</span><br><span class="line"> tmp.times = i->second;</span><br><span class="line"> st.<span class="built_in">insert</span>(tmp);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(set<Word,Rule>::iterator i = st.<span class="built_in">begin</span>(); </span><br><span class="line"> i != st.<span class="built_in">end</span>(); ++i) </span><br><span class="line"> cout << i->wd << <span class="string">" "</span> << i->times << endl;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
]]></content>
<tags>
<tag>algorithm</tag>
</tags>
</entry>
<entry>
<title>旧题整理</title>
<url>/2022/02/05/30/</url>
<content><![CDATA[<h2 id="郑轻oj"><a href="#郑轻oj" class="headerlink" title="郑轻oj"></a>郑轻oj</h2><h3 id="1124-两个有序数组合并"><a href="#1124-两个有序数组合并" class="headerlink" title="1124: 两个有序数组合并"></a>1124: 两个有序数组合并</h3><p><strong>题目描述</strong></p>
<p>已知数组a中有m个按升序序排列的元素,数组b中有n个降序排列的元素,编程将a与b中的所有元素按降序存入数组c中。</p>
<p><strong>输入</strong></p>
<p>输入有两行,第一行首先是一个正整数m,然后是m个整数;第二行首先是一个正整数n,然后是n个整数,m, n均小于等于1000000。</p>
<p><strong>输出</strong></p>
<p>输出合并后的m+n个整数,数据之间用空格隔开,输出占一行。</p>
<p><strong>样例输入</strong></p>
<figure class="highlight plaintext"><table><tbody><tr><td class="code"><pre><span class="line">4 1 2 5 7</span><br><span class="line">3 6 4 2</span><br></pre></td></tr></tbody></table></figure>
<p><strong>样例输出</strong></p>
<figure class="highlight plaintext"><table><tbody><tr><td class="code"><pre><span class="line">7 6 5 4 2 2 1</span><br></pre></td></tr></tbody></table></figure>
<figure class="highlight c"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><stdio.h></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><stdlib.h></span></span></span><br><span class="line"><span class="keyword">int</span> a[<span class="number">1000010</span>],b[<span class="number">1000010</span>];</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> i,j;</span><br><span class="line"> <span class="keyword">int</span> m,n;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>,&m);</span><br><span class="line"> <span class="keyword">for</span>(i=m<span class="number">-1</span>; i>=<span class="number">0</span>; i--)</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>,&a[i]);</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>,&n);</span><br><span class="line"> <span class="keyword">for</span>(j=<span class="number">0</span>; j<n; j++)</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>,&b[j]);</span><br><span class="line"> </span><br><span class="line"> i=<span class="number">0</span>;</span><br><span class="line"> j=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">int</span> k=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">int</span> c[m+n];</span><br><span class="line"> <span class="keyword">while</span>(i<m&&j<n)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(a[i]>=b[j])</span><br><span class="line"> c[k++]=a[i++];</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> c[k++]=b[j++];</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">while</span> (i < m)</span><br><span class="line"> c[k++] = a[i++];</span><br><span class="line"> <span class="keyword">while</span> (j < n)</span><br><span class="line"> c[k++] = b[j++];</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">for</span>(k=<span class="number">0</span>; k<m+n<span class="number">-1</span>; k++)</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d "</span>,c[k]);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d"</span>,c[m+n<span class="number">-1</span>]);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h3 id="1127-矩阵乘积"><a href="#1127-矩阵乘积" class="headerlink" title="1127: 矩阵乘积"></a>1127: 矩阵乘积</h3><p><strong>题目描述</strong></p>
<p>计算两个矩阵A和B的乘积。</p>
<p><strong>输入</strong></p>
<p>第一行三个正整数m、p和n,0<=m,n,p<=10,表示矩阵A是m行p列,矩阵B是p行n列;<br>接下来的m行是矩阵A的内容,每行p个整数,用空格隔开;<br>最后的p行是矩阵B的内容,每行n个整数,用空格隔开。</p>
<p><strong>输出</strong></p>
<p>输出乘积矩阵:输出占m行,每行n个数据,以空格隔开。</p>
<p><strong>样例输入</strong></p>
<figure class="highlight plaintext"><table><tbody><tr><td class="code"><pre><span class="line">2 3 4</span><br><span class="line"></span><br><span class="line">1 0 1</span><br><span class="line">0 0 1</span><br><span class="line"></span><br><span class="line">1 1 1 3</span><br><span class="line">4 5 6 7</span><br><span class="line">8 9 1 0</span><br></pre></td></tr></tbody></table></figure>
<figure class="highlight c"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><stdio.h></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><stdlib.h></span></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> a[<span class="number">11</span>][<span class="number">11</span>],b[<span class="number">11</span>][<span class="number">11</span>],c[<span class="number">11</span>][<span class="number">11</span>];</span><br><span class="line"> <span class="keyword">int</span> m,p,n;</span><br><span class="line"> <span class="keyword">int</span> i,j,k;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d%d%d"</span>,&m,&p,&n);</span><br><span class="line"> <span class="keyword">for</span>(i=<span class="number">0</span>; i<m; i++)</span><br><span class="line"> <span class="keyword">for</span>(j=<span class="number">0</span>; j<p; j++)</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>,&a[i][j]);</span><br><span class="line"> <span class="keyword">for</span>(i=<span class="number">0</span>; i<p; i++)</span><br><span class="line"> <span class="keyword">for</span>(j=<span class="number">0</span>; j<n; j++)</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>,&b[i][j]);</span><br><span class="line"> <span class="keyword">for</span>(i=<span class="number">0</span>; i<m; i++)</span><br><span class="line"> <span class="keyword">for</span>(j=<span class="number">0</span>; j<n; j++)</span><br><span class="line"> {</span><br><span class="line"> c[i][j]=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(k=<span class="number">0</span>; k<p; k++)</span><br><span class="line"> {</span><br><span class="line"> c[i][j]+=a[i][k]*b[k][j];</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(i=<span class="number">0</span>; i<m; i++)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">for</span>(j=<span class="number">0</span>; j<n; j++)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(j==<span class="number">0</span>)</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d"</span>,c[i][j]);</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">" %d"</span>,c[i][j]);</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"\n"</span>);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h3 id="1143-1144-多种进制"><a href="#1143-1144-多种进制" class="headerlink" title="1143-1144: 多种进制"></a>1143-1144: 多种进制</h3><p>将10进制正整数n转换成k进制数字符串输出</p>
<figure class="highlight c"><table><tbody><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">dToK</span><span class="params">(<span class="keyword">int</span> n,<span class="keyword">int</span> k,<span class="keyword">char</span> str[])</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> i,x=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(i=<span class="number">0</span>;n!=<span class="number">0</span>;i++)</span><br><span class="line"> {</span><br><span class="line"> str[i]=n%k;</span><br><span class="line"> n=n/k;</span><br><span class="line"> x++;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(i=x<span class="number">-1</span>;i>=<span class="number">0</span>;i--)</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d"</span>,str[i]);</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<p>将k进制数字符串转换成10进制正整数n输出</p>
<figure class="highlight c"><table><tbody><tr><td class="code"><pre><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">KToD</span><span class="params">(<span class="keyword">char</span> str[], <span class="keyword">int</span> k)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> i;</span><br><span class="line"> <span class="keyword">int</span> sum = <span class="number">0</span>;</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">for</span>(i=<span class="number">0</span>; i<<span class="built_in">strlen</span>(str); i++)</span><br><span class="line"> {</span><br><span class="line"> sum=sum*k+(str[i]-<span class="string">'0'</span>);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> sum;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h3 id="1151-大整数加法"><a href="#1151-大整数加法" class="headerlink" title="1151: 大整数加法"></a>1151: 大整数加法</h3><figure class="highlight c"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><stdio.h></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><string.h></span></span></span><br><span class="line"><span class="keyword">char</span> m[<span class="number">1022</span>],n[<span class="number">1022</span>];</span><br><span class="line"><span class="keyword">int</span> c[<span class="number">1022</span>];</span><br><span class="line"><span class="keyword">int</span> lres;</span><br><span class="line"><span class="keyword">int</span> lm,ln;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">update</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">if</span>(c[lres] >=<span class="number">10</span>)</span><br><span class="line"> c[++lres]++,c[lres<span class="number">-1</span>] %=<span class="number">10</span>;</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> lres++;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> t;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>,&t);</span><br><span class="line"> getchar();</span><br><span class="line"> <span class="keyword">while</span>(t--)</span><br><span class="line"> {</span><br><span class="line"> <span class="built_in">memset</span>(c,<span class="number">0</span>,<span class="keyword">sizeof</span>(c));</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%s%s"</span>,m,n);</span><br><span class="line"> lm = <span class="built_in">strlen</span>(m)<span class="number">-1</span>;</span><br><span class="line"> ln = <span class="built_in">strlen</span>(n)<span class="number">-1</span>;</span><br><span class="line"> lres = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span>(lm>=<span class="number">0</span>&&ln>=<span class="number">0</span>)</span><br><span class="line"> c[lres] += m[lm--]-<span class="string">'0'</span> + n[ln--] - <span class="string">'0'</span>,update();</span><br><span class="line"> <span class="keyword">while</span>(lm>=<span class="number">0</span>)</span><br><span class="line"> c[lres]+=m[lm--]-<span class="string">'0'</span>,update();</span><br><span class="line"> <span class="keyword">while</span>(ln>=<span class="number">0</span>)</span><br><span class="line"> c[lres] += n[ln--]-<span class="string">'0'</span>,update();</span><br><span class="line"> <span class="keyword">int</span> f = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=lres; i>=<span class="number">0</span>; i--)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(c[i]!=<span class="number">0</span>) f=<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">if</span>(f == <span class="number">1</span>)</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d"</span>,c[i]);</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"\n"</span>);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h3 id="1152-二分搜索"><a href="#1152-二分搜索" class="headerlink" title="1152: 二分搜索"></a>1152: 二分搜索</h3><figure class="highlight c"><table><tbody><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">BSearch</span><span class="params">(<span class="keyword">int</span> a[],<span class="keyword">int</span> x,<span class="keyword">int</span> low,<span class="keyword">int</span> high)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> mid;</span><br><span class="line"> <span class="keyword">if</span>(low>high)</span><br><span class="line"> <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> {</span><br><span class="line"> mid=(low+high)/<span class="number">2</span>;</span><br><span class="line"> <span class="keyword">if</span>(x==a[mid])</span><br><span class="line"> <span class="keyword">return</span> mid;</span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">if</span>(x<a[mid])</span><br><span class="line"> <span class="keyword">return</span> BSearch(a,x,low,mid<span class="number">-1</span>);</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="keyword">return</span> BSearch(a,x,mid+<span class="number">1</span>,high);</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h3 id="1673-机智的字符"><a href="#1673-机智的字符" class="headerlink" title="1673: 机智的字符"></a><a href="http://acm.zzuli.edu.cn/problem.php?id=1673">1673: 机智的字符</a></h3><figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> m,n,idx=<span class="number">0</span>,i,j;</span><br><span class="line"> cin>>m>>n;</span><br><span class="line"> <span class="keyword">for</span>(i=<span class="number">0</span>;i<m;i++)</span><br><span class="line"> {</span><br><span class="line"> cout<<<span class="string">">"</span>;</span><br><span class="line"> <span class="keyword">for</span>(j=<span class="number">0</span>;j<n;j++,idx++)</span><br><span class="line"> {</span><br><span class="line"> cout<<(<span class="keyword">char</span>)(<span class="string">'a'</span>+idx%<span class="number">26</span>);</span><br><span class="line"> }</span><br><span class="line"> cout<<<span class="string">"<"</span><<endl;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h3 id="2838-小豪的室友"><a href="#2838-小豪的室友" class="headerlink" title="2838: 小豪的室友"></a><a href="http://acm.zzuli.edu.cn/problem.php?id=2838">2838: 小豪的室友</a></h3><p>最终状态有两种: </p>
<p>要么 wbwbwbw…. </p>
<p>要么 bwbwbwb…. </p>
<p>所以我们枚举两种情况,对于任何一种情况,分别用 x 和 y 表示没有 放对位置的黑书和白书的数量。对于每种情况,我们都先找出最小 的 交换次数,即 min(x,y),剩下的我们直接从小豪那里拿书替代。 故每种情况最少的操作次数为 : </p>
<blockquote>
<p>min(x,y)+[max(x,y)-min(x,y)]=max(x,y); </p>
</blockquote>
<p>则最终结果: </p>
<blockquote>
<p>ans=min(max(x1,y1),max(x2,y2));</p>
</blockquote>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><stdio.h></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><string.h></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><ctype.h></span></span></span><br><span class="line"><span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">long</span> ll;</span><br><span class="line"><span class="keyword">char</span> S[<span class="number">100010</span>];</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">max</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> y)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">if</span> (x > y) <span class="keyword">return</span> x;</span><br><span class="line"> <span class="keyword">return</span> y;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">min</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> y)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">if</span> (x > y) <span class="keyword">return</span> y;</span><br><span class="line"> <span class="keyword">return</span> x;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> n, len;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &n);</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%s"</span>, S);</span><br><span class="line"> <span class="keyword">int</span> flag1, flag2;</span><br><span class="line"> <span class="keyword">int</span> sum1, sum2;</span><br><span class="line"> flag1 = flag2 = sum1 = sum2 = <span class="number">0</span>;</span><br><span class="line"> len = <span class="built_in">strlen</span>(S);</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < len; i++) {</span><br><span class="line"> <span class="keyword">if</span> (S[i] == <span class="string">'w'</span>) {</span><br><span class="line"> <span class="keyword">if</span> (i & <span class="number">1</span>)flag1++;</span><br><span class="line"> <span class="keyword">else</span> flag2++;</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> <span class="keyword">if</span> (i & <span class="number">1</span>) sum1++;</span><br><span class="line"> <span class="keyword">else</span> sum2++;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">int</span> ans = <span class="built_in">min</span>(<span class="built_in">max</span>(flag1, sum2), <span class="built_in">max</span>(sum1, flag2));</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d"</span>, ans);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h3 id="2859-高难度运算"><a href="#2859-高难度运算" class="headerlink" title="2859: 高难度运算"></a><a href="http://acm.zzuli.edu.cn/problem.php?id=2859">2859: 高难度运算</a></h3><p>有一个不错的位运算公式</p>
<blockquote>
<p>(a&b)+(a|b)=a+b;</p>
</blockquote>
<p>然后就是根据序列和来推出原序列了</p>
<h3 id="2872-小T的操作序列"><a href="#2872-小T的操作序列" class="headerlink" title="2872: 小T的操作序列"></a><a href="http://acm.zzuli.edu.cn/problem.php?id=2872">2872: 小T的操作序列</a></h3><figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">char</span> s[<span class="number">1010</span>];</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">long</span> <span class="keyword">long</span> n;</span><br><span class="line"> <span class="keyword">int</span> t;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%lld%d"</span>, &n, &t);</span><br><span class="line"> <span class="keyword">while</span> (t--)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">int</span> x, y;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d%d"</span>, &x, &y);</span><br><span class="line"> <span class="keyword">int</span> a = n & (<span class="number">1</span> << (x - <span class="number">1</span>));</span><br><span class="line"> <span class="keyword">if</span> (a)</span><br><span class="line"> {</span><br><span class="line"> n -= (<span class="number">1</span> << (x - <span class="number">1</span>));</span><br><span class="line"> x += y; </span><br><span class="line"> <span class="keyword">int</span> b = n & (<span class="number">1</span> << (x - <span class="number">1</span>));</span><br><span class="line"> n += (<span class="number">1</span> << (x - <span class="number">1</span>));</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%lld\n"</span>, n);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="牛客"><a href="#牛客" class="headerlink" title="牛客"></a>牛客</h2><p>待更新</p>
]]></content>
<tags>
<tag>contest</tag>
</tags>
</entry>
<entry>
<title>C++入门笔记(1)</title>
<url>/2022/02/05/31/</url>
<content><![CDATA[<h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><p>笔记整理自mooc<a href="https://www.icourse163.org/course/PKU-1002029030">程序设计与算法(三)C++面向对象程序设计</a></p>
<p>郭炜老师讲的还是很清楚的</p>
<h2 id="第一章:从-C-到C"><a href="#第一章:从-C-到C" class="headerlink" title="第一章:从 C 到C++"></a>第一章:从 C 到C++</h2><h3 id="引用"><a href="#引用" class="headerlink" title="引用"></a>引用</h3><blockquote>
<p>类型名 & 引用名 = 某变量名;</p>
</blockquote>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="keyword">int</span> n = <span class="number">4</span>; </span><br><span class="line"><span class="keyword">int</span> & r = n; </span><br></pre></td></tr></tbody></table></figure>
<p>r引用了n,r的类型是<strong>int &</strong></p>
<p>之后对r的修改就是对n的修改,对n的修改就是对r的修改</p>
<p>定义引用时一定要将其初始化成引用某个<strong>变量</strong>,之后不会再引用其他变量</p>
<p>在C语言中,我们可以通过指针实现两个数交换</p>
<figure class="highlight c"><table><tbody><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">swap</span><span class="params">( <span class="keyword">int</span> * a, <span class="keyword">int</span> * b)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"><span class="keyword">int</span> tmp;</span><br><span class="line">tmp = * a; * a = * b; * b = tmp;</span><br><span class="line">}</span><br><span class="line"><span class="keyword">int</span> n1, n2;</span><br><span class="line">swap(& n1,& n2) ; </span><br></pre></td></tr></tbody></table></figure>
<p>有了C++的引用以后,可以这样写</p>
<figure class="highlight c"><table><tbody><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">swap</span><span class="params">( <span class="keyword">int</span> & a, <span class="keyword">int</span> & b)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"><span class="keyword">int</span> tmp;</span><br><span class="line">tmp = a; a = b; b = tmp;</span><br><span class="line">}</span><br><span class="line"><span class="keyword">int</span> n1, n2;</span><br><span class="line">swap(n1,n2) ;</span><br></pre></td></tr></tbody></table></figure>
<p><strong>引用作为函数的返回值</strong></p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="keyword">int</span> n = <span class="number">4</span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> & <span class="title">SetValue</span><span class="params">()</span> </span></span><br><span class="line"><span class="function"></span>{ </span><br><span class="line"> <span class="keyword">return</span> n; </span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="built_in">SetValue</span>() = <span class="number">40</span>;<span class="comment">//类似于指针指向n</span></span><br><span class="line"> cout << n;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">} <span class="comment">//输出:40</span></span><br></pre></td></tr></tbody></table></figure>
<p><strong>常引用</strong></p>
<p>定义引用时,前面加const关键字,即为“常引用”</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="keyword">int</span> n; </span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> & r = n; </span><br></pre></td></tr></tbody></table></figure>
<p> r 的类型是 const int &</p>
<p>注意:不能通过常引用去修改其引用的内容</p>
<h3 id="“const”关键字的用法"><a href="#“const”关键字的用法" class="headerlink" title="“const”关键字的用法"></a>“const”关键字的用法</h3><p><strong>定义常量指针,不可通过常量指针修改其指向的内容,但可以修改指向</strong></p>
<p>意义:函数参数为常量指针时,可避免函数内部不小心改变参数指针所指地方的内容</p>
<h3 id="动态内存分配"><a href="#动态内存分配" class="headerlink" title="动态内存分配"></a>动态内存分配</h3><p><strong>用new 运算符实现动态内存分配</strong></p>
<p>第一种用法,分配一个变量:</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line">P = <span class="keyword">new</span> Type;</span><br></pre></td></tr></tbody></table></figure>
<p>T是任意<strong>类型名</strong>,P是类型为T<em> 的指针。 动态分配出一片大小为 sizeof(T)字节的内存空间,并且将该 内存空间的<em>*起始地址</em></em>赋值给P,如:</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="keyword">int</span> * pn;</span><br><span class="line">pn = <span class="keyword">new</span> <span class="keyword">int</span>; </span><br><span class="line">* pn = <span class="number">5</span>;</span><br></pre></td></tr></tbody></table></figure>
<p>第二种用法,分配一个数组:</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line">P = <span class="keyword">new</span> T[N];</span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> * pn;</span><br><span class="line"><span class="keyword">int</span> i = <span class="number">5</span>;</span><br><span class="line">pn = <span class="keyword">new</span> <span class="keyword">int</span>[i * <span class="number">20</span>];</span><br><span class="line">pn[<span class="number">0</span>] = <span class="number">20</span>;</span><br></pre></td></tr></tbody></table></figure>
<p>用“new”动态分配的内存空间,一定要用 “delete”(delete指针)运算符进行释放</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="keyword">int</span> * p = <span class="keyword">new</span> <span class="keyword">int</span>;</span><br><span class="line">* p = <span class="number">5</span>;</span><br><span class="line"><span class="keyword">delete</span> p;<span class="comment">//只能释放一次</span></span><br><span class="line"><span class="comment">//对于数组:</span></span><br><span class="line"><span class="keyword">int</span> * p = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">20</span>];</span><br><span class="line">p[<span class="number">0</span>] = <span class="number">1</span>;</span><br><span class="line"><span class="keyword">delete</span> [ ] p;</span><br></pre></td></tr></tbody></table></figure>
<h3 id="内联函数"><a href="#内联函数" class="headerlink" title="内联函数"></a>内联函数</h3><p>可以减少一些很简单的函数过多调用造成的开销</p>
<p>如:</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">int</span> <span class="title">Max</span><span class="params">(<span class="keyword">int</span> a,<span class="keyword">int</span> b)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"><span class="keyword">if</span>( a > b) <span class="keyword">return</span> a;</span><br><span class="line"><span class="keyword">return</span> b;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h3 id="函数重载"><a href="#函数重载" class="headerlink" title="函数重载"></a>函数重载</h3><p>一个或多个函数,名字相同,然而参数个数或参数类型不相同,这叫做函数的重载。</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">Max</span><span class="params">(<span class="keyword">double</span> f1,<span class="keyword">double</span> f2)</span> </span>{ }</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">Max</span><span class="params">(<span class="keyword">int</span> n1,<span class="keyword">int</span> n2)</span> </span>{ }</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">Max</span><span class="params">(<span class="keyword">int</span> n1,<span class="keyword">int</span> n2,<span class="keyword">int</span> n3)</span> </span>{ }</span><br><span class="line"><span class="built_in">Max</span>(<span class="number">3.4</span>,<span class="number">2.5</span>); <span class="comment">//调用 (1)</span></span><br><span class="line"><span class="built_in">Max</span>(<span class="number">2</span>,<span class="number">4</span>); <span class="comment">//调用 (2)</span></span><br><span class="line"><span class="built_in">Max</span>(<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>); <span class="comment">//调用 (3)</span></span><br><span class="line"><span class="built_in">Max</span>(<span class="number">3</span>,<span class="number">2.4</span>); <span class="comment">//error,二义性</span></span><br></pre></td></tr></tbody></table></figure>
<p>编译器会根据调用语句的中的实参的个数和类型判断应该调用哪个函数</p>
<h3 id="函数的缺省参数"><a href="#函数的缺省参数" class="headerlink" title="函数的缺省参数"></a>函数的缺省参数</h3><p>C++中,定义函数的时候可以让最右边的连续若干个参数有缺省值,那么调用函数的时候,若相应位置不写参数,参数就是缺省值。</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">func</span><span class="params">( <span class="keyword">int</span> x1, <span class="keyword">int</span> x2 = <span class="number">2</span>, <span class="keyword">int</span> x3 = <span class="number">3</span>)</span> </span></span><br><span class="line"><span class="function"></span>{ }</span><br><span class="line"><span class="built_in">func</span>(<span class="number">10</span> ) ; <span class="comment">//等效于 func(10,2,3)</span></span><br><span class="line"><span class="built_in">func</span>(<span class="number">10</span>,<span class="number">8</span>) ; <span class="comment">//等效于 func(10,8,3)</span></span><br><span class="line"><span class="built_in">func</span>(<span class="number">10</span>, , <span class="number">8</span>) ; <span class="comment">//不行,只能最右边的连续若干个参数缺省</span></span><br></pre></td></tr></tbody></table></figure>
<p>目的在于提高程序的可扩充性</p>
]]></content>
<tags>
<tag>C++</tag>
</tags>
</entry>
<entry>
<title>C++入门笔记(2)</title>
<url>/2022/02/08/32/</url>
<content><![CDATA[<h2 id="第二章-类和对象基础"><a href="#第二章-类和对象基础" class="headerlink" title="第二章 类和对象基础"></a>第二章 类和对象基础</h2><h3 id="类成员的可访问范围"><a href="#类成员的可访问范围" class="headerlink" title="类成员的可访问范围"></a>类成员的可访问范围</h3><p>在类的定义中,用下列访问范围关键字来说明类成员可被访问的范围:</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line">– <span class="keyword">private</span>: 私有成员,只能在成员函数内访问</span><br><span class="line">– <span class="keyword">public</span> : 公有成员,可以在任何地方访问</span><br><span class="line">– <span class="keyword">protected</span>: 保护成员</span><br></pre></td></tr></tbody></table></figure>
<p>以上三种关键字出现的<strong>次数和先后次序</strong>都没有限制。</p>
<p><strong>定义一个类:</strong></p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">className</span> {</span></span><br><span class="line"> <span class="keyword">private</span>:</span><br><span class="line"> 私有属性和函数</span><br><span class="line"> <span class="keyword">public</span>:</span><br><span class="line"> 公有属性和函数</span><br><span class="line"> <span class="keyword">protected</span>:</span><br><span class="line"> 保护属性和函数</span><br><span class="line">};</span><br></pre></td></tr></tbody></table></figure>
<p>如过某个成员前面没有上述关键字,则缺省地被认为是私有成员。</p>
<p><strong>注意</strong>:在类的成员函数以外的地方,只能够访问该类对象的<strong>公有成员</strong>。</p>
<p>“隐藏”的目的是强制对成员变量的访问一定要通过成员函数进行,那么以后成员变量的类型等属性修改后,只需要更改成员函数即可。否则,所有直接访问成员变量的语句都需要修改。</p>
<p><strong>用struct定义类:</strong></p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">CEmployee</span> {</span> </span><br><span class="line"> <span class="keyword">char</span> szName[<span class="number">30</span>]; <span class="comment">//缺省即公有</span></span><br><span class="line"> <span class="keyword">public</span> :</span><br><span class="line"> <span class="keyword">int</span> salary;</span><br><span class="line"> <span class="function"><span class="keyword">void</span> <span class="title">setName</span><span class="params">(<span class="keyword">char</span> * name)</span></span>; </span><br><span class="line"> <span class="function"><span class="keyword">void</span> <span class="title">getName</span><span class="params">(<span class="keyword">char</span> * name)</span></span>;</span><br><span class="line"> <span class="function"><span class="keyword">void</span> <span class="title">averageSalary</span><span class="params">(CEmployee e1,CEmployee e2)</span></span>;</span><br><span class="line">};</span><br></pre></td></tr></tbody></table></figure>
<p>和用”class”的唯一区别,就是未说明是公有还是私有的成员,就是公有</p>
<h3 id="成员函数的重载及参数缺省"><a href="#成员函数的重载及参数缺省" class="headerlink" title="成员函数的重载及参数缺省"></a>成员函数的重载及参数缺省</h3><figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Location</span> {</span></span><br><span class="line"> <span class="keyword">private</span> :</span><br><span class="line"> <span class="keyword">int</span> x, y;</span><br><span class="line"> <span class="keyword">public</span>:</span><br><span class="line"> <span class="function"><span class="keyword">void</span> <span class="title">init</span><span class="params">( <span class="keyword">int</span> x=<span class="number">0</span> , <span class="keyword">int</span> y = <span class="number">0</span> )</span></span>;</span><br><span class="line"> <span class="function"><span class="keyword">void</span> <span class="title">valueX</span><span class="params">( <span class="keyword">int</span> val )</span> </span>{ x = val ;}</span><br><span class="line"> <span class="function"><span class="keyword">int</span> <span class="title">valueX</span><span class="params">()</span> </span>{ <span class="keyword">return</span> x; }<span class="comment">//重载</span></span><br><span class="line">};</span><br></pre></td></tr></tbody></table></figure>
<p>成员函数可以重载,可以带缺省参数。</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">valueX</span><span class="params">( <span class="keyword">int</span> val = <span class="number">0</span>)</span> </span>{ x = val; }</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">valueX</span><span class="params">()</span> </span>{ <span class="keyword">return</span> x; }</span><br><span class="line">......</span><br><span class="line">A.<span class="built_in">valueX</span>();<span class="comment">//存在二义性,编译器无法判断调用哪个valueX</span></span><br></pre></td></tr></tbody></table></figure>
<h3 id="构造函数-constructor"><a href="#构造函数-constructor" class="headerlink" title="构造函数 (constructor)"></a>构造函数 (constructor)</h3><figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Complex</span> {</span></span><br><span class="line"> <span class="keyword">private</span> :</span><br><span class="line"> <span class="keyword">double</span> real, imag;</span><br><span class="line"> <span class="keyword">public</span>:</span><br><span class="line"> <span class="function"><span class="keyword">void</span> <span class="title">Set</span><span class="params">( <span class="keyword">double</span> r, <span class="keyword">double</span> i)</span></span>;</span><br><span class="line">}; <span class="comment">//编译器自动生成默认构造函数</span></span><br><span class="line">Complex c1; <span class="comment">//默认构造函数被调用</span></span><br></pre></td></tr></tbody></table></figure>
<p>成员函数的一种,<strong>名字与类名相同</strong>,可以有参数,不能有返回值(void也不行) </p>
<p><strong>作用</strong>是对对象进行初始化,如给成员变量<strong>赋初值</strong> </p>
<p>如果定义类时没写构造函数,则编译器生成一个<strong>默认的无参数的构造函数</strong>,默认构造函数无参数,不做任何操作,如果定义了构造函数,则编译器<strong>不生成</strong>默认的无参数的构造函数</p>
<p>对象生成时构造函数<strong>自动被调用</strong>,对象一旦生成,就再也不能在其上执行构造函数</p>
<p><strong>为什么需要:</strong></p>
<p>不必专门再写初始化函数,也不用担心忘记调用初始化函数</p>
<p>自己定义:</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Complex</span> {</span></span><br><span class="line"> <span class="keyword">private</span> :</span><br><span class="line"> <span class="keyword">double</span> real, imag;</span><br><span class="line"> <span class="keyword">public</span>:</span><br><span class="line"> <span class="built_in">Complex</span>( <span class="keyword">double</span> r, <span class="keyword">double</span> i = <span class="number">0</span>);</span><br><span class="line">}; </span><br><span class="line"> Complex::<span class="built_in">Complex</span>( <span class="keyword">double</span> r, <span class="keyword">double</span> i) </span><br><span class="line">{</span><br><span class="line"> real = r; imag = i;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function">Complex <span class="title">c1</span><span class="params">(<span class="number">2</span>,<span class="number">4</span>)</span>, <span class="title">c2</span><span class="params">(<span class="number">3</span>,<span class="number">5</span>)</span></span>;</span><br><span class="line">Complex * pc = <span class="keyword">new</span> <span class="built_in">Complex</span>(<span class="number">3</span>,<span class="number">4</span>);</span><br></pre></td></tr></tbody></table></figure>
<p>一个类可以有<strong>多个</strong>构造函数,类似<strong>重载</strong></p>
<h3 id="构造函数在数组中的使用"><a href="#构造函数在数组中的使用" class="headerlink" title="构造函数在数组中的使用"></a>构造函数在数组中的使用</h3><figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line">CSample array2[<span class="number">2</span>] = {<span class="number">4</span>,<span class="number">5</span>};</span><br><span class="line">CSample array3[<span class="number">2</span>] = {<span class="number">3</span>};<span class="comment">//注意这里只对array3[0]初始化成3,array3[1]默认初始化成0</span></span><br></pre></td></tr></tbody></table></figure>
<p>还可以这样写:</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Test</span> {</span></span><br><span class="line"> <span class="keyword">public</span>:</span><br><span class="line"> <span class="built_in">Test</span>( <span class="keyword">int</span> n) { } <span class="comment">//(1)</span></span><br><span class="line"> <span class="built_in">Test</span>( <span class="keyword">int</span> n, <span class="keyword">int</span> m) { } <span class="comment">//(2) </span></span><br><span class="line"> <span class="built_in">Test</span>() { } <span class="comment">//(3)</span></span><br><span class="line">};</span><br><span class="line">Test array1[<span class="number">3</span>] = { <span class="number">1</span>, <span class="built_in">Test</span>(<span class="number">1</span>,<span class="number">2</span>) };</span><br><span class="line"><span class="comment">// 三个元素分别用(1),(2),(3)初始化</span></span><br></pre></td></tr></tbody></table></figure>
<h3 id="复制构造函数-copyconstructor"><a href="#复制构造函数-copyconstructor" class="headerlink" title="复制构造函数 (copyconstructor)"></a>复制构造函数 (copyconstructor)</h3><p>只有一个参数,即<strong>对同类对象的引用</strong>。</p>
<p>形如 <strong>X::X( X& )或X::X(const X &)</strong>, 后者能以常量对象作为参数</p>
<p>如果没有定义复制构造函数,那么编译器生成<strong>默认复制构造函数</strong>。默认的复制构造函数完成复制功能。如果定义的自己的复制构造函数,则默认的复制构造函数不存在。</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Complex</span> {</span></span><br><span class="line"> <span class="keyword">public</span> :</span><br><span class="line"> <span class="keyword">double</span> real,imag;</span><br><span class="line"> <span class="built_in">Complex</span>(){ }</span><br><span class="line"> <span class="built_in">Complex</span>( <span class="keyword">const</span> Complex & c ) {</span><br><span class="line"> real = c.real;</span><br><span class="line"> imag = c.imag;</span><br><span class="line"> cout << “Copy Constructor called”;</span><br><span class="line"> }</span><br><span class="line">}; </span><br><span class="line">Complex c1; <span class="comment">//调用缺省无参构造函数</span></span><br><span class="line"><span class="function">Complex <span class="title">c2</span><span class="params">(c1)</span></span>;<span class="comment">//调用缺省的复制构造函数,将 c2 初始化成和c1一样</span></span><br></pre></td></tr></tbody></table></figure>
<h4 id="复制构造函数起作用的三种情况"><a href="#复制构造函数起作用的三种情况" class="headerlink" title="复制构造函数起作用的三种情况"></a>复制构造函数起作用的三种情况</h4><h5 id="1-当用一个对象去初始化同类的另一个对象时"><a href="#1-当用一个对象去初始化同类的另一个对象时" class="headerlink" title="1)当用一个对象去初始化同类的另一个对象时"></a>1)当用一个对象去初始化同类的另一个对象时</h5><figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="function">Complex <span class="title">c2</span><span class="params">(c1)</span></span>;</span><br><span class="line">Complex c2 = c1; <span class="comment">//初始化语句,非赋值语句</span></span><br></pre></td></tr></tbody></table></figure>
<h5 id="2-如果某函数有一个参数是类-A-的对象,-那么该函数被调用时,类A的复制构造函数将被调用"><a href="#2-如果某函数有一个参数是类-A-的对象,-那么该函数被调用时,类A的复制构造函数将被调用" class="headerlink" title="2)如果某函数有一个参数是类 A 的对象, 那么该函数被调用时,类A的复制构造函数将被调用"></a>2)如果某函数有一个参数是类 A 的对象, 那么该函数被调用时,类A的复制构造函数将被调用</h5><figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">A</span> </span></span><br><span class="line"><span class="class">{</span></span><br><span class="line"> <span class="keyword">public</span>:</span><br><span class="line"> <span class="built_in">A</span>() { };</span><br><span class="line"> <span class="built_in">A</span>( A & a) { </span><br><span class="line"> cout << <span class="string">"Copy constructor called"</span> <<endl;</span><br><span class="line"> }</span><br><span class="line">};</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Func</span><span class="params">(A a1)</span></span>{ }</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>{</span><br><span class="line"> A a2;</span><br><span class="line"> <span class="built_in">Func</span>(a2);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line">输出结果: </span><br><span class="line">Copy constructor called</span><br></pre></td></tr></tbody></table></figure>
<h5 id="3-如果函数的返回值是类A的对象时,则函数返回时,-A的复制构造函数被调用"><a href="#3-如果函数的返回值是类A的对象时,则函数返回时,-A的复制构造函数被调用" class="headerlink" title="3) 如果函数的返回值是类A的对象时,则函数返回时, A的复制构造函数被调用"></a>3) 如果函数的返回值是类A的对象时,则函数返回时, A的复制构造函数被调用</h5><figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">A</span> </span></span><br><span class="line"><span class="class">{</span></span><br><span class="line"> <span class="keyword">public</span>:</span><br><span class="line"> <span class="keyword">int</span> v;</span><br><span class="line"> <span class="built_in">A</span>(<span class="keyword">int</span> n) { v = n; };</span><br><span class="line"> <span class="built_in">A</span>( <span class="keyword">const</span> A & a) { </span><br><span class="line"> v = a.v;</span><br><span class="line"> cout << <span class="string">"Copy constructor called"</span> <<endl;</span><br><span class="line"> }</span><br><span class="line">};</span><br><span class="line"><span class="function">A <span class="title">Func</span><span class="params">()</span> </span>{ </span><br><span class="line"> <span class="function">A <span class="title">b</span><span class="params">(<span class="number">4</span>)</span></span>; </span><br><span class="line"> <span class="keyword">return</span> b; </span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{ </span><br><span class="line"> cout << <span class="built_in">Func</span>().v << endl; </span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>; </span><br><span class="line">}</span><br><span class="line">输出结果:</span><br><span class="line">Copy constructor called</span><br><span class="line"><span class="number">4</span></span><br></pre></td></tr></tbody></table></figure>
<p><strong>注意</strong>:对象间赋值并不导致复制构造函数被调用,还有就是<strong>引用前加上const</strong>可以防止实参被误改</p>
<h3 id="类型转换构造函数"><a href="#类型转换构造函数" class="headerlink" title="类型转换构造函数"></a>类型转换构造函数</h3><p>定义转换构造函数的目的是<strong>实现类型的自动转换</strong>。</p>
<p>只有一个参数,而且不是复制构造函数的构造函数,一般就可以看作是转换构造函数。</p>
<p>当需要的时候,编译系统会自动调用转换构造函数,<strong>建立一个无名的临时对象</strong>(或临时变量)。</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Complex</span> {</span></span><br><span class="line"> <span class="keyword">public</span>: </span><br><span class="line"> <span class="keyword">double</span> real, imag;</span><br><span class="line"> <span class="built_in">Complex</span>( <span class="keyword">int</span> i) {<span class="comment">//类型转换构造函数</span></span><br><span class="line"> cout << <span class="string">"IntConstructor called"</span> << endl;</span><br><span class="line"> real = i; imag = <span class="number">0</span>; </span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">Complex</span>(<span class="keyword">double</span> r,<span class="keyword">double</span> i) {real = r; imag = i; }</span><br><span class="line">};</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span> <span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="function">Complex <span class="title">c1</span><span class="params">(<span class="number">7</span>,<span class="number">8</span>)</span></span>;</span><br><span class="line"> Complex c2 = <span class="number">12</span>;</span><br><span class="line"> c1 = <span class="number">9</span>; <span class="comment">//9不是对象不能初始化,所以9被自动转换成一个临时Complex对象</span></span><br><span class="line"> cout << c1.real << <span class="string">","</span> << c1.imag << endl;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h3 id="析构函数-destructors"><a href="#析构函数-destructors" class="headerlink" title="析构函数 (destructors)"></a>析构函数 (destructors)</h3><p>名字<strong>与类名相同</strong>,在前面加‘~’ ,没有参数和返回值,一个类<strong>最多只能有一个析构函数</strong>。</p>
<p>析构函数对象消亡时即自动被调用。可以定义析构函数来在对象消亡前做善后工作,比如释放分配的空间等。</p>
<p>如果定义类时没写析构函数,则编译器生成缺省析构函数。 缺省析构函数什么也不做。</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">String</span>{</span></span><br><span class="line"> <span class="keyword">private</span> :</span><br><span class="line"> <span class="keyword">char</span> * p;</span><br><span class="line"> <span class="keyword">public</span>:</span><br><span class="line"> <span class="built_in">String</span> () {</span><br><span class="line"> p = <span class="keyword">new</span> <span class="keyword">char</span>[<span class="number">10</span>];</span><br><span class="line"> }</span><br><span class="line"> ~ <span class="built_in">String</span> () ;</span><br><span class="line">};</span><br><span class="line">String ::~ <span class="built_in">String</span>()</span><br><span class="line">{</span><br><span class="line"> <span class="keyword">delete</span> [] p;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<p>对象数组生命期结束时,对象数组的每个元素的析构函数都会被调用。</p>
<p>delete对象导致析构函数调用</p>
<p>析构函数在对象作为函数返回值返回后被调用</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">CMyclass</span> {</span></span><br><span class="line"> <span class="keyword">public</span>:</span><br><span class="line"> ~<span class="built_in">CMyclass</span>() { cout << <span class="string">"destructor"</span> << endl; }</span><br><span class="line">};</span><br><span class="line"> CMyclass obj;</span><br><span class="line"> <span class="function">CMyclass <span class="title">fun</span><span class="params">(CMyclass sobj )</span> </span>{ <span class="comment">//参数对象消亡也会导致析</span></span><br><span class="line"><span class="comment">//构函数被调用</span></span><br><span class="line"> <span class="keyword">return</span> sobj; <span class="comment">//函数调用返回时生成临时对象返回</span></span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>{</span><br><span class="line"> obj = <span class="built_in">fun</span>(obj); <span class="comment">//函数调用的返回值(临时对象)被</span></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>; <span class="comment">//用过后,该临时对象析构函数被调用</span></span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<p>下面有一个不错的例子可以检验一下你的学习成果✨</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Demo</span> {</span></span><br><span class="line"> <span class="keyword">int</span> id;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line"> <span class="built_in">Demo</span>(<span class="keyword">int</span> i) {</span><br><span class="line"> id = i;</span><br><span class="line"> cout << <span class="string">"id="</span> << id << <span class="string">" constructed"</span> << endl;</span><br><span class="line">}</span><br><span class="line"> ~<span class="built_in">Demo</span>() {</span><br><span class="line"> cout << <span class="string">"id="</span> << id << <span class="string">" destructed"</span> << endl;</span><br><span class="line"> }</span><br><span class="line">};</span><br><span class="line"><span class="function">Demo <span class="title">d1</span><span class="params">(<span class="number">1</span>)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Func</span><span class="params">()</span> </span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="function"><span class="keyword">static</span> Demo <span class="title">d2</span><span class="params">(<span class="number">2</span>)</span></span>;</span><br><span class="line"> <span class="function">Demo <span class="title">d3</span><span class="params">(<span class="number">3</span>)</span></span>;</span><br><span class="line"> cout << <span class="string">"func"</span> << endl;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span> <span class="params">()</span> </span>{</span><br><span class="line"> <span class="function">Demo <span class="title">d4</span><span class="params">(<span class="number">4</span>)</span></span>;</span><br><span class="line"> d4 = <span class="number">6</span>;</span><br><span class="line"> cout << <span class="string">"main"</span> << endl;</span><br><span class="line">{ </span><br><span class="line"> <span class="function">Demo <span class="title">d5</span><span class="params">(<span class="number">5</span>)</span></span>; </span><br><span class="line">}</span><br><span class="line"> <span class="built_in">Func</span>();</span><br><span class="line"> cout << <span class="string">"main ends"</span> << endl;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<p>看一看输出是不是和你想的一样:</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line">输出结果:</span><br><span class="line">id=<span class="number">1</span> constructed</span><br><span class="line">id=<span class="number">4</span> constructed</span><br><span class="line">id=<span class="number">6</span> constructed</span><br><span class="line">id=<span class="number">6</span> destructed</span><br><span class="line">main</span><br><span class="line">id=<span class="number">5</span> constructed</span><br><span class="line">id=<span class="number">5</span> destructed</span><br><span class="line">id=<span class="number">2</span> constructed</span><br><span class="line">id=<span class="number">3</span> constructed</span><br><span class="line">func</span><br><span class="line">id=<span class="number">3</span> destructed</span><br><span class="line">main ends</span><br><span class="line">id=<span class="number">6</span> destructed</span><br><span class="line">id=<span class="number">2</span> destructed</span><br><span class="line">id=<span class="number">1</span> destructed</span><br></pre></td></tr></tbody></table></figure>
]]></content>
<tags>
<tag>C++</tag>
</tags>
</entry>
<entry>
<title>寒假冬训营递推专题题解</title>
<url>/2022/02/09/33/</url>
<content><![CDATA[<h2 id="传送门"><a href="#传送门" class="headerlink" title="传送门"></a><a href="https://vjudge.csgrandeur.cn/contest/479448">传送门</a></h2><p><strong>密码:202202060000</strong></p>
<h2 id="A-上台阶2"><a href="#A-上台阶2" class="headerlink" title="A - 上台阶2"></a><a href="https://vjudge.csgrandeur.cn/contest/479448#problem/A">A - 上台阶2</a></h2><p>分情况递归</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N=<span class="number">100005</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> mod=<span class="number">100003</span>;</span><br><span class="line"><span class="keyword">int</span> dp[N];</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> dp[<span class="number">0</span>]=<span class="number">1</span>,dp[<span class="number">1</span>]=<span class="number">1</span>,dp[<span class="number">2</span>]=<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">int</span> n;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>,&n);</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">3</span>;i<=n;++i){</span><br><span class="line"> <span class="keyword">if</span>(n>=<span class="number">5</span>)</span><br><span class="line"> dp[i]=(dp[i]+dp[i<span class="number">-1</span>]+dp[i<span class="number">-3</span>]+dp[i<span class="number">-5</span>])%mod;</span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">if</span>(n>=<span class="number">3</span>)</span><br><span class="line"> dp[i]=(dp[i]+dp[i<span class="number">-1</span>]+dp[i<span class="number">-3</span>])%mod;</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>,dp[n]);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="B-数字三角形"><a href="#B-数字三角形" class="headerlink" title="B - 数字三角形"></a><a href="https://vjudge.csgrandeur.cn/contest/479448#problem/B">B - 数字三角形</a></h2><p>二维数组向上递推,推到最后maxSum [ 1 ] [ 1 ]就是最大的和</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">int</span> D[<span class="number">110</span>][<span class="number">110</span>];</span><br><span class="line"><span class="keyword">int</span> n;</span><br><span class="line"><span class="keyword">int</span> maxSum[<span class="number">110</span>][<span class="number">110</span>];</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> i,j; </span><br><span class="line"> cin>>n; </span><br><span class="line"> <span class="keyword">for</span>(i=<span class="number">1</span>;i<=n;i++) </span><br><span class="line"> <span class="keyword">for</span>(j=<span class="number">1</span>;j<=i;j++) </span><br><span class="line"> cin>>D[i][j];</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=n;++i)</span><br><span class="line"> maxSum[n][i]=D[n][i];</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=n<span class="number">-1</span>;i>=<span class="number">1</span>;--i) </span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">1</span>;j<=i;++j) </span><br><span class="line"> maxSum[i][j]=<span class="built_in">max</span>(maxSum[i+<span class="number">1</span>][j],maxSum[i+<span class="number">1</span>][j+<span class="number">1</span>])+D[i][j];</span><br><span class="line"> cout<<maxSum[<span class="number">1</span>][<span class="number">1</span>]<<endl;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<p>用指针进行空间优化:</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> MAX 101</span></span><br><span class="line"><span class="keyword">int</span> D[MAX][MAX];</span><br><span class="line"><span class="keyword">int</span> n;</span><br><span class="line"><span class="keyword">int</span> *maxSum;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> i,j;</span><br><span class="line"> cin>>n;</span><br><span class="line"> <span class="keyword">for</span>(i=<span class="number">1</span>;i<=n;i++)</span><br><span class="line"> <span class="keyword">for</span>(j=<span class="number">1</span>;j<=i;j++)</span><br><span class="line"> cin >> D[i][j];</span><br><span class="line"> maxSum = D[n]; <span class="comment">//maxSum指向第n行</span></span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=n<span class="number">-1</span>;i>=<span class="number">1</span>;--i)</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">1</span>;j<=i;++j)</span><br><span class="line"> maxSum[j]=<span class="built_in">max</span>(maxSum[j],maxSum[j+<span class="number">1</span>])+D[i][j];</span><br><span class="line"> cout<<maxSum[<span class="number">1</span>]<<endl; </span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="C-矩阵取数问题"><a href="#C-矩阵取数问题" class="headerlink" title="C - 矩阵取数问题"></a><a href="https://vjudge.csgrandeur.cn/contest/479448#problem/C">C - 矩阵取数问题</a></h2><p>很经典的递推</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">int</span> a[<span class="number">510</span>][<span class="number">510</span>];</span><br><span class="line"><span class="keyword">int</span> dp[<span class="number">510</span>][<span class="number">510</span>];</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> N;</span><br><span class="line"> cin>>N;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=N;i++)</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">1</span>;j<=N;j++)</span><br><span class="line"> cin>>a[i][j];</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i<N;i++) </span><br><span class="line"> dp[i][<span class="number">0</span>]=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i<N;i++) </span><br><span class="line"> dp[<span class="number">0</span>][i]=<span class="number">0</span>; </span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=N;i++) </span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">1</span>;j<=N;j++)</span><br><span class="line"> dp[i][j]=<span class="built_in">max</span>(dp[i][j<span class="number">-1</span>],dp[i<span class="number">-1</span>][j])+a[i][j]; </span><br><span class="line"> cout<<dp[N][N]<<endl;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="D-背包问题"><a href="#D-背包问题" class="headerlink" title="D - 背包问题"></a><a href="https://vjudge.csgrandeur.cn/contest/479448#problem/D">D - 背包问题</a></h2><p>01背包,不会的可以去学一下,推荐<a href="https://www.icourse163.org/course/PKU-1001894005">郭炜老师的算法课</a></p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">int</span> f[<span class="number">101</span>][<span class="number">10001</span>];</span><br><span class="line"><span class="keyword">int</span> v[<span class="number">101</span>];</span><br><span class="line"><span class="keyword">int</span> w[<span class="number">101</span>];</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">fun</span><span class="params">(<span class="keyword">int</span> n,<span class="keyword">int</span> m)</span></span>{</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=n;i++)</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">1</span>;j<=m;j++){</span><br><span class="line"> <span class="keyword">if</span>(j<w[i])<span class="comment">//装不下</span></span><br><span class="line"> f[i][j]=f[i<span class="number">-1</span>][j];</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> f[i][j]=<span class="built_in">max</span>(f[i<span class="number">-1</span>][j],f[i<span class="number">-1</span>][j-w[i]]+v[i]);<span class="comment">//装得下就可以比较装与不装哪个价值大</span></span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> f[n][m];</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> n,m;</span><br><span class="line"> cin>>n>>m;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=n;i++)</span><br><span class="line"> cin>>w[i]>>v[i];</span><br><span class="line"> cout<<<span class="built_in">fun</span>(n,m);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="E-完全背包"><a href="#E-完全背包" class="headerlink" title="E - 完全背包"></a><a href="https://vjudge.csgrandeur.cn/contest/479448#problem/E">E - 完全背包</a></h2><p>和01背包不同之处在于每件物品可以无限取,把01背包公式里的<strong>f [ i - 1 ] [ j - w [ i ] ]</strong>改成<strong>f [ i ] [ j - w [ i ] ]</strong>就行了</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">int</span> f[<span class="number">110</span>][<span class="number">50010</span>];</span><br><span class="line"><span class="keyword">int</span> v[<span class="number">10010</span>]; </span><br><span class="line"><span class="keyword">int</span> w[<span class="number">10010</span>];</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">fun</span><span class="params">(<span class="keyword">int</span> n,<span class="keyword">int</span> m)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=n;i++){</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">1</span>;j<=m;j++){</span><br><span class="line"> <span class="keyword">if</span>(j<w[i])</span><br><span class="line"> f[i][j]=f[i<span class="number">-1</span>][j];</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> f[i][j]=<span class="built_in">max</span>(f[i<span class="number">-1</span>][j],f[i][j-w[i]]+v[i]);<span class="comment">//改成i就可以重复取当前的物品</span></span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> f[n][m];</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> n,m;</span><br><span class="line"> cin>>n>>m;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=n;i++)</span><br><span class="line"> cin>>w[i]>>v[i];</span><br><span class="line"> cout<<<span class="built_in">fun</span>(n,m);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<p>用一维数组进行空间优化:</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">int</span> f[<span class="number">50010</span>];</span><br><span class="line"><span class="keyword">int</span> v[<span class="number">10010</span>];</span><br><span class="line"><span class="keyword">int</span> w[<span class="number">10010</span>];</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">fun</span><span class="params">(<span class="keyword">int</span> n,<span class="keyword">int</span> m)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=n;i++){</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j=w[i];j<=m;j++){</span><br><span class="line"> f[j]=<span class="built_in">max</span>(f[j],f[j-w[i]]+v[i]);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> f[m];</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> n,m;</span><br><span class="line"> cin>>n>>m;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=n;i++)</span><br><span class="line"> cin>>w[i]>>v[i];</span><br><span class="line"> cout<<<span class="built_in">fun</span>(n,m);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="F-背包问题-V2"><a href="#F-背包问题-V2" class="headerlink" title="F - 背包问题 V2"></a><a href="https://vjudge.csgrandeur.cn/contest/479448#problem/F">F - 背包问题 V2</a></h2><p>多重背包,介绍两种方法,首先三个循环下来肯定超时,所以我们可以尝试减少物品数量:</p>
<p>我们可以<a href="https://blog.csdn.net/seveny_/article/details/82230252">用二进制来化简</a></p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">int</span> dp[<span class="number">50050</span>];</span><br><span class="line"><span class="keyword">int</span> wi[<span class="number">10000</span>],p[<span class="number">100000</span>];</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> n,w;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d%d"</span>,&n,&w);</span><br><span class="line"> <span class="keyword">int</span> a,b,c,kp=<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=n;i++){</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d%d%d"</span>,&a,&b,&c);</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">1</span>;c;j*=<span class="number">2</span>){</span><br><span class="line"> <span class="keyword">if</span> (c>=j){</span><br><span class="line"> wi[kp]=a*j;</span><br><span class="line"> p[kp++]=b*j;</span><br><span class="line"> c-=j;</span><br><span class="line"> }<span class="keyword">else</span>{</span><br><span class="line"> wi[kp]=a*c;</span><br><span class="line"> p[kp++]=b*c;</span><br><span class="line"> c=<span class="number">0</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<kp;i++)</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j=w;j>=wi[i];j--)</span><br><span class="line"> dp[j]=<span class="built_in">max</span>(dp[j],dp[j-wi[i]]+p[i]);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>,dp[w]);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<p>还可以这样想:</p>
<p>如果一种物品的重量*数量>背包容量,就可以当做完全背包来看待</p>
<p>如果小于,我们再用二进制分解,这是一个模板</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> MAXN = <span class="number">101</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> SIZE = <span class="number">50001</span>;</span><br><span class="line"><span class="keyword">int</span> dp[SIZE];</span><br><span class="line"><span class="keyword">int</span> volume[MAXN], value[MAXN], c[MAXN];</span><br><span class="line"><span class="keyword">int</span> n, v; <span class="comment">// 总物品数,背包容量</span></span><br><span class="line"><span class="comment">// 01背包</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">ZeroOnepark</span><span class="params">(<span class="keyword">int</span> val, <span class="keyword">int</span> vol)</span> </span>{</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> j = v ; j >= vol; j--)</span><br><span class="line"> {</span><br><span class="line"> dp[j] = <span class="built_in">max</span>(dp[j], dp[j - vol] + val);</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"><span class="comment">// 完全背包</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Completepark</span><span class="params">(<span class="keyword">int</span> val, <span class="keyword">int</span> vol)</span> </span>{</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> j = vol; j <= v; j++)</span><br><span class="line"> {</span><br><span class="line"> dp[j] = <span class="built_in">max</span>(dp[j], dp[j - vol] + val);</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"><span class="comment">// 多重背包</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Multiplepark</span><span class="params">(<span class="keyword">int</span> val, <span class="keyword">int</span> vol, <span class="keyword">int</span> amount)</span> </span>{</span><br><span class="line"> <span class="keyword">if</span> (vol * amount >= v) {</span><br><span class="line"> <span class="built_in">Completepark</span>(val, vol);</span><br><span class="line"> }<span class="keyword">else</span>{</span><br><span class="line"> <span class="keyword">int</span> k = <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">while</span> (k < amount) {</span><br><span class="line"> <span class="built_in">ZeroOnepark</span>(k * val, k * vol);</span><br><span class="line"> amount -= k;</span><br><span class="line"> k <<= <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span> (amount > <span class="number">0</span>)</span><br><span class="line"> <span class="built_in">ZeroOnepark</span>(amount * val, amount * vol);</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> cin >> n >> v; </span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span> ; i <= n ; i++)</span><br><span class="line"> cin >> volume[i] >> value[i] >> c[i];<span class="comment">// 费用,价值,数量</span></span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i <= n; i++)</span><br><span class="line"> <span class="built_in">Multiplepark</span>(value[i], volume[i], c[i]);</span><br><span class="line"> cout << dp[v] << endl;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="G-最长上升子序列"><a href="#G-最长上升子序列" class="headerlink" title="G - 最长上升子序列"></a><a href="https://vjudge.csgrandeur.cn/contest/479448#problem/G">G - 最长上升子序列</a></h2><p>递推</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">int</span> a[<span class="number">1010</span>];</span><br><span class="line"><span class="keyword">int</span> maxLen[<span class="number">1010</span>];</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> N;</span><br><span class="line"> cin>>N;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=N;++i){</span><br><span class="line"> cin>>a[i];</span><br><span class="line"> maxLen[i]=<span class="number">1</span>;<span class="comment">//初始化边界为1</span></span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">2</span>;i<=N;++i)</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">1</span>;j<i;++j)</span><br><span class="line"> <span class="keyword">if</span>(a[i]>a[j])</span><br><span class="line"> maxLen[i]=<span class="built_in">max</span>(maxLen[i],maxLen[j]+<span class="number">1</span>);<span class="comment">//上升就比较加1的情况和原来的情况,大小是不确定的,你可以找数验证</span></span><br><span class="line"> cout<< *<span class="built_in">max_element</span>(maxLen+<span class="number">1</span>,maxLen+N+<span class="number">1</span>);<span class="comment">//输出数组中最大的元素</span></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="H-最长公共子序列Lcs"><a href="#H-最长公共子序列Lcs" class="headerlink" title="H - 最长公共子序列Lcs"></a><a href="https://vjudge.csgrandeur.cn/contest/479448#problem/H">H - 最长公共子序列Lcs</a></h2><p>这个题原本是一道O(mn)过不了的题,需要用bitset优化,有兴趣的可以<a href="https://loj.ac/p/6564">去看看</a>,推荐<a href="https://www.cnblogs.com/-Wallace-/p/bit-lcs.html">这个</a>教程</p>
<p>改题了之后就好多了,这个题可以用二维数组递推,也可以用滚动数组(这里不再赘述)</p>
<p>先推出递推公式,再想办法推出一个原序列,我这里给大家推一下公式:</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="keyword">if</span>(a[i<span class="number">-1</span>]==b[j<span class="number">-1</span>])</span><br><span class="line">dp[i][j]=dp[i<span class="number">-1</span>][j<span class="number">-1</span>]+<span class="number">1</span>;</span><br></pre></td></tr></tbody></table></figure>
<p>如果i串和j串当前字符相等,那当前i,j 的状态就是没算上这个公共字符的i-1,j-1状态再加1</p>
<p>如果i串和j串当前字符不相等,我们要证明:</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line">dp[i][j]=<span class="built_in">max</span>(dp[i][j<span class="number">-1</span>],dp[i<span class="number">-1</span>][j]);</span><br></pre></td></tr></tbody></table></figure>
<p>可以反证:</p>
<p>S1 [ i - 1] != s2 [ j - 1]时,MaxLen(S1,S2)不会比MaxLen(S1,S2j-1)和MaxLen(S1i-1,S2)两者之中任何一个小,也不会比两者都大。</p>
<p>MaxLen(S1,S2)不会比MaxLen(S1,S2j-1)和MaxLen(S1i-1,S2)两者之中任何一个小是很容易看出来的,因为MaxLen状态是递增的,下标大的状态不会小于下标小的状态。</p>
<p>后半句我们再反证一下:</p>
<p>也就是说,假设它比两者都大,那么对于MaxLen(S1,S2j-1),S2j-1就算是最后一个公共字符了,否则MaxLen(S1,S2)就和MaxLen(S1,S2j-1)相等了;同理我们可以推出S1i-1也是最后一个公共字符,那么S1 [ i - 1]就和s2 [ j - 1]相等了,和前提S1 [ i - 1] != s2 [ j - 1]矛盾,至此得证。</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> maxn=<span class="number">1005</span>;</span><br><span class="line"><span class="keyword">int</span> dp[maxn][maxn];</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">char</span> a[maxn],b[maxn],lcs[maxn];</span><br><span class="line"> <span class="keyword">int</span> i,j;</span><br><span class="line"> cin>>a>>b;</span><br><span class="line"> <span class="keyword">int</span> lena=<span class="built_in">strlen</span>(a),lenb=<span class="built_in">strlen</span>(b);</span><br><span class="line"> <span class="keyword">for</span>(i=<span class="number">1</span>;i<=lena;i++)</span><br><span class="line"> <span class="keyword">for</span> (j=<span class="number">1</span>;j<=lenb;j++){</span><br><span class="line"> <span class="keyword">if</span>(a[i<span class="number">-1</span>]==b[j<span class="number">-1</span>])</span><br><span class="line"> dp[i][j]=dp[i<span class="number">-1</span>][j<span class="number">-1</span>]+<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> dp[i][j]=<span class="built_in">max</span>(dp[i][j<span class="number">-1</span>],dp[i<span class="number">-1</span>][j]);<span class="comment">//比较难理解的地方</span></span><br><span class="line"> }</span><br><span class="line"> i=lena,j=lenb;</span><br><span class="line"> <span class="keyword">int</span> len=dp[lena][lenb];</span><br><span class="line"> lcs[len]=<span class="string">'\0'</span>;</span><br><span class="line"> <span class="keyword">while</span>(dp[i][j]){<span class="comment">//倒推出一个最大公共子序列</span></span><br><span class="line"> <span class="keyword">if</span> (dp[i][j]==dp[i<span class="number">-1</span>][j]) i--;</span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">if</span> (dp[i][j]==dp[i][j<span class="number">-1</span>]) j--;</span><br><span class="line"> <span class="keyword">else</span> lcs[--len]=a[i<span class="number">-1</span>],i--,j--;</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%s\n"</span>,lcs);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="I-石子合并"><a href="#I-石子合并" class="headerlink" title="I - 石子合并"></a><a href="https://vjudge.csgrandeur.cn/contest/479448#problem/I">I - 石子合并</a></h2><p>可以先去了解一下矩阵连乘,四边形优化</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N = <span class="number">405</span>;</span><br><span class="line"><span class="keyword">int</span> a[N],s[N],f[N][N],g[N][N];</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> n;</span><br><span class="line"> cin>>n;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>;i <= n;i++){</span><br><span class="line"> cin>>a[i];</span><br><span class="line"> a[i + n] = a[i];</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>;i <= <span class="number">2</span>*n;i++) </span><br><span class="line"> s[i] = s[i<span class="number">-1</span>] + a[i];</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> len = <span class="number">2</span>;len <= n;len++){</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> l = <span class="number">1</span>;l + len - <span class="number">1</span> <= <span class="number">2</span>*n;l++){</span><br><span class="line"> <span class="keyword">int</span> r = l + len - <span class="number">1</span>;</span><br><span class="line"> f[l][r] = <span class="number">1e9</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> k = l;k < r;k++){</span><br><span class="line"> f[l][r] = <span class="built_in">min</span>(f[l][r],f[l][k]+f[k+<span class="number">1</span>][r]+s[r]-s[l<span class="number">-1</span>]);</span><br><span class="line"> g[l][r] = <span class="built_in">max</span>(g[l][r],g[l][k]+g[k+<span class="number">1</span>][r]+s[r]-s[l<span class="number">-1</span>]);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">int</span> res1 = <span class="number">1e9</span>,res2 = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>;i <= n;i++){</span><br><span class="line"> res1 = <span class="built_in">min</span>(res1,f[i][i+n<span class="number">-1</span>]);</span><br><span class="line"> res2 = <span class="built_in">max</span>(res2,g[i][i+n<span class="number">-1</span>]);</span><br><span class="line"> }</span><br><span class="line"> cout<<res1<<endl<<res2<<endl;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="J-循环数组最大子段和"><a href="#J-循环数组最大子段和" class="headerlink" title="J - 循环数组最大子段和"></a><a href="https://vjudge.csgrandeur.cn/contest/479448#problem/J">J - 循环数组最大子段和</a></h2><p>这个题思路比较巧妙</p>
<p>如果不循环,那么状态转移方程:</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="keyword">if</span>(dp[i<span class="number">-1</span>]><span class="number">0</span>)</span><br><span class="line"> dp[i]=dp[i<span class="number">-1</span>]+a[i];</span><br><span class="line"><span class="keyword">else</span></span><br><span class="line"> dp[i]=a[i];</span><br></pre></td></tr></tbody></table></figure>
<p>如果循环,可以认为不要中间一段最大负段和,要的是头部一段加尾部一段</p>
<p>求最大负段和,可以把数组所有值求个相反数,然后求最大正段和</p>
<p><strong>注意: 这里说的负段和不是连续的负数,别误解了</strong></p>
<p>最后,只需要比较头部一段加尾部一段的和与最大正段和的大小</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ll long long</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line">ll n,a[<span class="number">50005</span>];</span><br><span class="line"><span class="function">ll <span class="title">maxsum</span><span class="params">(ll a[])</span></span>{<span class="comment">//求最大段和</span></span><br><span class="line"> ll sum=<span class="number">0</span>,m=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i<n;i++){</span><br><span class="line"> <span class="keyword">if</span>(sum<<span class="number">0</span>) </span><br><span class="line"> sum=a[i];</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> sum+=a[i];</span><br><span class="line"> <span class="keyword">if</span>(sum>m)</span><br><span class="line"> m=sum; </span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> m;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> cin>>n;</span><br><span class="line"> ll sum=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i<n;i++){</span><br><span class="line"> cin>>a[i];</span><br><span class="line"> sum+=a[i];</span><br><span class="line"> }</span><br><span class="line"> ll ans=<span class="built_in">maxsum</span>(a);<span class="comment">//正向求不循环的最大段和</span></span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i<n;i++)</span><br><span class="line"> a[i]=-a[i];</span><br><span class="line"> ll ans1=<span class="built_in">maxsum</span>(a);<span class="comment">//求序列最大的负段和的相反数</span></span><br><span class="line"> cout<<<span class="built_in">max</span>(ans,ans1+sum)<<endl;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="K-没有上司的舞会"><a href="#K-没有上司的舞会" class="headerlink" title="K - 没有上司的舞会"></a><a href="https://vjudge.csgrandeur.cn/contest/479448#problem/K">K - 没有上司的舞会</a></h2><p>树形dp,感兴趣的可以<a href="https://blog.csdn.net/weixin_43540515/article/details/102507640">了解一下</a></p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> dp[<span class="number">100010</span>][<span class="number">2</span>],happy[<span class="number">100010</span>],flag[<span class="number">100010</span>];</span><br><span class="line">vector <<span class="keyword">long</span> <span class="keyword">long</span>> son[<span class="number">100010</span>],link[<span class="number">100010</span>];</span><br><span class="line">queue<<span class="keyword">long</span> <span class="keyword">long</span>>q;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">f</span><span class="params">(<span class="keyword">long</span> <span class="keyword">long</span> x)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> dp[x][<span class="number">0</span>]=<span class="number">0</span>;</span><br><span class="line"> dp[x][<span class="number">1</span>]=happy[x];</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i<son[x].<span class="built_in">size</span>();i++)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">long</span> <span class="keyword">long</span> y=son[x][i];</span><br><span class="line"> <span class="built_in">f</span>(y);</span><br><span class="line"> dp[x][<span class="number">0</span>]+=<span class="built_in">max</span>(dp[y][<span class="number">0</span>],dp[y][<span class="number">1</span>]);</span><br><span class="line"> dp[x][<span class="number">1</span>]+=dp[y][<span class="number">0</span>];</span><br><span class="line"> } </span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> ios::<span class="built_in">sync_with_stdio</span>(<span class="literal">false</span>);</span><br><span class="line"> <span class="keyword">long</span> <span class="keyword">long</span> n,a,b;</span><br><span class="line"> cin>>n;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=n;i++)</span><br><span class="line"> cin>>happy[i];</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<n;i++)</span><br><span class="line"> {</span><br><span class="line"> cin>>a>>b;</span><br><span class="line"> link[a].<span class="built_in">push_back</span>(b);</span><br><span class="line"> link[b].<span class="built_in">push_back</span>(a);</span><br><span class="line"> }</span><br><span class="line"> q.<span class="built_in">push</span>(<span class="number">1</span>);flag[<span class="number">1</span>]=<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">while</span>(!q.<span class="built_in">empty</span>())</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">long</span> <span class="keyword">long</span> t=q.<span class="built_in">front</span>(),tmp;</span><br><span class="line"> q.<span class="built_in">pop</span>();</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i<link[t].<span class="built_in">size</span>();i++)</span><br><span class="line"> <span class="keyword">if</span>(!flag[link[t][i]])</span><br><span class="line"> {</span><br><span class="line"> tmp=link[t][i];</span><br><span class="line"> q.<span class="built_in">push</span>(tmp);</span><br><span class="line"> flag[tmp]=<span class="number">1</span>;</span><br><span class="line"> son[t].<span class="built_in">push_back</span>(tmp);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">f</span>(<span class="number">1</span>);</span><br><span class="line"> cout<<<span class="built_in">max</span>(dp[<span class="number">1</span>][<span class="number">0</span>],dp[<span class="number">1</span>][<span class="number">1</span>]);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>; </span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="L-滑雪"><a href="#L-滑雪" class="headerlink" title="L - 滑雪"></a><a href="https://vjudge.csgrandeur.cn/contest/479448#problem/L">L - 滑雪</a></h2><p>记忆化搜索</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">int</span> R,C,snow[<span class="number">105</span>][<span class="number">105</span>],sum;</span><br><span class="line"><span class="keyword">int</span> rmb[<span class="number">105</span>][<span class="number">105</span>];<span class="comment">//记住搜索过的点,避免无用的重复搜索,节省大量时间</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">dfs</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> y,<span class="keyword">int</span> len)</span></span>{</span><br><span class="line"> <span class="keyword">if</span>(x<<span class="number">1</span>||y<<span class="number">1</span>||x>R||y>C)</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> <span class="keyword">if</span>(rmb[x][y]!=<span class="number">0</span>){</span><br><span class="line"> sum=<span class="built_in">max</span>(sum,len+rmb[x][y]<span class="number">-1</span>);</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> sum=<span class="built_in">max</span>(sum,len);</span><br><span class="line"> <span class="keyword">if</span>(snow[x][y+<span class="number">1</span>]<snow[x][y]) <span class="built_in">dfs</span>(x,y+<span class="number">1</span>,len+<span class="number">1</span>);</span><br><span class="line"> <span class="keyword">if</span>(snow[x+<span class="number">1</span>][y]<snow[x][y]) <span class="built_in">dfs</span>(x+<span class="number">1</span>,y,len+<span class="number">1</span>);</span><br><span class="line"> <span class="keyword">if</span>(snow[x][y<span class="number">-1</span>]<snow[x][y]) <span class="built_in">dfs</span>(x,y<span class="number">-1</span>,len+<span class="number">1</span>);</span><br><span class="line"> <span class="keyword">if</span>(snow[x<span class="number">-1</span>][y]<snow[x][y]) <span class="built_in">dfs</span>(x<span class="number">-1</span>,y,len+<span class="number">1</span>);</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d%d"</span>,&R,&C);</span><br><span class="line"> <span class="keyword">int</span> Max=<span class="number">-1</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=R;++i){</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">1</span>;j<=C;++j){</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>,&snow[i][j]);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=R;++i){</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">1</span>;j<=C;++j){</span><br><span class="line"> sum=<span class="number">0</span>;</span><br><span class="line"> <span class="built_in">dfs</span>(i,j,<span class="number">1</span>);</span><br><span class="line"> rmb[i][j]=sum;</span><br><span class="line"> Max=<span class="built_in">max</span>(Max,sum);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d"</span>,Max);</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
]]></content>
<tags>
<tag>contest</tag>
</tags>
</entry>
<entry>
<title>C++入门笔记(3)</title>
<url>/2022/02/10/34/</url>
<content><![CDATA[<h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><p>由于开学在即,竞赛也不远了,可我的算法却没学多少,所以我打算先整理STL</p>
<h2 id="string-类"><a href="#string-类" class="headerlink" title="string 类"></a>string 类</h2><p>string 类是模板类,可以当作char的替代品,使用string类要包含头文件< string ></p>
<p>string对象的三种初始化方式:</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="function">string <span class="title">s1</span><span class="params">(<span class="string">"Hello"</span>)</span></span>; </span><br><span class="line">string month = <span class="string">"March"</span>; <span class="comment">//要以字符串的形势初始化</span></span><br><span class="line"><span class="function">string <span class="title">s2</span><span class="params">(<span class="number">8</span>,’x’)</span></span>;</span><br></pre></td></tr></tbody></table></figure>
<p>可以将字符赋给string对象:</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line">string s;</span><br><span class="line">s = ‘n’;</span><br></pre></td></tr></tbody></table></figure>
<p>string类可以用getline读入:</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="built_in">getline</span>(cin,str);</span><br></pre></td></tr></tbody></table></figure>
<h3 id="string的赋值-连接与比较"><a href="#string的赋值-连接与比较" class="headerlink" title="string的赋值,连接与比较"></a>string的赋值,连接与比较</h3><p>• 用 = 赋值</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="function">string <span class="title">s1</span><span class="params">(<span class="string">"cat"</span>)</span>, s2</span>;</span><br><span class="line">s2 = s1; </span><br></pre></td></tr></tbody></table></figure>
<p>• 用 assign 成员函数复制</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="function">string <span class="title">s1</span><span class="params">(<span class="string">"cat"</span>)</span>, s3</span>; </span><br><span class="line">s3.<span class="built_in">assign</span>(s1); </span><br></pre></td></tr></tbody></table></figure>
<p>• 用 assign 成员函数部分复制</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="function">string <span class="title">s1</span><span class="params">(<span class="string">"catpig"</span>)</span>, s3</span>; </span><br><span class="line">s3.<span class="built_in">assign</span>(s1, <span class="number">1</span>, <span class="number">3</span>);<span class="comment">//从s1 中下标为1的字符开始复制3个字符给s3</span></span><br></pre></td></tr></tbody></table></figure>
<p>• 单个字符复制</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line">s2[<span class="number">5</span>] = s1[<span class="number">3</span>] = ‘a’;</span><br></pre></td></tr></tbody></table></figure>
<p>• 逐个访问string对象中的字符</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="function">string <span class="title">s1</span><span class="params">(<span class="string">"Hello"</span>)</span></span>;</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i<s1.<span class="built_in">length</span>();i++)</span><br><span class="line">cout << s1.<span class="built_in">at</span>(i) << endl;</span><br></pre></td></tr></tbody></table></figure>
<p>成员函数at会做范围检查,如果超出范围,会抛出 out_of_range异常,而下标运算符[]不做范围检查。</p>
<p>• 用 + 运算符连接字符串</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="function">string <span class="title">s1</span><span class="params">(<span class="string">"good "</span>)</span>, <span class="title">s2</span><span class="params">(<span class="string">"morning! "</span>)</span></span>;</span><br><span class="line">s1 += s2;</span><br><span class="line">cout << s1;</span><br></pre></td></tr></tbody></table></figure>
<p>• 用成员函数 append 连接字符串</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="function">string <span class="title">s1</span><span class="params">(<span class="string">"good "</span>)</span>, <span class="title">s2</span><span class="params">(<span class="string">"morning! "</span>)</span></span>;</span><br><span class="line">s1.<span class="built_in">append</span>(s2);</span><br><span class="line">cout << s1;</span><br><span class="line">s2.<span class="built_in">append</span>(s1, <span class="number">3</span>, s1.<span class="built_in">size</span>());<span class="comment">//s1.size(),s1字符数</span></span><br><span class="line">cout << s2;</span><br><span class="line"><span class="comment">// 下标为3开始,s1.size()个字符,如果字符串内没有足够字符,则复制到字符串最后一个字符</span></span><br></pre></td></tr></tbody></table></figure>
<p>• 用关系运算符比较string的大小</p>
<p>== , >, >=, <, <=, !=</p>
<p>返回值都是bool类型,成立返回true, 否则返回false</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="function">string <span class="title">s1</span><span class="params">(<span class="string">"hello"</span>)</span>,<span class="title">s2</span><span class="params">(<span class="string">"hello"</span>)</span>,<span class="title">s3</span><span class="params">(<span class="string">"hell"</span>)</span></span>;</span><br><span class="line"><span class="keyword">bool</span> b = (s1 == s2); </span><br><span class="line">cout << b << endl;<span class="comment">//1</span></span><br><span class="line">b = (s1 == s3); <span class="comment">//0</span></span><br><span class="line">cout << b << endl;</span><br><span class="line">b = (s1 > s3); <span class="comment">//1</span></span><br><span class="line">cout << b << endl;</span><br></pre></td></tr></tbody></table></figure>
<p>• 用成员函数compare比较string的大小</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="function">string <span class="title">s1</span><span class="params">(<span class="string">"hello"</span>)</span>,<span class="title">s2</span><span class="params">(<span class="string">"hello"</span>)</span>,<span class="title">s3</span><span class="params">(<span class="string">"hell"</span>)</span></span>;</span><br><span class="line"><span class="keyword">int</span> f1 = s1.<span class="built_in">compare</span>(s2);</span><br><span class="line"><span class="keyword">int</span> f2 = s1.<span class="built_in">compare</span>(s3);</span><br><span class="line"><span class="keyword">int</span> f3 = s3.<span class="built_in">compare</span>(s1);</span><br><span class="line"><span class="keyword">int</span> f4 = s1.<span class="built_in">compare</span>(<span class="number">1</span>,<span class="number">2</span>,s3,<span class="number">0</span>,<span class="number">3</span>); <span class="comment">//s1 1-2; s3 0-3</span></span><br><span class="line"><span class="keyword">int</span> f5 = s1.<span class="built_in">compare</span>(<span class="number">0</span>,s1.<span class="built_in">size</span>(),s3);<span class="comment">//s1 0-end</span></span><br><span class="line">cout << f1 << endl << f2 << endl << f3 << endl; </span><br><span class="line">cout << f4 << endl << f5 << endl; </span><br></pre></td></tr></tbody></table></figure>
<h3 id="string-类的成员函数"><a href="#string-类的成员函数" class="headerlink" title="string 类的成员函数"></a>string 类的成员函数</h3><figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line">str.<span class="built_in">assign</span>()<span class="comment">//起复制作用,上文已提到</span></span><br><span class="line">str.<span class="built_in">at</span>()<span class="comment">//起访问的作用,上文已提到</span></span><br><span class="line">str.<span class="built_in">compare</span>()<span class="comment">//起比较的作用,上文已提到</span></span><br><span class="line">str.<span class="built_in">length</span>()<span class="comment">//返回字符串长度</span></span><br><span class="line">str.<span class="built_in">size</span>()<span class="comment">//返回字符串长度,和上面一样</span></span><br><span class="line">str.<span class="built_in">substr</span>()<span class="comment">//返回指定范围的子串</span></span><br><span class="line">str.<span class="built_in">swap</span>()<span class="comment">//交换字符串</span></span><br><span class="line"><span class="comment">/*以下带有find的成员函数在找不到时都会返回string::npos*/</span></span><br><span class="line">str.<span class="built_in">find_first_of</span>()<span class="comment">//从前向后找字符串中任何一个字符第一次出现的地方</span></span><br><span class="line">str.<span class="built_in">find_last_of</span>()<span class="comment">//从后往前找字符串中任何一个字符第一次出现的地方</span></span><br><span class="line">str.<span class="built_in">find_first_not_of</span>()<span class="comment">//从前向后找非字符串中任何一个字符的第一个字符</span></span><br><span class="line">str.<span class="built_in">find_last_not_of</span>()<span class="comment">//从后往前找非字符串中任何一个字符的第一个字符</span></span><br><span class="line">str.<span class="built_in">find</span>()<span class="comment">//返回找到的字符串的首字母位置,rfind()则是从后往前找</span></span><br><span class="line">str.<span class="built_in">erase</span>()<span class="comment">//删除指定范围的字符串</span></span><br><span class="line">str.<span class="built_in">replace</span>()<span class="comment">//替换指定范围的字符串</span></span><br><span class="line">str.<span class="built_in">insert</span>()<span class="comment">//在指定位置插入字符串</span></span><br><span class="line">str.<span class="built_in">c_str</span>()<span class="comment">//转换成C语言式const char *字符串</span></span><br><span class="line">str.<span class="built_in">data</span>()<span class="comment">//转换成C语言式char *字符串,不安全</span></span><br><span class="line">str.<span class="built_in">copy</span>()<span class="comment">//拷贝指定范围的字符串</span></span><br></pre></td></tr></tbody></table></figure>
<h2 id="STL标准模板库"><a href="#STL标准模板库" class="headerlink" title="STL标准模板库"></a>STL标准模板库</h2><h3 id="STL中的基本的概念"><a href="#STL中的基本的概念" class="headerlink" title="STL中的基本的概念"></a>STL中的基本的概念</h3><p><strong>容器</strong>:可容纳各种数据类型的通用数据结构,是<strong>类模板</strong> </p>
<p><strong>迭代器</strong>:可用于依次存取容器中元素,<strong>类似于指针</strong> </p>
<p><strong>算法</strong>:用来操作容器中的元素的<strong>函数模板</strong> </p>
<p>通俗理解:</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="keyword">int</span> array[<span class="number">100</span>]; </span><br><span class="line"><span class="built_in">sort</span>(array,array+<span class="number">70</span>); <span class="comment">//将前70个元素排序</span></span><br></pre></td></tr></tbody></table></figure>
<p>该数组就是容器,而 int * 类型的指针变量就可以作为迭代器,sort算法可以作用于该容器上,对其进行排序</p>
<p>容器分为三种:</p>
<p>1)顺序容器 vector, deque(双向队列), list(链表)</p>
<p>2)关联容器 set, multiset, map, multimap </p>
<p>3)容器适配器 stack(栈), queue(队列), priority_queue(优先队列)</p>
<p>对象被插入容器中时,被插入的是对象的一个<strong>复制品</strong>。许多算法,比如排序,查找,要求对容器中的元 素进行比较,有的容器本身就是排序的,所以,放入容器的对象所属的类,往往还应该<strong>重载 == 和 <</strong> 运算符</p>
<h3 id="顺序容器"><a href="#顺序容器" class="headerlink" title="顺序容器"></a>顺序容器</h3><h4 id="vector"><a href="#vector" class="headerlink" title="vector"></a>vector</h4><p><strong>动态数组</strong>。元素在内存连续存放。随机存取任何元素都能在常数时间完成。在<strong>尾端增删</strong>元素具有较佳的性能(大部分情况下是常数时间)</p>
<img src="/2022/02/10/34/1.jpg" class="" title="blog">
<h4 id="deque"><a href="#deque" class="headerlink" title="deque"></a>deque</h4><p><strong>双向队列</strong>。元素在内存连续存放。随机存取任何元素都能在常数时间完成(但次于vector)。在<strong>两端增删</strong>元素具有较佳的性能(大部分情况下是常数时间)</p>
<img src="/2022/02/10/34/2.jpg" class="" title="blog">
<h4 id="list"><a href="#list" class="headerlink" title="list"></a>list</h4><p><strong>双向链表</strong>。元素在内存不连续存放。在<strong>任何位置增删</strong>元素都能在常数时间完成。<strong>不支持随机存取</strong></p>
<img src="/2022/02/10/34/3.jpg" class="" title="blog">
<h3 id="关联容器"><a href="#关联容器" class="headerlink" title="关联容器"></a>关联容器</h3><p>• 元素是<strong>排序</strong>的 </p>
<p>• 插入任何元素,都按相应的排序规则来确定其位置 </p>
<p>• 在<strong>查找</strong>时具有非常好的性能 </p>
<p>• 通常以<strong>平衡二叉树</strong>方式实现,插入和检索的时间都是 O(log(N))</p>
<h4 id="set-multiset"><a href="#set-multiset" class="headerlink" title="set/multiset"></a>set/multiset</h4><p>set 即集合。set中<strong>不允许</strong>相同元素,multiset中允许存在相同的元素</p>
<h4 id="map-multimap"><a href="#map-multimap" class="headerlink" title="map/multimap"></a>map/multimap</h4><p>map与set的不同在于map中存放的元素有且仅有两个成员变量,一个名为first(表示关键字),另一个名为second(表示值), map<strong>根据first值对元素进行从小到大排序</strong>,并可快速地<strong>根据first来检索元素</strong>。 </p>
<p>map同multimap的不同在于是否允许相同first值的元素。</p>
<h3 id="容器适配器"><a href="#容器适配器" class="headerlink" title="容器适配器"></a>容器适配器</h3><h4 id="stack"><a href="#stack" class="headerlink" title="stack"></a>stack</h4><p>栈。是项的有限序列,并满足序列中被删除、检索和修改的项只能是最近插入序列的项(栈顶的项)。<strong>后进先出</strong>。</p>
<img src="/2022/02/10/34/4.jpg" class="" title="blog">
<h4 id="queue"><a href="#queue" class="headerlink" title="queue"></a>queue</h4><p>队列。插入只可以在尾部进行,删除、检索和修改只允许从头部进行。<strong>先进先出</strong>。</p>
<img src="/2022/02/10/34/5.jpg" class="" title="blog">
<h4 id="priority-queue"><a href="#priority-queue" class="headerlink" title="priority_queue"></a>priority_queue</h4><p>优先级队列。最高优先级(自己定义)元素总是第一个出列</p>
<h3 id="顺序容器和关联容器中都有的成员函数"><a href="#顺序容器和关联容器中都有的成员函数" class="headerlink" title="顺序容器和关联容器中都有的成员函数"></a>顺序容器和关联容器中都有的成员函数</h3><p><strong>begin</strong> 返回指向容器中第一个元素的迭代器 </p>
<p><strong>end</strong> 返回指向容器中最后一个元素后面的位置的迭代器 </p>
<p><strong>rbegin</strong> 返回指向容器中最后一个元素的迭代器 </p>
<p><strong>rend</strong> 返回指向容器中第一个元素前面的位置的迭代器 </p>
<p><strong>erase</strong> 从容器中删除一个或几个元素 </p>
<p><strong>clear</strong> 从容器中删除所有元素</p>
<h3 id="顺序容器的常用成员函数"><a href="#顺序容器的常用成员函数" class="headerlink" title="顺序容器的常用成员函数"></a>顺序容器的常用成员函数</h3><p><strong>front</strong> :返回容器中第一个元素的引用 </p>
<p><strong>back</strong> : 返回容器中最后一个元素的引用 </p>
<p><strong>push_back</strong> : 在容器末尾增加新元素 </p>
<p><strong>pop_back</strong> : 删除容器末尾的元素 </p>
<p><strong>erase</strong> :删除迭代器指向的元素(可能会使该迭代器失效),或删除一个区间,返回被删除元素后面的那个元素的迭代器</p>
]]></content>
<tags>
<tag>C++</tag>
</tags>
</entry>
<entry>
<title>琐事记(2)</title>
<url>/2022/02/21/35/</url>
<content><![CDATA[<h2 id="Spring"><a href="#Spring" class="headerlink" title="Spring"></a>Spring</h2><p>As the begining of a year,spring has its familiar flavour.</p>
<p>With the soft wind and relax moods,we steps the new journey.</p>
<p>There should be a strong impluse for us to struggle for a better life,which pulls us out of our comfort zone.</p>
<p>Time is mean,but you can be more mean than time.</p>
<p>We embrase the pain,to cultivate our abilities.</p>
<p>We hold the ethic, to fit into the society.</p>
<p>When we get up at the spring of a day,we throw away all the sadness and happiness.</p>
<p>Be tolerant of ourselves.</p>
<p>Everything is as you wish.✨</p>
]]></content>
<tags>
<tag>memories</tag>
</tags>
</entry>
<entry>
<title>水一篇博客</title>
<url>/2022/04/03/36/</url>
<content><![CDATA[<h2 id="水文"><a href="#水文" class="headerlink" title="水文"></a>水文</h2><p>时间过得飞快,转眼间就来到了四月份。每天不停的忙碌着,不断地宽容别人,不断地安慰自己,看似一无所获,实则学了些许有用的东西。</p>
<img src="/2022/04/03/36/1.jpg" class="" title="blog">
<p>疫情似乎要陪我度过大学四年了,我还是挺乐观的,计算机的学生封不封校都一样,每天依旧忙碌着。在每一个忙碌的日子里,我不断地取舍,不断地思考,不断地罚座,时钟自顾自的走动,不曾为我动容。</p>
<p>没有信仰的人先入为主,觉得入积极分子是在浪费时间;过于功利的人开始摆烂,觉得上水课是在浪费生命。教育早已偏离了正轨,我们也将错就错。</p>
<p>身处于大学的幸福中,仿佛早已忘记高中留下的遗憾。只觉得待我们走入社会的牢笼,生活才会撕开它的笑容。</p>
<p>我们不必过于忧虑,但也不可昏睡过去,时常反问自己,是否依旧清醒。</p>
<p>我们不必困于过往,但也不能忘记耻辱,时常反思过去,只为更好前行。</p>
]]></content>
<tags>
<tag>meaningless</tag>
</tags>
</entry>
<entry>
<title>题型整理(1)</title>
<url>/2022/04/23/37/</url>
<content><![CDATA[<h3 id="卡特兰数"><a href="#卡特兰数" class="headerlink" title="卡特兰数"></a>卡特兰数</h3><p><strong>应用场景:</strong></p>
<p>1.n个节点能组成几种二叉树<br>2.括号匹配给出n对括号,求可以组成的合法表达式的个数<br>3.凸多边形的三角划分<br>4.一个顺序为1~n的进栈序列,有多少种出栈序列</p>
<p>卡特兰数求解代码(从0开始,用的组合数方法求):</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">long</span> ll;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N=<span class="number">2e5</span>+<span class="number">10</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> MOD=<span class="number">1e9</span>+<span class="number">7</span>;</span><br><span class="line">ll fact[N];</span><br><span class="line">ll inv_fact[N];</span><br><span class="line"><span class="function">ll <span class="title">qpow</span><span class="params">(ll x,ll y)</span></span>{</span><br><span class="line"> ll base=x,ret=<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">while</span>(y){</span><br><span class="line"> <span class="keyword">if</span>(y&<span class="number">1</span>)</span><br><span class="line"> ret=ret*base%MOD;</span><br><span class="line"> base=base*base%MOD;</span><br><span class="line"> y>>=<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> ret;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">init</span><span class="params">()</span></span>{</span><br><span class="line"> fact[<span class="number">0</span>]=inv_fact[<span class="number">0</span>]=<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>,inv;i<N;++i){</span><br><span class="line"> fact[i]=(fact[i<span class="number">-1</span>]*i)%MOD;</span><br><span class="line"> inv=<span class="built_in">qpow</span>(i,MOD<span class="number">-2</span>);</span><br><span class="line"> inv_fact[i]=(inv_fact[i<span class="number">-1</span>]*inv)%MOD;</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"><span class="function">ll <span class="title">C</span><span class="params">(<span class="keyword">int</span> a,<span class="keyword">int</span> b)</span></span>{</span><br><span class="line"> <span class="keyword">return</span> ((fact[a]*inv_fact[b])%MOD*inv_fact[a-b])%MOD;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">solve</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="built_in">init</span>();</span><br><span class="line"> <span class="keyword">int</span> n;</span><br><span class="line"> cin>>n;</span><br><span class="line"> ll ans=((<span class="built_in">C</span>(<span class="number">2</span>*n,n)-<span class="built_in">C</span>(<span class="number">2</span>*n,n<span class="number">-1</span>))%MOD+MOD)%MOD;</span><br><span class="line"> cout<<ans<<endl;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> std::ios::<span class="built_in">sync_with_stdio</span>(<span class="literal">false</span>),cin.<span class="built_in">tie</span>(<span class="number">0</span>),cout.<span class="built_in">tie</span>(<span class="number">0</span>);</span><br><span class="line"> <span class="built_in">solve</span>();</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<p>题的变形:</p>
<p>括号匹配给出n对括号,k种括号,求可以组成的合法表达式的个数</p>
<p>答案就成了n的卡特兰数*pow(k,n)</p>
<h3 id="字符串不同子串个数"><a href="#字符串不同子串个数" class="headerlink" title="字符串不同子串个数"></a>字符串不同子串个数</h3><figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> maxn 100005</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> re register</span></span><br><span class="line"><span class="keyword">int</span> n,cnt=<span class="number">1</span>,lst=<span class="number">1</span>;</span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> ans;</span><br><span class="line"><span class="keyword">char</span> S[maxn];</span><br><span class="line"><span class="keyword">int</span> fa[maxn<<<span class="number">1</span>],len[maxn<<<span class="number">1</span>],son[maxn<<<span class="number">1</span>][<span class="number">26</span>];</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">void</span> <span class="title">ins</span><span class="params">(<span class="keyword">int</span> c)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> f=lst,p=++cnt; lst=p;</span><br><span class="line"> len[p]=len[f]+<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">while</span>(f&&!son[f][c]) son[f][c]=p,f=fa[f];</span><br><span class="line"> <span class="keyword">if</span>(!f) {fa[p]=<span class="number">1</span>;<span class="keyword">return</span>;}</span><br><span class="line"> <span class="keyword">int</span> x=son[f][c];</span><br><span class="line"> <span class="keyword">if</span>(len[f]+<span class="number">1</span>==len[x]) {fa[p]=x;<span class="keyword">return</span>;}</span><br><span class="line"> <span class="keyword">int</span> y=++cnt;</span><br><span class="line"> len[y]=len[f]+<span class="number">1</span>,fa[y]=fa[x],fa[x]=fa[p]=y;</span><br><span class="line"> <span class="keyword">for</span>(re <span class="keyword">int</span> i=<span class="number">0</span>;i<<span class="number">26</span>;i++) son[y][i]=son[x][i];</span><br><span class="line"> <span class="keyword">while</span>(f&&son[f][c]==x) son[f][c]=y,f=fa[f];</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>,&n),<span class="built_in">scanf</span>(<span class="string">"%s"</span>,S+<span class="number">1</span>);</span><br><span class="line"> <span class="keyword">for</span>(re <span class="keyword">int</span> i=<span class="number">1</span>;i<=n;i++) <span class="built_in">ins</span>(S[i]-<span class="string">'a'</span>);</span><br><span class="line"> <span class="keyword">for</span>(re <span class="keyword">int</span> i=<span class="number">2</span>;i<=cnt;i++) ans+=(<span class="keyword">long</span> <span class="keyword">long</span>)(len[i]-len[fa[i]]);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%lld\n"</span>,ans);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h3 id="字符串某一个子串的数目"><a href="#字符串某一个子串的数目" class="headerlink" title="字符串某一个子串的数目"></a>字符串某一个子串的数目</h3><figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">int</span> dp[<span class="number">1010</span>][<span class="number">1010</span>];</span><br><span class="line">string s,t;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> len1,len2;</span><br><span class="line"> cin>>s>>t;</span><br><span class="line"> len1=s.<span class="built_in">size</span>();</span><br><span class="line"> len2=t.<span class="built_in">size</span>();</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span> ; i <= len1 ; i++){</span><br><span class="line"> dp[i][<span class="number">0</span>] = <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span> ; i <= len1 ; i++){</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">1</span> ; j <= len2 ; j++){</span><br><span class="line"> <span class="keyword">if</span>(s[i - <span class="number">1</span>] == t[j - <span class="number">1</span>])</span><br><span class="line"> dp[i][j] = dp[i<span class="number">-1</span>][j<span class="number">-1</span>] + dp[i<span class="number">-1</span>][j];</span><br><span class="line"> <span class="keyword">else</span> </span><br><span class="line"> dp[i][j] = dp[i - <span class="number">1</span>][j];</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> cout<<dp[len1][len2];</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h3 id="数组不同子序列个数"><a href="#数组不同子序列个数" class="headerlink" title="数组不同子序列个数"></a>数组不同子序列个数</h3><figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ll long long</span></span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N = <span class="number">1e5</span>+<span class="number">5</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> mod = <span class="number">1000000007</span>;</span><br><span class="line"><span class="keyword">int</span> a[N];</span><br><span class="line">ll dp[N];</span><br><span class="line"><span class="keyword">int</span> vis[N];</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> n;</span><br><span class="line"> <span class="keyword">while</span>(cin >> n){</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=n;i++){</span><br><span class="line"> cin >> a[i];</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">memset</span>(dp,<span class="number">0</span>,<span class="built_in"><span class="keyword">sizeof</span></span>(dp));</span><br><span class="line"> <span class="built_in">memset</span>(vis,<span class="number">0</span>,<span class="built_in"><span class="keyword">sizeof</span></span>(vis));</span><br><span class="line"> dp[<span class="number">1</span>]=<span class="number">1</span>;</span><br><span class="line"> vis[a[<span class="number">1</span>]]=<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">2</span>;i<=n;i++){</span><br><span class="line"> <span class="keyword">if</span>(vis[a[i]]==<span class="number">0</span>){</span><br><span class="line"> dp[i] = (dp[i<span class="number">-1</span>]*<span class="number">2</span> + <span class="number">1</span>)%mod;</span><br><span class="line"> }<span class="keyword">else</span>{</span><br><span class="line"> dp[i] = (dp[i<span class="number">-1</span>]*<span class="number">2</span> - dp[ vis[a[i]] - <span class="number">1</span> ] + mod) % mod;</span><br><span class="line"> }</span><br><span class="line"> vis[a[i]]=i;</span><br><span class="line"> }</span><br><span class="line"> cout<<dp[n]<<endl;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h3 id="两端相等子数组最大和,返回最后一次相等左,右位置和"><a href="#两端相等子数组最大和,返回最后一次相等左,右位置和" class="headerlink" title="两端相等子数组最大和,返回最后一次相等左,右位置和"></a>两端相等子数组最大和,返回最后一次相等左,右位置和</h3><figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">int</span> num[<span class="number">210000</span>];</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> t;</span><br><span class="line"> cin>>t;</span><br><span class="line"> <span class="keyword">while</span>(t--){</span><br><span class="line"> <span class="keyword">int</span> n,sum=<span class="number">0</span>,sum1=<span class="number">0</span>,l;</span><br><span class="line"> cin>>n;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i<n;i++)</span><br><span class="line"> cin>>num[i],sum+=num[i];</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i<n;i++){</span><br><span class="line"> sum1+=num[i];</span><br><span class="line"> <span class="keyword">if</span>(sum1>=sum/<span class="number">2</span>){</span><br><span class="line"> l=i;</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">int</span> r=l+<span class="number">1</span>,sum2=sum-sum1;</span><br><span class="line"> <span class="keyword">while</span>(sum1!=sum2){</span><br><span class="line"> <span class="keyword">if</span>(sum1>sum2)</span><br><span class="line"> sum1-=num[l--];</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> sum2-=num[r++];</span><br><span class="line"> }</span><br><span class="line"> cout<<l+n-r+<span class="number">1</span><<endl;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
]]></content>
<tags>
<tag>contest</tag>
</tags>
</entry>
<entry>
<title>2022年蓝桥杯省赛C++B组部分题解</title>
<url>/2022/04/25/38/</url>
<content><![CDATA[<h2 id="试题-A-九进制转十进制"><a href="#试题-A-九进制转十进制" class="headerlink" title="试题 A: 九进制转十进制"></a><strong>试题 A: 九进制转十进制</strong></h2><p>本题总分:5 分<br><strong>【问题描述】</strong><br>九进制正整数2022转换成十进制等于多少?</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> t;</span><br><span class="line"> t=<span class="number">2</span>*<span class="built_in">pow</span>(<span class="number">9</span>,<span class="number">3</span>)+<span class="number">2</span>*<span class="number">9</span>+<span class="number">2</span>;</span><br><span class="line"> cout<<t;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="试题-B-顺子日期"><a href="#试题-B-顺子日期" class="headerlink" title="试题 B: 顺子日期"></a>试题 B: 顺子日期</h2><p>本题总分:5 分<br><strong>【问题描述】</strong><br>小明特别喜欢顺子。顺子指的就是连续的三个数字:123、456 等。顺子日<br>期指的就是在日期的 yyyymmdd 表示法中,存在任意连续的三位数是一个顺子的日期。例如 20220123 就是一个顺子日期,因为它出现了一个顺子:123; 而 20221023 则不是一个顺子日期,它一个顺子也没有。小明想知道在整个 2022年份中,一共有多少个顺子日期。</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="number">20220120</span></span><br><span class="line"><span class="number">20220121</span></span><br><span class="line"><span class="number">20220122</span></span><br><span class="line"><span class="number">20220123</span></span><br><span class="line"><span class="number">20220124</span></span><br><span class="line"><span class="number">20220125</span></span><br><span class="line"><span class="number">20220126</span></span><br><span class="line"><span class="number">20220127</span></span><br><span class="line"><span class="number">20220128</span></span><br><span class="line"><span class="number">20220129</span></span><br><span class="line"><span class="number">20221012</span></span><br><span class="line"><span class="number">20221123</span></span><br><span class="line"><span class="number">20221230</span></span><br><span class="line"><span class="number">20221231</span></span><br><span class="line"><span class="number">14</span></span><br></pre></td></tr></tbody></table></figure>
<h2 id="试题-C-刷题统计"><a href="#试题-C-刷题统计" class="headerlink" title="试题 C: 刷题统计"></a>试题 C: 刷题统计</h2><p>时间限制: 1.0s 内存限制: 256.0MB 本题总分:10 分<br><strong>【问题描述】</strong><br>小明决定从下周一开始努力刷题准备蓝桥杯竞赛。他计划周一至周五每天<br>做 a 道题目,周六和周日每天做 b 道题目。请你帮小明计算,按照计划他将在第几天实现做题数大于等于 n 题?<br><strong>【输入格式】</strong><br>输入一行包含三个整数 a, b 和 n.<br><strong>【输出格式】</strong><br>输出一个整数代表天数。<br><strong>【样例输入】</strong><br>10 20 99<br><strong>【样例输出】</strong><br>8<br><strong>【评测用例规模与约定】</strong><br>对于 50% 的评测用例,1 ≤ a, b, n ≤ 1e6.<br>对于 100% 的评测用例,1 ≤ a, b, n ≤ 1e18.</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">long</span> ll;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> ll n,m,k,t,chu,mo,flag=<span class="number">0</span>;</span><br><span class="line"> cin>>n>>m>>k;</span><br><span class="line"> t=n*<span class="number">5</span>+m*<span class="number">2</span>;</span><br><span class="line"> chu=k/t;</span><br><span class="line"> chu*=<span class="number">7</span>;</span><br><span class="line"> mo=k%t;</span><br><span class="line"> <span class="keyword">if</span>(mo><span class="number">0</span>){</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=<span class="number">5</span>;i++){</span><br><span class="line"> <span class="keyword">if</span>(mo<i*n){</span><br><span class="line"> chu+=i;</span><br><span class="line"> flag=<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=<span class="number">2</span>;i++){</span><br><span class="line"> <span class="keyword">if</span>(mo<i*m+n*<span class="number">5</span>&&flag==<span class="number">0</span>){</span><br><span class="line"> chu+=i+<span class="number">5</span>;</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> cout<<chu;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="试题-D-修剪灌木"><a href="#试题-D-修剪灌木" class="headerlink" title="试题 D: 修剪灌木"></a>试题 D: 修剪灌木</h2><p>时间限制: 1.0s 内存限制: 256.0MB 本题总分:10 分<br><strong>【问题描述】</strong><br>爱丽丝要完成一项修剪灌木的工作。<br>有 N 棵灌木整齐的从左到右排成一排。爱丽丝在每天傍晚会修剪一棵灌<br>木,让灌木的高度变为 0 厘米。爱丽丝修剪灌木的顺序是从最左侧的灌木开始,每天向右修剪一棵灌木。当修剪了最右侧的灌木后,她会调转方向,下一天开始向左修剪灌木。直到修剪了最左的灌木后再次调转方向。然后如此循环往复。灌木每天从早上到傍晚会长高 1 厘米,而其余时间不会长高。在第一天的早晨,所有灌木的高度都是 0 厘米。爱丽丝想知道每棵灌木最高长到多高。<br><strong>【输入格式】</strong><br>一个正整数 N ,含义如题面所述。<br><strong>【输出格式】</strong><br>输出 N 行,每行一个整数,第行表示从左到右第 i 棵树最高能长到多高。<br><strong>【样例输入】</strong><br>3<br><strong>【样例输出】</strong><br>4<br>2<br>4<br><strong>【评测用例规模与约定】</strong><br>对于 30% 的数据,N ≤ 10.<br>对于 100% 的数据,1 < N ≤ 10000.</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">long</span> ll;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> n;</span><br><span class="line"> cin>>n;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=n;i++){</span><br><span class="line"> cout<<<span class="number">2</span>*<span class="built_in">max</span>(i<span class="number">-1</span>,n-i)<<endl;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="试题-F-统计子矩阵"><a href="#试题-F-统计子矩阵" class="headerlink" title="试题 F: 统计子矩阵"></a>试题 F: 统计子矩阵</h2><p>时间限制: 1.0s 内存限制: 256.0MB 本题总分:15 分<br><strong>【问题描述】</strong><br>给定一个 N × M 的矩阵 A,请你统计有多少个子矩阵 (最小 1 × 1,最大<br>N × M) 满足子矩阵中所有数的和不超过给定的整数 K?<br><strong>【输入格式】</strong><br>第一行包含三个整数 N, M 和 K.<br>之后 N 行每行包含 M 个整数,代表矩阵 A.<br><strong>【输出格式】</strong><br>一个整数代表答案。<br><strong>【样例输入】</strong><br>3 4 10<br>1 2 3 4<br>5 6 7 8<br>9 10 11 12<br><strong>【样例输出】</strong><br>19<br><strong>【样例说明】</strong><br>满足条件的子矩阵一共有 19,包含:<br>大小为 1 × 1 的有 10 个。<br>大小为 1 × 2 的有 3 个。<br>大小为 1 × 3 的有 2 个。<br>大小为 1 × 4 的有 1 个。<br>大小为 2 × 1 的有 3 个。<br><strong>【评测用例规模与约定】</strong><br>对于 30% 的数据,N, M ≤ 20.<br>对于 70% 的数据,N, M ≤ 100.<br>对于 100% 的数据,1 ≤ N, M ≤ 500; 0 ≤ Ai j ≤ 1000; 1 ≤ K ≤ 250000000.</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">int</span> a[<span class="number">2100</span>][<span class="number">2100</span>],s[<span class="number">2100</span>][<span class="number">2100</span>];</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> n,m,k,ret=<span class="number">0</span>,cnt=<span class="number">0</span>;</span><br><span class="line"> cin>>n>>m>>k;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=n;i++)</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">1</span>;j<=m;j++)</span><br><span class="line"> cin>>a[i][j];</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=n;i++)</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">1</span>;j<=m;j++)</span><br><span class="line"> a[i][j]=a[i<span class="number">-1</span>][j]+a[i][j<span class="number">-1</span>]-a[i<span class="number">-1</span>][j<span class="number">-1</span>]+a[i][j];</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=n;i++){<span class="comment">//x1</span></span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> f=i;f<=n;f++){<span class="comment">//x2</span></span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">1</span>;j<=m;j++){<span class="comment">//y1</span></span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> b=j;b<=m;b++){<span class="comment">//y2</span></span><br><span class="line"> ret=a[f][b]-a[f][j<span class="number">-1</span>]-a[i<span class="number">-1</span>][b]+a[i<span class="number">-1</span>][j<span class="number">-1</span>];</span><br><span class="line"> <span class="keyword">if</span>(ret<=k)</span><br><span class="line"> cnt++;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> cout<<cnt;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
]]></content>
<tags>
<tag>contest</tag>
</tags>
</entry>
<entry>
<title>题型整理(2)</title>
<url>/2022/04/30/39/</url>
<content><![CDATA[<h2 id="古代猪文"><a href="#古代猪文" class="headerlink" title="古代猪文"></a>古代猪文</h2><h4 id="213-古代猪文-AcWing题库"><a href="#213-古代猪文-AcWing题库" class="headerlink" title="213. 古代猪文 - AcWing题库"></a><a href="https://www.acwing.com/problem/content/215/">213. 古代猪文 - AcWing题库</a></h4><p>数论好题</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> int long long</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N = <span class="number">37000</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> MOD = <span class="number">999911658</span>;</span><br><span class="line"><span class="keyword">int</span> plist[<span class="number">5</span>] = {<span class="number">0</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4679</span>,<span class="number">35617</span>};</span><br><span class="line"><span class="keyword">int</span> n,q;</span><br><span class="line"><span class="keyword">int</span> a1,a2,a3,a4;</span><br><span class="line"><span class="keyword">int</span> fac[N],a[N];</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">int</span> <span class="title">gcd</span><span class="params">(<span class="keyword">int</span> a,<span class="keyword">int</span> b)</span> </span>{</span><br><span class="line"> <span class="keyword">if</span>(b == <span class="number">0</span>) <span class="keyword">return</span> a;</span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">return</span> <span class="built_in">gcd</span>(b,a % b);</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">int</span> <span class="title">ksm</span><span class="params">(<span class="keyword">int</span> base,<span class="keyword">int</span> power,<span class="keyword">int</span> mod)</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> res = <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">while</span>(power) {</span><br><span class="line"> <span class="keyword">if</span>(power & <span class="number">1</span>) {</span><br><span class="line"> res = res * base % mod;</span><br><span class="line"> }</span><br><span class="line"> base = base * base % mod;</span><br><span class="line"> power = power >> <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> res;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">void</span> <span class="title">init</span><span class="params">(<span class="keyword">int</span> mod)</span> </span>{</span><br><span class="line"> fac[<span class="number">0</span>] = <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span> ; i <= mod ; i ++)</span><br><span class="line"> fac[i] = fac[i - <span class="number">1</span>] * i % mod;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">int</span> <span class="title">ni</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> mod)</span> </span>{<span class="comment">//预处理阶乘的逆元</span></span><br><span class="line"> <span class="keyword">return</span> <span class="built_in">ksm</span>(fac[x],mod - <span class="number">2</span>,mod);</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">int</span> <span class="title">C</span><span class="params">(<span class="keyword">int</span> n,<span class="keyword">int</span> m,<span class="keyword">int</span> mod)</span> </span>{</span><br><span class="line"> <span class="keyword">if</span>(m > n) <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">return</span> fac[n] * <span class="built_in">ni</span>(m,mod) % mod * <span class="built_in">ni</span>(n - m,mod) % mod;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">int</span> <span class="title">Lucas</span><span class="params">(<span class="keyword">int</span> n,<span class="keyword">int</span> m,<span class="keyword">int</span> mod)</span> </span>{<span class="comment">//卢卡斯定理</span></span><br><span class="line"> <span class="keyword">if</span>(n < m) <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">if</span>(n == <span class="number">0</span>) <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">return</span> <span class="built_in">Lucas</span>(n / mod,m / mod,mod) * <span class="built_in">C</span>(n % mod,m % mod,mod) % mod;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">void</span> <span class="title">memge</span><span class="params">(<span class="keyword">long</span> <span class="keyword">long</span> &a1,<span class="keyword">long</span> <span class="keyword">long</span> &m1,<span class="keyword">long</span> <span class="keyword">long</span> a2,<span class="keyword">long</span> <span class="keyword">long</span> m2)</span> </span>{<span class="comment">//合并素数</span></span><br><span class="line"> <span class="keyword">if</span>(m2>m1) <span class="built_in">swap</span>(m1,m2),<span class="built_in">swap</span>(a1,a2);</span><br><span class="line"> <span class="keyword">while</span>(a1%m2!=a2)a1=a1+m1;</span><br><span class="line"> m1 =(m2/<span class="built_in">gcd</span>(m1,m2))*m1;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">CRT</span><span class="params">()</span> </span>{<span class="comment">//扩展中国剩余定理</span></span><br><span class="line"> <span class="keyword">int</span> m1 = plist[<span class="number">1</span>],a1 = a[<span class="number">1</span>];</span><br><span class="line"> a1 %= m1;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">2</span> ; i <= <span class="number">4</span> ; i ++) {</span><br><span class="line"> <span class="keyword">int</span> m2 = plist[i],a2 = a[i];</span><br><span class="line"> a2 %= m2;</span><br><span class="line"> <span class="built_in">memge</span>(a1,m1,a2,m2);</span><br><span class="line"> }</span><br><span class="line"> cout<<<span class="built_in">ksm</span>(q,(a1 + m1) % m1,MOD + <span class="number">1</span>);</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">signed</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%lld%lld"</span>,&n,&q);</span><br><span class="line"> <span class="keyword">if</span>(q % (MOD + <span class="number">1</span>) == <span class="number">0</span>) {</span><br><span class="line"> cout << <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span> ; i <= <span class="number">4</span> ; i ++) {</span><br><span class="line"> <span class="built_in">init</span>(plist[i]);</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">1</span> ; j * j <= n ; j ++) {</span><br><span class="line"> <span class="keyword">if</span>(n % j == <span class="number">0</span>) {</span><br><span class="line"> a[i] = (a[i] + <span class="built_in">Lucas</span>(n,j,plist[i])) % plist[i];</span><br><span class="line"> <span class="keyword">if</span>(j * j != n) {</span><br><span class="line"> a[i] = (a[i] + <span class="built_in">Lucas</span>(n,n / j,plist[i])) % plist[i];</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">CRT</span>();</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="数列"><a href="#数列" class="headerlink" title="数列"></a>数列</h2><p><a href="http://oj.daimayuan.top/course/10/problem/664">数列 - 题目 - Daimayuan Online Judge</a></p>
<p>思维题</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="keyword">int</span> d, m;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">solve</span><span class="params">()</span> </span>{</span><br><span class="line"> cin >> d >> m;</span><br><span class="line"> <span class="keyword">int</span> ans = <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">int</span> tmp = d;</span><br><span class="line"> vector<<span class="keyword">int</span>> v;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= tmp; i <<= <span class="number">1</span>) {</span><br><span class="line"> v.<span class="built_in">push_back</span>(i+<span class="number">1</span>);</span><br><span class="line"> tmp -= i;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(tmp) v.<span class="built_in">push_back</span>(tmp + <span class="number">1</span>);</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i < v.<span class="built_in">size</span>(); i++) {</span><br><span class="line"> ans = ans * v[i] % m;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(!ans) ans = m - <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">else</span> ans--;</span><br><span class="line"> cout << ans << endl;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="多重集组合数"><a href="#多重集组合数" class="headerlink" title="多重集组合数"></a>多重集组合数</h2><p>n种物品,第i种物品有ai个,不同种类物品可以互相区分但是相同种类无法区分。从这些物品中取出m个,求方案数。</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> LL long long</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> MAXN 1005</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> INF 0x3f3f3f3f</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> MOD 1000000</span></span><br><span class="line"><span class="keyword">int</span> dp[MAXN][MAXN*<span class="number">10</span>];</span><br><span class="line"><span class="keyword">int</span> n,tot,s,b;</span><br><span class="line"><span class="keyword">int</span> a[MAXN];</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d %d %d %d"</span>,&n,&tot,&s,&b);</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=tot;i++)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">int</span> tmp;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>,&tmp);</span><br><span class="line"> a[tmp]++;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i<=n;i++)</span><br><span class="line"> dp[i][<span class="number">0</span>]=<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=n;i++)</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">1</span>;j<=b;j++)</span><br><span class="line"> <span class="keyword">if</span>(j<span class="number">-1</span>-<span class="built_in">min</span>(j,a[i])>=<span class="number">0</span>)</span><br><span class="line"> dp[i][j]=(dp[i][j<span class="number">-1</span>]+dp[i<span class="number">-1</span>][j]-dp[i<span class="number">-1</span>][j<span class="number">-1</span>-<span class="built_in">min</span>(j,a[i])]+MOD)%MOD;</span><br><span class="line"> <span class="keyword">else</span> dp[i][j]=(dp[i][j<span class="number">-1</span>]+dp[i<span class="number">-1</span>][j])%MOD; </span><br><span class="line"> <span class="keyword">int</span> ans=<span class="number">0</span>; </span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=s;i<=b;i++)</span><br><span class="line"> ans=(dp[n][i]+ans)%MOD;</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>,ans);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
]]></content>
<tags>
<tag>contest</tag>
</tags>
</entry>
<entry>
<title>题型整理(3)</title>
<url>/2022/05/02/40/</url>
<content><![CDATA[<h2 id="余数之和"><a href="#余数之和" class="headerlink" title="余数之和"></a>余数之和</h2><p>给出正整数 n 和 k,计算 j(n,k)=k mod 1 + k mod 2 + k mod 3 + … + k mod n 的值。</p>
<p>例如j(5,3) = 3 mod 1 + 3 mod 2 + 3 mod 3 + 3 mod 4 + 3 mod 5 = 0 + 1 + 0 + 3 + 3 = 7。</p>
<p><strong>输入格式</strong></p>
<p>输入仅一行,包含两个整数 n,,k。</p>
<p><strong>输出格式</strong></p>
<p>输出仅一行,即 j(n,k)。</p>
<p><strong>数据范围</strong></p>
<p>1≤n,k≤10^9</p>
<p><strong>输入样例</strong></p>
<p>5 3</p>
<p><strong>输出样例</strong></p>
<p>7</p>
<p>思路:</p>
<p>整除分块:</p>
<script type="math/tex; mode=display">
\displaystyle\sum_{i=1}^n\lfloor\frac{n}{i}\rfloor</script><p>(它喵的,不知道别人网站里的公式可以复制,自己学了半天mathjax语法)</p>
<p>$\lfloor x \rfloor$表示对x向下取整,通过打表可以发现n/i会有多个相等的区间,区间的右端点即为n/(n/i),则整除分块的代码为:</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> l=<span class="number">1</span>,r;l<=n;l=r+<span class="number">1</span>){</span><br><span class="line"> r=n/(n/l);</span><br><span class="line"> ans+=(r-l+<span class="number">1</span>)*(n/l);</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<p>而本题的答案为</p>
<script type="math/tex; mode=display">
ans=n*k-\displaystyle\sum_{i=1}^n\lfloor\frac{k}{i}\rfloor*i</script><p>只要通过等差数列求和公式就能推出每个区间要减去(k/l)(r-l+1)(l+r)/2</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">long</span> ll;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> ll n,k;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%lld%lld"</span>,&n,&k);</span><br><span class="line"> ll ans=n*k;</span><br><span class="line"> <span class="keyword">for</span>(ll l=<span class="number">1</span>,r;l<=n;l=r+<span class="number">1</span>) {</span><br><span class="line"> <span class="keyword">if</span>(k/l!=<span class="number">0</span>) r=<span class="built_in">min</span>(k/(k/l),n); </span><br><span class="line"> <span class="keyword">else</span> r=n;</span><br><span class="line"> ans-=(k/l)*(r-l+<span class="number">1</span>)*(l+r)/<span class="number">2</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%lld"</span>,ans);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="Matrix"><a href="#Matrix" class="headerlink" title="Matrix"></a>Matrix</h2><p>Fill an n×n matrix with numbers in [1,n2], where each number occurs exactly once.</p>
<p>For a fixed number filling method, let ai be the mininum number in the iith row, and S={a1,a2,…,an}∩{1,2,…,n}.</p>
<p>You need to calculate ∑|S|(mod998244353), i.e. the sum of the size of S over all possible methods.</p>
<p><strong>Input</strong></p>
<p>This problem contains multiple test cases.</p>
<p>The first line contains a single integer T (1≤T≤30).</p>
<p>Then T cases follow, each of which contains a single interger n (1≤n≤5000).</p>
<p><strong>Output</strong></p>
<p>For each test case, output one line contains the value of ∑|S|(mod998244353)</p>
<p><strong>input</strong></p>
<figure class="highlight plaintext"><table><tbody><tr><td class="code"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td></tr></tbody></table></figure>
<p><strong>output</strong></p>
<figure class="highlight plaintext"><table><tbody><tr><td class="code"><pre><span class="line">40</span><br></pre></td></tr></tbody></table></figure>
<p>思路:<br>考虑每一个数的贡献,比如1可以作为贡献的方案数就是n <em> C(n </em> n-1,n-1)<br>其中组合数部分表示1作为该行的最小值出现,选择剩下的数的方案数;n表示该行可以出现在1-n里任意一行。<br>最后还要乘n ! ∗ ( n * n − n ) !,前者表示1所在行的所有数都可以全排列一遍,后者表示剩下的数可以全排列,然后累加出1 − n的贡献</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ll long long</span></span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> maxn = <span class="number">25000001</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> mod = <span class="number">998244353</span>;</span><br><span class="line"> </span><br><span class="line"><span class="function">ll <span class="title">qpow</span><span class="params">(ll a, ll b, ll p)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> ll ans = <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">while</span> (b)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span> (b & <span class="number">1</span>)</span><br><span class="line"> ans = ans * a % p;</span><br><span class="line"> a = a * a % p;</span><br><span class="line"> b >>= <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> ans;</span><br><span class="line">}</span><br><span class="line"> </span><br><span class="line">ll fac[maxn];</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">init</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> fac[<span class="number">0</span>] = <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i < maxn; i++)</span><br><span class="line"> {</span><br><span class="line"> fac[i] = fac[i - <span class="number">1</span>] * i % mod;</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"><span class="function">ll <span class="title">infac</span><span class="params">(ll x)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">return</span> <span class="built_in">qpow</span>(x, mod - <span class="number">2</span>, mod);</span><br><span class="line">}</span><br><span class="line"><span class="function">ll <span class="title">C</span><span class="params">(ll n, ll m)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">if</span> (n < m)</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">return</span> fac[n] * <span class="built_in">infac</span>(fac[m]) % mod * <span class="built_in">infac</span>(fac[n - m]) % mod;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="built_in">init</span>();</span><br><span class="line"> <span class="keyword">int</span> t = <span class="number">1</span>;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &t);</span><br><span class="line"> <span class="keyword">while</span> (t--)</span><br><span class="line"> {</span><br><span class="line"> ll n;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%lld"</span>, &n);</span><br><span class="line"> ll ans = n * fac[n] % mod * fac[n * n - n] % mod;</span><br><span class="line"> ll sum = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i <= n; i++)</span><br><span class="line"> {</span><br><span class="line"> sum += <span class="built_in">C</span>(n * n - i, n - <span class="number">1</span>);</span><br><span class="line"> sum = sum % mod;</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%lld\n"</span>, ans * sum % mod);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
]]></content>
<tags>
<tag>contest</tag>
</tags>
</entry>
<entry>
<title>欧拉定理的应用:Counting regions</title>
<url>/2022/05/10/41/</url>
<content><![CDATA[<h2 id="欧拉定理的应用:Counting-regions"><a href="#欧拉定理的应用:Counting-regions" class="headerlink" title="欧拉定理的应用:Counting regions"></a>欧拉定理的应用:Counting regions</h2><p><a href="https://ac.nowcoder.com/acm/problem/17369">Counting regions</a></p>
<p>Niuniu likes mathematics. He also likes drawing pictures. One day, he was trying to draw a regular polygon with n vertices. He connected every pair of the vertices by a straight line as well. He counted the number of regions inside the polygon after he completed his picture. He was wondering how to calculate the number of regions without the picture. Can you calculate the number of regions modulo 1000000007? <strong>It is guaranteed that n is odd.</strong></p>
<p>在图论里有这样一个定理:</p>
<p><strong>对于一个平面图,顶点数-边数+面数=2</strong></p>
<p>我最早看到这个式子是在浙大的高中数学竞赛书中,最近离散数学课也推到图论了,里面讲到了这场多校赛的两个基础知识点,欧拉定理和哈密顿图。</p>
<p>我当时只知道,n阶完全图的边数为C(n,2),欧拉公式只适用于立体图;</p>
<p>一开始想找找规律,wa了两发;</p>
<p>后来我想,如果把完全图看作一个立体图,也就是说<strong>n阶完全图还有一个由外围的n个点形成的面</strong>。</p>
<p>这样我们要求的区域数再+1就是立体图的面数;</p>
<p>到这里只需要分别求出顶点数和边数就能带式子了:</p>
<p>顶点数:我们可以这么想每一个内部的交点都是由两条线相交构成的,这个线的两端点是从外面一圈点中选的,那么内部的交点数就相当于从外围的个点中选四个数,构成两条线,两条线再构成一个交点,即交点数为C(n,4),然后还要加上外围的n个点,所以顶点数为C(n,4)+n;</p>
<img src="/2022/05/10/41/1.jpg" class="" title="blog">
<p>边数:尤其要注意的是,对角线之间互相切割的线段才是边,我们首先知道n阶完全图的边数是C(n,2),同时我们注意到上图中每形成一个交点都会使两条对角线互相切割,切割成了4段,也就是比原边数多了两段,也就是交点数 <em> 2,即C(n,4) </em> 2,然后再把C(n,2)加上就行了,于是边数也就是C(n,4) * 2+C(n,2);</p>
<p>带入公式:</p>
<p>C(n,4)+n-C(n,4) * 2-C(n,2)+x=2;</p>
<p>x=qu+1;</p>
<p>于是求得</p>
<p>qu=C(n,4)+1-n;</p>
<p>接下来用组合数求就行了;</p>
<p>当然要用到费马小定理求逆元,就不再赘述了</p>
<p>代码:</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">long</span> ll;</span><br><span class="line"><span class="keyword">const</span> ll M=<span class="number">1e9</span>+<span class="number">7</span>;</span><br><span class="line"><span class="function">ll <span class="title">qmi</span><span class="params">(ll a,ll k,ll p)</span></span>{</span><br><span class="line"> ll res=<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">while</span>(k){</span><br><span class="line"> <span class="keyword">if</span>(k&<span class="number">1</span>)res=(ll)res*a%p;</span><br><span class="line"> a=(ll)a*a%p;</span><br><span class="line"> k>>=<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> res;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">C</span><span class="params">(<span class="keyword">int</span> a,<span class="keyword">int</span> b,<span class="keyword">int</span> p)</span></span>{</span><br><span class="line"> <span class="keyword">if</span>(a<b) <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"> ll x=<span class="number">1</span>,y=<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=a,j=<span class="number">1</span>;j<=b;i--,j++){</span><br><span class="line"> x=(ll)x*i%p;</span><br><span class="line"> y=(ll)y*j%p;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> x*(ll)<span class="built_in">qmi</span>(y,p<span class="number">-2</span>,p)%p;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">lucas</span><span class="params">(ll a,ll b,<span class="keyword">int</span> p)</span></span>{</span><br><span class="line"> <span class="keyword">if</span> (a<p&&b<p) <span class="keyword">return</span> <span class="built_in">C</span>(a,b,p);</span><br><span class="line"> <span class="keyword">return</span> (ll)<span class="built_in">C</span>(a%p,b%p,p)*<span class="built_in">lucas</span>(a/p,b/p,p)%p;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> ll n;</span><br><span class="line"> cin>>n;</span><br><span class="line"> cout<<(((<span class="built_in">C</span>(n,<span class="number">2</span>,M)%M+<span class="built_in">C</span>(n,<span class="number">4</span>,M)%M)%M)%M+(<span class="number">1</span>-n+M)%M)%M;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<p>这着实是个好题,但不太正经。。。</p>
]]></content>
<tags>
<tag>contest</tag>
</tags>
</entry>
<entry>
<title>思语(1)</title>
<url>/2022/05/26/42/</url>
<content><![CDATA[<h2 id="思语"><a href="#思语" class="headerlink" title="思语"></a>思语</h2><p><strong>1.变得愈发少言,愈发令我欢喜。</strong></p>
<p><strong>2.不只是幽默,沉默也是交谈的艺术。</strong></p>
<p><strong>3.捂上耳朵,闭上眼睛,便可以倾听心语。</strong></p>
<p><strong>4.我希望自己宽以待人,严于律己。</strong></p>
<p><strong>5.旅途中若瞥见风景,便用心去欣赏,到达目的地,便用心去做事。</strong></p>
<p><strong>6.这个世界仿佛是只需看你一时的惊艳,便知晓你十年的汗水似的。</strong></p>
<p><strong>7.再多的悲伤与欢乐,都将在梦醒时索然无味。</strong></p>
<p><strong>8.纵使我们忘记了许多任何事,也不能忘记自己的存在。</strong></p>
<p><strong>9.如果你有海一般的胸怀,放大幸福、淡化苦痛也不再是什么难事了。</strong></p>
<p><strong>10.为了活出自我,我们要少一些伪装的行径,呈现出自己真实的一面。</strong></p>
]]></content>
<tags>
<tag>life</tag>
</tags>
</entry>
<entry>
<title>butterfly主题图标不能加载的问题(cdn更换)</title>
<url>/2022/06/07/43/</url>
<content><![CDATA[<h2 id="butterfly主题图标不能加载的问题"><a href="#butterfly主题图标不能加载的问题" class="headerlink" title="butterfly主题图标不能加载的问题"></a>butterfly主题图标不能加载的问题</h2><p>期末考试完的我兴奋的打开网站,却发现图标君不在了!这对我这种菜狗来说是搞不明白的😥</p>
<p>f12一下看到有很多cdn.jsdelivr.net的文件出现了网络问题</p>
<p>于是我挂上梯子试了试,发现图片正常显示了,但我一直不知道为什么</p>
<p>直到我搜到相同的问题,才发现jsdelivr的cdn在5月17日挂了</p>
<p><a href="https://blog.zhheo.com/p/790087d9.html">这里</a>是一位大佬发的butterfly主题的cdn更换教程</p>
<p>只需要<strong>把主题配置文件下的cdn更换即可</strong>,等jsdelivr修好了再说吧</p>
<p>网站图片加载速度太慢什么的以后再说吧(摆烂ing)</p>
]]></content>
<tags>
<tag>problem</tag>
</tags>
</entry>
<entry>
<title>题型整理(3)</title>
<url>/2022/06/24/44/</url>
<content><![CDATA[<h2 id="题型整理(3)"><a href="#题型整理(3)" class="headerlink" title="题型整理(3)"></a>题型整理(3)</h2><h3 id="拉格朗日插值法的应用"><a href="#拉格朗日插值法的应用" class="headerlink" title="拉格朗日插值法的应用"></a><strong>拉格朗日插值法的应用</strong></h3><p>给n个点推原函数求 f(k) mod 998244353</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> maxn=<span class="number">2010</span>;<span class="comment">//n的范围</span></span><br><span class="line"><span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">long</span> ll;</span><br><span class="line">ll mod=<span class="number">998244353</span>;</span><br><span class="line">ll n,k,x[maxn],y[maxn],ans,s1,s2;</span><br><span class="line"><span class="function">ll <span class="title">powmod</span><span class="params">(ll a,ll x)</span></span>{</span><br><span class="line"> ll ret=<span class="number">1ll</span>,nww=a;</span><br><span class="line"> <span class="keyword">while</span>(x){</span><br><span class="line"> <span class="keyword">if</span>(x&<span class="number">1</span>)ret=ret*nww%mod;</span><br><span class="line"> nww=nww*nww%mod;x>>=<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> ret;</span><br><span class="line">}</span><br><span class="line"><span class="function">ll <span class="title">inv</span><span class="params">(ll x)</span></span>{</span><br><span class="line"> <span class="keyword">return</span> <span class="built_in">powmod</span>(x,mod<span class="number">-2</span>);</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%lld%lld"</span>,&n,&k);</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=n;i++)</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%lld%lld"</span>,x+i,y+i);</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=n;i++){</span><br><span class="line"> s1=y[i]%mod;s2=<span class="number">1ll</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">1</span>;j<=n;j++)<span class="keyword">if</span>(i!=j)s1=s1*(k-x[j])%mod,s2=s2*((x[i]-x[j]%mod)%mod)%mod;</span><br><span class="line"> ans+=s1*<span class="built_in">inv</span>(s2)%mod;ans=(ans+mod)%mod;</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%lld\n"</span>,ans);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h3 id="字典树的应用"><a href="#字典树的应用" class="headerlink" title="字典树的应用"></a><strong>字典树的应用</strong></h3><h4 id="子数组最大异或和"><a href="#子数组最大异或和" class="headerlink" title="子数组最大异或和"></a><strong>子数组最大异或和</strong></h4><figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N = <span class="number">100000</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> M = N * <span class="number">32</span> + <span class="number">1</span>;</span><br><span class="line"><span class="keyword">int</span> trie[M][<span class="number">2</span>], idx;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">add</span><span class="params">( <span class="keyword">int</span> x )</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> p = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span> ( <span class="keyword">int</span> i = <span class="number">31</span>; i >= <span class="number">0</span>; --i) {</span><br><span class="line"> <span class="keyword">int</span> u = x >> i & <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">int</span> &t = trie[p][u];</span><br><span class="line"> <span class="keyword">if</span> ( !t ) t = ++idx;</span><br><span class="line"> p = t;</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">query</span><span class="params">( <span class="keyword">int</span> x )</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> ret = <span class="number">0</span>, p = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">int</span> u = x >> <span class="number">31</span> & <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">if</span> ( !trie[p][u] ) {</span><br><span class="line"> ret |= <span class="number">1</span> << <span class="number">31</span>;</span><br><span class="line"> u ^= <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> p = trie[p][u];</span><br><span class="line"> <span class="keyword">for</span> ( <span class="keyword">int</span> i = <span class="number">30</span>; i >= <span class="number">0</span>; --i ) {</span><br><span class="line"> u = x >> i & <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">int</span> &t = trie[p][u ^ <span class="number">1</span>];</span><br><span class="line"> <span class="keyword">if</span> ( t ) {</span><br><span class="line"> ret |= <span class="number">1</span> << i;</span><br><span class="line"> p = t;</span><br><span class="line"> } <span class="keyword">else</span> p = trie[p][u];</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> ret;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> n, x;</span><br><span class="line"> <span class="keyword">int</span> ret = <span class="number">0</span>, eor = <span class="number">0</span>;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &n);</span><br><span class="line"> <span class="keyword">while</span> ( n-- ) {</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &x);</span><br><span class="line"> eor ^= x;</span><br><span class="line"> ret = <span class="built_in">max</span>( ret, <span class="built_in">query</span>(eor) );</span><br><span class="line"> <span class="built_in">add</span>( eor );</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, ret);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h4 id="长度为m的子数组最大异或和(正数序列)"><a href="#长度为m的子数组最大异或和(正数序列)" class="headerlink" title="长度为m的子数组最大异或和(正数序列)"></a>长度为m的子数组最大异或和(正数序列)</h4><figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N =( <span class="number">1e5</span> + <span class="number">10</span>) * <span class="number">31</span>, M = <span class="number">1e5</span> + <span class="number">10</span>;</span><br><span class="line"><span class="keyword">int</span> son[N][<span class="number">2</span>],idx,arr[M], s[M],cnt[N];</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">insert</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> val)</span></span>{</span><br><span class="line"> <span class="keyword">int</span> p = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">30</span> ;i >= <span class="number">0</span>; i --){</span><br><span class="line"> <span class="keyword">int</span> u = x >> i & <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">if</span>(!son[p][u])son[p][u] = ++idx;</span><br><span class="line"> p = son[p][u];</span><br><span class="line"> cnt[p] += val;</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">query</span><span class="params">(<span class="keyword">int</span> x)</span></span>{</span><br><span class="line"> <span class="keyword">int</span> p = <span class="number">0</span>,res = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">30</span>; i >= <span class="number">0</span>; i --){</span><br><span class="line"> <span class="keyword">int</span> u = x >> i & <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">if</span>(cnt[ son[p][!u] ] ){</span><br><span class="line"> res += <span class="number">1</span> << i;</span><br><span class="line"> p = son[p][!u];</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span> p = son[p][u];</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> res;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> n,m,res = <span class="number">0</span>;</span><br><span class="line"> cin>>n>>m;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= n; i ++ ){</span><br><span class="line"> <span class="keyword">int</span> x;</span><br><span class="line"> cin>>x;</span><br><span class="line"> s[i] = s[i - <span class="number">1</span>] ^ x;</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">insert</span>(s[<span class="number">0</span>],<span class="number">1</span>);</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>;i <= n; i ++ ){ </span><br><span class="line"> <span class="keyword">if</span>(i > m )<span class="built_in">insert</span>(s[i - m - <span class="number">1</span>], <span class="number">-1</span>);</span><br><span class="line"> res = <span class="built_in">max</span>(res, <span class="built_in">query</span>(s[i]));</span><br><span class="line"> <span class="built_in">insert</span>(s[i],<span class="number">1</span>);</span><br><span class="line"> }</span><br><span class="line"> cout<<res;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h3 id="最小生成树"><a href="#最小生成树" class="headerlink" title="最小生成树"></a>最小生成树</h3><p>普里姆算法的时间复杂度为O(n <em> n),n为顶点数,适用于稠密图;<br>克鲁斯卡尔算法的时间复杂度为O(e </em> loge),e为边数,适用于稀疏图;</p>
<h4 id="Kruskal算法"><a href="#Kruskal算法" class="headerlink" title="Kruskal算法"></a>Kruskal算法</h4><figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">int</span> n,m,sum,cnt;</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">node</span>{</span></span><br><span class="line"> <span class="keyword">int</span> start,end,power;<span class="comment">//start为起始点,end为终止点,power为权值</span></span><br><span class="line">}edge[<span class="number">5050</span>];</span><br><span class="line"><span class="keyword">int</span> pre[<span class="number">5050</span>];</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">cmp</span><span class="params">(node a, node b)</span></span>{</span><br><span class="line"> <span class="keyword">return</span> a.power<b.power;<span class="comment">//按照权值排序</span></span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">find</span><span class="params">(<span class="keyword">int</span> x)</span></span>{<span class="comment">//并查集找祖先</span></span><br><span class="line"> <span class="keyword">if</span>(x!=pre[x]){</span><br><span class="line"> pre[x]=<span class="built_in">find</span>(pre[x]);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> pre[x];</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">merge</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> y,<span class="keyword">int</span> n)</span></span>{<span class="comment">//并查集合并函数,n是用来记录最短路中应该加入哪个点</span></span><br><span class="line"> <span class="keyword">int</span> fx=<span class="built_in">find</span>(x);</span><br><span class="line"> <span class="keyword">int</span> fy=<span class="built_in">find</span>(y);</span><br><span class="line"> <span class="keyword">if</span>(fx!=fy){</span><br><span class="line"> pre[fx]=fy;</span><br><span class="line"> cnt++;</span><br><span class="line"> sum+=edge[n].power;</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d%d"</span>,&n,&m);</span><br><span class="line"> <span class="keyword">int</span> start,end,power;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>; i<=m; i++){</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d %d %d"</span>, &start, &end, &power);</span><br><span class="line"> edge[i].start=start,edge[i].end=end,edge[i].power=power;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>; i<=m; i++){</span><br><span class="line"> pre[i]=i;</span><br><span class="line"> }<span class="comment">//并查集初始化</span></span><br><span class="line"> <span class="built_in">sort</span>(edge+<span class="number">1</span>, edge+m+<span class="number">1</span>,cmp);</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>; i <= m; i++){</span><br><span class="line"> <span class="built_in">merge</span>(edge[i].start,edge[i].end,i);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(cnt<n<span class="number">-1</span>)</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"?\n"</span>);</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>,sum);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h4 id="prim算法(邻接矩阵)"><a href="#prim算法(邻接矩阵)" class="headerlink" title="prim算法(邻接矩阵)"></a>prim算法(邻接矩阵)</h4><figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N = <span class="number">510</span>, INF = <span class="number">0x3f3f3f3f</span>;</span><br><span class="line"><span class="keyword">int</span> n, m;</span><br><span class="line"><span class="keyword">int</span> g[N][N];</span><br><span class="line"><span class="keyword">int</span> dist[N];</span><br><span class="line"><span class="keyword">bool</span> st[N];</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">prim</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="built_in">memset</span>(dist, <span class="number">0x3f</span>, <span class="keyword">sizeof</span> dist);</span><br><span class="line"> <span class="keyword">int</span> res = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < n; i ++ ){</span><br><span class="line"> <span class="keyword">int</span> t = <span class="number">-1</span>;</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">1</span>; j <= n; j ++ )</span><br><span class="line"> <span class="keyword">if</span> (!st[j] && (t == <span class="number">-1</span> || dist[t] > dist[j]))</span><br><span class="line"> t = j;</span><br><span class="line"> <span class="keyword">if</span> (i && dist[t] == INF) <span class="keyword">return</span> INF;</span><br><span class="line"> <span class="keyword">if</span> (i) res += dist[t];</span><br><span class="line"> st[t] = <span class="literal">true</span>;</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">1</span>; j <= n; j ++ ) dist[j] = <span class="built_in">min</span>(dist[j], g[t][j]);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> res;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d%d"</span>, &n, &m);</span><br><span class="line"> <span class="built_in">memset</span>(g, <span class="number">0x3f</span>, <span class="keyword">sizeof</span> g);</span><br><span class="line"> <span class="keyword">while</span> (m -- ){</span><br><span class="line"> <span class="keyword">int</span> a, b, c;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d%d%d"</span>, &a, &b, &c);</span><br><span class="line"> g[a][b] = g[b][a] = <span class="built_in">min</span>(g[a][b], c);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">int</span> t = <span class="built_in">prim</span>();</span><br><span class="line"> <span class="keyword">if</span> (t == INF) <span class="built_in">puts</span>(<span class="string">"?"</span>);</span><br><span class="line"> <span class="keyword">else</span> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, t);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h3 id="最大子序列和"><a href="#最大子序列和" class="headerlink" title="最大子序列和"></a>最大子序列和</h3><p><strong>在线处理</strong></p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">MaxSubseqSum1</span><span class="params">(<span class="keyword">int</span> A[], <span class="keyword">int</span> N)</span></span>{</span><br><span class="line"> <span class="keyword">int</span> ThisSum, MaxSum = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">int</span> i;</span><br><span class="line"> ThisSum = MaxSum=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span> (i = <span class="number">0</span>;i < N;i++){</span><br><span class="line"> ThisSum += A[i];</span><br><span class="line"> <span class="keyword">if</span> (ThisSum > MaxSum)</span><br><span class="line"> MaxSum = ThisSum;</span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">if</span> (ThisSum < <span class="number">0</span>)</span><br><span class="line"> ThisSum = <span class="number">0</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> MaxSum;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
]]></content>
<tags>
<tag>C++</tag>
</tags>
</entry>
<entry>
<title>HTML5入门</title>
<url>/2022/06/25/45/</url>
<content><![CDATA[<h2 id="HTML5入门"><a href="#HTML5入门" class="headerlink" title="HTML5入门"></a>HTML5入门</h2><p>刚学html1小时,简单做了个说明页面,没有css美化不堪入目</p>
<figure class="highlight html"><table><tbody><tr><td class="code"><pre><span class="line"><span class="meta"><!DOCTYPE <span class="meta-keyword">html</span>></span></span><br><span class="line"><span class="tag"><<span class="name">html</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">head</span>></span></span><br><span class="line"> <span class="comment"><!--注释--></span></span><br><span class="line"> <span class="tag"><<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"utf-8"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">title</span>></span>瑜璟的小屋<span class="tag"></<span class="name">title</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">head</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">body</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">h1</span>></span>说明<span class="tag"></<span class="name">h1</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">p</span>></span><span class="tag"><<span class="name">strong</span>></span>这是一个登录界面<span class="tag"></<span class="name">strong</span>></span><span class="tag"></<span class="name">p</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">p</span>></span>前往我的<span class="tag"><<span class="name">a</span> <span class="attr">href</span>=<span class="string">"https://yujingsea.github.io"</span> <span class="attr">target</span>=<span class="string">"_blank"</span>></span>技术博客<span class="tag"></<span class="name">a</span>></span><span class="tag"></<span class="name">p</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">h2</span>></span>博客的特点<span class="tag"></<span class="name">h2</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">ol</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">li</span>></span><span class="tag"><<span class="name">em</span>></span>绿色无广告<span class="tag"></<span class="name">em</span>></span><span class="tag"></<span class="name">li</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">li</span>></span><span class="tag"><<span class="name">em</span>></span>分享编程基础教学内容<span class="tag"></<span class="name">em</span>></span><span class="tag"></<span class="name">li</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">li</span>></span><span class="tag"><<span class="name">em</span>></span>内置烟花播放器网页<span class="tag"></<span class="name">em</span>></span><span class="tag"></<span class="name">li</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">li</span>></span><span class="tag"><<span class="name">em</span>></span>加载迅速<span class="tag"></<span class="name">em</span>></span><span class="tag"></<span class="name">li</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">ol</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">table</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">thead</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">tr</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">th</span>></span>作者<span class="tag"></<span class="name">th</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">th</span>></span>位置<span class="tag"></<span class="name">th</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">th</span>></span>邮箱<span class="tag"></<span class="name">th</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">tr</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">thead</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">thead</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">tr</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">td</span>></span>瑜璟<span class="tag"></<span class="name">td</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">td</span>></span>河南郑州<span class="tag"></<span class="name">td</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">td</span>></span>[email protected]<span class="tag"></<span class="name">td</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">tr</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">thead</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">table</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">br</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">hr</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">br</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">form</span> <span class="attr">action</span>=<span class="string">"form.js"</span> <span class="attr">method</span>=<span class="string">"post"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">div</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">label</span>></span>账号<span class="tag"></<span class="name">label</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"text"</span> <span class="attr">name</span>=<span class="string">"name"</span> <span class="attr">placeholder</span>=<span class="string">"输入你的账号"</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">div</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">div</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">label</span>></span>密码<span class="tag"></<span class="name">label</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"password"</span> <span class="attr">name</span>=<span class="string">"password"</span> <span class="attr">placeholder</span>=<span class="string">"输入你的密码"</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">div</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">name</span>=<span class="string">"submit"</span> <span class="attr">value</span>=<span class="string">"登录"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">name</span>=<span class="string">"按钮"</span> <span class="attr">value</span>=<span class="string">"注册"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">br</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">name</span>=<span class="string">"按钮"</span> <span class="attr">value</span>=<span class="string">"这是一只可爱的按钮"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">p</span>></span><span class="tag"><<span class="name">abbr</span> <span class="attr">title</span>=<span class="string">"你怎么这么笨"</span>></span>忘记密码?<span class="tag"></<span class="name">abbr</span>></span><span class="tag"></<span class="name">p</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">form</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">body</span>></span></span><br><span class="line"><span class="tag"></<span class="name">html</span>></span></span><br></pre></td></tr></tbody></table></figure>
]]></content>
<tags>
<tag>html5</tag>
</tags>
</entry>
<entry>
<title>题型整理(4)</title>
<url>/2022/06/28/46/</url>
<content><![CDATA[<h2 id="题型整理(4)"><a href="#题型整理(4)" class="headerlink" title="题型整理(4)"></a>题型整理(4)</h2><h3 id="单调栈"><a href="#单调栈" class="headerlink" title="单调栈"></a>单调栈</h3><h4 id="1-视野总和"><a href="#1-视野总和" class="headerlink" title="1.视野总和"></a>1.视野总和</h4><p>描述:有n个人站队,所有的人全部向右看,个子高的可以看到个子低的发型,给出每个人的身高,问所有人能看到其他人发现总和是多少。</p>
<p>输入:4 3 7 1<br>输出:2</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">FieldSum</span><span class="params">(vector<<span class="keyword">int</span>>& v)</span></span>{</span><br><span class="line"> v.<span class="built_in">push_back</span>(INT_MAX);<span class="comment">//压入最大值保证栈中元素能完全出栈</span></span><br><span class="line"> stack<<span class="keyword">int</span>> st;</span><br><span class="line"> <span class="keyword">int</span> sum = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < (<span class="keyword">int</span>)v.<span class="built_in">size</span>(); i++){</span><br><span class="line"> <span class="keyword">if</span> (st.<span class="built_in">empty</span>() || v[st.<span class="built_in">top</span>()] > v[i])</span><br><span class="line"> st.<span class="built_in">push</span>(i);</span><br><span class="line"> <span class="keyword">else</span>{</span><br><span class="line"> <span class="keyword">while</span> (!st.<span class="built_in">empty</span>() && v[st.<span class="built_in">top</span>()] <= v[i]){</span><br><span class="line"> <span class="keyword">int</span> top = st.<span class="built_in">top</span>();</span><br><span class="line"> st.<span class="built_in">pop</span>();</span><br><span class="line"> sum += (i - top - <span class="number">1</span>);</span><br><span class="line"> }</span><br><span class="line"> st.<span class="built_in">push</span>(i);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> sum;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h4 id="2-柱状图中的最大矩形"><a href="#2-柱状图中的最大矩形" class="headerlink" title="2.柱状图中的最大矩形"></a><a href="https://leetcode.cn/problems/largest-rectangle-in-histogram/">2.柱状图中的最大矩形</a></h4><p>描述:给定 <em>n</em> 个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为 1 , 求在该柱状图中,能够勾勒出来的矩形的最大面积。</p>
<p>输入:2 1 5 6 2 3</p>
<p>输出:10</p>
<figure class="highlight c++"><table><tbody><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">maxS</span><span class="params">(vector<<span class="keyword">int</span>>& heights)</span> </span>{</span><br><span class="line"> heights.<span class="built_in">push_back</span>(<span class="number">0</span>);<span class="comment">//压入最小值保证栈中元素能完全出栈</span></span><br><span class="line"> stack<<span class="keyword">int</span>> stk;</span><br><span class="line"> <span class="keyword">int</span> maxs= <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>;i<heights.<span class="built_in">size</span>();i++){</span><br><span class="line"> <span class="keyword">while</span>(!stk.<span class="built_in">empty</span>() && heights[i]<heights[stk.<span class="built_in">top</span>()]){</span><br><span class="line"> <span class="keyword">int</span> top= stk.<span class="built_in">top</span>();</span><br><span class="line"> stk.<span class="built_in">pop</span>();</span><br><span class="line"> maxs = <span class="built_in">max</span>(maxs,heights[top]*(stk.<span class="built_in">empty</span>()? i:(i - stk.<span class="built_in">top</span>() <span class="number">-1</span>)));</span><br><span class="line"> }</span><br><span class="line"> stk.<span class="built_in">push</span>(i);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> maxs;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>