-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathch3.html
More file actions
1073 lines (988 loc) · 48.4 KB
/
Copy pathch3.html
File metadata and controls
1073 lines (988 loc) · 48.4 KB
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
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ch.3 알고리즘 | CS Visualizer</title>
<link href="https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@300;400;500;600;700&family=Noto+Sans+KR:wght@300;400;500;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="style.css">
<style>
/* ── ch3-only overrides ── */
.bm-row{display:flex;align-items:center;gap:12px;flex-wrap:wrap;margin:10px 0;}
.bm-label{font-family:'JetBrains Mono',monospace;font-size:0.82rem;color:var(--text-dim);width:28px;flex-shrink:0;}
.bm-bits{display:flex;gap:4px;}
.bm-bit{width:38px;height:38px;display:flex;align-items:center;justify-content:center;font-family:'JetBrains Mono',monospace;font-size:1.1rem;font-weight:700;border:2px solid var(--border);border-radius:6px;cursor:pointer;transition:all 0.18s;user-select:none;color:var(--text-dim);background:var(--surface2);}
.bm-bit.one{border-color:var(--accent4);background:rgba(255,170,0,0.15);color:var(--accent4);box-shadow:0 0 10px rgba(255,170,0,0.15);}
.bm-bit.result-bit{cursor:default;}
.bm-bit.result-bit.one{border-color:var(--accent);background:rgba(0,255,170,0.15);color:var(--accent);}
.bm-decimal{font-family:'JetBrains Mono',monospace;font-size:1.1rem;font-weight:700;color:var(--accent4);margin-left:10px;}
.bm-result-decimal{color:var(--accent);}
.bm-op-display{font-family:'JetBrains Mono',monospace;font-size:0.9rem;color:var(--text-dim);margin:16px 0;padding:12px 16px;background:var(--surface2);border-radius:8px;border:1px solid var(--border);display:flex;gap:8px;flex-wrap:wrap;align-items:center;}
.bm-op-display .op-sym{color:var(--accent2);font-weight:700;font-size:1.1rem;}
.bm-op-display .eq-sym{color:var(--text-dim);}
.bm-examples{margin-top:20px;display:flex;flex-wrap:wrap;gap:10px;}
.bm-example-card{background:var(--surface2);border:1px solid var(--border);border-radius:8px;padding:12px 16px;font-family:'JetBrains Mono',monospace;font-size:0.78rem;line-height:1.8;}
.bm-example-card .ex-title{color:var(--accent4);font-size:0.7rem;margin-bottom:4px;}
.bm-example-card code{color:var(--accent);background:rgba(0,255,170,0.08);padding:1px 5px;border-radius:3px;}
/* suffix array */
.sa-table{width:100%;border-collapse:collapse;font-family:'JetBrains Mono',monospace;font-size:0.82rem;margin-top:12px;}
.sa-table th{background:var(--surface2);color:var(--text-dim);padding:8px 12px;text-align:left;border-bottom:1px solid var(--border);font-size:0.72rem;letter-spacing:1px;}
.sa-table td{padding:7px 12px;border-bottom:1px solid rgba(42,42,58,0.5);transition:all 0.2s;color:var(--text-dim);}
.sa-table tr.sa-match td{background:rgba(0,255,170,0.08);color:var(--accent);border-left:2px solid var(--accent);}
.sa-table td.sa-suffix{color:var(--text);}
.sa-table td.sa-rank{color:var(--accent3);}
.sa-table td.sa-lcp{color:var(--accent2);}
.sa-input-row{display:flex;gap:8px;align-items:center;flex-wrap:wrap;margin-bottom:12px;}
.sa-input{background:var(--surface2);border:1px solid var(--border);border-radius:8px;color:var(--text);font-family:'JetBrains Mono',monospace;font-size:1rem;padding:9px 14px;outline:none;width:180px;}
.sa-input:focus{border-color:var(--accent4);}
.sa-search-row{display:flex;gap:8px;align-items:center;flex-wrap:wrap;margin-top:16px;}
.sa-match-info{font-family:'JetBrains Mono',monospace;font-size:0.82rem;color:var(--text-dim);margin-top:10px;min-height:20px;}
/* flow graph */
.flow-info{font-family:'JetBrains Mono',monospace;font-size:0.85rem;color:var(--text-dim);margin-top:14px;display:flex;gap:24px;flex-wrap:wrap;align-items:center;}
.flow-info .flow-val{font-size:1.6rem;font-weight:700;color:var(--accent);}
</style>
</head>
<body class="page-body">
<nav class="page-nav">
<a href="index.html" class="nav-back">← 홈</a>
<div class="nav-chapter-title" style="color:var(--accent4)">Ch.3 — 알고리즘</div>
<div class="nav-sections">
<a href="#sorting" class="nav-sec-link">정렬</a>
<a href="#search" class="nav-sec-link">탐색</a>
<a href="#recursion" class="nav-sec-link">재귀</a>
<a href="#bigo" class="nav-sec-link">빅오</a>
<a href="#bitmask" class="nav-sec-link">비트마스크</a>
<a href="#suffixarray" class="nav-sec-link">접미사배열</a>
<a href="#dp" class="nav-sec-link">동적프로그래밍</a>
</div>
</nav>
<!-- ══════════════════════════════════════════════════ -->
<!-- SECTION 1: 정렬 알고리즘 -->
<!-- ══════════════════════════════════════════════════ -->
<section id="sorting">
<div class="section-header">
<div class="tag tag-ch3">01 — SORTING</div>
<h2>정렬 알고리즘</h2>
<p>배열을 정렬하는 다양한 알고리즘을 시각적으로 비교합니다. 노란색=비교, 분홍색=교환, 초록색=정렬 완료.</p>
</div>
<div class="viz-box">
<div class="controls">
<button class="btn" id="sortBtnBubble" onclick="startSort('bubble')">버블 정렬</button>
<button class="btn" id="sortBtnSelection" onclick="startSort('selection')">선택 정렬</button>
<button class="btn" id="sortBtnInsertion" onclick="startSort('insertion')">삽입 정렬</button>
<button class="btn" id="sortBtnQuick" onclick="startSort('quick')">퀵 정렬</button>
<button class="btn" id="sortBtnMerge" onclick="startSort('merge')">병합 정렬</button>
<button class="btn danger" onclick="resetSort()">새 배열</button>
<div class="sort-speed">
속도: <input type="range" id="sortSpeed" min="1" max="10" value="5"> <span id="sortSpeedLabel">5</span>
</div>
</div>
<div class="sort-bars" id="sortBars"></div>
<div class="sort-array-view" id="sortArrayView"></div>
<div class="info-text" id="sortInfo">알고리즘을 선택하면 시각화가 시작됩니다.</div>
</div>
</section>
<div class="section-divider"></div>
<!-- ══════════════════════════════════════════════════ -->
<!-- SECTION 2: 탐색 알고리즘 -->
<!-- ══════════════════════════════════════════════════ -->
<section id="search">
<div class="section-header">
<div class="tag tag-ch3">02 — SEARCH</div>
<h2>탐색 알고리즘</h2>
<p>선형 탐색과 이진 탐색을 동시에 비교합니다. 정렬된 배열에서 이진 탐색이 얼마나 효율적인지 확인하세요.</p>
</div>
<div class="viz-box">
<div class="search-input-row">
<label style="font-family:'JetBrains Mono',monospace;font-size:0.82rem;color:var(--text-dim)">찾는 값:</label>
<input type="number" class="search-input" id="searchTarget" value="42" min="1" max="99">
<button class="btn" onclick="runSearch()">동시 실행</button>
<button class="btn danger" onclick="resetSearch()">초기화</button>
</div>
<div class="search-grid">
<div>
<div class="search-label">선형 탐색 (Linear Search)</div>
<div class="search-arr" id="linearArr"></div>
<div class="search-steps"><span id="linearSteps">0</span><br><small>단계</small></div>
</div>
<div>
<div class="search-label">이진 탐색 (Binary Search)</div>
<div class="search-arr" id="binaryArr"></div>
<div class="search-steps"><span id="binarySteps">0</span><br><small>단계</small></div>
</div>
</div>
<div class="info-text" id="searchInfo">배열에서 찾을 값을 입력하고 "동시 실행"을 클릭하세요.</div>
</div>
</section>
<div class="section-divider"></div>
<!-- ══════════════════════════════════════════════════ -->
<!-- SECTION 3: 재귀 -->
<!-- ══════════════════════════════════════════════════ -->
<section id="recursion">
<div class="section-header">
<div class="tag tag-ch3">03 — RECURSION</div>
<h2>재귀</h2>
<p>재귀 함수는 자기 자신을 호출합니다. factorial(n)의 콜 스택이 쌓이고 되돌아오는 과정을 확인하세요.</p>
</div>
<div class="viz-box">
<div class="recursion-controls">
<label style="font-family:'JetBrains Mono',monospace;font-size:0.82rem;color:var(--text-dim)">n =</label>
<input type="range" id="recursionN" min="1" max="8" value="5" oninput="updateRecursion()">
<span class="recursion-n-display" id="recursionNDisplay">5</span>
<button class="btn" onclick="updateRecursion()">시각화</button>
</div>
<div class="recursion-stack-visual" id="recursionStack"></div>
<div class="info-text" id="recursionInfo"></div>
</div>
</section>
<div class="section-divider"></div>
<!-- ══════════════════════════════════════════════════ -->
<!-- SECTION 4: 빅오 표기법 -->
<!-- ══════════════════════════════════════════════════ -->
<section id="bigo">
<div class="section-header">
<div class="tag tag-ch3">04 — BIG-O</div>
<h2>빅오 표기법</h2>
<p>알고리즘의 시간 복잡도를 비교합니다. n이 커질수록 각 복잡도 클래스의 차이가 얼마나 커지는지 확인하세요.</p>
</div>
<div class="viz-box">
<div class="controls" style="margin-bottom:16px">
<div class="sort-speed">
n: <input type="range" id="bigoN" min="1" max="50" value="20" oninput="drawBigO()">
<span id="bigoNDisplay" class="bigo-n-display">20</span>
</div>
</div>
<canvas id="bigoCanvas" height="300"></canvas>
<div class="bigo-legend" id="bigoLegend"></div>
</div>
</section>
<div class="section-divider"></div>
<!-- ══════════════════════════════════════════════════ -->
<!-- SECTION 5: 비트마스크 [NEW] -->
<!-- ══════════════════════════════════════════════════ -->
<section id="bitmask">
<div class="section-header">
<div class="tag tag-ch3">05 — BITMASK ✦ NEW</div>
<h2>비트마스크</h2>
<p>비트마스크는 정수의 각 비트를 플래그로 사용해 집합을 표현합니다. 상태 압축, 부분집합 탐색에서 강력한 도구입니다. AND, OR, XOR, NOT, SHIFT 연산을 직접 체험해보세요.</p>
</div>
<div class="viz-box">
<div class="controls">
<button class="btn" id="bmBtnAND" onclick="setBmOp('AND')" >AND</button>
<button class="btn" id="bmBtnOR" onclick="setBmOp('OR')" >OR</button>
<button class="btn" id="bmBtnXOR" onclick="setBmOp('XOR')" >XOR</button>
<button class="btn" id="bmBtnNOT" onclick="setBmOp('NOT')" >NOT A</button>
<button class="btn" id="bmBtnSHL" onclick="setBmOp('SHL')" ><<1</button>
<button class="btn" id="bmBtnSHR" onclick="setBmOp('SHR')" >>>1</button>
</div>
<!-- A row -->
<div class="bm-row">
<span class="bm-label">A</span>
<div class="bm-bits" id="bmBitsA"></div>
<span class="bm-decimal" id="bmDecA">0</span>
</div>
<!-- op symbol -->
<div id="bmOpSymbol" style="font-family:'JetBrains Mono',monospace;font-size:1rem;color:var(--accent2);margin:4px 0 4px 40px;font-weight:700;">AND</div>
<!-- B row -->
<div class="bm-row" id="bmBRowEl">
<span class="bm-label">B</span>
<div class="bm-bits" id="bmBitsB"></div>
<span class="bm-decimal" id="bmDecB">0</span>
</div>
<div style="border-top:1px solid var(--border);margin:10px 0 10px 40px;"></div>
<!-- Result row -->
<div class="bm-row">
<span class="bm-label" style="color:var(--accent)">=</span>
<div class="bm-bits" id="bmBitsR"></div>
<span class="bm-decimal bm-result-decimal" id="bmDecR">0</span>
</div>
<div class="bm-op-display" id="bmOpDisplay"></div>
<!-- Practical examples -->
<div style="font-family:'JetBrains Mono',monospace;font-size:0.75rem;color:var(--text-dim);margin-top:20px;margin-bottom:8px;letter-spacing:1px;">실용 예제</div>
<div class="bm-examples">
<div class="bm-example-card">
<div class="ex-title">집합 표현</div>
집합 {1,3,5} → 비트 1,3,5 ON<br>
<code>0b00101010 = 42</code><br>
bit i가 1 ↔ 원소 i 포함
</div>
<div class="bm-example-card">
<div class="ex-title">원소 추가/제거</div>
추가: <code>S |= (1 << i)</code><br>
제거: <code>S &= ~(1 << i)</code><br>
포함: <code>(S >> i) & 1</code>
</div>
<div class="bm-example-card">
<div class="ex-title">부분집합 순회</div>
<code>for(s=S;s;s=(s-1)&S)</code><br>
전체 부분집합: <code>2^n</code>가지<br>
상태압축 DP에서 활용
</div>
<div class="bm-example-card">
<div class="ex-title">비트 트릭</div>
최하위비트: <code>x & (-x)</code><br>
비트수 세기: <code>popcount(x)</code><br>
짝수 검사: <code>(x & 1) == 0</code>
</div>
</div>
</div>
</section>
<div class="section-divider"></div>
<!-- ══════════════════════════════════════════════════ -->
<!-- SECTION 6: 접미사 배열 [NEW] -->
<!-- ══════════════════════════════════════════════════ -->
<section id="suffixarray">
<div class="section-header">
<div class="tag tag-ch3">06 — SUFFIX ARRAY ✦ NEW</div>
<h2>접미사 배열</h2>
<p>문자열의 모든 접미사를 정렬한 배열입니다. 문자열 검색(O(m log n)), 최장 공통 부분 문자열 탐색 등에 사용됩니다. 접미사를 사전순 정렬하면 이진 탐색으로 패턴을 빠르게 찾을 수 있습니다.</p>
</div>
<div class="viz-box">
<div class="sa-input-row">
<label style="font-family:'JetBrains Mono',monospace;font-size:0.82rem;color:var(--text-dim)">문자열:</label>
<input type="text" class="sa-input" id="saInput" value="banana" maxlength="20" oninput="renderSuffixArray()">
<button class="btn" onclick="renderSuffixArray()">배열 생성</button>
</div>
<div class="sa-search-row">
<label style="font-family:'JetBrains Mono',monospace;font-size:0.82rem;color:var(--text-dim)">패턴 검색:</label>
<input type="text" class="sa-input" id="saPattern" placeholder="패턴 입력" maxlength="10">
<button class="btn" onclick="searchPattern()">검색</button>
<button class="btn danger" onclick="clearSaSearch()">초기화</button>
</div>
<div class="sa-match-info" id="saMatchInfo"></div>
<div style="overflow-x:auto;margin-top:4px">
<table class="sa-table" id="saTable">
<thead><tr><th>순위</th><th>원본 인덱스</th><th>접미사</th><th>LCP</th></tr></thead>
<tbody id="saTableBody"></tbody>
</table>
</div>
<div class="info-text" style="margin-top:16px">
LCP = 인접한 정렬된 접미사 간의 최장 공통 접두사 길이. 첫 번째 항목은 정의되지 않습니다(—).
</div>
</div>
</section>
<div class="section-divider"></div>
<!-- SECTION 7: 동적 프로그래밍 -->
<section id="dp">
<div class="section-header">
<div class="tag tag-ch3">07 — DYNAMIC PROGRAMMING</div>
<h2>동적 프로그래밍 (DP)</h2>
<p>복잡한 문제를 부분 문제로 나누고, 그 결과를 저장(메모이제이션)하여 중복 계산을 피하는 알고리즘 기법입니다. 최적 부분 구조 + 중복 부분 문제가 핵심 조건입니다.</p>
</div>
<div class="viz-box">
<!-- Fibonacci Memoization -->
<div>
<div style="font-family:'JetBrains Mono',monospace;font-size:0.85rem;font-weight:700;margin-bottom:12px;color:var(--accent4)">① 피보나치 — 메모이제이션 vs 단순 재귀</div>
<div style="display:flex;gap:12px;align-items:center;flex-wrap:wrap;margin-bottom:14px;">
<label style="font-family:'JetBrains Mono',monospace;font-size:0.82rem;color:var(--text-dim);">n =</label>
<input type="range" id="fibN" min="1" max="12" value="7"
style="width:140px;-webkit-appearance:none;height:4px;background:var(--border);border-radius:2px;outline:none;cursor:pointer;"
oninput="runFib()">
<span id="fibNVal" style="font-family:'JetBrains Mono',monospace;color:var(--accent4);font-weight:700;font-size:1.1rem;">7</span>
</div>
<div id="fibMemoTable" style="display:flex;flex-wrap:wrap;gap:6px;margin-bottom:12px;"></div>
<div class="info-text" id="fibInfo"></div>
</div>
<!-- LCS -->
<div style="border-top:1px solid var(--border);padding-top:24px;margin-top:24px;">
<div style="font-family:'JetBrains Mono',monospace;font-size:0.85rem;font-weight:700;margin-bottom:12px;color:var(--accent4)">② 최장 공통 부분 수열 (LCS)</div>
<div style="display:flex;gap:10px;flex-wrap:wrap;margin-bottom:14px;align-items:center;">
<input type="text" id="lcsA" value="ABCBDAB" maxlength="9" class="sa-input" style="width:130px;text-transform:uppercase;" oninput="runLCS()">
<span style="font-family:'JetBrains Mono',monospace;color:var(--text-dim);">vs</span>
<input type="text" id="lcsB" value="BDCABA" maxlength="9" class="sa-input" style="width:130px;text-transform:uppercase;" oninput="runLCS()">
</div>
<div id="lcsTable" style="overflow-x:auto;"></div>
<div class="info-text" style="margin-top:10px;" id="lcsInfo"></div>
</div>
<!-- Knapsack -->
<div style="border-top:1px solid var(--border);padding-top:24px;margin-top:24px;">
<div style="font-family:'JetBrains Mono',monospace;font-size:0.85rem;font-weight:700;margin-bottom:8px;color:var(--accent4)">③ 0/1 배낭 문제 (Knapsack)</div>
<div style="font-family:'JetBrains Mono',monospace;font-size:0.76rem;color:var(--text-dim);margin-bottom:12px;">용량 W 이하에서 최대 가치를 선택하는 문제. DP[i][w] = 아이템 i까지 고려 시 용량 w에서 최대 가치.</div>
<div style="display:flex;gap:10px;align-items:center;flex-wrap:wrap;margin-bottom:14px;">
<label style="font-family:'JetBrains Mono',monospace;font-size:0.82rem;color:var(--text-dim);">용량 W =</label>
<input type="range" id="ksW" min="5" max="15" value="10"
style="width:120px;-webkit-appearance:none;height:4px;background:var(--border);border-radius:2px;outline:none;cursor:pointer;"
oninput="runKnapsack()">
<span id="ksWVal" style="font-family:'JetBrains Mono',monospace;color:var(--accent4);font-weight:700;font-size:1.1rem;">10</span>
</div>
<div id="ksItemList" style="display:flex;gap:8px;flex-wrap:wrap;margin-bottom:14px;"></div>
<div id="ksTable" style="overflow-x:auto;"></div>
<div class="info-text" style="margin-top:10px;" id="ksInfo"></div>
</div>
<div class="info-text" style="margin-top:20px;">
DP 핵심 조건: <strong style="color:var(--accent)">최적 부분 구조</strong> (큰 문제의 최적해 = 부분 문제 최적해 조합) & <strong style="color:var(--accent2)">중복 부분 문제</strong> (같은 계산 반복).
접근법: <strong style="color:var(--accent4)">탑다운</strong>(재귀+메모이제이션) vs <strong style="color:var(--accent3)">바텀업</strong>(반복문+테이블).
</div>
</div>
</section>
<footer>CS Visualizer · <a href="index.html" style="color:var(--accent4);text-decoration:none;">목차로 돌아가기</a></footer>
<script>
// ═══════════════════════════════════════════════════════════════════
// SECTION 1: SORTING
// ═══════════════════════════════════════════════════════════════════
let sA = [], sR = false;
const N_BARS = 30;
function gD() {
const spd = parseInt(document.getElementById('sortSpeed').value);
return Math.max(10, 310 - spd * 30);
}
function sl(ms) { return new Promise(r => setTimeout(r, ms)); }
function initSort() {
sA = Array.from({length: N_BARS}, () => Math.floor(Math.random() * 95) + 5);
renderBars([]);
document.getElementById('sortInfo').textContent = '알고리즘을 선택하면 시각화가 시작됩니다.';
}
function renderBars(state) {
const container = document.getElementById('sortBars');
const arrView = document.getElementById('sortArrayView');
const max = Math.max(...sA);
container.innerHTML = '';
arrView.innerHTML = '';
sA.forEach((v, i) => {
const bar = document.createElement('div');
bar.className = 'sort-bar' + (state[i] ? ' ' + state[i] : '');
bar.style.height = (v / max * 240 + 10) + 'px';
container.appendChild(bar);
const cell = document.createElement('div');
cell.className = 'sarr-cell' + (state[i] ? ' ' + state[i] : '');
cell.textContent = v;
arrView.appendChild(cell);
});
}
function rSB(indices, cls, duration) {
const state = {};
indices.forEach(i => { state[i] = cls; });
renderBars(state);
return sl(duration);
}
async function startSort(type) {
if (sR) return;
sR = true;
// disable buttons
['Bubble','Selection','Insertion','Quick','Merge'].forEach(t =>
document.getElementById('sortBtn' + t).disabled = true
);
const arr = [...sA];
document.getElementById('sortInfo').textContent = {
bubble: '버블 정렬: 인접 원소 비교·교환 O(n²)',
selection: '선택 정렬: 최솟값 선택 O(n²)',
insertion: '삽입 정렬: 올바른 위치에 삽입 O(n²)',
quick: '퀵 정렬: 피벗 기준 분할 O(n log n) avg',
merge: '병합 정렬: 분할 후 병합 O(n log n)'
}[type];
if (type === 'bubble') await bubbleSort();
else if (type === 'selection') await selectionSort();
else if (type === 'insertion') await insertionSort();
else if (type === 'quick') await quickSortWrapper();
else if (type === 'merge') await mergeSortWrapper();
// mark all sorted
const s = {};
sA.forEach((_, i) => s[i] = 'sorted');
renderBars(s);
sR = false;
['Bubble','Selection','Insertion','Quick','Merge'].forEach(t =>
document.getElementById('sortBtn' + t).disabled = false
);
}
async function bubbleSort() {
const n = sA.length;
for (let i = 0; i < n - 1; i++) {
for (let j = 0; j < n - 1 - i; j++) {
await rSB([j, j + 1], 'comparing', gD());
if (sA[j] > sA[j + 1]) {
[sA[j], sA[j + 1]] = [sA[j + 1], sA[j]];
await rSB([j, j + 1], 'swapping', gD());
}
}
}
}
async function selectionSort() {
const n = sA.length;
for (let i = 0; i < n - 1; i++) {
let minIdx = i;
for (let j = i + 1; j < n; j++) {
await rSB([minIdx, j], 'comparing', gD());
if (sA[j] < sA[minIdx]) minIdx = j;
}
if (minIdx !== i) {
[sA[i], sA[minIdx]] = [sA[minIdx], sA[i]];
await rSB([i, minIdx], 'swapping', gD());
}
}
}
async function insertionSort() {
const n = sA.length;
for (let i = 1; i < n; i++) {
let j = i;
while (j > 0) {
await rSB([j - 1, j], 'comparing', gD());
if (sA[j - 1] > sA[j]) {
[sA[j - 1], sA[j]] = [sA[j], sA[j - 1]];
await rSB([j - 1, j], 'swapping', gD());
j--;
} else break;
}
}
}
async function quickSortWrapper() {
await quickSort(0, sA.length - 1);
}
async function quickSort(lo, hi) {
if (lo >= hi) return;
let pivot = sA[hi], i = lo;
for (let j = lo; j < hi; j++) {
await rSB([j, hi], 'comparing', gD());
if (sA[j] <= pivot) {
[sA[i], sA[j]] = [sA[j], sA[i]];
if (i !== j) await rSB([i, j], 'swapping', gD());
i++;
}
}
[sA[i], sA[hi]] = [sA[hi], sA[i]];
await rSB([i, hi], 'swapping', gD());
await quickSort(lo, i - 1);
await quickSort(i + 1, hi);
}
async function mergeSortWrapper() {
await mergeSort(0, sA.length - 1);
}
async function mergeSort(lo, hi) {
if (lo >= hi) return;
const mid = (lo + hi) >> 1;
await mergeSort(lo, mid);
await mergeSort(mid + 1, hi);
await merge(lo, mid, hi);
}
async function merge(lo, mid, hi) {
const left = sA.slice(lo, mid + 1);
const right = sA.slice(mid + 1, hi + 1);
let i = 0, j = 0, k = lo;
while (i < left.length && j < right.length) {
await rSB([k, mid + 1 + j], 'comparing', gD());
if (left[i] <= right[j]) sA[k++] = left[i++];
else sA[k++] = right[j++];
await rSB([k - 1], 'swapping', gD() / 2);
}
while (i < left.length) sA[k++] = left[i++];
while (j < right.length) sA[k++] = right[j++];
}
function resetSort() {
sR = false;
['Bubble','Selection','Insertion','Quick','Merge'].forEach(t =>
document.getElementById('sortBtn' + t).disabled = false
);
initSort();
}
document.getElementById('sortSpeed').addEventListener('input', function() {
document.getElementById('sortSpeedLabel').textContent = this.value;
});
// ═══════════════════════════════════════════════════════════════════
// SECTION 2: SEARCH
// ═══════════════════════════════════════════════════════════════════
let searchArr = [];
function initSearch() {
const set = new Set();
while (set.size < 20) set.add(Math.floor(Math.random() * 90) + 5);
searchArr = [...set].sort((a, b) => a - b);
renderSearchBoth([], [], -1, -1, [], []);
}
function renderSearchBoth(linState, binState, linFound, binFound, binRange, binMid) {
['linear', 'binary'].forEach(type => {
const el = document.getElementById(type + 'Arr');
el.innerHTML = '';
searchArr.forEach((v, i) => {
const cell = document.createElement('div');
let cls = 'search-cell';
if (type === 'linear') {
if (i < linState.length) cls += ' ' + (i === linFound ? 'found' : linState[i] ? 'cur' : '');
} else {
if (binRange.length && (i < binRange[0] || i > binRange[1])) cls += ' out';
else if (i === binMid[0]) cls += ' mid';
else if (binRange.length) cls += ' range';
if (i === binFound) cls += ' found';
}
cell.className = cls.trim();
cell.textContent = v;
el.appendChild(cell);
});
});
}
async function runSearch() {
const target = parseInt(document.getElementById('searchTarget').value);
if (isNaN(target)) return;
document.getElementById('linearSteps').textContent = '0';
document.getElementById('binarySteps').textContent = '0';
document.getElementById('searchInfo').textContent = '탐색 중...';
// Run both simultaneously
let linDone = false, binDone = false;
let linStep = 0, binStep = 0;
let linFound = -1, binFound = -1;
let binLo = 0, binHi = searchArr.length - 1;
let i = 0;
// Interleave animation
const delay = 400;
const maxSteps = Math.max(searchArr.length, Math.ceil(Math.log2(searchArr.length)) + 2);
for (let step = 0; step < maxSteps + searchArr.length; step++) {
let anyProgress = false;
// linear step
if (!linDone && i < searchArr.length) {
linStep++;
if (searchArr[i] === target) { linFound = i; linDone = true; }
anyProgress = true;
}
// binary step
if (!binDone && binLo <= binHi) {
const mid = (binLo + binHi) >> 1;
binStep++;
const range = [binLo, binHi];
const midArr = [mid];
// draw
const linState = Array.from({length: searchArr.length}, (_, idx) => idx < linStep ? true : false);
renderSearchBoth(linState, [], linFound, binLo <= mid && mid <= binHi ? -1 : binFound, range, midArr);
document.getElementById('linearSteps').textContent = linStep;
document.getElementById('binarySteps').textContent = binStep;
await sl(delay);
if (searchArr[mid] === target) { binFound = mid; binDone = true; }
else if (searchArr[mid] < target) binLo = mid + 1;
else binHi = mid - 1;
anyProgress = true;
}
if (!anyProgress && linDone && binDone) break;
// advance linear pointer
if (!linDone) i++;
const linState2 = Array.from({length: searchArr.length}, (_, idx) => idx < i ? true : false);
const rng = binDone ? [] : [binLo, binHi];
const md = [];
renderSearchBoth(linState2, [], linFound, binFound, rng, md);
document.getElementById('linearSteps').textContent = linStep;
document.getElementById('binarySteps').textContent = binStep;
if (linDone && binDone) break;
await sl(delay / 2);
}
const linResult = linFound >= 0 ? `선형: ${linStep}단계에서 발견` : `선형: ${linStep}단계, 없음`;
const binResult = binFound >= 0 ? `이진: ${binStep}단계에서 발견` : `이진: ${binStep}단계, 없음`;
document.getElementById('searchInfo').textContent = `${target} → ${linResult} | ${binResult}`;
document.getElementById('linearSteps').textContent = linStep;
document.getElementById('binarySteps').textContent = binStep;
}
function resetSearch() {
initSearch();
document.getElementById('linearSteps').textContent = '0';
document.getElementById('binarySteps').textContent = '0';
document.getElementById('searchInfo').textContent = '배열에서 찾을 값을 입력하고 "동시 실행"을 클릭하세요.';
}
// ═══════════════════════════════════════════════════════════════════
// SECTION 3: RECURSION
// ═══════════════════════════════════════════════════════════════════
function updateRecursion() {
const n = parseInt(document.getElementById('recursionN').value);
document.getElementById('recursionNDisplay').textContent = n;
const stack = document.getElementById('recursionStack');
stack.innerHTML = '';
// Build call frames bottom to top (displayed column-reverse)
// First: show all calls descending, then base case, then results
const frames = [];
// Descending calls
for (let i = n; i >= 1; i--) {
frames.push({ call: `factorial(${i})`, result: i === 1 ? '= 1' : `= ${i} × factorial(${i-1})`, isBase: i === 1 });
}
let factorial = 1;
const results = [];
for (let i = 1; i <= n; i++) factorial *= i;
// Compute partial results
const partials = [1];
for (let i = 2; i <= n; i++) partials.push(partials[partials.length-1] * i);
frames.forEach((f, idx) => {
const div = document.createElement('div');
div.className = 'call-frame' + (f.isBase ? ' base-case' : '');
div.style.animationDelay = (idx * 0.06) + 's';
const callN = n - idx;
const returnVal = partials[callN - 1];
div.innerHTML = `<span class="call-fn">factorial(${callN})</span>
<span class="call-result">${f.isBase ? '→ 1 (기저 사례)' : `→ ${callN} × ${partials[callN-2] || 1} = ${returnVal}`}</span>`;
stack.appendChild(div);
});
document.getElementById('recursionInfo').textContent =
`factorial(${n}) = ${factorial} — 콜 스택 깊이: ${n}`;
}
// ═══════════════════════════════════════════════════════════════════
// SECTION 4: BIG-O
// ═══════════════════════════════════════════════════════════════════
const BIGO_COLORS = ['#00ffaa', '#6b8aff', '#ffaa00', '#ff6b9d', '#ff4466'];
const BIGO_LABELS = ['O(1)', 'O(log n)', 'O(n)', 'O(n log n)', 'O(n²)'];
const BIGO_FNS = [
n => 1,
n => Math.log2(n + 1),
n => n,
n => n * Math.log2(n + 1),
n => n * n
];
function drawBigO() {
const canvas = document.getElementById('bigoCanvas');
const nSlider = parseInt(document.getElementById('bigoN').value);
document.getElementById('bigoNDisplay').textContent = nSlider;
const W = canvas.offsetWidth || 800;
const H = 300;
canvas.width = W;
canvas.height = H;
const ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, W, H);
const pad = { l: 50, r: 20, t: 20, b: 40 };
const maxN = nSlider;
// Compute max value (cap at n² for display)
const maxVal = Math.min(BIGO_FNS[4](maxN), maxN * maxN);
ctx.strokeStyle = 'rgba(255,255,255,0.05)';
ctx.lineWidth = 1;
for (let g = 0; g <= 4; g++) {
const y = pad.t + (1 - g / 4) * (H - pad.t - pad.b);
ctx.beginPath(); ctx.moveTo(pad.l, y); ctx.lineTo(W - pad.r, y); ctx.stroke();
ctx.fillStyle = 'rgba(136,136,170,0.5)';
ctx.font = '10px JetBrains Mono, monospace';
ctx.fillText(Math.round(maxVal * g / 4), 2, y + 4);
}
// Axes
ctx.strokeStyle = 'rgba(255,255,255,0.15)';
ctx.lineWidth = 1.5;
ctx.beginPath();
ctx.moveTo(pad.l, pad.t); ctx.lineTo(pad.l, H - pad.b);
ctx.lineTo(W - pad.r, H - pad.b);
ctx.stroke();
ctx.fillStyle = 'rgba(136,136,170,0.6)';
ctx.font = '11px JetBrains Mono, monospace';
ctx.fillText('n', W - pad.r + 4, H - pad.b + 4);
ctx.fillText('ops', pad.l - 28, pad.t + 4);
BIGO_FNS.forEach((fn, idx) => {
ctx.strokeStyle = BIGO_COLORS[idx];
ctx.lineWidth = 2.5;
ctx.shadowColor = BIGO_COLORS[idx];
ctx.shadowBlur = 4;
ctx.beginPath();
let first = true;
for (let n = 1; n <= maxN; n++) {
const x = pad.l + (n / maxN) * (W - pad.l - pad.r);
const val = Math.min(fn(n), maxVal);
const y = H - pad.b - (val / maxVal) * (H - pad.t - pad.b);
if (first) { ctx.moveTo(x, y); first = false; }
else ctx.lineTo(x, y);
}
ctx.stroke();
ctx.shadowBlur = 0;
});
// Legend
const legend = document.getElementById('bigoLegend');
legend.innerHTML = BIGO_LABELS.map((label, i) =>
`<div class="bigo-item">
<div class="bigo-dot" style="background:${BIGO_COLORS[i]}"></div>
<span style="color:${BIGO_COLORS[i]}">${label}</span>
<span style="color:var(--text-dim);font-size:0.7rem;margin-left:4px">
= ${Math.round(BIGO_FNS[i](nSlider))}
</span>
</div>`
).join('');
}
// ═══════════════════════════════════════════════════════════════════
// SECTION 5: BITMASK
// ═══════════════════════════════════════════════════════════════════
let bmA = 0b10110101, bmB = 0b01100110, bmOp = 'AND';
function setBmOp(op) {
bmOp = op;
document.querySelectorAll('[id^=bmBtn]').forEach(b => b.classList.remove('active'));
const map = {AND:'bmBtnAND',OR:'bmBtnOR',XOR:'bmBtnXOR',NOT:'bmBtnNOT',SHL:'bmBtnSHL',SHR:'bmBtnSHR'};
document.getElementById(map[op]).classList.add('active');
// Hide B row for unary ops
const bRow = document.getElementById('bmBRowEl');
bRow.style.opacity = (op === 'NOT' || op === 'SHL' || op === 'SHR') ? '0.25' : '1';
document.getElementById('bmOpSymbol').textContent = {
AND:'AND',OR:'OR',XOR:'XOR',NOT:'NOT A',SHL:'A << 1',SHR:'A >> 1'
}[op];
bmCalc();
}
function bmCalc() {
let r;
if (bmOp === 'AND') r = bmA & bmB;
else if (bmOp === 'OR') r = bmA | bmB;
else if (bmOp === 'XOR') r = bmA ^ bmB;
else if (bmOp === 'NOT') r = (~bmA) & 0xFF;
else if (bmOp === 'SHL') r = (bmA << 1) & 0xFF;
else if (bmOp === 'SHR') r = (bmA >> 1) & 0xFF;
renderBitmask(r);
}
function renderBitmask(result) {
const renderBits = (containerId, value, interactive, whichAB) => {
const el = document.getElementById(containerId);
el.innerHTML = '';
for (let bit = 7; bit >= 0; bit--) {
const isOne = (value >> bit) & 1;
const cell = document.createElement('div');
cell.className = 'bm-bit' + (isOne ? ' one' : '') + (interactive ? '' : ' result-bit');
cell.textContent = isOne ? '1' : '0';
if (interactive) {
const b = bit; const w = whichAB;
cell.onclick = () => toggleBit(w, b);
cell.title = `비트 ${bit} 토글`;
}
el.appendChild(cell);
}
};
renderBits('bmBitsA', bmA, true, 'A');
renderBits('bmBitsB', bmB, true, 'B');
renderBits('bmBitsR', result, false, null);
document.getElementById('bmDecA').textContent = bmA;
document.getElementById('bmDecB').textContent = bmB;
document.getElementById('bmDecR').textContent = result;
const opSym = { AND: '&', OR: '|', XOR: '^', NOT: '~A', SHL: 'A<<1', SHR: 'A>>1' }[bmOp];
const showB = !(bmOp === 'NOT' || bmOp === 'SHL' || bmOp === 'SHR');
const binStr = v => ('00000000' + v.toString(2)).slice(-8);
document.getElementById('bmOpDisplay').innerHTML =
`<span>A = <b style="color:var(--accent4)">${bmA}</b> (0b${binStr(bmA)})</span>
<span class="op-sym">${opSym}</span>
${showB ? `<span>B = <b style="color:var(--accent4)">${bmB}</b> (0b${binStr(bmB)})</span>` : ''}
<span class="eq-sym">=</span>
<span style="color:var(--accent)"><b>${result}</b> (0b${binStr(result)})</span>`;
}
function toggleBit(which, pos) {
if (which === 'A') bmA ^= (1 << pos);
else bmB ^= (1 << pos);
bmCalc();
}
// ═══════════════════════════════════════════════════════════════════
// SECTION 6: SUFFIX ARRAY
// ═══════════════════════════════════════════════════════════════════
let currentSA = [];
let currentStr = '';
function buildSuffixArray(s) {
const n = s.length;
const suffixes = Array.from({length: n}, (_, i) => ({idx: i, suf: s.slice(i)}));
suffixes.sort((a, b) => a.suf < b.suf ? -1 : a.suf > b.suf ? 1 : 0);
return suffixes;
}
function computeLCP(sa, s) {
const lcp = [null]; // first is undefined
for (let i = 1; i < sa.length; i++) {
const a = sa[i - 1].suf, b = sa[i].suf;
let l = 0;
while (l < a.length && l < b.length && a[l] === b[l]) l++;
lcp.push(l);
}
return lcp;
}
function renderSuffixArray(matchStart, matchEnd) {
const s = document.getElementById('saInput').value.trim();
if (!s) return;
currentStr = s;
currentSA = buildSuffixArray(s);
const lcp = computeLCP(currentSA, s);
const body = document.getElementById('saTableBody');
body.innerHTML = '';
currentSA.forEach((item, rank) => {
const tr = document.createElement('tr');
const isMatch = (matchStart !== undefined && rank >= matchStart && rank <= matchEnd);
if (isMatch) tr.classList.add('sa-match');
tr.innerHTML = `<td class="sa-rank">${rank}</td>
<td>${item.idx}</td>
<td class="sa-suffix">${item.suf}</td>
<td class="sa-lcp">${lcp[rank] === null ? '—' : lcp[rank]}</td>`;
body.appendChild(tr);
});
document.getElementById('saMatchInfo').textContent = '';
}
function searchPattern() {
const pattern = document.getElementById('saPattern').value;
if (!pattern || !currentSA.length) return;
// Binary search on suffix array
let lo = 0, hi = currentSA.length - 1;
let firstMatch = -1, lastMatch = -1;
// Find first occurrence
let l = 0, r = currentSA.length;
while (l < r) {
const mid = (l + r) >> 1;
if (currentSA[mid].suf.slice(0, pattern.length) < pattern) l = mid + 1;
else r = mid;
}
firstMatch = l;
// Find last occurrence
l = 0; r = currentSA.length;
while (l < r) {
const mid = (l + r) >> 1;
if (currentSA[mid].suf.slice(0, pattern.length) <= pattern) {
if (currentSA[mid].suf.slice(0, pattern.length) === pattern) l = mid + 1;
else l = mid + 1;
} else r = mid;
}
lastMatch = l - 1;
// Validate
const valid = firstMatch <= lastMatch &&
firstMatch < currentSA.length &&
currentSA[firstMatch].suf.startsWith(pattern);
if (!valid) {
renderSuffixArray();
document.getElementById('saMatchInfo').textContent =
`"${pattern}" 패턴을 찾을 수 없습니다.`;
document.getElementById('saMatchInfo').style.color = 'var(--danger)';
} else {
renderSuffixArray(firstMatch, lastMatch);
const count = lastMatch - firstMatch + 1;
document.getElementById('saMatchInfo').textContent =
`"${pattern}" — ${count}개 발견 (순위 ${firstMatch}~${lastMatch})`;
document.getElementById('saMatchInfo').style.color = 'var(--accent)';
// Scroll to first match
const rows = document.querySelectorAll('#saTableBody tr');
if (rows[firstMatch]) rows[firstMatch].scrollIntoView({behavior:'smooth', block:'nearest'});
}
}
function clearSaSearch() {
document.getElementById('saPattern').value = '';
renderSuffixArray();
document.getElementById('saMatchInfo').textContent = '';
}
// ═══════════════════════════════════════════════════════════════════
// INIT
// ═══════════════════════════════════════════════════════════════════
window.addEventListener('load', () => {
initSort();
initSearch();
updateRecursion();
drawBigO();
// bitmask
setBmOp('AND');
// suffix array
renderSuffixArray();
});
window.addEventListener('resize', () => { drawBigO(); });
// ═══════════════════════════════════════════════
// SECTION 7: DYNAMIC PROGRAMMING
// ═══════════════════════════════════════════════
// --- ① Fibonacci Memoization ---
let fibMemo = {};
let fibCallsWithMemo = 0;
function fibM(n) {
fibCallsWithMemo++;
if (n <= 1) return n;
if (fibMemo[n] !== undefined) return fibMemo[n];
fibMemo[n] = fibM(n-1) + fibM(n-2);
return fibMemo[n];
}
function naiveCalls(n) {
// rough estimate: T(n) = T(n-1)+T(n-2)+1, approx 2^n
return Math.round(Math.pow(1.618, n+1) / Math.sqrt(5));
}
function runFib() {
const n = parseInt(document.getElementById('fibN').value);
document.getElementById('fibNVal').textContent = n;
fibMemo = {}; fibCallsWithMemo = 0;
const result = fibM(n);
const memoDiv = document.getElementById('fibMemoTable');
memoDiv.innerHTML = '';
for (let i = 0; i <= n; i++) {
const v = i <= 1 ? i : fibMemo[i];
const cell = document.createElement('div');
cell.style.cssText = 'background:var(--surface2);border:1px solid var(--border);border-radius:6px;padding:6px 10px;font-family:"JetBrains Mono",monospace;font-size:0.8rem;text-align:center;min-width:50px;';
cell.innerHTML = `<div style="color:var(--text-dim);font-size:0.6rem;margin-bottom:2px;">F(${i})</div><div style="color:${i<=1?'var(--accent2)':'var(--accent)'};font-weight:700;">${v}</div>`;
memoDiv.appendChild(cell);
}
const naive = naiveCalls(n);
document.getElementById('fibInfo').innerHTML =
`F(${n}) = <span style="color:var(--accent);font-weight:700;">${result}</span> | `+
`메모이제이션 호출: <span style="color:var(--accent)">${fibCallsWithMemo}회</span> | `+
`단순 재귀 호출: <span style="color:var(--danger)">~${naive.toLocaleString()}회</span> | `+
`절약: <span style="color:var(--accent4)">${Math.round((1-fibCallsWithMemo/Math.max(naive,1))*100)}%</span>`;
}
// --- ② LCS ---
function runLCS() {
const a = document.getElementById('lcsA').value.toUpperCase().replace(/[^A-Z]/g,'');
const b = document.getElementById('lcsB').value.toUpperCase().replace(/[^A-Z]/g,'');
const m = a.length, n = b.length;
if (!m || !n) { document.getElementById('lcsTable').innerHTML=''; return; }
const dp = Array.from({length:m+1}, ()=>Array(n+1).fill(0));
for (let i=1;i<=m;i++) for (let j=1;j<=n;j++)
dp[i][j] = a[i-1]===b[j-1] ? dp[i-1][j-1]+1 : Math.max(dp[i-1][j],dp[i][j-1]);
// backtrack LCS
let lcs='', ci=m, cj=n;
while(ci>0&&cj>0){
if(a[ci-1]===b[cj-1]){lcs=a[ci-1]+lcs;ci--;cj--;}
else if(dp[ci-1][cj]>dp[ci][cj-1])ci--;else cj--;
}
// highlight LCS path cells
const path = new Set();
ci=m;cj=n;
while(ci>0&&cj>0){
if(a[ci-1]===b[cj-1]){path.add(`${ci},${cj}`);ci--;cj--;}
else if(dp[ci-1][cj]>dp[ci][cj-1])ci--;else cj--;
}
let html='<table style="border-collapse:collapse;font-family:\'JetBrains Mono\',monospace;font-size:0.72rem;">';
html+=`<tr><th style="padding:5px 8px;color:var(--text-dim);"></th><th style="padding:5px 8px;color:var(--text-dim);">ε</th>`;
for(let j=0;j<n;j++) html+=`<th style="padding:5px 8px;color:var(--accent2);">${b[j]}</th>`;
html+='</tr>';
for(let i=0;i<=m;i++){
html+=`<tr><th style="padding:5px 8px;color:var(--accent);">${i===0?'ε':a[i-1]}</th>`;
for(let j=0;j<=n;j++){