-
Notifications
You must be signed in to change notification settings - Fork 7
/
codejam-2015-r2pbC.html
973 lines (898 loc) · 89 KB
/
codejam-2015-r2pbC.html
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
<!DOCTYPE html>
<html lang="zh-Hant"
>
<head>
<title>codejam2015-round2-pbC 的三种解法 - mx's blog</title>
<!-- Using the latest rendering mode for IE -->
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="theme-color" content="#6b5594">
<meta name="msapplication-navbutton-color" content="#6b5594">
<meta name="apple-mobile-web-app-status-bar-style" content="#6b5594">
<link rel="manifest" href="/manifest.json">
<link rel="canonical" href="https://x-wei.github.io/codejam-2015-r2pbC.html">
<meta name="author" content="mx" />
<meta name="keywords" content="algorithm,codejam" />
<meta name="description" content="昨天做的一道codejam题目, 这个题目的三种解法都非常有代表性, 特此一记. 题目链接在这里: https://code.google.com/codejam/contest/8234486/dashboard#s=p2 Elliot's parents speak French and English to him at home. He has heard a lot of words, but it isn't always clear to him which word comes from which language! Elliot knows one sentence ..." />
<meta property="og:site_name" content="mx's blog" />
<meta property="og:type" content="article"/>
<meta property="og:title" content="codejam2015-round2-pbC 的三种解法"/>
<meta property="og:url" content="https://x-wei.github.io/codejam-2015-r2pbC.html"/>
<meta property="og:description" content="昨天做的一道codejam题目, 这个题目的三种解法都非常有代表性, 特此一记. 题目链接在这里: https://code.google.com/codejam/contest/8234486/dashboard#s=p2 Elliot's parents speak French and English to him at home. He has heard a lot of words, but it isn't always clear to him which word comes from which language! Elliot knows one sentence ..."/>
<meta property="article:published_time" content="2016-05-27" />
<meta property="article:section" content="tech" />
<meta property="article:tag" content="algorithm" />
<meta property="article:tag" content="codejam" />
<meta property="article:author" content="mx" />
<meta property="og:image"
content="https://x-wei.github.io/codejam-2015-r2pbC.png"/>
<!-- Bootstrap -->
<link href="https://x-wei.github.io/theme/css/bootstrap.min.css" rel="stylesheet">
<link href="https://x-wei.github.io/theme/css/font-awesome.min.css" rel="stylesheet">
<link href="https://x-wei.github.io/theme/css/pygments/manni.css" rel="stylesheet">
<link href="https://x-wei.github.io/theme/tipuesearch/tipuesearch.css" rel="stylesheet">
<link rel="stylesheet" href="https://x-wei.github.io/theme/css/style.css" type="text/css"/>
<link href="https://x-wei.github.io/feeds/atom.xml" type="application/atom+xml" rel="alternate"
title="mx's blog ATOM Feed"/>
<link href="https://x-wei.github.io/theme/css/material.min.css" rel="stylesheet">
<link href="https://x-wei.github.io/theme/css/ripples.css" rel="stylesheet">
</head>
<body>
<div style="display:none" id="title">codejam2015-round2-pbC 的三种解法 - mx's blog</div>
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
<span class="sr-only">切换导航</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a href="https://x-wei.github.io/" class="navbar-brand">
mx's blog </a>
</div>
<div class="collapse navbar-collapse navbar-ex1-collapse">
<ul class="nav navbar-nav">
<li class="dropdown hidden-md hidden-lg hidden-xl">
<a class="dropdown-toggle" data-toggle="dropdown" href="javascript:void(0)">
<i class="fa fa-user"></i><span class="caret"></span>
</a>
<ul class="dropdown-menu">
</ul>
</li>
<ul class="nav navbar-nav hidden-xs hidden-sm">
</ul>
</ul>
<ul class="nav navbar-nav hidden-md hidden-lg hidden-xl">
<li class="dropdown hidden-md hidden-lg hidden-xl">
<a class="dropdown-toggle" data-toggle="dropdown" href="javascript:void(0)">
<i class="fa fa-folder-o"></i><span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li >
<a href="https://x-wei.github.io/category/misc.html"><i class="fa fa-folder-o"></i> Misc</a>
</li>
<li >
<a href="https://x-wei.github.io/category/music.html"><i class="fa fa-folder-o"></i> Music</a>
</li>
<li >
<a href="https://x-wei.github.io/category/notes.html"><i class="fa fa-folder-o"></i> Notes</a>
</li>
<li >
<a href="https://x-wei.github.io/category/soft.html"><i class="fa fa-folder-o"></i> Soft</a>
</li>
<li class="active">
<a href="https://x-wei.github.io/category/tech.html"><i class="fa fa-folder-o"></i> Tech</a>
</li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav hidden-xs hidden-sm">
<li >
<a href="https://x-wei.github.io/category/misc.html"><i class="fa fa-folder-o"></i> Misc</a>
</li>
<li >
<a href="https://x-wei.github.io/category/music.html"><i class="fa fa-folder-o"></i> Music</a>
</li>
<li >
<a href="https://x-wei.github.io/category/notes.html"><i class="fa fa-folder-o"></i> Notes</a>
</li>
<li >
<a href="https://x-wei.github.io/category/soft.html"><i class="fa fa-folder-o"></i> Soft</a>
</li>
<li class="active">
<a href="https://x-wei.github.io/category/tech.html"><i class="fa fa-folder-o"></i> Tech</a>
</li>
</ul>
<ul class="nav navbar-nav navbar-right hidden-sm hidden-md hidden-lg hidden-xl">
<li class="dropdown hidden-md hidden-lg hidden-xl">
<a class="dropdown-toggle" data-toggle="dropdown" href="javascript:void(0)">
<i class="fa fa-search"></i><span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li><span>
<form class="navbar-search" action="/search.html">
<input type="text" class="search-query form-control col-lg-16" placeholder="Search" name="q" id="tipue_search_input" required>
</form></span>
</li>
</ul>
</li>
</ul>
<ul class="nav navbar-right navbar-form hidden-xs">
<li><span>
<form class="navbar-search" action="/search.html">
<input type="text" class="search-query form-control col-lg-16" placeholder="查找" name="q" id="tipue_search_input" required>
</form></span>
</li>
</ul>
<ul class="nav navbar-nav navbar-right hidden-xs">
<li><a href="https://x-wei.github.io/archives.html"><i class="fa fa-th-list"></i><span class="icon-label">Archive</span></a></li>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
</div> <!-- /.navbar -->
<!-- Banner -->
<!-- End Banner -->
<div class="container" style="min-height: 100%;height: auto !important;height: 100%;">
<div class="row" style="padding-bottom:80px;padding-top:80px;">
<div class="col-xl-21 col-lg-20 col-md-18">
<div id="loading-block">
<ol class="breadcrumb">
<li><a href="https://x-wei.github.io/" title="mx's blog"><i class="fa fa-home fa-lg"></i></a></li>
<li><a href="https://x-wei.github.io/category/tech.html" title="tech">tech</a></li>
<li class="active">codejam2015-round2-pbC 的三种解法</li>
</ol>
<section id="content" class="article-content">
<article>
<header class="page-header jumbotron jumbotron-primary panel-primary" id="article-header">
<div class="panel-heading">
<h1>
codejam2015-round2-pbC 的三种解法
<a href="https://x-wei.github.io/codejam-2015-r2pbC.html"
rel="bookmark"
class="btn btn-primary btn-lg"
title="到 codejam2015-round2-pbC 的三种解法 的永久链接">
<i class="mdi-action-launch"></i>
</a>
</h1>
</div>
<div class="panel-body">
<div class="post-info">
<span class="published">
<time datetime="2016-05-27T00:00:00+02:00"><i class="fa fa-calendar"></i> Fri, 27 May 2016</time>
</span>
<span class="btn-group">
<a href="https://x-wei.github.io/tag/algorithm.html" class="btn btn-primary btn-xs"><i class="fa fa-tag"></i> algorithm</a>
<a href="https://x-wei.github.io/tag/codejam.html" class="btn btn-primary btn-xs"><i class="fa fa-tag"></i> codejam</a>
</span>
</div><!-- /.post-info --> </div>
</header>
<div class="entry-content jumbotron" id="article-content">
<div class="panel panel-default">
<div class="panel-heading">
目录
</div>
<div class="panel-boy">
<div id="toc"><ul><li><a class="toc-href" href="#fa-1-qiong-ju-bruteforce-for-small-testcase" title="法1: 穷举 bruteforce (for small testcase)">法1: 穷举 bruteforce (for small testcase)</a><ul><li><a class="toc-href" href="#naive-bruteforce" title="naive bruteforce">naive bruteforce</a></li><li><a class="toc-href" href="#bruteforce-using-bitmap" title="bruteforce using bitmap">bruteforce using bitmap</a></li></ul></li><li><a class="toc-href" href="#fa-2-zui-da-liu-maxflow_1" title="法2: 最大流 maxflow">法2: 最大流 maxflow</a></li><li><a class="toc-href" href="#fa-3-xian-xing-gui-hua-integer-linear-programming" title="法3: 线性规划 (integer) linear programming">法3: 线性规划 (integer) linear programming</a></li></ul></div>
</div>
</div>
<p>昨天做的一道codejam题目, 这个题目的三种解法都非常有代表性, 特此一记. </p>
<p>题目链接在这里: <br/>
<a href="https://code.google.com/codejam/contest/8234486/dashboard#s=p2">https://code.google.com/codejam/contest/8234486/dashboard#s=p2</a> </p>
<blockquote>
<p>Elliot's parents speak French and English to him at home. He has heard a lot of words, but it isn't always clear to him which word comes from which language! Elliot knows one sentence that he's sure is English and one sentence that he's sure is French, and some other sentences that could be either English or French. If a word appears in an English sentence, it must be a word in English. If a word appears in a French sentence, it must be a word in French. <br/>
Considering all the sentences that Elliot has heard, what is the minimum possible number of words that he's heard that must be words in both English and French? </p>
<ul>
<li>
<p>Input <br/>
The first line of the input gives the number of test cases, T. T test cases follow. Each starts with a single line containing an integer N. N lines follow, each of which contains a series of space-separated "words". Each "word" is made up only of lowercase characters a-z. The first of those N lines is a "sentence" in English, and the second is a "sentence" in French. The rest could be "sentences" in either English or French. (Note that the "words" and "sentences" are not guaranteed to be valid in any real language.) </p>
</li>
<li>
<p>Output <br/>
For each test case, output one line containing "Case #x: y", where x is the test case number (starting from 1) and y is the minimum number of words that Elliot has heard that must be words in both English and French. </p>
</li>
<li>
<p>Limits <br/>
1 ≤ T ≤ 25. <br/>
Each word will contain no more than 10 characters. <br/>
The two "known" sentences will contain no more than 1000 words each. <br/>
The "unknown" sentences will contain no more than 10 words each. <br/>
Small dataset <br/>
2 ≤ N ≤ 20. <br/>
Large dataset <br/>
2 ≤ N ≤ 200. </p>
</li>
</ul>
</blockquote>
<p>我的codejam程序模板长这样: </p>
<div class="highlight"><pre><span class="code-line"><span></span><span class="n">def</span> <span class="n">readval</span><span class="p">(</span><span class="n">typ</span><span class="o">=</span><span class="nb">int</span><span class="p">):</span> </span>
<span class="code-line"> <span class="k">return</span> <span class="n">typ</span><span class="p">(</span> <span class="n">raw_input</span><span class="p">()</span> <span class="p">)</span></span>
<span class="code-line"></span>
<span class="code-line"><span class="n">def</span> <span class="n">readvals</span><span class="p">(</span><span class="n">typ</span><span class="o">=</span><span class="nb">int</span><span class="p">):</span> </span>
<span class="code-line"> <span class="k">return</span> <span class="k">map</span><span class="p">(</span> <span class="n">typ</span><span class="p">,</span> <span class="n">raw_input</span><span class="p">().</span><span class="n">split</span><span class="p">()</span> <span class="p">)</span></span>
<span class="code-line"></span>
<span class="code-line"><span class="n">def</span> <span class="n">testcase</span><span class="p">(</span><span class="n">cas</span><span class="p">):</span> </span>
<span class="code-line"> <span class="n">print</span> <span class="s1">'Case #%d: %d'</span> <span class="o">%</span> <span class="p">(</span> <span class="n">cas</span><span class="p">,</span> <span class="n">res</span> <span class="p">)</span></span>
<span class="code-line"></span>
<span class="code-line"><span class="k">if</span> <span class="n">__name__</span><span class="o">==</span><span class="s1">'__main__'</span><span class="p">:</span> </span>
<span class="code-line"> <span class="n">T</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">raw_input</span><span class="p">())</span> </span>
<span class="code-line"> <span class="k">for</span> <span class="n">i</span> <span class="k">in</span> <span class="n">xrange</span><span class="p">(</span><span class="n">T</span><span class="p">):</span> </span>
<span class="code-line"> <span class="n">testcase</span><span class="p">(</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span></span>
</pre></div>
<h1 id="fa-1-qiong-ju-bruteforce-for-small-testcase">法1: 穷举 bruteforce (for small testcase)</h1>
<p>在small set(N=20) 中, 对于那18个未知语言的句子, 每句可能是英语或法语. 那么穷举所有可能性, 然后选择双语单词最少的即可. 2^18约等于几十万 , 按说python还是可以handle的 (2^10=1k, 2^ 20=1M, python每秒大约能循环几百万次). </p>
<h2 id="naive-bruteforce">naive bruteforce</h2>
<p>生成所有的可能性组合, python的<code>itertools</code>包里提供了<a href="https://docs.python.org/2/library/itertools.html#itertools.combinations">combinations</a>函数, <code>combinations(iterable, r)</code>返回所有在iterable中大小为r的子集(的迭代器): </p>
<div class="highlight"><pre><span class="code-line"><span></span><span class="o">>>></span> <span class="kn">from</span> <span class="nn">itertools</span> <span class="kn">import</span> <span class="n">combinations</span> </span>
<span class="code-line"><span class="o">>>></span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">combinations</span><span class="p">(</span><span class="s1">'ABCD'</span><span class="p">,</span> <span class="mi">2</span><span class="p">):</span> <span class="k">print</span> <span class="n">c</span> </span>
<span class="code-line"><span class="p">(</span><span class="s1">'A'</span><span class="p">,</span> <span class="s1">'B'</span><span class="p">)</span> </span>
<span class="code-line"><span class="p">(</span><span class="s1">'A'</span><span class="p">,</span> <span class="s1">'C'</span><span class="p">)</span> </span>
<span class="code-line"><span class="p">(</span><span class="s1">'A'</span><span class="p">,</span> <span class="s1">'D'</span><span class="p">)</span> </span>
<span class="code-line"><span class="p">(</span><span class="s1">'B'</span><span class="p">,</span> <span class="s1">'C'</span><span class="p">)</span> </span>
<span class="code-line"><span class="p">(</span><span class="s1">'B'</span><span class="p">,</span> <span class="s1">'D'</span><span class="p">)</span> </span>
<span class="code-line"><span class="p">(</span><span class="s1">'C'</span><span class="p">,</span> <span class="s1">'D'</span><span class="p">)</span></span>
</pre></div>
<p>用<code>combination</code>函数即可实现枚举每句话是英语还是法语的功能. </p>
<p>当每句话是英语还是法语已经确定以后, 可以用集合取交集的方法(<code>set1.intersection(set2)</code>)得到两种语言一共有多少个重复的单词. 程序长这样: </p>
<div class="highlight"><pre><span class="code-line"><span></span><span class="n">def</span><span class="w"> </span><span class="n">testcase</span><span class="p">(</span><span class="n">cas</span><span class="p">)</span><span class="err">:</span><span class="w"> </span><span class="err">#</span><span class="w"> </span><span class="ow">not</span><span class="w"> </span><span class="n">fast</span><span class="w"> </span><span class="n">enough</span><span class="p">...</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="n">N</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">readval</span><span class="p">()</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="n">En</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">set</span><span class="p">(</span><span class="w"> </span><span class="n">readvals</span><span class="p">(</span><span class="nf">str</span><span class="p">)</span><span class="w"> </span><span class="p">)</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="n">Fr</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">set</span><span class="p">(</span><span class="w"> </span><span class="n">readvals</span><span class="p">(</span><span class="nf">str</span><span class="p">)</span><span class="w"> </span><span class="p">)</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">N</span><span class="o">==</span><span class="mi">2</span><span class="err">:</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="k">print</span><span class="w"> </span><span class="s1">'Case #%d: %d'</span><span class="w"> </span><span class="o">%</span><span class="w"> </span><span class="p">(</span><span class="w"> </span><span class="n">cas</span><span class="p">,</span><span class="w"> </span><span class="nf">len</span><span class="p">(</span><span class="n">En</span><span class="p">.</span><span class="k">intersection</span><span class="p">(</span><span class="n">Fr</span><span class="p">))</span><span class="w"> </span><span class="p">)</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="k">return</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="n">scent</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="err">[]</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="n">xrange</span><span class="p">(</span><span class="n">N</span><span class="o">-</span><span class="mi">2</span><span class="p">)</span><span class="err">:</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="n">scent</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="w"> </span><span class="k">set</span><span class="p">(</span><span class="n">readvals</span><span class="p">(</span><span class="nf">str</span><span class="p">))</span><span class="w"> </span><span class="p">)</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="n">def</span><span class="w"> </span><span class="k">partition</span><span class="p">(</span><span class="n">engsubset</span><span class="p">)</span><span class="err">:</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="n">allEn</span><span class="p">,</span><span class="w"> </span><span class="n">allFr</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">En</span><span class="p">.</span><span class="n">copy</span><span class="p">(),</span><span class="w"> </span><span class="n">Fr</span><span class="p">.</span><span class="n">copy</span><span class="p">()</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="n">xrange</span><span class="p">(</span><span class="n">N</span><span class="o">-</span><span class="mi">2</span><span class="p">)</span><span class="err">:</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="nl">engsubset</span><span class="p">:</span><span class="w"> </span><span class="n">allEn</span><span class="p">.</span><span class="k">update</span><span class="p">(</span><span class="n">scent</span><span class="o">[</span><span class="n">i</span><span class="o">]</span><span class="p">)</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="k">else</span><span class="err">:</span><span class="w"> </span><span class="n">allFr</span><span class="p">.</span><span class="k">update</span><span class="p">(</span><span class="n">scent</span><span class="o">[</span><span class="n">i</span><span class="o">]</span><span class="p">)</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nf">len</span><span class="p">(</span><span class="w"> </span><span class="n">allEn</span><span class="p">.</span><span class="k">intersection</span><span class="p">(</span><span class="n">allFr</span><span class="p">)</span><span class="w"> </span><span class="p">)</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="n">possibleres</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="err">[]</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">l</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="n">xrange</span><span class="p">(</span><span class="n">N</span><span class="o">-</span><span class="mi">2</span><span class="p">)</span><span class="err">:</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">engsubset</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="n">combinations</span><span class="p">(</span><span class="n">xrange</span><span class="p">(</span><span class="n">N</span><span class="o">-</span><span class="mi">2</span><span class="p">),</span><span class="w"> </span><span class="n">l</span><span class="p">)</span><span class="err">:</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="n">possibleres</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="k">partition</span><span class="p">(</span><span class="n">engsubset</span><span class="p">))</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">min</span><span class="p">(</span><span class="n">possibleres</span><span class="p">)</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="k">print</span><span class="w"> </span><span class="s1">'Case #%d: %d'</span><span class="w"> </span><span class="o">%</span><span class="w"> </span><span class="p">(</span><span class="w"> </span><span class="n">cas</span><span class="p">,</span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="p">)</span><span class="w"></span></span>
</pre></div>
<p>然而, 下载了small testcase以后运行程序, 速度还是不够快, 大约要十分钟才有结果 — 而codejam的提交时间限制是4分钟啊... 程序每次检查2^18种可能性, 但是由于每次检查都要进行集合的union和intersection操作(这种操作的效率并不高, 甚至C++里也是一样), 这个操作太耗费时间了所以不行... </p>
<h2 id="bruteforce-using-bitmap">bruteforce using bitmap</h2>
<p>需要更加聪明的穷举方法, 自然的想法就是用bitmap(或者叫bitvector?). 之前的两个基本操作都可以用bitmap完成: </p>
<ul>
<li>枚举各个句子的语言种类: 如果每个句子用一位来表示的话(1代表英语, 0代表法语), 那么用N位的bitmap即可表示一种情形. 这个bitmap只需从0增加到2^N-1, 就把2^N个可能性都遍历了. (实际上是2^N-2个可能性, 因为前两个句子已经确定语言了). </li>
<li>两种语言的词汇表进行union/intersection: 假设共有K个不同的单词, 那么用一个K位的bitmap即可表示一种语言包含了哪些单词. 然后集合的union和intersection即可表示为OR和AND的逻辑运算. </li>
</ul>
<p>bit manipulation蛮subtle的, 不过习惯了就好... 另外python的integer可以任意长度, 不用像C/java那样考虑bitmap位数大于64的情况, 还是非常方便的. 代码如下: </p>
<div class="highlight"><pre><span class="code-line"><span></span><span class="n">def</span><span class="w"> </span><span class="n">testcase</span><span class="p">(</span><span class="n">cas</span><span class="p">)</span><span class="err">:</span><span class="w"> </span><span class="err">#</span><span class="w"> </span><span class="k">use</span><span class="w"> </span><span class="n">bitmap</span><span class="w"> </span><span class="n">instead</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="k">set</span><span class="w"> </span><span class="k">to</span><span class="w"> </span><span class="n">speedup</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">small</span><span class="w"> </span><span class="k">case</span><span class="w"> </span><span class="err">!!</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="n">N</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">readval</span><span class="p">()</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="n">scent</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="err">[]</span><span class="p">;</span><span class="w"> </span><span class="n">allwords</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">set</span><span class="p">()</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="n">xrange</span><span class="p">(</span><span class="n">N</span><span class="p">)</span><span class="err">:</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="n">scent</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="w"> </span><span class="n">readval</span><span class="p">(</span><span class="nf">str</span><span class="p">)</span><span class="w"> </span><span class="p">)</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="n">allwords</span><span class="p">.</span><span class="k">update</span><span class="p">(</span><span class="w"> </span><span class="n">scent</span><span class="o">[</span><span class="n">i</span><span class="o">]</span><span class="p">.</span><span class="n">split</span><span class="p">()</span><span class="w"> </span><span class="p">)</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="n">words</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">sorted</span><span class="p">(</span><span class="n">allwords</span><span class="p">)</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="err">#</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">K</span><span class="w"> </span><span class="k">distinct</span><span class="w"> </span><span class="n">words</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="n">total</span><span class="p">,</span><span class="w"> </span><span class="k">each</span><span class="w"> </span><span class="n">sentence</span><span class="w"> </span><span class="n">can</span><span class="w"> </span><span class="n">be</span><span class="w"> </span><span class="n">reprensented</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">K</span><span class="o">-</span><span class="nc">bit</span><span class="w"> </span><span class="n">bitmap</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="n">bitmaps</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="err">[]</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="n">xrange</span><span class="p">(</span><span class="n">N</span><span class="p">)</span><span class="err">:</span><span class="w"> </span><span class="n">#construct</span><span class="w"> </span><span class="n">bitmaps</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="n">bm</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">wd</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="n">scent</span><span class="o">[</span><span class="n">i</span><span class="o">]</span><span class="p">.</span><span class="n">split</span><span class="p">()</span><span class="err">:</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="n">bm</span><span class="w"> </span><span class="o">|=</span><span class="w"> </span><span class="p">(</span><span class="w"> </span><span class="mi">1</span><span class="o"><<</span><span class="w"> </span><span class="n">words</span><span class="p">.</span><span class="k">index</span><span class="p">(</span><span class="n">wd</span><span class="p">)</span><span class="w"> </span><span class="p">)</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="n">bitmaps</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="n">bm</span><span class="p">)</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">1e10</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="err">#</span><span class="w"> </span><span class="n">look</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="ow">all</span><span class="w"> </span><span class="n">combinations</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="n">xrange</span><span class="p">(</span><span class="mi">1</span><span class="o"><<</span><span class="p">(</span><span class="n">N</span><span class="o">-</span><span class="mi">2</span><span class="p">))</span><span class="err">:</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="n">en</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">bitmaps</span><span class="o">[</span><span class="n">0</span><span class="o">]</span><span class="p">;</span><span class="w"> </span><span class="n">fr</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">bitmaps</span><span class="o">[</span><span class="n">1</span><span class="o">]</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">k</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="n">xrange</span><span class="p">(</span><span class="n">N</span><span class="o">-</span><span class="mi">2</span><span class="p">)</span><span class="err">:</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="mi">1</span><span class="o"><<</span><span class="n">k</span><span class="p">)</span><span class="w"> </span><span class="o">&</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="mi">0</span><span class="err">:</span><span class="w"> </span><span class="n">en</span><span class="w"> </span><span class="o">|=</span><span class="w"> </span><span class="n">bitmaps</span><span class="o">[</span><span class="n">k+2</span><span class="o">]</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="k">else</span><span class="err">:</span><span class="w"> </span><span class="n">fr</span><span class="w"> </span><span class="o">|=</span><span class="w"> </span><span class="n">bitmaps</span><span class="o">[</span><span class="n">k+2</span><span class="o">]</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">min</span><span class="p">(</span><span class="w"> </span><span class="n">res</span><span class="p">,</span><span class="w"> </span><span class="n">bin</span><span class="p">(</span><span class="n">en</span><span class="o">&</span><span class="n">fr</span><span class="p">).</span><span class="nf">count</span><span class="p">(</span><span class="s1">'1'</span><span class="p">)</span><span class="w"> </span><span class="p">)</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="k">print</span><span class="w"> </span><span class="s1">'Case #%d: %d'</span><span class="w"> </span><span class="o">%</span><span class="w"> </span><span class="p">(</span><span class="w"> </span><span class="n">cas</span><span class="p">,</span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="p">)</span><span class="w"></span></span>
</pre></div>
<p>以上代码在我电脑上执行small的时候大约花费1分钟, 比最开始用set的速度提高了十倍. 当然, 对于large的case这种解法肯定就超时了... </p>
<h1 id="fa-2-zui-da-liu-maxflow_1">法2: 最大流 maxflow</h1>
<p>这个方法也是官方<a href="https://code.google.com/codejam/contest/8234486/dashboard#s=a&a=2">analysis</a>里提供的答案. 如果将所有句子<code>S</code>以及所有单词<code>w</code>看作节点, 每个句子的节点<code>S</code>于它包含的单词的节点<code>w</code>相连, 问题转化成了从节点<code>S1</code>到节点<code>S2</code>的vertex cut问题 (选取最小的节点集合, 将该集合的节点去掉以后S1和S2不再联通). </p>
<p>然后这个问题又可以转为边的min cut问题: 只需要把每个单词节点w分成左右两个节点<code>w1</code>和<code>w2</code>, 并且这样添加边(假设w在句子S中): </p>
<ul>
<li><code>w1-->w2</code>, capacity=1 </li>
<li><code>S-->w1</code>, capacity=INF </li>
<li><code>w2-->S</code>, capacity=INF </li>
</ul>
<p>在这样构造的图里计算maxflow即可得到mincut, 也就是题目的答案... </p>
<p>在实现这个解法的时候用到了networkx这个包, 它提供了比bgl好用100倍的接口(虽然也比bgl慢差不多100倍 ==...). <em>任何hashable的object都可以用来作为节点的index</em>, 所以写起来非常舒服, 15行搞定: </p>
<div class="highlight"><pre><span class="code-line"><span></span><span class="k">def</span> <span class="nf">testcase_maxflow</span><span class="p">(</span><span class="n">ind</span><span class="p">):</span> <span class="c1"># using maxflow ! </span></span>
<span class="code-line"> <span class="n">INF</span> <span class="o">=</span> <span class="mf">1e10</span> </span>
<span class="code-line"> <span class="kn">import</span> <span class="nn">networkx</span> <span class="kn">as</span> <span class="nn">nx</span> </span>
<span class="code-line"> <span class="n">G</span> <span class="o">=</span> <span class="n">nx</span><span class="o">.</span><span class="n">DiGraph</span><span class="p">()</span> </span>
<span class="code-line"> <span class="n">N</span> <span class="o">=</span> <span class="n">readval</span><span class="p">()</span> </span>
<span class="code-line"> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">xrange</span><span class="p">(</span><span class="n">N</span><span class="p">):</span> </span>
<span class="code-line"> <span class="n">words</span> <span class="o">=</span> <span class="n">readvals</span><span class="p">(</span><span class="nb">str</span><span class="p">)</span> </span>
<span class="code-line"> <span class="n">si</span> <span class="o">=</span> <span class="s1">'sent-</span><span class="si">%d</span><span class="s1">'</span><span class="o">%</span><span class="n">i</span> </span>
<span class="code-line"> <span class="n">G</span><span class="o">.</span><span class="n">add_node</span><span class="p">(</span> <span class="n">si</span> <span class="p">)</span> </span>
<span class="code-line"> <span class="k">for</span> <span class="n">wd</span> <span class="ow">in</span> <span class="n">words</span><span class="p">:</span> </span>
<span class="code-line"> <span class="n">G</span><span class="o">.</span><span class="n">add_edge</span><span class="p">(</span> <span class="n">wd</span><span class="o">+</span><span class="s1">'_l'</span><span class="p">,</span> <span class="n">wd</span><span class="o">+</span><span class="s1">'_r'</span><span class="p">,</span> <span class="n">capacity</span><span class="o">=</span><span class="mi">1</span> <span class="p">)</span> </span>
<span class="code-line"> <span class="n">G</span><span class="o">.</span><span class="n">add_edge</span><span class="p">(</span> <span class="n">si</span><span class="p">,</span> <span class="n">wd</span><span class="o">+</span><span class="s1">'_l'</span><span class="p">,</span> <span class="n">capacity</span><span class="o">=</span><span class="n">INF</span> <span class="p">)</span> </span>
<span class="code-line"> <span class="n">G</span><span class="o">.</span><span class="n">add_edge</span><span class="p">(</span> <span class="n">wd</span><span class="o">+</span><span class="s1">'_r'</span><span class="p">,</span> <span class="n">si</span><span class="p">,</span> <span class="n">capacity</span><span class="o">=</span><span class="n">INF</span> <span class="p">)</span> </span>
<span class="code-line"> <span class="n">flow_value</span><span class="p">,</span> <span class="n">flow_dict</span> <span class="o">=</span> <span class="n">nx</span><span class="o">.</span><span class="n">maximum_flow</span><span class="p">(</span><span class="n">G</span><span class="p">,</span> <span class="s1">'sent-0'</span><span class="p">,</span> <span class="s1">'sent-1'</span><span class="p">)</span> </span>
<span class="code-line"> <span class="k">print</span> <span class="s1">'Case #</span><span class="si">%d</span><span class="s1">: </span><span class="si">%d</span><span class="s1">'</span> <span class="o">%</span> <span class="p">(</span> <span class="n">ind</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">flow_value</span><span class="p">)</span> <span class="p">)</span></span>
</pre></div>
<p>用这个代码即可轻松通过large case... maxflow的建模还真是艺术...orz </p>
<h1 id="fa-3-xian-xing-gui-hua-integer-linear-programming">法3: 线性规划 (integer) linear programming</h1>
<p>写完上面那个解法以后, 我又看了看<a href="https://www.go-hero.net/jam/15/name/linguo">大神</a>的解法, 发现他居然用的是<a href="https://pythonhosted.org/PuLP/pulp.html">pulp</a>(python线性规划的包), 于是自己想了一下, 这个问题确实可以用线性规划来建模 ! </p>
<p>首先说一句pulp这个包, 它提供了非常好用的接口来构造LP问题, 增加约束或者定义objective只要用<code>prob+=[expression]</code>就好了, 可以说比AMPL好用不少... 下面是一个简单的例子: </p>
<div class="highlight"><pre><span class="code-line"><span></span><span class="o">>>></span> <span class="kn">from</span> <span class="nn">pulp</span> <span class="kn">import</span> <span class="o">*</span> </span>
<span class="code-line"><span class="o">>>></span> <span class="n">x</span> <span class="o">=</span> <span class="n">LpVariable</span><span class="p">(</span><span class="s2">"x"</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span> </span>
<span class="code-line"><span class="o">>>></span> <span class="n">y</span> <span class="o">=</span> <span class="n">LpVariable</span><span class="p">(</span><span class="s2">"y"</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="s1">'Integer'</span><span class="p">)</span> <span class="c1"># var category can be integer </span></span>
<span class="code-line"><span class="o">>>></span> <span class="n">prob</span> <span class="o">=</span> <span class="n">LpProblem</span><span class="p">(</span><span class="s2">"myProblem"</span><span class="p">,</span> <span class="n">LpMinimize</span><span class="p">)</span> </span>
<span class="code-line"><span class="o">>>></span> <span class="n">prob</span> <span class="o">+=</span> <span class="n">x</span> <span class="o">+</span> <span class="n">y</span> <span class="o"><=</span> <span class="mi">2</span> <span class="c1"># add constraint </span></span>
<span class="code-line"><span class="o">>>></span> <span class="n">prob</span> <span class="o">+=</span> <span class="o">-</span><span class="mi">4</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="n">y</span> <span class="c1"># add objective </span></span>
<span class="code-line"><span class="o">>>></span> <span class="n">status</span> <span class="o">=</span> <span class="n">prob</span><span class="o">.</span><span class="n">solve</span><span class="p">()</span> <span class="c1"># solve using default solver </span></span>
<span class="code-line"><span class="o">>>></span> <span class="n">status</span> <span class="o">=</span> <span class="n">prob</span><span class="o">.</span><span class="n">solve</span><span class="p">(</span><span class="n">GLPK</span><span class="p">(</span><span class="n">msg</span> <span class="o">=</span> <span class="mi">0</span><span class="p">))</span> <span class="c1"># or use glpk solver </span></span>
<span class="code-line"><span class="o">>>></span> <span class="n">LpStatus</span><span class="p">[</span><span class="n">status</span><span class="p">]</span> </span>
<span class="code-line"><span class="s1">'Optimal'</span> </span>
<span class="code-line"><span class="o">>>></span> <span class="n">value</span><span class="p">(</span><span class="n">prob</span><span class="o">.</span><span class="n">objective</span><span class="p">)</span> <span class="c1"># see objective value </span></span>
<span class="code-line"><span class="o">-</span><span class="mf">8.0</span> </span>
<span class="code-line"><span class="o">>>></span> <span class="n">value</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="c1"># see variable value </span></span>
<span class="code-line"><span class="mf">2.0</span></span>
</pre></div>
<p>OK, 本题的建模过程如下: <br/>
对于每一个句子<code>S</code>, 定义binary的LP变量<code>Se</code>, Se=1表示句子S为英语, =0表示为法语. <br/>
对于每个单词<code>w</code>, 定义两个binary变量<code>we</code>, <code>wf</code>, 表示单词w是英(法)语单词. <br/>
然后, 再定义变量<code>wef</code>, 它表示单词<code>w</code>既是英语单词又是法语单词. 所以有这个逻辑关系: <code>wef = we AND wf</code> 那么这种逻辑关系如何用线性约束描述呢? 可以这样: <code>wef >= we+wf-1</code> 非常巧妙吧... <br/>
(<a href="http://cs.stackexchange.com/questions/12102/express-boolean-logic-operations-in-zero-one-integer-linear-programming-ilp">这篇文章</a>总结了各种逻辑关系用线性规划的描述方式, 写的得非常详细. ) </p>
<p>要最小化的目标函数就是 <code>sum(wef)</code> 了, 约束除了刚才那个<code>wef >= we+wf-1</code>以外, 还要表达单词和句子之间的关系: 如果一个句子为英(法)语, 那么句子里的每一个单词都为英(法)语. 这是一个<code>se==>we</code>的逻辑关系, 用线性约束表达为: <code>we>=se</code>. </p>
<p>所以整个模型是: </p>
<div class="highlight"><pre><span class="code-line"><span></span><span class="err">Minimize sum(wef) </span></span>
<span class="code-line"><span class="c">st: </span></span>
<span class="code-line"><span class="err">we >= se </span></span>
<span class="code-line"><span class="err">wf >= (1-se) </span></span>
<span class="code-line"><span class="err">wef >= we+wf-1 </span></span>
<span class="code-line"><span class="err">Se[0]==1, Se[1]==0</span></span>
</pre></div>
<p>用pulp编写的代码如下: </p>
<div class="highlight"><pre><span class="code-line"><span></span><span class="n">def</span><span class="w"> </span><span class="n">testcase</span><span class="p">(</span><span class="n">ind</span><span class="p">)</span><span class="err">:#</span><span class="w"> </span><span class="n">formulate</span><span class="w"> </span><span class="n">it</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="n">linear</span><span class="w"> </span><span class="n">programming</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="n">N</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">readval</span><span class="p">()</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="n">sentc</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="err">[]</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="n">allwords</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">set</span><span class="p">()</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="n">xrange</span><span class="p">(</span><span class="n">N</span><span class="p">)</span><span class="err">:</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="n">sentc</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="w"> </span><span class="k">set</span><span class="p">(</span><span class="n">readvals</span><span class="p">(</span><span class="nf">str</span><span class="p">))</span><span class="w"> </span><span class="p">)</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="n">allwords</span><span class="p">.</span><span class="k">update</span><span class="p">(</span><span class="w"> </span><span class="n">sentc</span><span class="o">[</span><span class="n">-1</span><span class="o">]</span><span class="w"> </span><span class="p">)</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="n">words</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">sorted</span><span class="p">(</span><span class="w"> </span><span class="n">allwords</span><span class="w"> </span><span class="p">)</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="n">M</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">len</span><span class="p">(</span><span class="n">words</span><span class="p">)</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="n">wordsindex</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="err">{</span><span class="n">words</span><span class="o">[</span><span class="n">i</span><span class="o">]</span><span class="err">:</span><span class="n">i</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="n">xrange</span><span class="p">(</span><span class="n">M</span><span class="p">)</span><span class="err">}</span><span class="w"> </span><span class="err">#</span><span class="w"> </span><span class="n">mapping</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">word</span><span class="w"> </span><span class="k">to</span><span class="w"> </span><span class="n">its</span><span class="w"> </span><span class="k">index</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="n">pb</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">LpProblem</span><span class="p">(</span><span class="s1">'Bilingual'</span><span class="p">,</span><span class="w"> </span><span class="n">LpMinimize</span><span class="p">)</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="err">#</span><span class="w"> </span><span class="n">LP</span><span class="w"> </span><span class="n">variables</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="n">Se</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">[</span><span class="n"> LpVariable('Se_'+str(i), cat='Binary') for i in xrange(N) </span><span class="o">]</span><span class="w"> </span><span class="err">#</span><span class="w"> </span><span class="n">Se</span><span class="o">[</span><span class="n">i</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">indicator</span><span class="p">(</span><span class="n">scentence</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="k">is</span><span class="w"> </span><span class="n">english</span><span class="p">)</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="n">we</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">[</span><span class="n"> LpVariable('we_'+str(j), cat='Binary') for j in xrange(M) </span><span class="o">]</span><span class="w"> </span><span class="err">#</span><span class="w"> </span><span class="n">we</span><span class="o">[</span><span class="n">j</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">indicator</span><span class="p">(</span><span class="n">word</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="k">is</span><span class="w"> </span><span class="n">english</span><span class="p">)</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="n">wf</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">[</span><span class="n"> LpVariable('wf_'+str(j), cat='Binary') for j in xrange(M) </span><span class="o">]</span><span class="w"> </span><span class="err">#</span><span class="w"> </span><span class="n">wf</span><span class="o">[</span><span class="n">j</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">indicator</span><span class="p">(</span><span class="n">word</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="k">is</span><span class="w"> </span><span class="n">french</span><span class="p">)</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="n">wef</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">[</span><span class="n"> LpVariable('wef_'+str(j), cat='Binary') for j in xrange(M) </span><span class="o">]</span><span class="w"> </span><span class="err">#</span><span class="w"> </span><span class="n">wef</span><span class="o">[</span><span class="n">j</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">indicator</span><span class="p">(</span><span class="n">word</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="k">is</span><span class="w"> </span><span class="k">BOTH</span><span class="w"> </span><span class="n">en</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="n">fr</span><span class="p">)</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="n">pb</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="nf">sum</span><span class="p">(</span><span class="w"> </span><span class="n">wef</span><span class="w"> </span><span class="p">)</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="n">pb</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="n">Se</span><span class="o">[</span><span class="n">0</span><span class="o">]==</span><span class="mi">1</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="n">pb</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="n">Se</span><span class="o">[</span><span class="n">1</span><span class="o">]==</span><span class="mi">0</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="n">xrange</span><span class="p">(</span><span class="n">N</span><span class="p">)</span><span class="err">:</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="n">si</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">sentc</span><span class="o">[</span><span class="n">i</span><span class="o">]</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">wd</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="nl">si</span><span class="p">:</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">wordsindex</span><span class="o">[</span><span class="n">wd</span><span class="o">]</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="n">pb</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="n">we</span><span class="o">[</span><span class="n">j</span><span class="o">]</span><span class="w"> </span><span class="o">>=</span><span class="w"> </span><span class="n">Se</span><span class="o">[</span><span class="n">i</span><span class="o">]</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="n">pb</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="n">wf</span><span class="o">[</span><span class="n">j</span><span class="o">]</span><span class="w"> </span><span class="o">>=</span><span class="w"> </span><span class="p">(</span><span class="mi">1</span><span class="o">-</span><span class="n">Se</span><span class="o">[</span><span class="n">i</span><span class="o">]</span><span class="p">)</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="n">xrange</span><span class="p">(</span><span class="n">M</span><span class="p">)</span><span class="err">:</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="n">pb</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="n">wef</span><span class="o">[</span><span class="n">j</span><span class="o">]</span><span class="w"> </span><span class="o">>=</span><span class="w"> </span><span class="n">we</span><span class="o">[</span><span class="n">j</span><span class="o">]+</span><span class="n">wf</span><span class="o">[</span><span class="n">j</span><span class="o">]-</span><span class="mi">1</span><span class="w"> </span><span class="err">#</span><span class="w"> </span><span class="err">#</span><span class="w"> </span><span class="n">wef</span><span class="o">[</span><span class="n">i</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">we</span><span class="o">[</span><span class="n">i</span><span class="o">]</span><span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="n">wf</span><span class="o">[</span><span class="n">i</span><span class="o">]</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="err">#</span><span class="o">~</span><span class="w"> </span><span class="n">pb</span><span class="p">.</span><span class="n">solve</span><span class="p">(</span><span class="w"> </span><span class="n">GLPK</span><span class="p">(</span><span class="n">msg</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span><span class="w"> </span><span class="p">)</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="n">pb</span><span class="p">.</span><span class="n">solve</span><span class="p">(</span><span class="w"> </span><span class="p">)</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nc">int</span><span class="p">(</span><span class="w"> </span><span class="k">value</span><span class="p">(</span><span class="n">pb</span><span class="p">.</span><span class="n">objective</span><span class="p">)</span><span class="w"> </span><span class="p">)</span><span class="w"> </span></span>
<span class="code-line"><span class="w"> </span><span class="k">print</span><span class="w"> </span><span class="s1">'Case #%d: %d'</span><span class="w"> </span><span class="o">%</span><span class="w"> </span><span class="p">(</span><span class="w"> </span><span class="n">ind</span><span class="p">,</span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="p">)</span><span class="w"></span></span>
</pre></div>
<p>以上代码在small上运行约1分钟, large约3分钟. </p>
<p>另外我发现<a href="https://www.go-hero.net/jam/15/name/liutianren">另一个大神</a>代码里没有用Binary/Integer的lp变量, 也就是说他用的是连续的线性规划! 当我把变量的定义改成: </p>
<div class="highlight"><pre><span class="code-line"><span></span><span class="n">Se</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">[</span><span class="n"> LpVariable('Se_'+str(i), 0, 1) for i in xrange(N) </span><span class="o">]</span><span class="w"> </span><span class="err">#</span><span class="w"> </span><span class="n">Se</span><span class="o">[</span><span class="n">i</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">indicator</span><span class="p">(</span><span class="n">scentence</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="k">is</span><span class="w"> </span><span class="n">english</span><span class="p">)</span><span class="w"> </span></span>
<span class="code-line"><span class="n">we</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">[</span><span class="n"> LpVariable('we_'+str(j), 0) for j in xrange(M) </span><span class="o">]</span><span class="w"> </span><span class="err">#</span><span class="w"> </span><span class="n">we</span><span class="o">[</span><span class="n">j</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">indicator</span><span class="p">(</span><span class="n">word</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="k">is</span><span class="w"> </span><span class="n">english</span><span class="p">)</span><span class="w"> </span></span>
<span class="code-line"><span class="n">wf</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">[</span><span class="n"> LpVariable('wf_'+str(j), 0) for j in xrange(M) </span><span class="o">]</span><span class="w"> </span><span class="err">#</span><span class="w"> </span><span class="n">wf</span><span class="o">[</span><span class="n">j</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">indicator</span><span class="p">(</span><span class="n">word</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="k">is</span><span class="w"> </span><span class="n">french</span><span class="p">)</span><span class="w"> </span></span>
<span class="code-line"><span class="n">wef</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">[</span><span class="n"> LpVariable('wef_'+str(j), 0) for j in xrange(M) </span><span class="o">]</span><span class="w"> </span><span class="err">#</span><span class="w"> </span><span class="n">wef</span><span class="o">[</span><span class="n">j</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">indicator</span><span class="p">(</span><span class="n">word</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="k">is</span><span class="w"> </span><span class="k">BOTH</span><span class="w"> </span><span class="n">en</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="n">fr</span><span class="p">)</span><span class="w"></span></span>
</pre></div>
<p>这样以后, 得到的结果还是正确的!! (不过代码运行时间没有显著的提高). </p>
<p>所以是这个模型中的矩阵满足<a href="https://en.wikipedia.org/wiki/Unimodular_matrix#Total_unimodularity">totally unimodular</a>性质? <br/>
不过这个模型似乎并不满足wikipedia里写的那个充分条件?...@@ <br/>
<a href="https://kunigami.wordpress.com/2013/08/12/tu-matrix-recognition/">这里</a>有一篇真•大神的文章是讲如何识别TU矩阵的 (tl;dr......), 那么是不是各种solver内部已经有了自动判断TU的代码, 所以这两种程序的运行时间差不多?... </p>
<p>总而言之这道题目还是非常有意思的, 三种解法都很有代表性, 这里的技巧估计在codejam里会经常用到... </p>
</div>
<div class="entry-content jumbotron" id="source-content" style="display: none">
<!-- <pre><code id="source-code">
</code></pre> -->
<div id="source-code"></div>
</div>
<!-- /.entry-content -->
<div class="row" id="prevnext">
<div class="col-xs-12">
<a href="https://x-wei.github.io/java-use-weka.html" class="btn btn-default btn-lg" style="float:left;clear:both;background-color:#fff;">
<h4><i class="fa fa-arrow-left"></i>
在java程序里使用weka进行机器学习
</h4>
</a>
</div>
<div class="col-xs-12">
<a href="https://x-wei.github.io/codejam-python-tools.html" class="btn btn-default btn-lg" style="float:right;clear:both;background-color:#fff;">
<h4>
codejam常用(python)解题工具<i class="fa fa-arrow-right"></i>
</h4>
</a>
</div>
</div>
<section class="comments" id="comments">
<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
<div class="panel panel-primary">
<div class="panel-heading" role="tab" id="disqus-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#disqus-comments" aria-expanded="true" aria-controls="disqus-comments">
<i class="fa fa-comments-o"></i> Disqus 留言
</a>
</h4>
</div>
<div id="disqus-comments" class="panel-collapse collapse.show" role="tabpanel" aria-labelledby="disqus-heading">
<div class="panel-body">
<div class="tab-pane fade active in" id="disqus-comments">
<div id="disqus_thread"></div>
<script type="text/javascript">
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = 'xweisblog'; // required: replace example with your forum shortname
var disqus_identifier = 'codejam-2015-r2pbC';
var disqus_url = 'https://x-wei.github.io/codejam-2015-r2pbC.html';
var disqus_config = function () {
this.language = "zh";
};
/* * * DON'T EDIT BELOW THIS LINE * * */
(function () {
var dsq = document.createElement('script');
dsq.type = 'text/javascript';
dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by
Disqus.</a></noscript>
<a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
</div>
</div>
</div>
</div>
</div>
</section> </article>
</section>
</div>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<a data-dismiss="modal" href="javascript:void(0);">
<img id="mimg" src="" style="width:100%;height:auto">
</a>
</div>
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
</div>
<div class="col-xl-3 col-lg-4 col-md-6" id="sidebar">
<aside>
<section>
<div class="sidebar-container">
<div class="sidebar-item ">
<div class="panel panel-default">
<div class="panel-heading">
<h4>
<i class="fa fa-user fa-lg"></i>
<a href="https://x-wei.github.io/about.html">
关于 mx
</a>
</h4>
</div>
<div class="panel-body" id="aboutme">
<a href="https://x-wei.github.io/about.html"><img width="100%" src="https://x-wei.github.io/../images/mx.jpg"/></a>
<h3 style="text-align:center">
<a href="https://github.com/x-wei" target="_blank">
<i class="fa fa-github" style="text-align:center"></i></a>
<a href="https://weibo.com/u/1817154611" target="_blank">
<i class="fa fa-weibo" style="text-align:center"></i></a>
<a href="mailto:[email protected]" target="_blank">
<i class="mdi-communication-email" style="text-align:center"></i></a>
</h3>
<h4 class="widget-title">推荐文章</h4>
<div class="textwidget">
<li class="widget-container widget_text">
<a href="https://x-wei.github.io/TeXmacs_intro.html">学术文章写作利器: TeXmacs介绍</a><br></li>
<li class="widget-container widget_text">
<a href="https://x-wei.github.io/hashcode2014-solved-by-LP.html">运筹的力量: 用线性规划解决Google 2014 HashCode问题</a><br></li>
<li class="widget-container widget_text">
<a href="https://x-wei.github.io/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F%E5%85%A5%E9%97%A8%E7%AE%80%E4%BB%8B.html">正则表达式入门简介</a><br></li>
<li class="widget-container widget_text">
<a href="https://x-wei.github.io/%E6%88%91%E7%9A%84ubuntu10.04%E9%85%8D%E7%BD%AE%E6%80%BB%E7%BB%93.html">我的ubuntu10.04配置总结</a><br></li>
<li class="widget-container widget_text">
<a href="https://x-wei.github.io/PT-summery.html">2011巴黎高科(ParisTech)申请总结</a><br></li>
<li class="widget-container widget_text">
<a href="https://x-wei.github.io/GT-summery.html">用尽量少的时间考一个够用的分数--一点Tofel/GRE备考经验</a><br></li>
<li class="widget-container widget_text">
<a href="https://x-wei.github.io/pelican_github_blog.html">用pelican在github上创建自己的博客!</a><br></li>
</div>
<br><a href="https://www.polytechnique.edu/" target="_blank">
<img src="https://x-wei.github.io/images/x-logo.png" alt="X" width="180" border="0" />
</a><br/>
<br><a href="https://www.sjtu.edu.cn/">
<img src="https://x-wei.github.io/images/ssss.jpg" width="180" border="0" alt="上海西南某高校">
</a><br/>
<br>
<h4 class="widget-title">Visitors</h4>
<script type="text/javascript" src="//rf.revolvermaps.com/0/0/1.js?i=59olkba9w7e&s=220&m=3&v=true&r=false&b=000000&n=false&c=ff0000" async="async"></script>
<!-- hitwebcounter Code START -->
<a href="https://www.hitwebcounter.com/how-to/how-to-what-is-free-blog-counter.php" target="_blank">
<img src="https://hitwebcounter.com/counter/counter.php?page=5954927&style=0036&nbdigits=5&type=ip&initCount=0" title="web counter" Alt="web counter" border="0" ></a>
<br/>
</div>
</div>
</div>
<div class="sidebar-item ">
<div class="panel panel-default">
<div class="panel-heading">
<h4>
<a href="https://x-wei.github.io/tags.html"><i class="fa fa-tags fa-lg"></i><span class="icon-label">标签云</span></a>
</h4>
</div>
<div class="panel-body">
<ul class="list-group list-inline tagcloud" id="tags">
<li class="list-group-item tag-2">
<a href="https://x-wei.github.io/tag/pelican.html">
pelican <sup> 6</sup>
</a>
</li>
<li class="list-group-item tag-2">
<a href="https://x-wei.github.io/tag/google.html">
google <sup> 6</sup>
</a>
</li>
<li class="list-group-item tag-2">
<a href="https://x-wei.github.io/tag/torch.html">
torch <sup> 6</sup>
</a>
</li>
<li class="list-group-item tag-2">
<a href="https://x-wei.github.io/tag/tex.html">
tex <sup> 4</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="https://x-wei.github.io/tag/ba-li-gao-ke.html">
巴黎高科 <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-3">
<a href="https://x-wei.github.io/tag/markdown.html">
markdown <sup> 2</sup>
</a>
</li>
<li class="list-group-item tag-3">
<a href="https://x-wei.github.io/tag/linux.html">
linux <sup> 3</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="https://x-wei.github.io/tag/inf422.html">
inf422 <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="https://x-wei.github.io/tag/toefl.html">
Toefl <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="https://x-wei.github.io/tag/regex.html">
regex <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-2">
<a href="https://x-wei.github.io/tag/git.html">
git <sup> 5</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="https://x-wei.github.io/tag/weka.html">
weka <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-1">
<a href="https://x-wei.github.io/tag/scala.html">
scala <sup> 12</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="https://x-wei.github.io/tag/opencv.html">
opencv <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="https://x-wei.github.io/tag/scrapy.html">
scrapy <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="https://x-wei.github.io/tag/gre.html">
GRE <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="https://x-wei.github.io/tag/lp.html">
LP <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-2">
<a href="https://x-wei.github.io/tag/android.html">
android <sup> 9</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="https://x-wei.github.io/tag/pandas.html">
pandas <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-3">
<a href="https://x-wei.github.io/tag/codejam.html">
codejam <sup> 2</sup>
</a>
</li>
<li class="list-group-item tag-3">
<a href="https://x-wei.github.io/tag/fr.html">
fr <sup> 2</sup>
</a>
</li>
<li class="list-group-item tag-2">
<a href="https://x-wei.github.io/tag/shell.html">
shell <sup> 4</sup>
</a>
</li>
<li class="list-group-item tag-1">
<a href="https://x-wei.github.io/tag/python.html">
python <sup> 13</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="https://x-wei.github.io/tag/simongarfunkel.html">
Simon&Garfunkel <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-3">
<a href="https://x-wei.github.io/tag/french.html">
french <sup> 2</sup>
</a>
</li>
<li class="list-group-item tag-2">
<a href="https://x-wei.github.io/tag/wai-guan.html">
外观 <sup> 4</sup>
</a>
</li>
<li class="list-group-item tag-3">
<a href="https://x-wei.github.io/tag/zhong-wen-luan-ma.html">
中文乱码 <sup> 2</sup>
</a>
</li>
<li class="list-group-item tag-3">
<a href="https://x-wei.github.io/tag/host.html">
host <sup> 3</sup>
</a>
</li>
<li class="list-group-item tag-3">
<a href="https://x-wei.github.io/tag/ssh.html">
ssh <sup> 2</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="https://x-wei.github.io/tag/ubuntu.html">
ubuntu <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-2">
<a href="https://x-wei.github.io/tag/java.html">
java <sup> 4</sup>
</a>
</li>
<li class="list-group-item tag-2">
<a href="https://x-wei.github.io/tag/spark.html">
spark <sup> 6</sup>
</a>
</li>
<li class="list-group-item tag-3">
<a href="https://x-wei.github.io/tag/tips.html">
tips <sup> 3</sup>
</a>
</li>
<li class="list-group-item tag-1">
<a href="https://x-wei.github.io/tag/deep-learning.html">
deep learning <sup> 28</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="https://x-wei.github.io/tag/c.html">
C++ <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-1">
<a href="https://x-wei.github.io/tag/algorithm.html">
algorithm <sup> 35</sup>
</a>
</li>
<li class="list-group-item tag-4">
<a href="https://x-wei.github.io/tag/texmacs.html">
TeXmacs <sup> 1</sup>
</a>
</li>
<li class="list-group-item tag-2">
<a href="https://x-wei.github.io/tag/ml.html">
ml <sup> 4</sup>
</a>
</li>
<li class="list-group-item tag-2">
<a href="https://x-wei.github.io/tag/eclipse.html">
eclipse <sup> 4</sup>
</a>
</li>
<li class="list-group-item tag-2">
<a href="https://x-wei.github.io/tag/ocaml.html">
OCaml <sup> 8</sup>
</a>
</li>
<li class="list-group-item tag-2">
<a href="https://x-wei.github.io/tag/r.html">
R <sup> 4</sup>
</a>
</li>
<li class="list-group-item tag-3">
<a href="https://x-wei.github.io/tag/chu-guo.html">
出国 <sup> 2</sup>
</a>
</li>
<li class="list-group-item tag-3">
<a href="https://x-wei.github.io/tag/kuai-jie-jian.html">
快捷键 <sup> 3</sup>
</a>
</li>
</ul>
</div>
</div>
</div>
<div class="sidebar-item hidden-xs">
<div class="panel panel-default">
<div class="panel-heading">
<h4>
<i class="fa fa-github fa-lg"></i><span class="icon-label">GitHub仓库</span>
</h4>
</div>
<div class="panel-body">
<div id="gh_repos">
<p class="list-group-item">Status updating...</p>
</div>
<a href="https://github.com/x-wei">@x-wei</a> on GitHub
</div>
</div>
</div>
</div>
</section>
<div class="panel panel-default hidden-xs hidden-sm" id="affix-toc">
<div class="panel-heading"><h4>
目录</h4>
</div>
<div class="panel-boy">
<div id="toc"><ul><li><a class="toc-href" href="#fa-1-qiong-ju-bruteforce-for-small-testcase" title="法1: 穷举 bruteforce (for small testcase)">法1: 穷举 bruteforce (for small testcase)</a><ul><li><a class="toc-href" href="#naive-bruteforce" title="naive bruteforce">naive bruteforce</a></li><li><a class="toc-href" href="#bruteforce-using-bitmap" title="bruteforce using bitmap">bruteforce using bitmap</a></li></ul></li><li><a class="toc-href" href="#fa-2-zui-da-liu-maxflow_1" title="法2: 最大流 maxflow">法2: 最大流 maxflow</a></li><li><a class="toc-href" href="#fa-3-xian-xing-gui-hua-integer-linear-programming" title="法3: 线性规划 (integer) linear programming">法3: 线性规划 (integer) linear programming</a></li></ul></div>
</div>
</div>
</aside>
</div>
</div>
</div>
<footer id="fcfooter">
<hr/>
<div class="container">
links :
<a href="https://farseerfc.github.com/">farseerfc</a>
<a href="https://hyhx2008.github.com/">H.Y.</a>
<a href="https://reginald1787.github.io/">reginald1787</a>
<a href="https://log.dofine.me/">dofine</a>
<div class="row">
<div class="col-md-14">
<p><small>
© 2020 mx
· 通过
<a href="https://docs.getpelican.com/" target="_blank">Pelican</a> 生成 <a rel="license" href="https://creativecommons.org/licenses/by-nc-sa/4.0/"><img alt="Creative Commons License" style="border-width:0" src="//i.creativecommons.org/l/by-nc-sa/4.0/80x15.png" /></a>
<!-- Content -->
<!-- licensed under a <a rel="license" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">Creative Commons Attribution 4.0 International License</a>, except where indicated otherwise. -->
</small></p>
</div>
</div>
</div>
<a href="#" class="btn btn-primary btn-fab btn-raised mdi-editor-vertical-align-top withripple" style="position:fixed;bottom:30px;right:30px;z-index:1000"></a>
</footer>
<script src="https://x-wei.github.io/theme/js/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="https://x-wei.github.io/theme/js/bootstrap.min.js"></script>
<!-- Enable responsive features in IE8 with Respond.js (https://github.com/scottjehl/Respond) -->
<script src="https://x-wei.github.io/theme/js/respond.min.js"></script>
<!-- GitHub JS -->
<script type="text/javascript">
$(document).ready(function () {
if (!window.jXHR) {
var jxhr = document.createElement('script');
jxhr.type = 'text/javascript';
jxhr.src = 'https://x-wei.github.io/theme/js/jXHR.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(jxhr, s);
}
github.showRepos({
user: 'x-wei',
count: 5,
skip_forks: false,
target: '#gh_repos'
});
});
</script>
<script src="https://x-wei.github.io/theme/js/github.js" type="text/javascript"></script>
<!-- End GitHub JS Code -->
<!-- Disqus -->
<script type="text/javascript">
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = 'xweisblog'; // required: replace example with your forum shortname
/* * * DON'T EDIT BELOW THIS LINE * * */
(function () {
var s = document.createElement('script');
s.async = true;
s.type = 'text/javascript';
s.src = '//' + disqus_shortname + '.disqus.com/count.js';
(document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s);
}());
</script>
<!-- End Disqus Code -->
<!-- Google Analytics -->
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-30756331-1']);
_gaq.push(['_trackPageview']);
(function () {
var ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
})();
</script>
<!-- End Google Analytics Code -->
<script src="https://x-wei.github.io/theme/js/ripples.min.js"></script>
<script src="https://x-wei.github.io/theme/js/material.min.js"></script>
<script src="https://x-wei.github.io/theme/js/jquery.bootstrap-autohidingnavbar.min.js"></script>
<script>
$(document).ready(function() {
$.material.init();
$("div.navbar").autoHidingNavbar();
$(".img-responsive").css("cursor","pointer").on('click',function(){
var sr=$(this).attr('src');
$('#mimg').attr('src',sr);
$('#myModal').modal('show');
});
$('#affix-toc').affix({
offset: {
top: function(){
if($('#affix-toc').hasClass("affix"))
return $('#sidebar').height();
return $('#sidebar').height() - $('#affix-toc').height();
},
bottom: function (){
return $("#fcfooter").offset().top -
$("#article-content").offset().top -
$("#article-content").height() + 20;
}
}
});
$('#affix-toc').width($('#sidebar').width());
});
$(window).resize(function () {
$('#affix-toc').width($('#sidebar').width());
});
</script>
</body>
</html>