-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatom.xml
942 lines (849 loc) · 231 KB
/
atom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title><![CDATA[JSSU_Shen]]></title>
<subtitle><![CDATA[宁静致远]]></subtitle>
<link href="/atom.xml" rel="self"/>
<link href="http://jssu.github.io//"/>
<updated>2015-10-24T20:03:22.000Z</updated>
<id>http://jssu.github.io//</id>
<author>
<name><![CDATA[Junsu Shen]]></name>
</author>
<generator uri="http://hexo.io/">Hexo</generator>
<entry>
<title><![CDATA[Notes]]></title>
<link href="http://jssu.github.io/2022/04/20/ANotes/"/>
<id>http://jssu.github.io/2022/04/20/ANotes/</id>
<published>2022-04-21T05:22:22.000Z</published>
<updated>2015-10-24T20:03:22.000Z</updated>
<content type="html"><![CDATA[<h1 id="JavaScript">JavaScript</h1><a id="more"></a>
<h2 id="面试遇到">面试遇到</h2><blockquote>
<p>Function return function, Implement say(‘say’)(‘hello’)->out put “sayhello”</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">say</span>(<span class="params">v</span>)</span>{</span><br><span class="line"> <span class="keyword">return</span> v+<span class="function"><span class="keyword">function</span>(<span class="params">v2</span>)</span>{</span><br><span class="line"> <span class="keyword">return</span> v2;</span><br><span class="line"> };</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h2 id="windows-onload_和_document-onload_区别:">windows.onload 和 document.onload 区别:</h2><figure class="highlight coffeescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">window</span>.onload</span><br></pre></td></tr></table></figure>
<p>By default, it is fired when the entire page loads, including its content (images, css, scripts, etc.)<br>In some browsers it now takes over the role of document.onload and fires when the DOM is ready as well.<br><figure class="highlight coffeescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">document</span>.onload</span><br></pre></td></tr></table></figure></p>
<p>It is called when the DOM is ready which can be prior to images and other external content is loaded.</p>
<h1 id="DJango">DJango</h1><h2 id="virtualenv">virtualenv</h2><p>Note: All bash file are in: <code>.bash_profile</code><br>Command is in <code>urs/local/bin</code></p>
<h2 id="Command">Command</h2><ul>
<li><p>virtualenv <code>envName</code><br>新建一个环境文件夹</p>
</li>
<li><p>django-admin startproject <code>projectName</code><br>新建一个Django项目</p>
</li>
<li><p>python manage.py runserver<br>在内网上发布</p>
</li>
<li><p>urls.py<br>url(r’^hereIsTheURL$’,’hereIsTheViewMethod’)</p>
</li>
<li><p>穿件一个App<br>django-admin startapp blog</p>
</li>
<li><p>view页面</p>
<figure class="highlight python"><figcaption><span>view model</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> django.template <span class="keyword">import</span> loader, Context, Template</span><br><span class="line"><span class="keyword">from</span> django.http <span class="keyword">import</span> HttpResponse</span><br><span class="line"><span class="keyword">from</span> django.shortcuts <span class="keyword">import</span> render</span><br><span class="line"><span class="keyword">from</span> django.conf <span class="keyword">import</span> settings</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>index.html is template<br><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">index</span><span class="params">(request)</span>:</span></span><br><span class="line"> t=loader.get_template(<span class="string">'index.html'</span>) </span><br><span class="line"> c=Context({<span class="string">'uname'</span>:<span class="string">"Fred"</span>})</span><br><span class="line"> <span class="keyword">return</span> HttpResponse(t.render(c))</span><br></pre></td></tr></table></figure></p>
<p>Or<br><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">index</span><span class="params">(request)</span>:</span></span><br><span class="line"> t=Template(<span class="string">'<h1>hello{{uname}}</h1>'</span>)</span><br><span class="line"> c=Context({<span class="string">'uname'</span>:<span class="string">"fred"</span>})</span><br><span class="line"> <span class="keyword">return</span> HttpResponse(t.render(c))</span><br></pre></td></tr></table></figure></p>
<p>Or<br><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">index</span><span class="params">(request)</span>:</span></span><br><span class="line"> <span class="keyword">return</span> render_to_response(<span class="string">'index.html'</span>,{<span class="string">'uname'</span>:<span class="string">'Fred'</span>})</span><br></pre></td></tr></table></figure></p>
<p>Or<br><figure class="highlight nimrod"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">def index(request):</span><br><span class="line"> context=local()</span><br><span class="line"> <span class="keyword">template</span> = 'index.html'</span><br><span class="line"> <span class="keyword">return</span> render(request, <span class="keyword">template</span>, context)</span><br></pre></td></tr></table></figure></p>
<h2 id="Connect_to_MongoDB">Connect to MongoDB</h2><p>Source from<br><code>https://django-mongodb-engine.readthedocs.org/en/latest/topics/setup.html</code><br>Installation<br>Django MongoDB Engine depends on</p>
<p><code>Django-nonrel</code>, a fork of Django that adds support for non-relational databases<br>djangotoolbox, a bunch of utilities for non-relational Django applications and backends<br>It’s highly recommended (although not required) to use a virtualenv for your project to not mess up other Django setups.</p>
<p><code>virtualenv</code><br>If not already installed, grab a copy from the Cheeseshop:</p>
<p>pip install virtualenv<br>To set up a virtual environment for your project, use</p>
<p>virtualenv myproject<br>To join the environment, use (in Bash):</p>
<p>source myproject/bin/activate<br>Django-nonrel<br>pip install git+<a href="https://github.com/django-nonrel/[email protected]" target="_blank" rel="external">https://github.com/django-nonrel/[email protected]</a><br>djangotoolbox<br>pip install git+<a href="https://github.com/django-nonrel/djangotoolbox" target="_blank" rel="external">https://github.com/django-nonrel/djangotoolbox</a><br>Django MongoDB Engine<br>You should use the latest Git revision.</p>
<p>pip install git+<a href="https://github.com/django-nonrel/mongodb-engine" target="_blank" rel="external">https://github.com/django-nonrel/mongodb-engine</a><br>Configuration<br>Database setup is easy (see also the Django database setup docs):<br><figure class="highlight actionscript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">DATABASES = {</span><br><span class="line"> <span class="string">'default'</span> : {</span><br><span class="line"> <span class="string">'ENGINE'</span> : <span class="string">'django_mongodb_engine'</span>,</span><br><span class="line"> <span class="string">'NAME'</span> : <span class="string">'my_database'</span></span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure></p>
<p>Django MongoDB Engine also takes into account the HOST, PORT, USER, PASSWORD and OPTIONS settings.</p>
<p>Possible values of OPTIONS are described in the settings reference.</p>
<p>Done!<br>That’s it! You can now go straight ahead developing your Django application as you would do with any other database.</p>
]]></content>
<summary type="html">
<![CDATA[<h1 id="JavaScript">JavaScript</h1>]]>
</summary>
<category term="笔记Notes" scheme="http://jssu.github.io/tags/%E7%AC%94%E8%AE%B0Notes/"/>
<category term="学习" scheme="http://jssu.github.io/categories/%E5%AD%A6%E4%B9%A0/"/>
</entry>
<entry>
<title><![CDATA[Apple]]></title>
<link href="http://jssu.github.io/2017/09/05/Apple/"/>
<id>http://jssu.github.io/2017/09/05/Apple/</id>
<published>2017-09-05T18:57:05.000Z</published>
<updated>2017-09-05T20:55:09.000Z</updated>
<content type="html"><![CDATA[<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br></pre></td><td class="code"><pre><span class="line"><span class="preprocessor">#<span class="keyword">include</span> <iostream></span></span><br><span class="line"><span class="preprocessor">#<span class="keyword">include</span> <vector></span></span><br><span class="line"><span class="preprocessor">#<span class="keyword">include</span> <utility></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">calcScore</span><span class="params">(<span class="built_in">vector</span><pair<<span class="keyword">int</span>, <span class="keyword">int</span>>>& inp)</span> <span class="comment">//<5,6,10,9> --><10, 9, ></span></span><br><span class="line"></span>{</span><br><span class="line"> <span class="keyword">int</span> res =<span class="number">0</span>;</span><br><span class="line"> <span class="comment">//for handle a:</span></span><br><span class="line"> <span class="comment">// b, c</span></span><br><span class="line"> <span class="keyword">int</span> checkPair[<span class="number">13</span>]={<span class="number">0</span>}; <span class="comment">// A-king, 1-13 0-12 //-1</span></span><br><span class="line"> <span class="keyword">int</span> checkFlush[<span class="number">4</span>] ={<span class="number">0</span>}; <span class="comment">// 4 suits 0 ,1,2,3</span></span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">auto</span> item: inp)</span><br><span class="line"> {</span><br><span class="line"> checkPair[item.second-<span class="number">1</span>]++;</span><br><span class="line"> checkFlush[item.first]++;</span><br><span class="line"> <span class="keyword">if</span>(checkFlush[item.first]==<span class="number">4</span>) </span><br><span class="line"> res+=<span class="number">4</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">auto</span> item: checkPair)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(item==<span class="number">2</span>)</span><br><span class="line"> res+=<span class="number">2</span>;</span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">if</span>(item==<span class="number">3</span>)</span><br><span class="line"> {</span><br><span class="line"> <span class="comment">//c num 2</span></span><br><span class="line"> res+= <span class="number">6</span>; <span class="comment">// 2*3; // 3 => 3, 4=>6 (n-1)*(n-2)</span></span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">if</span>(item==<span class="number">4</span>)</span><br><span class="line"> {</span><br><span class="line"> res+=<span class="number">12</span>; <span class="comment">//2*6;</span></span><br><span class="line"> } </span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">auto</span> x: inp)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(x.second><span class="number">10</span>) x.second=<span class="number">10</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i<inp.size(); i++)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j=i+<span class="number">1</span>; j<inp.size(); j++)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(inp[i].second + inp[j].second>=<span class="number">15</span>)</span><br><span class="line"> res+=<span class="number">2</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> res;</span><br><span class="line">}</span><br><span class="line"><span class="comment">// To execute C++, please define "int main()"</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="built_in">vector</span><pair<<span class="keyword">int</span>, <span class="keyword">int</span>>> inp={</span><br><span class="line"> {<span class="number">0</span>,<span class="number">1</span>},{<span class="number">0</span>,<span class="number">2</span>},{<span class="number">0</span>,<span class="number">3</span>},{<span class="number">0</span>,<span class="number">4</span>}</span><br><span class="line"> };</span><br><span class="line"> <span class="built_in">cout</span><<calcScore(inp)<<endl; <span class="comment">//=> 4</span></span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line"> <span class="built_in">vector</span><pair<<span class="keyword">int</span>, <span class="keyword">int</span>>> inp2={</span><br><span class="line"> {<span class="number">0</span>,<span class="number">8</span>},{<span class="number">1</span>,<span class="number">9</span>},{<span class="number">0</span>,<span class="number">3</span>},{<span class="number">0</span>,<span class="number">7</span>} <span class="comment">//2,2,2</span></span><br><span class="line"> };</span><br><span class="line"> <span class="built_in">cout</span><<calcScore(inp2)<<endl; <span class="comment">//=> 4</span></span><br><span class="line"> </span><br><span class="line"> <span class="built_in">vector</span><pair<<span class="keyword">int</span>, <span class="keyword">int</span>>> inp3={</span><br><span class="line"> {<span class="number">0</span>,<span class="number">6</span>},{<span class="number">1</span>,<span class="number">6</span>},{<span class="number">2</span>,<span class="number">6</span>},{<span class="number">0</span>,<span class="number">2</span>} <span class="comment">//2,2,2</span></span><br><span class="line"> };</span><br><span class="line"> <span class="built_in">cout</span><<calcScore(inp3)<<endl; <span class="comment">//=> 4</span></span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line"> <span class="built_in">vector</span><pair<<span class="keyword">int</span>, <span class="keyword">int</span>>> inp4={</span><br><span class="line"> {<span class="number">0</span>,<span class="number">6</span>},{<span class="number">1</span>,<span class="number">6</span>},{<span class="number">2</span>,<span class="number">6</span>},{<span class="number">3</span>,<span class="number">6</span>} <span class="comment">//2,2,2</span></span><br><span class="line"> };</span><br><span class="line"> <span class="built_in">cout</span><<calcScore(inp4)<<endl; <span class="comment">//=> 4</span></span><br><span class="line"> </span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">/* </span><br><span class="line">Your previous Plain Text content is preserved below:</span><br><span class="line"></span><br><span class="line"><span class="comment">/* </span><br><span class="line"></span><br><span class="line">// Determine the Cribbage score (simplified) for a hand of four cards.</span><br><span class="line">Rules:</span><br><span class="line"># // a) "Fifteen" any 2 cards' values sum to 15 => 2 points</span><br><span class="line"># // (where value of Jack,Queen,King = 10, Ace = 1).</span><br><span class="line"># // b) "Pair" any two cards with same rank => 2 points. //2,2,2 -> 6</span><br><span class="line"># // c) "flush" all four cards with same suit => 4 points. //1,2,3,4 Hearts </span><br><span class="line"># // Example: [Jack Hearts, Jack Clubs, 5 Spades, Queen Clubs] ==> 8 pts</span><br><span class="line"># // 2 pts: Jack Hearts + 5 = 15</span><br><span class="line"># // 2 pts: Jack Clubs + 5 = 15</span><br><span class="line"># // 2 pts: Queen Clubs + 5 = 15</span><br><span class="line"># // 2 pts: Pair of Jacks)</span><br><span class="line"></span><br><span class="line"># Question: Implement a func calcScore that returns the score for a hand of</span><br><span class="line">cards</span><br><span class="line">//input 4.</span><br><span class="line">//c++</span><br><span class="line">//pair<int, int> heart-1, clubs -2.... //<suit, number></span><br><span class="line">//inp no dup</span><br><span class="line">//[C1 , C2, C3, C4] = 0+</span><br><span class="line">//checkPair[1,1,1,1,0,0,0...]</span><br><span class="line"> */</span></span></span><br></pre></td></tr></table></figure>]]></content>
<summary type="html">
<![CDATA[<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="]]>
</summary>
<category term="文章" scheme="http://jssu.github.io/tags/%E6%96%87%E7%AB%A0/"/>
<category term="学习" scheme="http://jssu.github.io/categories/%E5%AD%A6%E4%B9%A0/"/>
</entry>
<entry>
<title><![CDATA[Airbnb]]></title>
<link href="http://jssu.github.io/2017/08/28/AirBnb/"/>
<id>http://jssu.github.io/2017/08/28/AirBnb/</id>
<published>2017-08-29T06:57:05.000Z</published>
<updated>2017-09-05T19:24:27.000Z</updated>
<content type="html"><![CDATA[<h2 id="Text_adjust">Text adjust</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> Solution {<span class="comment">//</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line"> <span class="built_in">vector</span><<span class="built_in">string</span>> fullJustify(<span class="built_in">vector</span><<span class="built_in">string</span>>& words, <span class="keyword">int</span> maxWidth) {</span><br><span class="line"> <span class="keyword">int</span> i, l=<span class="number">0</span>, k=<span class="number">0</span>;</span><br><span class="line"> <span class="built_in">vector</span><<span class="built_in">string</span>> res;</span><br><span class="line"> <span class="keyword">for</span>(i=<span class="number">0</span>; i<words.size(); i+=k)</span><br><span class="line"> {</span><br><span class="line"> l+=words[i].size();</span><br><span class="line"> <span class="comment">//for(k=1; k+i<words.size() && l+k-1+words[i+k].size()<=maxWidth; k++)</span></span><br><span class="line"> <span class="keyword">for</span>(k=<span class="number">1</span>; k+i<words.size() && l+k+words[i+k].size()<=maxWidth; k++) <span class="comment">//k more than 1</span></span><br><span class="line"> l+=words[i+k].size();</span><br><span class="line"> <span class="comment">//cout<<"i ="<<i<<", k is"<<k<<", l:"<<l<<endl; </span></span><br><span class="line"> <span class="built_in">string</span> temp=words[i];<span class="comment">//todo k==0</span></span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">1</span>; j<k;j++) <span class="comment">//from j->k</span></span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(i+k>=words.size()) temp+=<span class="string">" "</span>+words[i+j];</span><br><span class="line"> <span class="keyword">else</span>{</span><br><span class="line"> <span class="keyword">int</span> space = (maxWidth-l)/(k-<span class="number">1</span>); <span class="keyword">int</span> extraspace=<span class="number">0</span>;<span class="comment">//int space = (maxWidth-l)/(i+k); int extraspace=0;</span></span><br><span class="line"> <span class="comment">//cout<<"maxWidth: "<<maxWidth<<endl;</span></span><br><span class="line"> <span class="comment">//cout<<"space is: "<<space<<endl; </span></span><br><span class="line"> extraspace=(maxWidth-l)%(k-<span class="number">1</span>)>=j;<span class="comment">//extraspace=(maxWidth-l)%(i+k)>=j;//2</span></span><br><span class="line"> temp+=<span class="built_in">string</span>(space+extraspace,<span class="string">' '</span>)+words[i+j];</span><br><span class="line"> <span class="comment">//cout<<"-"<<temp<<"-"<<endl;</span></span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> temp+=<span class="built_in">string</span>(maxWidth-temp.size(),<span class="string">' '</span>);</span><br><span class="line"> <span class="comment">//cout<<endl;</span></span><br><span class="line"> res.push_back(temp);</span><br><span class="line"> l=<span class="number">0</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> res;</span><br><span class="line"> }</span><br><span class="line">};</span><br><span class="line"><span class="comment">//problem</span></span><br><span class="line"><span class="comment">//k more than 1, </span></span><br><span class="line"><span class="comment">//k-1=0</span></span><br><span class="line"><span class="comment">//</span></span><br></pre></td></tr></table></figure>
<h2 id="Round_Point">Round Point</h2><p>他们公司list价格分成好几个部分,但是都是整数,如果在美金是整数,到了欧洲的网页显示汇率转换之后就变成了floating point,然后要round成整数,但是全部加起来round,和单独round再加起来,结果会不一样</p>
<blockquote>
<p>base price 100 => 131.13 => 131<br>cleaning fee 20 => 26.23 => 26<br>service fee 10 => 13.54 => 14<br>tax 5 => 6.5 => 7.<br> => 177.4E => 178E<br>sum 135$ => 178.93E => 179E </p>
</blockquote>
<p>那么问题就来了,给个input list of floating points, 要求output list of integers, 满足以下两个constraint, 就是和跟Round(x1+x2+… +xn)的结果一样,但是minimize output 和input的绝对值差之和</p>
<blockquote>
<p>Input: A = [x1, x2, …, xn]<br>Sum T = Round(x1+x2+… +xn) ; 178.93E => 179.<br>Output: B = [y1, y2, …., yn]</p>
<p>Constraint #1: y1+y2+…+yn = T<br>Constraint #2: minimize sum(abs(diff(xi - yi)))</p>
</blockquote>
<p>举例</p>
<blockquote>
<p>A = [1.2, 2.3, 3.4]<br>Round(1.2 + 2.3 + 3.4) = 6.9 => 7.<br>1 + 2 + 3 => 6</p>
<p>1 + 3 + 3 => 7<br>0.2 + 0.7 + 0.4 = 1.3</p>
<p>1 + 2 + 4 => 7<br>0.2 + 0.3 + 0.6 = 1.1<br>所以[1,2,4]比[1,3,3]要好</p>
</blockquote>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> Solution{</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line"> <span class="built_in">vector</span><<span class="keyword">int</span>> getMin(<span class="built_in">vector</span><<span class="keyword">float</span>> inp)</span><br><span class="line"> {</span><br><span class="line"> <span class="built_in">vector</span><<span class="keyword">int</span>> res(inp.begin(), inp.end());</span><br><span class="line"> <span class="keyword">double</span> T =<span class="number">0</span>; <span class="keyword">int</span> sum=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i<inp.size(); i++)</span><br><span class="line"> {</span><br><span class="line"> T += inp[i];</span><br><span class="line"> sum += res[i];</span><br><span class="line"> }</span><br><span class="line"> T = round(T);</span><br><span class="line"> <span class="keyword">int</span> left = T-sum;</span><br><span class="line"> <span class="keyword">if</span>(left == <span class="number">0</span>) <span class="keyword">return</span> res;</span><br><span class="line"> <span class="built_in">vector</span><<span class="keyword">int</span>> resIndex(inp.size());</span><br><span class="line"> iota(resIndex.begin(), resIndex.end(), <span class="number">0</span>);</span><br><span class="line"> sort(resIndex.begin(), resIndex.end(),</span><br><span class="line"> [res](<span class="keyword">int</span> a, <span class="keyword">int</span> b){</span><br><span class="line"> <span class="keyword">return</span> res[a]>res[b];</span><br><span class="line"> });</span><br><span class="line"> <span class="keyword">int</span> index=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span>(left><span class="number">0</span>)</span><br><span class="line"> {</span><br><span class="line"> res[resIndex[index++]]++;</span><br><span class="line"> left--;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> res;</span><br><span class="line"> }</span><br><span class="line">};</span><br></pre></td></tr></table></figure>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="preprocessor">#<span class="keyword">include</span> <iostream></span></span><br><span class="line"><span class="preprocessor">#<span class="keyword">include</span> <math.h></span></span><br><span class="line"><span class="preprocessor">#<span class="keyword">include</span> <numeric> </span></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> Solution{</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line"> <span class="built_in">vector</span><<span class="keyword">int</span>> getMin(<span class="built_in">vector</span><<span class="keyword">float</span>> inp)</span><br><span class="line"> {</span><br><span class="line"> <span class="built_in">vector</span><<span class="keyword">int</span>> res(inp.begin(), inp.end());</span><br><span class="line"> <span class="keyword">double</span> T =<span class="number">0</span>; <span class="keyword">int</span> sum=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i<inp.size(); i++)</span><br><span class="line"> {</span><br><span class="line"> T += inp[i];</span><br><span class="line"> sum += res[i];</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">cout</span><<<span class="string">"T is: "</span><<T<<endl;</span><br><span class="line"> T = round(T);</span><br><span class="line"> <span class="built_in">cout</span><<<span class="string">"T is: "</span><<T<<<span class="string">", and sum is: "</span><<sum<<endl;</span><br><span class="line"> <span class="keyword">int</span> left = T-sum;</span><br><span class="line"> <span class="keyword">if</span>(left == <span class="number">0</span>) <span class="keyword">return</span> res;</span><br><span class="line"> <span class="built_in">vector</span><<span class="keyword">int</span>> resIndex(inp.size());</span><br><span class="line"> iota(resIndex.begin(), resIndex.end(), <span class="number">0</span>);</span><br><span class="line"> sort(resIndex.begin(), resIndex.end(),</span><br><span class="line"> [res](<span class="keyword">int</span> a, <span class="keyword">int</span> b){</span><br><span class="line"> <span class="keyword">return</span> res[a]>res[b];</span><br><span class="line"> });</span><br><span class="line"> <span class="keyword">int</span> index=<span class="number">0</span>;</span><br><span class="line"> <span class="built_in">cout</span><<<span class="string">"Index sort is: "</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">auto</span> item: resIndex) <span class="built_in">cout</span><<item<<<span class="string">" "</span>;<span class="built_in">cout</span><<endl;</span><br><span class="line"> <span class="built_in">cout</span><<<span class="string">"left is: "</span><<left<<endl;</span><br><span class="line"> <span class="keyword">while</span>(left><span class="number">0</span>)</span><br><span class="line"> {</span><br><span class="line"> res[resIndex[index++]]++;</span><br><span class="line"> left--;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> res;</span><br><span class="line"> }</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="built_in">vector</span><<span class="keyword">float</span>> inp = {<span class="number">1.2</span>, <span class="number">2.3</span>, <span class="number">3.4</span>};</span><br><span class="line"> Solution S;</span><br><span class="line"> <span class="built_in">vector</span><<span class="keyword">int</span>> res = S.getMin(inp);</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">auto</span> x: res) <span class="built_in">cout</span><<x<<<span class="string">" "</span>;</span><br><span class="line"> <span class="built_in">cout</span><<endl;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h2 id="20180824">20180824</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//aba, aabbaa </span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// ["gab", "cat", "Bag", "alpha"] => find(ab) find(ga) ... find(bag)</span></span><br><span class="line"><span class="comment">// => [["gab", "Bag"], ["bag", "gab"]]</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 012</span></span><br><span class="line"><span class="comment">// "gab" index 0->3 => g, ga, gab</span></span><br><span class="line"><span class="comment">/*</span><br><span class="line">if(is_P(g)) => is_p(ga) X => gab X</span><br><span class="line">{</span><br><span class="line"> hashtable.find(ba) //=> ba gab, =>bag</span><br><span class="line">}</span><br><span class="line">// "gab" index 3->0 => b, ab, gab</span><br><span class="line"> hashtable exist => ag, X, X => bag</span><br><span class="line">*/</span></span><br><span class="line"><span class="comment">//every lower case</span></span><br><span class="line"><span class="comment">//no dup</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// check from beg, end two if</span></span><br><span class="line"></span><br><span class="line"><span class="preprocessor">#<span class="keyword">include</span> <iostream></span></span><br><span class="line"><span class="preprocessor">#<span class="keyword">include</span> <unordered_map></span></span><br><span class="line"><span class="preprocessor">#<span class="keyword">include</span> <vector></span></span><br><span class="line"><span class="preprocessor">#<span class="keyword">include</span> <algorithm></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> Solution{</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line"> <span class="built_in">vector</span><<span class="built_in">vector</span><<span class="built_in">string</span>>> findPPairs(<span class="built_in">vector</span><<span class="built_in">string</span>> words)</span><br><span class="line"> {</span><br><span class="line"> <span class="built_in">vector</span><<span class="built_in">vector</span><<span class="built_in">string</span>>> result;</span><br><span class="line"> <span class="built_in">unordered_map</span><<span class="built_in">string</span>, <span class="keyword">int</span>> dict; <span class="comment">// the string, the string's index</span></span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i<words.size(); i++) dict[words[i]] = i;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i<words.size(); i++)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">0</span>; j<=(<span class="keyword">int</span>)words[i].size();j++)</span><br><span class="line"> {</span><br><span class="line"> <span class="comment">//check beg</span></span><br><span class="line"> <span class="keyword">if</span>(is_p(words[i], j, (<span class="keyword">int</span>)words[i].size() - <span class="number">1</span>)) <span class="comment">// aaa, aa</span></span><br><span class="line"> {</span><br><span class="line"> <span class="built_in">string</span> suffix = words[i].substr(<span class="number">0</span>, j);</span><br><span class="line"> reverse(suffix.begin(), suffix.end());</span><br><span class="line"> <span class="keyword">if</span>(dict.find(suffix) != dict.end() && i!=dict[suffix])</span><br><span class="line"> {</span><br><span class="line"> <span class="built_in">vector</span><<span class="built_in">string</span>> temp;</span><br><span class="line"> temp.push_back(words[i]);</span><br><span class="line"> temp.push_back(words[dict[suffix]]);</span><br><span class="line"> result.push_back(temp);</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//check end</span></span><br><span class="line"> <span class="keyword">if</span>(j><span class="number">0</span> && is_p(words[i], <span class="number">0</span>, j-<span class="number">1</span>)) <span class="comment">// aaa, aa 1,2 </span></span><br><span class="line"> {</span><br><span class="line"> <span class="built_in">string</span> prefix = words[i].substr(j);</span><br><span class="line"> reverse(prefix.begin(), prefix.end());</span><br><span class="line"> <span class="keyword">if</span>(dict.find(prefix) != dict.end() && i!=dict[prefix])</span><br><span class="line"> {</span><br><span class="line"> <span class="built_in">vector</span><<span class="built_in">string</span>> temp;</span><br><span class="line"> temp.push_back(words[i]);</span><br><span class="line"> temp.push_back(words[dict[prefix]]);</span><br><span class="line"> result.push_back(temp);</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> result;</span><br><span class="line"> </span><br><span class="line"> }</span><br><span class="line"> <span class="function"><span class="keyword">bool</span> <span class="title">is_p</span><span class="params">(<span class="built_in">string</span> s, <span class="keyword">int</span> start, <span class="keyword">int</span> end)</span></span>{</span><br><span class="line"> <span class="keyword">while</span>(start < end)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(s[start++]!=s[end--])</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line">}; </span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="comment">//vector<string> inp ={"gab", "cat", "bag", "alpha"};</span></span><br><span class="line"> <span class="built_in">vector</span><<span class="built_in">string</span>> inp = {<span class="string">"gab"</span>, <span class="string">"cat"</span>, <span class="string">"bag"</span>, <span class="string">"alpha"</span>, <span class="string">"nurses"</span>, <span class="string">"race"</span>, <span class="string">"car"</span>, <span class="string">"run"</span>};</span><br><span class="line"> Solution s;</span><br><span class="line"> <span class="built_in">vector</span><<span class="built_in">vector</span><<span class="built_in">string</span>>> res = s.findPPairs(inp);</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">auto</span> item: res)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">auto</span> x: item)</span><br><span class="line"> <span class="built_in">cout</span><<x<<<span class="string">" "</span>;</span><br><span class="line"> <span class="built_in">cout</span><<endl;</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
]]></content>
<summary type="html">
<![CDATA[<h2 id="Text_adjust">Text adjust</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span c]]>
</summary>
<category term="文章" scheme="http://jssu.github.io/tags/%E6%96%87%E7%AB%A0/"/>
<category term="学习" scheme="http://jssu.github.io/categories/%E5%AD%A6%E4%B9%A0/"/>
</entry>
<entry>
<title><![CDATA[Sublime-project 一键大开方法]]></title>
<link href="http://jssu.github.io/2016/08/13/SublimeProject%E4%BD%BF%E7%94%A8/"/>
<id>http://jssu.github.io/2016/08/13/SublimeProject使用/</id>
<published>2016-08-13T12:32:06.000Z</published>
<updated>2017-01-26T05:15:54.000Z</updated>
<content type="html"><![CDATA[<p>在你喜欢的地方建个 ‘.sublime-project’ extension 的文件,在里面用下面这格式:<br><a id="more"></a><br>‘’’cpp<br>{<br> “folders”:<br> [<br> {<br> “path”: “PATH”<br> }<br> ]<br>}<br>‘’’</p>
<p>好久都没更新了,玩了大半年,今天妈过生日,准备刷半年题然后换工作<br>刚建了个静态网站刷提用,感谢github :)<br>链接是:<a href="https://jssu.github.io/DocLeetCode/">https://jssu.github.io/DocLeetCode/</a><br>等有空了建个牛B的莫,现在凑合着用吧,目标一天1+题。。。可能吧</p>
]]></content>
<summary type="html">
<![CDATA[<p>在你喜欢的地方建个 ‘.sublime-project’ extension 的文件,在里面用下面这格式:<br>]]>
</summary>
<category term="文章" scheme="http://jssu.github.io/tags/%E6%96%87%E7%AB%A0/"/>
<category term="技术" scheme="http://jssu.github.io/categories/%E6%8A%80%E6%9C%AF/"/>
</entry>
<entry>
<title><![CDATA[Python 输出 Excel]]></title>
<link href="http://jssu.github.io/2016/01/04/pythonExcel/"/>
<id>http://jssu.github.io/2016/01/04/pythonExcel/</id>
<published>2016-01-05T00:07:05.000Z</published>
<updated>2016-01-05T00:15:08.000Z</updated>
<content type="html"><![CDATA[<p>先装model:pip install xlwt</p>
<p>下面是sample code:<br>‘<a href="https://pypi.python.org/pypi/xlwt" target="_blank" rel="external">https://pypi.python.org/pypi/xlwt</a>‘</p>
<p>‘’’<br>import xlwt<br>from datetime import datetime</p>
<p>style0 = xlwt.easyxf(‘font: name Times New Roman, color-index red, bold on’,<br> num_format_str=’#,##0.00’)<br>style1 = xlwt.easyxf(num_format_str=’D-MMM-YY’)</p>
<p>wb = xlwt.Workbook()<br>ws = wb.add_sheet(‘A Test Sheet’)</p>
<p>ws.write(0, 0, 1234.56, style0)<br>ws.write(1, 0, datetime.now(), style1)<br>ws.write(2, 0, 1)<br>ws.write(2, 1, 1)<br>ws.write(2, 2, xlwt.Formula(“A3+B3”))</p>
<p>wb.save(‘example.xls’)<br>‘’’</p>
<iframe width="560" height="315" src="https://www.youtube.com/embed/gd38-X3HpbM" frameborder="0" allowfullscreen></iframe>
]]></content>
<summary type="html">
<![CDATA[<p>先装model:pip install xlwt</p>
<p>下面是sample code:<br>‘<a href="https://pypi.python.org/pypi/xlwt" target="_blank" rel="external">https://py]]>
</summary>
<category term="Python" scheme="http://jssu.github.io/tags/Python/"/>
<category term="学习" scheme="http://jssu.github.io/categories/%E5%AD%A6%E4%B9%A0/"/>
</entry>
<entry>
<title><![CDATA[Amazon]]></title>
<link href="http://jssu.github.io/2015/11/07/amazon/"/>
<id>http://jssu.github.io/2015/11/07/amazon/</id>
<published>2015-11-08T02:40:53.000Z</published>
<updated>2015-11-08T14:51:23.000Z</updated>
<content type="html"><![CDATA[<p>Asked to find sub-strings from a string which are valid dictionary words.</p>
<p>Two sentences and wanted me to return a list of words that exists in one sentence but not on the other one. for example s1 = “I am happy” s2 = “I am old”, you have to return [“happy”, “old”]</p>
<h1 id="the_difference_betwemarken_abstract_class_and_interface">the difference betwemarken abstract class and interface</h1><h2 id="interface">interface</h2><p>An interface is a contract: the guy writing the interface says, “hey, I accept things looking that way”, and the guy using the interface says “Ok, the class I write looks that way”.</p>
<p>An interface is an empty shell, there are only the signatures of the methods, which implies that the methods do not have a body. The interface can’t do anything. It’s just a pattern.<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// I say all motor vehicles should look like this:</span></span><br><span class="line"><span class="class"><span class="keyword">interface</span> <span class="title">MotorVehicle</span></span><br><span class="line"></span>{</span><br><span class="line"> <span class="function"><span class="keyword">void</span> <span class="title">run</span><span class="params">()</span></span>;</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">int</span> <span class="title">getFuel</span><span class="params">()</span></span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// my team mate complies and writes vehicle looking that way</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Car</span> <span class="keyword">implements</span> <span class="title">MotorVehicle</span></span><br><span class="line"></span>{</span><br><span class="line"></span><br><span class="line"> <span class="keyword">int</span> fuel;</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">void</span> <span class="title">run</span><span class="params">()</span></span><br><span class="line"> </span>{</span><br><span class="line"> print(<span class="string">"Wrroooooooom"</span>);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">int</span> <span class="title">getFuel</span><span class="params">()</span></span><br><span class="line"> </span>{</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">this</span>.fuel;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure></p>
<p>Implementing an interface consumes very little CPU, because it’s not a class, just a bunch of names, and therefore there is no expensive look-up to do. It’s great when it matters such as in embedded devices.</p>
<h2 id="Abstract_class">Abstract class</h2><p>Abstract classes, unlike interfaces, are classes. They are more expensive to use because there is a look-up to do when you inherit from them.</p>
<p>Abstract classes look a lot like interfaces, but they have something more : you can define a behavior for them. It’s more about a guy saying, “these classes should look like that, and they have that in common, so fill in the blanks!”.<br><figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// I say all motor vehicles should look like this :</span></span><br><span class="line"><span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">MotorVehicle</span></span><br><span class="line"></span>{</span><br><span class="line"></span><br><span class="line"> int fuel;</span><br><span class="line"></span><br><span class="line"> <span class="comment">// they ALL have fuel, so why not let others implement this?</span></span><br><span class="line"> <span class="comment">// let's make it for everybody</span></span><br><span class="line"> int getFuel()</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">this</span>.fuel;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">// that can be very different, force them to provide their</span></span><br><span class="line"> <span class="comment">// implementation</span></span><br><span class="line"> <span class="keyword">abstract</span> void run();</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// my team mate complies and writes vehicle looking that way</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Car</span> <span class="keyword"><span class="keyword">extends</span></span> <span class="title">MotorVehicle</span></span><br><span class="line"></span>{</span><br><span class="line"> void run()</span><br><span class="line"> {</span><br><span class="line"> print(<span class="string">"Wrroooooooom"</span>);</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure></p>
<h1 id="Implementation">Implementation</h1><p>While abstract classes and interfaces are supposed to be different concepts, the implementations make that statement sometimes untrue. Sometimes, they are not even what you think they are.</p>
<p>In Java, this rule is strongly enforced, while in PHP, interfaces are abstract classes with no method declared.</p>
<p>In Python, abstract classes are more a programming trick you can get from the ABC module and is actually using metaclasses, and therefore classes. And interfaces are more related to duck typing in this language and it’s a mix between conventions and special methods that call descriptors (the <strong>method</strong> methods).</p>
<p>As usual with programming, there is theory, practice, and practice in another language :-)</p>
]]></content>
<summary type="html">
<![CDATA[<p>Asked to find sub-strings from a string which are valid dictionary words.</p>
<p>Two sentences and wanted me to return a list of words th]]>
</summary>
<category term="面试" scheme="http://jssu.github.io/tags/%E9%9D%A2%E8%AF%95/"/>
<category term="学习" scheme="http://jssu.github.io/categories/%E5%AD%A6%E4%B9%A0/"/>
</entry>
<entry>
<title><![CDATA[OOP]]></title>
<link href="http://jssu.github.io/2015/11/07/oop/"/>
<id>http://jssu.github.io/2015/11/07/oop/</id>
<published>2015-11-08T02:40:53.000Z</published>
<updated>2015-11-08T15:11:41.000Z</updated>
<content type="html"><![CDATA[<a id="more"></a>
<p>转载:<a href="https://www3.ntu.edu.sg/home/ehchua/programming/cpp/cp3_OOP.html" target="_blank" rel="external">https://www3.ntu.edu.sg/home/ehchua/programming/cpp/cp3_OOP.html</a><br>C++ Programming Language<br>Object-Oriented Programming (OOP) in C++</p>
<h2 id="Why_OOP?">Why OOP?</h2><p>Suppose that you want to assemble your own PC, you go to a hardware store and pick up a motherboard, a processor, some RAMs, a hard disk, a casing, a power supply, and put them together. You turn on the power, and the PC runs. You need not worry whether the motherboard is a 4-layer or 6-layer board, whether the hard disk has 4 or 6 plates; 3 inches or 5 inches in diameter, whether the RAM is made in Japan or Korea, and so on. You simply put the hardware components together and expect the machine to run. Of course, you have to make sure that you have the correct interfaces, i.e., you pick an IDE hard disk rather than a SCSI hard disk, if your motherboard supports only IDE; you have to select RAMs with the correct speed rating, and so on. Nevertheless, it is not difficult to set up a machine from hardware components.</p>
<p>Similarly, a car is assembled from parts and components, such as chassis, doors, engine, wheels, brake, and transmission. The components are reusable, e.g., a wheel can be used in many cars (of the same specifications).<br>Hardware, such as computers and cars, are assembled from parts, which are reusable components.<br>How about software? Can you “assemble” a software application by picking a routine here, a routine there, and expect the program to run? The answer is obviously no! Unlike hardware, it is very difficult to “assemble” an application from software components. Since the advent of computer 60 years ago, we have written tons and tons of programs. However, for each new application, we have to re-invent the wheels and write the program from scratch.<br>Why re-invent the wheels?</p>
]]></content>
<summary type="html">
<![CDATA[<a id="more"></a>
<p>转载:<a href="https://www3.ntu.edu.sg/home/ehchua/programming/cpp/cp3_OOP.html" target="_blank" rel="external">https://ww]]>
</summary>
<category term="C++" scheme="http://jssu.github.io/tags/C/"/>
<category term="OOP" scheme="http://jssu.github.io/tags/OOP/"/>
<category term="学习" scheme="http://jssu.github.io/categories/%E5%AD%A6%E4%B9%A0/"/>
</entry>
<entry>
<title><![CDATA[MongoDB]]></title>
<link href="http://jssu.github.io/2015/10/24/mongo/"/>
<id>http://jssu.github.io/2015/10/24/mongo/</id>
<published>2015-10-25T05:22:22.000Z</published>
<updated>2015-10-28T00:42:44.000Z</updated>
<content type="html"><![CDATA[<p><code>简写t</code></p>
<ul>
<li>简写替换:如果db=test, collection=profile, 可以简写t=db.profile, 以后t就可以代替db.profile了</li>
<li><p>mongoimport</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">> mongoimport <span class="operator">-d</span> DBName -c CollectionName FileName</span><br></pre></td></tr></table></figure>
</li>
<li><p>bool 不用加引号(EX: {“registered”:false})</p>
</li>
</ul>
<h1 id="Insert">Insert</h1><p>db.<collection>.insert(<>)</collection></p>
<h1 id="Full_update">Full update</h1><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.<span class="tag"><<span class="title">collection</span>></span>.update(<span class="tag"><<span class="title">where</span>></span>,<span class="tag"><<span class="title">full</span> <span class="attribute">or</span> <span class="attribute">patial</span> <span class="attribute">update</span> <span class="attribute">expression</span>></span>[,<span class="tag"><<span class="title">upsert</span>></span>,<span class="tag"><<span class="title">multi</span>></span>])</span><br></pre></td></tr></table></figure>
<h2 id="mongo_shell_update_use_javascript">mongo shell update use javascript</h2><p>myobj=t.findOne() <code>存现有值到一个myobj变量里, 注意一定要用findOne()</code></p>
<ul>
<li>t.update({_id : myobj._id}, myobj)</li>
<li>t.save(myobj)<a id="more"></a>
<h1 id="Patial_update(BSON_Size_limit_16MB/Doc)">Patial update(BSON Size limit 16MB/Doc)</h1>$set —db.collection.update({WHERE},{$set:{“NEWINSERT”:”NEWVALUE}})<br>mydoc={‘$set’:{‘INSERT’:100}}<br>t.update({_id:myobj._id},mydoc)</li>
</ul>
<p>$push —db.collection.update({WHERE},{$push:{“ARRAY_NAME”:”ARRAY_VALUE}})<br>push to an array if not(Last position), create one<br>$addToSet —insert if not present</p>
<h1 id="删除">删除</h1><h2 id="$Remove/Delete_—db-remove()—_={…}">$Remove/Delete —db.<collectionname>.remove(<where(multi)>)— <where(multi)>={…}</where(multi)></where(multi)></collectionname></h2><p>db.collection.remove({}) —remove all</p>
<h1 id="multi_update_default_is_false">multi update default is false</h1><p>db.<collection>.update(<where>,<full or="" patial="" update="" expression="">,<multi>) —<multi>default false</multi></multi></full></where></collection></p>
<h1 id="upsert">upsert</h1><p>db.<collection>.update(<where>,<full or="" patial="" update="" expression="">,<upsert>)</upsert></full></where></collection></p>
<h1 id="Command-">Command.</h1><p>BSON wire protocal<br>Query, Insert, Update, Remove, GetMore<br>getLastError,isMaster,drop, create compact—db.runCommand({})<br>ensureIndex, dropIndex, currentOp, killOp —help, db.help()</p>
<ul>
<li>db.serverStatus()</li>
<li>db.cuurentOp()<figure class="highlight less"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">db</span><span class="class">.currentOp</span>()</span><br><span class="line">(New Window)<span class="tag">mongo</span> <span class="tag">localhost</span>/<span class="tag">test</span></span><br><span class="line">(New Window)<span class="tag">while</span>(<span class="number">1</span>){<span class="tag">db</span><span class="class">.teest3</span><span class="class">.insert</span>({<span class="attribute">ts</span>:new <span class="function">Date</span>()}); <span class="tag">sleep</span>(<span class="number">5</span>);}</span><br><span class="line"><span class="tag">db</span><span class="class">.currentOp</span>()就有东西了</span><br><span class="line"><span class="tag">killOp</span>()</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>[Q]If you’re looking for problems with database performance, what is a good place to look, when you run db.currentOp()?<br>[A]“secs_running”: (look for long times)</p>
<ul>
<li><p>collection.stats()</p>
</li>
<li><p>collection.drop() 把所有东西drop掉— db.collection.remove({ })</p>
<figure class="highlight fortran"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">{</span><br><span class="line"> Server:[isMaster, serverStatus, logout]</span><br><span class="line"> db:[dropDatabase, repairDatabase, clone, copydb, dbStats]</span><br><span class="line"> collection:{dba:[create(<span class="type">implicit</span>), drop, collStats, renameCollection], </span><br><span class="line"> user:[<span class="built_in">count</span>,aggregate, mapReduce, FindAndModify, geo]}</span><br><span class="line"> <span class="built_in">index</span>:[ensureIndex, dropIndex]</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
</li>
</ul>
<h1 id="Bulk_operations_and_methods">Bulk operations and methods</h1><figure class="highlight armasm"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="label">var</span> <span class="keyword">bulk= </span>db.<span class="keyword">items.initializeUnorderedBulkOp();</span><br><span class="line"></span><span class="keyword">bulk.insert({BULABULABULA})</span><br><span class="line"></span><span class="label">...</span></span><br><span class="line"><span class="keyword">bulk.execute()</span></span><br></pre></td></tr></table></figure>
<figure class="highlight openscad"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">var b= db.items.initializeOrderedBulkOp<span class="params">()</span>;</span><br><span class="line">b.find<span class="params">({item:<span class="string">"abc123"</span>})</span>.remove<span class="params">()</span></span><br><span class="line">b.find<span class="params">({item:<span class="string">"abc123"</span>})</span>.update<span class="params">({$inc:{points:<span class="number">1</span>}})</span></span><br><span class="line">...</span><br><span class="line">bulk.execute<span class="params">()</span></span><br></pre></td></tr></table></figure>
<p>//————-Quiz Note—————<br>In the shell, add that this user likes “football”. You should not need to pass “tennis” or “golf” to the update query at all.<br>db.users.update({},{$addToSet:{likes:”football”}},true,true)</p>
<ul>
<li><p>HW2—<br>b = db.products_bak; db.products.find().forEach( function(o){ b.insert(o) } )<br>// check it worked:<br>b.count()<br>// should print 11<br>homework.a()</p>
</li>
<li><blockquote>
<p>db.products.find({_id:ObjectId(“507d95d5719dbef170f15c00”)})<br>db.products.update({_id:ObjectId(“507d95d5719dbef170f15c00”)},{$inc:{term_years:1}})<br>db.products.update({_id:ObjectId(“507d95d5719dbef170f15c00”)},{$inc:{“limits.sms.over_rate”:0.01}})</p>
</blockquote>
</li>
<li><p>How many products have a voice limit? (That is, have a voice field present in the limits subdocument.)<br>db.products.find({“limits.voice”:{$exists:true}}).count()</p>
</li>
</ul>
]]></content>
<summary type="html">
<![CDATA[<p><code>简写t</code></p>
<ul>
<li>简写替换:如果db=test, collection=profile, 可以简写t=db.profile, 以后t就可以代替db.profile了</li>
<li><p>mongoimport</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">> mongoimport <span class="operator">-d</span> DBName -c CollectionName FileName</span><br></pre></td></tr></table></figure>
</li>
<li><p>bool 不用加引号(EX: {“registered”:false})</p>
</li>
</ul>
<h1 id="Insert">Insert</h1><p>db.<collection>.insert(<>)</p>
<h1 id="Full_update">Full update</h1><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.<span class="tag"><<span class="title">collection</span>></span>.update(<span class="tag"><<span class="title">where</span>></span>,<span class="tag"><<span class="title">full</span> <span class="attribute">or</span> <span class="attribute">patial</span> <span class="attribute">update</span> <span class="attribute">expression</span>></span>[,<span class="tag"><<span class="title">upsert</span>></span>,<span class="tag"><<span class="title">multi</span>></span>])</span><br></pre></td></tr></table></figure>
<h2 id="mongo_shell_update_use_javascript">mongo shell update use javascript</h2><p>myobj=t.findOne() <code>存现有值到一个myobj变量里, 注意一定要用findOne()</code></p>
<ul>
<li>t.update({_id : myobj._id}, myobj)</li>
<li>t.save(myobj)]]>
</summary>
<category term="笔记Notes" scheme="http://jssu.github.io/tags/%E7%AC%94%E8%AE%B0Notes/"/>
<category term="学习" scheme="http://jssu.github.io/categories/%E5%AD%A6%E4%B9%A0/"/>
</entry>
<entry>
<title><![CDATA[CSRG_Token]]></title>
<link href="http://jssu.github.io/2015/10/17/CSRG_Token/"/>
<id>http://jssu.github.io/2015/10/17/CSRG_Token/</id>
<published>2015-10-17T23:07:05.000Z</published>
<updated>2015-10-18T02:14:12.000Z</updated>
<content type="html"><![CDATA[<p>From: <a href="http://www.acunetix.com/websitesecurity/csrf-attacks/" target="_blank" rel="external">http://www.acunetix.com/websitesecurity/csrf-attacks/</a></p>
<h2 id="CSRF_Attacks,_XSRF_or_Sea-Surf_–_What_They_Are_and_How_to_Defend_Against_Them">CSRF Attacks, XSRF or Sea-Surf – What They Are and How to Defend Against Them</h2><p>Cross-Site Request Forgery, or CSRF for short is a common and regular online attack. CSRF also goes by the acronym XSRF and the phrase “Sea-Surf”. CSRF attacks include a malicious exploit of a website in which a user will transmit malicious requests that the target website trusts without the user’s consent. In Cross-Site Scripting (XSS), the attacker exploits the trust a user has for a website, with CSRF on the other hand, the attacker exploits the trust a website has against a user’s browser.</p>
<p>Basically, an attacker will use CSRF to trick a victim into accessing a website or clicking a URL link that contains malicious or unauthorized requests. It is called ‘malicious’ since the CSRF attack will use the identity and privileges of the victim and impersonate them in order to perform any actions desired by the attacker, such as change form submission details, and launch purchases or payments for the attacker or a third-party account.<br><a id="more"></a><br>Upon a request against most websites, browsers will include along any credentials related with the particular website, such as the session cookie of the user, basic authentication credentials, the IP address of the user, etc. Thus, if user’s authentication session is still valid, an attacker can use CSRF to launch any desired requests against the website, without the website being able to distinguish whether the requests are legitimate or not.</p>
<h3 id="A_Simple_Example_of_a_Cross-Site_Request_Forgery">A Simple Example of a Cross-Site Request Forgery</h3><p>As described above, in order for a CSRF attack to be performed, the user must be authenticated with the target website. Assuming the victim is authenticated, the attacker can include a link or script in a third-party website that the victim visits. Thus, when the victim visits that website or link, the rogue script will be executed without the victim being aware of it. For instance, in a chat forum, an attacker posts a message which contains an image tag or an HTML image element. However, the source of the image contains a link which performs an action on a victim’s bank website account. So, instead of an image file the attacker has included a link that performs a bank transaction. Below is an example of the image tag containing a rogue URL.<br><figure class="highlight stylus"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><<span class="tag">img</span> src=<span class="string">"http://bank.example.com/withdraw?account=bob&amount=1000000&for=Fred"</span>></span><br></pre></td></tr></table></figure></p>
<p>The above is a CSRF attack using an HTTP GET request. As we shall see later, a prevention method would allow only HTTP POST requests, in order to prevent the above attack method. However, this can be easily bypassed, since an attacker can use an HTTP POST request to perform a CSRF attack.</p>
<h3 id="CSRF_Example_Using_an_HTTP_POST_Request">CSRF Example Using an HTTP POST Request</h3><p>In this example the attacker will use an HTTP POST request to realize a CSRF attack. Since the HTTP GET request is not allowed to be used as a prevention measure against a CSRF attack, an attacker can use the HTTP POST request which will perform the CSRF as successfully as the HTTP GET request. It is very difficult for the target website to distinguish between legitimate and rogue HTTP GET or POST requests, since the requests are sent from a “trusted” browser. That means that if no prevention measures are in place, a CSRF attack can be performed transparently without the victim or target website realizing it.</p>
<p>The purpose of the attack, in this example, is to change the profile information of a particular user (victim) on the target website. The target website for this example will be <a href="http://testphp.vulnweb.com/" target="_blank" rel="external">http://testphp.vulnweb.com/</a>.</p>
<p><img src="https://scontent-iad3-1.xx.fbcdn.net/hphotos-xap1/v/t1.0-9/12066048_10205241507372816_9074937616735437404_n.jpg?oh=2b667d54ea94a950c2c105f0df1d814e&oe=5686F5B5" alt="00"></p>
<p>The victim has an account on testphp.vulnweb.com which includes personal information as seen below.</p>
<p><img src="https://scontent-iad3-1.xx.fbcdn.net/hphotos-xtp1/v/t1.0-9/12144908_10205241507412817_1347109091082019320_n.jpg?oh=7448d153729a238c362c9482b40ad066&oe=568C50C8" alt="01"><br>The attacker uses CSRF to change the information on the victim’s profile. This, as mentioned earlier, requires the victim to be authenticated with the target website. A user can update the profile information by using the given form in the ‘Your profile’ page. The code of the particular form is shown below.</p>
<p>CSRF Example in – <a href="http://testphp.vulnweb.com/userinfo.php" target="_blank" rel="external">http://testphp.vulnweb.com/userinfo.php</a><br><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="title">form</span> <span class="attribute">name</span>=<span class="value">"form1"</span> <span class="attribute">method</span>=<span class="value">"post"</span> <span class="attribute">action</span>=<span class="value">""</span>></span></span><br><span class="line"><span class="tag"><<span class="title">table</span> <span class="attribute">border</span>=<span class="value">"0"</span> <span class="attribute">cellspacing</span>=<span class="value">"1"</span> <span class="attribute">cellpadding</span>=<span class="value">"4"</span>></span></span><br><span class="line"><span class="tag"><<span class="title">tr</span>></span></span><br><span class="line"><span class="tag"><<span class="title">td</span> <span class="attribute">valign</span>=<span class="value">"top"</span>></span>Name:<span class="tag"></<span class="title">td</span>></span><span class="tag"><<span class="title">td</span>></span><span class="tag"><<span class="title">input</span> <span class="attribute">type</span>=<span class="value">"text"</span> <span class="attribute">value</span>=<span class="value">"James Markus"</span> <span class="attribute">name</span>=<span class="value">"urname"</span> <span class="attribute">style</span>=<span class="value">"width:200px"</span>></span><span class="tag"></<span class="title">td</span>></span></span><br><span class="line"><span class="tag"></<span class="title">tr</span>></span></span><br><span class="line"><span class="tag"><<span class="title">tr</span>></span><span class="tag"><<span class="title">td</span> <span class="attribute">valign</span>=<span class="value">"top"</span>></span>Credit card number:<span class="tag"></<span class="title">td</span>></span><span class="tag"><<span class="title">td</span>></span><span class="tag"><<span class="title">input</span> <span class="attribute">type</span>=<span class="value">"text"</span> <span class="attribute">value</span>=<span class="value">"1254-5498-5233-5569"</span> <span class="attribute">name</span>=<span class="value">"ucc"</span> <span class="attribute">style</span>=<span class="value">"width:200px"</span>></span><span class="tag"></<span class="title">td</span>></span></span><br><span class="line"><span class="tag"></<span class="title">tr</span>></span></span><br><span class="line"><span class="tag"><<span class="title">tr</span>></span><span class="tag"><<span class="title">td</span> <span class="attribute">valign</span>=<span class="value">"top"</span>></span>E-Mail:<span class="tag"></<span class="title">td</span>></span><span class="tag"><<span class="title">td</span>></span><span class="tag"><<span class="title">input</span> <span class="attribute">type</span>=<span class="value">"text"</span> <span class="attribute">value</span>=<span class="value">"[email protected]"</span> <span class="attribute">name</span>=<span class="value">"uemail"</span> <span class="attribute">style</span>=<span class="value">"width:200px"</span>></span><span class="tag"></<span class="title">td</span>></span></span><br><span class="line"><span class="tag"></<span class="title">tr</span>></span></span><br><span class="line"><span class="tag"><<span class="title">tr</span>></span><span class="tag"><<span class="title">td</span> <span class="attribute">valign</span>=<span class="value">"top"</span>></span>Phone number:<span class="tag"></<span class="title">td</span>></span><span class="tag"><<span class="title">td</span>></span><span class="tag"><<span class="title">input</span> <span class="attribute">type</span>=<span class="value">"text"</span> <span class="attribute">value</span>=<span class="value">"+44 123 12345 123"</span> <span class="attribute">name</span>=<span class="value">"uphone"</span> <span class="attribute">style</span>=<span class="value">"width:200px"</span>></span><span class="tag"></<span class="title">td</span>></span></span><br><span class="line"><span class="tag"></<span class="title">tr</span>></span></span><br><span class="line"><span class="tag"><<span class="title">tr</span>></span><span class="tag"><<span class="title">td</span> <span class="attribute">valign</span>=<span class="value">"top"</span>></span>Address:<span class="tag"></<span class="title">td</span>></span><span class="tag"><<span class="title">td</span>></span><span class="tag"><<span class="title">textarea</span> <span class="attribute">wrap</span>=<span class="value">"soft"</span> <span class="attribute">name</span>=<span class="value">"uaddress"</span> <span class="attribute">rows</span>=<span class="value">"5"</span> <span class="attribute">style</span>=<span class="value">"width:200px"</span>></span>North London, London, England<span class="tag"></<span class="title">textarea</span>></span><span class="tag"></<span class="title">td</span>></span></span><br><span class="line"><span class="tag"></<span class="title">tr</span>></span></span><br><span class="line"><span class="tag"><<span class="title">tr</span>></span><span class="tag"><<span class="title">td</span> <span class="attribute">colspan</span>=<span class="value">"2"</span> <span class="attribute">align</span>=<span class="value">"right"</span>></span><span class="tag"><<span class="title">input</span> <span class="attribute">type</span>=<span class="value">"submit"</span> <span class="attribute">value</span>=<span class="value">"update"</span> <span class="attribute">name</span>=<span class="value">"update"</span>></span><span class="tag"></<span class="title">td</span>></span><span class="tag"></<span class="title">tr</span>></span><span class="tag"></<span class="title">table</span>></span></span><br><span class="line"><span class="tag"></<span class="title">form</span>></span></span><br></pre></td></tr></table></figure></p>
<p>From the above code we can identify the input fields which will receive information from a user and send to the website. These are called ‘urname’, ‘ucc’, ‘uemail’, ‘uphone’ and ‘uaddress’ and are shown below.<br><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="title">input</span> <span class="attribute">type</span>=<span class="value">"text"</span> <span class="attribute">value</span>=<span class="value">"John Doe"</span> <span class="attribute">name</span>=<span class="value">"urname"</span> <span class="attribute">style</span>=<span class="value">"width:200px"</span>></span></span><br><span class="line"><span class="tag"><<span class="title">input</span> <span class="attribute">type</span>=<span class="value">"text"</span> <span class="attribute">value</span>=<span class="value">"1254-5498-5233-5569"</span> <span class="attribute">name</span>=<span class="value">"ucc"</span> <span class="attribute">style</span>=<span class="value">"width:200px"</span>></span></span><br><span class="line"><span class="tag"><<span class="title">input</span> <span class="attribute">type</span>=<span class="value">"text"</span> <span class="attribute">value</span>=<span class="value">"[email protected]"</span> <span class="attribute">name</span>=<span class="value">"uemail"</span> <span class="attribute">style</span>=<span class="value">"width:200px"</span>></span></span><br><span class="line"><span class="tag"><<span class="title">input</span> <span class="attribute">type</span>=<span class="value">"text"</span> <span class="attribute">value</span>=<span class="value">"+44 123 12345 123"</span> <span class="attribute">name</span>=<span class="value">"uphone"</span> <span class="attribute">style</span>=<span class="value">"width:200px"</span>></span></span><br><span class="line"><span class="tag"><<span class="title">textarea</span> <span class="attribute">wrap</span>=<span class="value">"soft"</span> <span class="attribute">name</span>=<span class="value">"uaddress"</span> <span class="attribute">rows</span>=<span class="value">"5"</span> <span class="attribute">style</span>=<span class="value">"width:200px"</span>></span></span><br><span class="line">North London, London, England<span class="tag"></<span class="title">textarea</span>></span></span><br></pre></td></tr></table></figure></p>
<p>When the user clicks the ‘Update’ button of the form userinfo.php, an HTTP POST request will be sent that will contain the above parameters along with their values accordingly.</p>
<p>Since the website does not have any prevention measures against CSRF, the attacker can use this form (<a href="http://testphp.vulnweb.com/userinfo.php" target="_blank" rel="external">http://testphp.vulnweb.com/userinfo.php</a>) to submit any desired information without the user’s consent. The attacker will perform this by embedding the actual code of the update form in his own website and when the victim visits the attacker’s website, the form, including any desired information of the attacker, will be submitted to the target website.</p>
<p>This is the malicious website of the attacker.</p>
<p><img src="https://scontent-iad3-1.xx.fbcdn.net/hphotos-xpa1/v/t1.0-9/12096617_10205241507532820_3180453729503538699_n.jpg?oh=d7cc5730ec0be1fc53503ded624a1e00&oe=56C86CED" alt="02"><br>The attacker’s website is a normal online photo gallery website. However, it contains a hidden form which will auto submit and update the victim’s profile on testphp.vulnweb.com.</p>
<p>The hidden ‘iframe’ exists in the ‘myimages.php’ page.</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="title">iframe</span> <span class="attribute">src</span>=<span class="value">"http://www.vulnweb.com/updateif.php"</span> <span class="attribute">style</span>=<span class="value">"display:none"</span>></span><span class="tag"></<span class="title">iframe</span>></span></span><br></pre></td></tr></table></figure>
<p>This loads another page of the attacker’s website. The website contains the actual ‘userinfo.php’ page code which auto submits and updates the particular ‘userinfo.php’ of the current victim. This happens automatically every time a user accesses this website.</p>
<p>The ‘updateif.php’ page contains the actual form code which auto submits the desired information the attacker has set.</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="title">body</span> <span class="attribute">onload</span>=<span class="value">"document.getElementById('f').submit()"</span>></span></span><br><span class="line"><span class="tag"><<span class="title">form</span> <span class="attribute">id</span>=<span class="value">"f"</span> <span class="attribute">action</span>=<span class="value">"http://testphp.vulnweb.com/userinfo.php"</span> <span class="attribute">method</span>=<span class="value">"post"</span> <span class="attribute">name</span>=<span class="value">"form1"</span>></span></span><br><span class="line"><span class="tag"><<span class="title">input</span> <span class="attribute">name</span>=<span class="value">"urname"</span> <span class="attribute">value</span>=<span class="value">"attacker’svalue"</span>></span></span><br><span class="line"><span class="tag"><<span class="title">input</span> <span class="attribute">name</span>=<span class="value">"ucc"</span> <span class="attribute">value</span>=<span class="value">" attacker’svalue"</span>></span></span><br><span class="line"><span class="tag"><<span class="title">input</span> <span class="attribute">name</span>=<span class="value">"uemail"</span> <span class="attribute">value</span>=<span class="value">" attacker’svalue"</span>></span></span><br><span class="line"><span class="tag"><<span class="title">input</span> <span class="attribute">name</span>=<span class="value">"uphone"</span> <span class="attribute">value</span>=<span class="value">" attacker’svalue"</span>></span></span><br><span class="line"><span class="tag"><<span class="title">textarea</span> <span class="attribute">name</span>=<span class="value">"uaddress"</span> <span class="attribute">wrap</span>=<span class="value">"soft"</span>></span><span class="tag"><<span class="title">attacker’svalue</span>></span><span class="tag"></<span class="title">textarea</span>></span></span><br><span class="line"><span class="tag"><<span class="title">input</span> <span class="attribute">name</span>=<span class="value">"update"</span> <span class="attribute">value</span>=<span class="value">"update"</span>></span></span><br><span class="line"><span class="tag"></<span class="title">form</span>></span></span><br><span class="line"><span class="tag"></<span class="title">body</span>></span></span><br></pre></td></tr></table></figure>
<p>This form retrieves the value information from a text file. When the updateif.php is called, the information (set earlier) by the attacker is retrieved and placed in the value fields. Then the form is auto submitted and the target page is loaded. These operations are performed inside a hidden iframe, thus the victim will not see the target website.</p>
<p>The attacker has an admin page – www.vulnweb.com/hackpanel – from where values values to be submitted on the target website can be set.</p>
<p><img src="https://scontent-iad3-1.xx.fbcdn.net/hphotos-xla1/v/t1.0-9/12141518_10205241507452818_7908015421779506589_n.jpg?oh=679511ceb75e689209ec1553678298f4&oe=5686F824" alt="03"></p>
<p>The admin hack panel is a control page where the attacker can set the information that will be submitted to the target website when the CSRF attack is realized.</p>
<p>From this website, the attacker can set new information. (Note: For the purposes of this example, there is a reset button which will reset the values of the target website) This information is stored in a file, from which the updateif.php (seen earlier) will load and submit the attacker’s value.</p>
<p>So, as we mentioned earlier, in order for the attacker to perform a CSRF attack and his information to be submitted, the main requirement is for the victim to be logged into the target website. When the victim visits the attackers’ website, the hidden iframe will load the code of the update profile form found in the userinfo.php (target’s update profile form) with the attacker’s desired information and auto submit them to the target website. This operation is the exact operation the victim could perform to update his profile. However, due to the CSRF vulnerability a third party entity such as an attacker can use this operation to submit malicious information without the user being able to know about it.</p>
<p>The attacker sets the desired information in the <a href="http://www.vulnweb.com/hackpanel/" target="_blank" rel="external">http://www.vulnweb.com/hackpanel/</a> page and clicks ‘Update’. The information is stored in the file.</p>
<p><img src="https://scontent-iad3-1.xx.fbcdn.net/hphotos-xaf1/v/t1.0-9/11218696_10205241507972831_4055496495921599492_n.jpg?oh=f6c1f139fcf2a2fcd8671d7c73c4f583&oe=56C9AAF6" alt="04"></p>
<p>The attacker’s information is ready to be loaded when the attacker’s website is visited.</p>
<p>When the victim visits the attacker’s website at ‘www.vulnweb.com/index.php’, nothing will happen since there is not any malicious code in the ‘Home’ page. The victim needs to access the ‘www.vulnweb.com/myimages.php’ page where the malicious code exists, and the attacker’s information will be submitted to the target website (testphp.vulnweb.com/).</p>
<p><img src="https://scontent-iad3-1.xx.fbcdn.net/hphotos-xtp1/v/t1.0-9/12096362_10205241508092834_5917240479044934007_n.jpg?oh=90f3879cb5b04d225e0a1caaca4ed60f&oe=568ABEFE" alt="05"></p>
<p>As soon as the victim visits the myimages.php page, the hidden ‘iframe’ is loaded executing the CSRF attack. Below is the HTTP POST request which is made when the victim accesses the attacker’s malicious ‘/myimages.php’ page.<br><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">Host: testphp.vulnweb.com</span><br><span class="line">Connection: keep-alive</span><br><span class="line">Content-Length: 140</span><br><span class="line">Cache-Control: max-age=0</span><br><span class="line">Origin: http://www.vulnweb.com</span><br><span class="line">User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5</span><br><span class="line">Content-Type: application/x-www-form-urlencoded</span><br><span class="line">Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8</span><br><span class="line">Referer: http://www.vulnweb.com/updateif.php</span><br><span class="line">Accept-Encoding: gzip,deflate,sdch</span><br><span class="line">Accept-Language: en-GB,en-US;q=0.8,en;q=0.6</span><br><span class="line">Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3</span><br><span class="line">Cookie: login=acuart%2Facuart</span><br><span class="line"></span><br><span class="line">urname=h4xor&ucc=1111-2222-3333-4444&uemail=spam%40myspam.com&uphone=%2B800+666+666+666&uaddress=Hacking+the+universe%21%21%21&update=update</span><br></pre></td></tr></table></figure></p>
<p>The above HTTP POST request shows that the Host to which the POST request is sent is testphp.vulnweb.com but the origin is www.vulnweb.com with a referrer being the updateif.php page of the attackers website. Moreover, the Cookie information is included in the POST request which is the first requirement in order for the POST request to be authenticated and the CSRF to be realized. Finally, the parameters information is included in the POST request and will be submitted to the target website.</p>
<p>When the POST request is made the browser already has the authentication session for the target website and it includes the authentication details in the POST request as it should do in any other legitimate POST request made by the victim. This particular rogue POST request is exactly the same as a legitimate POST request with the same Host target but from a different origin. The browser, as normal, sends the POST request and the server (in this case) is not able to differentiate between a legitimate and rogue POST request since both are performed by the trusted browser in the same way.</p>
<p>There is not any information included in the POST request (such as a token value, seen later) which will help the server to validate a POST request as not malicious. This results in the server processing both POST requests in the normal way.</p>
<p><img src="https://scontent-iad3-1.xx.fbcdn.net/hphotos-xap1/v/t1.0-9/12105754_10205241508052833_1562714104470498791_n.jpg?oh=82b0a82b9f9b9769658c37a4df0ad6e8&oe=56C5C02D" alt="06"></p>
<p>From the above image you can notice that the profile information of the victim on the target website has been changed.</p>
<p>The rogue information was submitted and successfully updated the victim’s profile on the target website. The victim has no indication of what happened since this operation is transparent to the user. The attacker has completed his attack successfully in this scenario. In another scenario an attacker could change the admin passwords, perform illegal transactions, and more.</p>
<p>NOTE: The particular operation will be performed for any user that accesses the attacker’s website. In case the particular user has an account in the target website and is logged in, then the user’s profile information will be updated with the ones of the attacker. For this particular example, we assumed that there were no security measures in place that might block the CSRF attack.</p>
<h3 id="Prevention_Measures">Prevention Measures</h3><p>There are many suggested prevention measures that can be implemented to mitigate CSRF attacks. Some of them, though, are not complete solutions and leave room for the attack to still work. For example:<br>+The use of a secret cookie – This method will not work because all cookies related to the target website will be submitted as usual as in a normal (legitimate) HTTP request.<br>+Accept POST requests only – This suggestion falls short because attackers can deceive an end-user to submit a forged POST request unknowingly using social engineering methods.<br>+URL Rewriting – An incomplete solution since some session information is included or exposed in the URL.<br>Let’s take a look at some measures that do actually mitigate a CSRF attack.</p>
<h3 id="Use_of_Tokens">Use of Tokens</h3><p>A prevention measure could be the implementation and inclusion of tokens in a user’s (current) session. Tokens are long cryptographic values that are difficult to guess. These will be generated when a user’s session begins and will be associated with this particular user’s session. This challenge token will be included in each request, which will be used by the server side to verify the legitimacy of the end-user’s request.</p>
<p>In order for an attacker to forge a HTTP request, they would have to know the particular challenge value (token) of the victim’s session. The disclosure of the challenge token in the URL (GET requests) should be done wisely and with awareness of the CSRF attack.</p>
<p>Challenge tokens can be used in the ViewState option of the ASP.NET. Since it is possible for an attacker to obtain or guess the parameter values of a ViewState then the inclusion and use of a token can make the ViewState unique and protected to CSRF attacks.</p>
<p>Moreover, tokens can be used in the submission of double cookies. The server-side will generate a strong random value which will be included in the submitted cookie on the user’s machine. This will act as the session ID. On sending a POST request, the website will require the particular session ID to be included as a hidden value in the submission form and be included in the cookie as well. If the two values are the same, the POST request will be considered as valid and submitted successfully. Therefore, even if the attacker is able to include any value in the form, based on the same-origin policy, the attacker will not be able to retrieve or modify the token value in the cookie and launch a CSRF attack unless they manage to guess the session ID value.</p>
<h3 id="Other_Security_Measures">Other Security Measures</h3><p>Another prevention measure is the use of challenge-response options. Despite the fact that this measure affects the user experience, it can strongly defend against CSRF attacks.</p>
<p>Furthermore, users should be made aware of potential threats. For example, users should:<br>+Log out from web applications when they have finished using them.<br>+Use the web browser with safety – that means making sure not to save any login credentials on the web browser and using legitimate and secure browser extensions.<br>Finally, you should scan your website using a web vulnerability scanner to detect any Cross-Site Request Forgery vulnerabilities so you can fix them before they cause any issues.</p>
<p><img src="https://scontent-iad3-1.xx.fbcdn.net/hphotos-xlp1/v/t1.0-9/11181895_10205241508132835_8086169082623058490_n.jpg?oh=34ff23fe68b7fbd56eb7d0db0682ade3&oe=568E1E20" alt="07"></p>
<p>Acunetix Web Vulnerability Scanner will crawl your website, detect any CSRF vulnerabilities and alert you if any are found. It also provides you with a detailed description of weakness and the exact location your website is vulnerable, and gives you a detailed explanation on how to solve it. Make sure your website is secure by ‘downloading the trial version of Acunetix Web Vulnerability Scanner’.</p>
]]></content>
<summary type="html">
<![CDATA[<p>From: <a href="http://www.acunetix.com/websitesecurity/csrf-attacks/">http://www.acunetix.com/websitesecurity/csrf-attacks/</a></p>
<h2 id="CSRF_Attacks,_XSRF_or_Sea-Surf_–_What_They_Are_and_How_to_Defend_Against_Them">CSRF Attacks, XSRF or Sea-Surf – What They Are and How to Defend Against Them</h2><p>Cross-Site Request Forgery, or CSRF for short is a common and regular online attack. CSRF also goes by the acronym XSRF and the phrase “Sea-Surf”. CSRF attacks include a malicious exploit of a website in which a user will transmit malicious requests that the target website trusts without the user’s consent. In Cross-Site Scripting (XSS), the attacker exploits the trust a user has for a website, with CSRF on the other hand, the attacker exploits the trust a website has against a user’s browser.</p>
<p>Basically, an attacker will use CSRF to trick a victim into accessing a website or clicking a URL link that contains malicious or unauthorized requests. It is called ‘malicious’ since the CSRF attack will use the identity and privileges of the victim and impersonate them in order to perform any actions desired by the attacker, such as change form submission details, and launch purchases or payments for the attacker or a third-party account.<br>]]>
</summary>
<category term="Django" scheme="http://jssu.github.io/tags/Django/"/>
<category term="学习" scheme="http://jssu.github.io/categories/%E5%AD%A6%E4%B9%A0/"/>
</entry>
<entry>
<title><![CDATA[Python Dict]]></title>
<link href="http://jssu.github.io/2015/10/15/python_dict/"/>
<id>http://jssu.github.io/2015/10/15/python_dict/</id>
<published>2015-10-15T19:47:59.000Z</published>
<updated>2015-07-27T18:23:31.000Z</updated>
<content type="html"><![CDATA[<p>Python dict的定义方法:<br><figure class="highlight livecodeserver"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="operator">a</span> = dict(<span class="constant">one</span>=<span class="number">1</span>, <span class="constant">two</span>=<span class="number">2</span>, <span class="constant">three</span>=<span class="number">3</span>)</span><br><span class="line">b = {<span class="string">'one'</span>: <span class="number">1</span>, <span class="string">'two'</span>: <span class="number">2</span>, <span class="string">'three'</span>: <span class="number">3</span>}</span><br><span class="line">c = dict(zip([<span class="string">'one'</span>, <span class="string">'two'</span>, <span class="string">'three'</span>], [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>])) </span><br><span class="line">d = dict([(<span class="string">'two'</span>, <span class="number">2</span>), (<span class="string">'one'</span>, <span class="number">1</span>), (<span class="string">'three'</span>, <span class="number">3</span>)])</span><br><span class="line">e = dict({<span class="string">'three'</span>: <span class="number">3</span>, <span class="string">'one'</span>: <span class="number">1</span>, <span class="string">'two'</span>: <span class="number">2</span>})</span><br></pre></td></tr></table></figure></p>
]]></content>
<summary type="html">
<![CDATA[<p>Python dict的定义方法:<br><figure class="highlight livecodeserver"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span cla]]>
</summary>
<category term="Python" scheme="http://jssu.github.io/tags/Python/"/>
<category term="学习" scheme="http://jssu.github.io/categories/%E5%AD%A6%E4%B9%A0/"/>
</entry>
<entry>
<title><![CDATA[AngularNote]]></title>
<link href="http://jssu.github.io/2015/10/12/AngularJS/"/>
<id>http://jssu.github.io/2015/10/12/AngularJS/</id>
<published>2015-10-13T06:20:06.000Z</published>
<updated>2015-10-17T15:34:12.000Z</updated>
<content type="html"><![CDATA[<p>学习笔记:<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><div ng-app=<span class="string">"myApp"</span> ng-controller=<span class="string">"myCtrl"</span>></span><br><span class="line">名: <span class="xml"><span class="tag"><<span class="title">input</span> <span class="attribute">type</span>=<span class="value">"text"</span> <span class="attribute">ng-model</span>=<span class="value">"firstName"</span>></span><span class="tag"><<span class="title">br</span>></span></span><br><span class="line">名: <span class="tag"><<span class="title">input</span> <span class="attribute">type</span>=<span class="value">"text"</span> <span class="attribute">ng-model</span>=<span class="value">"lastName"</span>></span><span class="tag"><<span class="title">br</span>></span></span><br><span class="line"><span class="tag"><<span class="title">br</span>></span></span><br><span class="line">姓名: {{firstName + " " + lastName}}</span><br><span class="line"><span class="tag"></<span class="title">div</span>></span></span><br><span class="line"><span class="tag"><<span class="title">script</span>></span><span class="php"></span><br><span class="line"><span class="keyword">var</span> app = angular.module(<span class="string">'myApp'</span>, []);</span><br><span class="line">app.controller(<span class="string">'myCtrl'</span>, <span class="function"><span class="keyword">function</span><span class="params">(<span class="variable">$scope</span>)</span> </span>{</span><br><span class="line"> <span class="variable">$scope</span>.firstName = <span class="string">"John"</span>;</span><br><span class="line"> <span class="variable">$scope</span>.lastName = <span class="string">"Doe"</span>;</span><br><span class="line">});</span><br><span class="line"></span><span class="tag"></<span class="title">script</span>></span></span></span><br></pre></td></tr></table></figure></p>
<a id="more"></a>
<p>Note: The version and sequence with those CDNs.<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="title">script</span> <span class="attribute">src</span>=<span class="value">"https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"</span>></span><span class="undefined"></span><span class="tag"></<span class="title">script</span>></span></span><br><span class="line"><span class="tag"><<span class="title">script</span> <span class="attribute">type</span>=<span class="value">"text/javascript"</span> <span class="attribute">src</span>=<span class="value">"http://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"</span>></span><span class="undefined"></span><span class="tag"></<span class="title">script</span>></span></span><br><span class="line"><span class="tag"><<span class="title">script</span> <span class="attribute">type</span>=<span class="value">"text/javascript"</span> <span class="attribute">src</span>=<span class="value">"http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/jquery-ui.min.js"</span>></span><span class="undefined"></span><span class="tag"></<span class="title">script</span>></span></span><br></pre></td></tr></table></figure></p>
<p>EX Controller:<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">angular.module(<span class="string">'myApp'</span>, []).controller(<span class="string">'namesCtrl'</span>, <span class="function"><span class="keyword">function</span>(<span class="params">$scope</span>) </span>{</span><br><span class="line"> $scope.names = [</span><br><span class="line"> {name:<span class="string">'Jani'</span>,country:<span class="string">'Norway'</span>},</span><br><span class="line"> {name:<span class="string">'Hege'</span>,country:<span class="string">'Sweden'</span>},</span><br><span class="line"> {name:<span class="string">'Kai'</span>,country:<span class="string">'Denmark'</span>}</span><br><span class="line"> ];</span><br><span class="line">});</span><br></pre></td></tr></table></figure></p>
<p>上面文件叫namesController.js<br><figure class="highlight handlebars"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="xml"><span class="tag"><<span class="title">div</span> <span class="attribute">ng-app</span>=<span class="value">"myApp"</span> <span class="attribute">ng-controller</span>=<span class="value">"namesCtrl"</span>></span></span><br><span class="line"><span class="tag"><<span class="title">ul</span>></span></span><br><span class="line"> <span class="tag"><<span class="title">li</span> <span class="attribute">ng-repeat</span>=<span class="value">"x in names"</span>></span></span><br><span class="line"> </span><span class="expression">{{ <span class="variable">x.name</span> + ', ' + <span class="variable">x.country</span> }}</span><span class="xml"></span><br><span class="line"> <span class="tag"></<span class="title">li</span>></span></span><br><span class="line"><span class="tag"></<span class="title">ul</span>></span></span><br><span class="line"><span class="tag"></<span class="title">div</span>></span></span><br><span class="line"><span class="tag"><<span class="title">script</span> <span class="attribute">src</span>=<span class="value">"namesController.js"</span>></span><span class="undefined"></span><span class="tag"></<span class="title">script</span>></span></span></span><br></pre></td></tr></table></figure></p>
<p>AngularJS connect to Database:</p>
<p>Single Page Applications can generally be said to be entirely client-side, that is, the HTML that is displayed to the end-user is generated on the client itself through Javascript.</p>
<p>When you want to work with a shared back-end database server you would write a web-service (presumably using a RESTful JSON endpoint, as that’s what all the cool kids are using thesedays, no love for XML and SOAP :/ ) and you would then use XmlHttpRequest to make requests to this webservice which then performs the CRUD database operations for you.</p>
<p>自己在前端开发中还有很多新知识要学习,今天开始进入《用 AngularJS 开发下一代 Web 应用》一书的学习之旅。</p>
<p>AngularJS是一款开源 JavaScript函式库,由Google维护, 主要用于单页面应用开发。 目标是增强基于浏览器的应用,并带有MVC模式功能,这为了使得开发和测试变得更加容易。</p>
<p>一些概念<br>客户端模板</p>
<p>传统的 web 应用会在服务器中创建 HTML,然后和数据生成页面一起发送到浏览器中。 而现在单页面应用则会把模板存放在客户端中,而服务器则是仅仅为这些模板提供静态资源和数据。</p>
<p>有种方法:<br><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable">$scope</span>.addBook = <span class="function"><span class="keyword">function</span><span class="params">()</span></span><br><span class="line"></span>{</span><br><span class="line">http({</span><br><span class="line"></span><br><span class="line">method : <span class="string">'POST'</span>,</span><br><span class="line">url: <span class="string">'index.php?urls'</span>,</span><br><span class="line">data: {book_name : <span class="variable">$scope</span>.bookName, book_price : <span class="variable">$scope</span>.bookPrice},</span><br><span class="line">headers : {<span class="string">'Content-type'</span> : <span class="string">'application/json'</span>} </span><br><span class="line">}).success(<span class="function"><span class="keyword">function</span><span class="params">(data, status, headers, config)</span></span>{</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span>(data.success)</span><br><span class="line">{ </span><br><span class="line"><span class="variable">$scope</span>.books.push(data); </span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line">}).error(<span class="function"><span class="keyword">function</span><span class="params">(data, status, headers, config)</span></span>{</span><br><span class="line"></span><br><span class="line"><span class="comment">//set error message.</span></span><br><span class="line"></span><br><span class="line">});</span><br><span class="line"></span><br><span class="line">}</span><br></pre></td></tr></table></figure></p>
<p>addBook() is one of the method in view site. The addBook() method call to controller site.</p>
<figure class="highlight stylus"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">ex:</span><br><span class="line"><<span class="tag">button</span> ng-click = <span class="string">"addBook()"</span> >Add book </button></span><br><span class="line">book_name, book_price -> books <span class="tag">table</span> column names.</span><br><span class="line"><span class="variable">$scope</span><span class="class">.bookName</span> , <span class="variable">$scope</span><span class="class">.bookPrice</span> -> <span class="tag">html</span> textbox ngmodel name. </span><br><span class="line"></span><br><span class="line">ex:</span><br><span class="line"><<span class="tag">input</span> type=<span class="string">"text"</span> ng-model=<span class="string">"bookName"</span>></span><br><span class="line"><<span class="tag">input</span> type=<span class="string">"text"</span> ng-model=<span class="string">"bookPrice"</span>></span><br><span class="line">Touch with me! Gain more!</span><br></pre></td></tr></table></figure>
<figure class="highlight handlebars"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="xml">hello world:</span><br><span class="line"></span><br><span class="line"><span class="doctype"><!doctype html></span></span><br><span class="line"><span class="tag"><<span class="title">html</span> <span class="attribute">lang</span>=<span class="value">"en"</span> <span class="attribute">ng-app</span>></span></span><br><span class="line"><span class="tag"><<span class="title">head</span>></span></span><br><span class="line"> <span class="tag"><<span class="title">meta</span> <span class="attribute">charset</span>=<span class="value">"UTF-8"</span> /></span></span><br><span class="line"> <span class="tag"><<span class="title">title</span>></span>hello<span class="tag"></<span class="title">title</span>></span></span><br><span class="line"> <span class="tag"><<span class="title">script</span> <span class="attribute">src</span>=<span class="value">"assets/angular.min.js"</span>></span><span class="undefined"></span><span class="tag"></<span class="title">script</span>></span></span><br><span class="line"><span class="tag"></<span class="title">head</span>></span></span><br><span class="line"><span class="tag"><<span class="title">body</span>></span></span><br><span class="line"> <span class="tag"><<span class="title">input</span> <span class="attribute">type</span>=<span class="value">"text"</span> <span class="attribute">ng-model</span>=<span class="value">"name"</span> /></span></span><br><span class="line"> <span class="tag"><<span class="title">h2</span>></span>Hello </span><span class="expression">{{<span class="variable">name</span>}}</span><span class="xml">!<span class="tag"></<span class="title">h2</span>></span></span><br><span class="line"><span class="tag"></<span class="title">body</span>></span></span><br><span class="line"><span class="tag"></<span class="title">html</span>></span></span></span><br></pre></td></tr></table></figure>
<p>从 hello world 中可以看到:</p>
<p>html 里没有 class 或者 ID 来标记哪里添加事件。<br>数据绑定,改变数据页面也会随之改变。<br>Model View Controller (MVC)</p>
<p>MVC 的核心理念:把管理数据的代码(model)、应用逻辑代码(controller)和展示数据的代码(view)清晰的分离开。</p>
<p>AngularJS 中,视图是 Document Object Model(DOM,文档对象模型),控制器为 JavaScript 类,模型数据被存储在对象的属性中。</p>
<p>数据绑定</p>
<p>使用 jQuery 开发,我们会把获取的数据插入到 DOM 中,或者直接操作 DOM 的内容。</p>
<p>而在 AngularJS 中,仅仅声明 UI 中的 DOM 映射到某个 JavaScript 属性,然后修改 JavaScript 属性,就可以自动更新 DOM 中的值。</p>
<p>依赖注入</p>
<p>开发风格:类只是简单的获取所需要的东西,并不需要创建所需要的东西。这种风格叫迪米特法则,也叫最小知识原则。</p>
<p>例如:$scope 对象表示 Controller 模型中的内部状态;$location 对象表示浏览器 URL 地址状态。</p>
<p>指令</p>
<p>AngularJS 引入了强大的 DOM 转换引擎,用来扩展 HTML 语法。</p>
<p>如:<br><figure class="highlight django"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="xml"></span><span class="variable">{{}}</span><span class="xml">:表示数据绑定</span><br><span class="line">ng-controller:指定控制器范围</span><br><span class="line">ng-model:把输入数据绑定到模型的属性上</span></span><br></pre></td></tr></table></figure></p>
]]></content>
<summary type="html">
<![CDATA[<p>学习笔记:<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><div ng-app=<span class="string">"myApp"</span> ng-controller=<span class="string">"myCtrl"</span>></span><br><span class="line">名: <span class="xml"><span class="tag"><<span class="title">input</span> <span class="attribute">type</span>=<span class="value">"text"</span> <span class="attribute">ng-model</span>=<span class="value">"firstName"</span>></span><span class="tag"><<span class="title">br</span>></span></span><br><span class="line">名: <span class="tag"><<span class="title">input</span> <span class="attribute">type</span>=<span class="value">"text"</span> <span class="attribute">ng-model</span>=<span class="value">"lastName"</span>></span><span class="tag"><<span class="title">br</span>></span></span><br><span class="line"><span class="tag"><<span class="title">br</span>></span></span><br><span class="line">姓名: {{firstName + " " + lastName}}</span><br><span class="line"><span class="tag"></<span class="title">div</span>></span></span><br><span class="line"><span class="tag"><<span class="title">script</span>></span><span class="php"></span><br><span class="line"><span class="keyword">var</span> app = angular.module(<span class="string">'myApp'</span>, []);</span><br><span class="line">app.controller(<span class="string">'myCtrl'</span>, <span class="function"><span class="keyword">function</span><span class="params">(<span class="variable">$scope</span>)</span> </span>{</span><br><span class="line"> <span class="variable">$scope</span>.firstName = <span class="string">"John"</span>;</span><br><span class="line"> <span class="variable">$scope</span>.lastName = <span class="string">"Doe"</span>;</span><br><span class="line">});</span><br><span class="line"></span><span class="tag"></<span class="title">script</span>></span></span></span><br></pre></td></tr></table></figure></p>]]>
</summary>
<category term="前端" scheme="http://jssu.github.io/tags/%E5%89%8D%E7%AB%AF/"/>
<category term="学习" scheme="http://jssu.github.io/categories/%E5%AD%A6%E4%B9%A0/"/>
</entry>
<entry>
<title><![CDATA[Word Breaker II]]></title>
<link href="http://jssu.github.io/2015/08/09/wordbreakerii/"/>
<id>http://jssu.github.io/2015/08/09/wordbreakerii/</id>
<published>2015-08-09T23:07:05.000Z</published>
<updated>2015-08-09T16:00:41.000Z</updated>
<content type="html"><![CDATA[<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="preprocessor">#<span class="keyword">include</span> <iostream></span></span><br><span class="line"><span class="preprocessor">#<span class="keyword">include</span> <string></span></span><br><span class="line"><span class="preprocessor">#<span class="keyword">include</span> <vector></span></span><br><span class="line"><span class="preprocessor">#<span class="keyword">include</span> <stack></span></span><br><span class="line"><span class="preprocessor">#<span class="keyword">include</span> <unordered_set></span></span><br><span class="line"><span class="preprocessor">#<span class="keyword">include</span> <unordered_map></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> Solution{</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line"> <span class="built_in">unordered_map</span><<span class="built_in">string</span>, <span class="built_in">vector</span><<span class="built_in">string</span>>> m;</span><br><span class="line"> <span class="built_in">vector</span><<span class="built_in">string</span>> combineword(<span class="built_in">string</span> word, <span class="built_in">vector</span><<span class="built_in">string</span>> pre)</span><br><span class="line"> { </span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i<pre.size(); i++)</span><br><span class="line"> {</span><br><span class="line"> pre[i]+=<span class="string">" "</span>+word; </span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> pre;</span><br><span class="line"> }</span><br></pre></td></tr></table></figure>
<a id="more"></a>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span>:</span><br><span class="line"> <span class="built_in">vector</span><<span class="built_in">string</span>> wordBreak(<span class="built_in">string</span> s, <span class="built_in">unordered_set</span><<span class="built_in">string</span>>& dict)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(m.count(s))</span><br><span class="line"> <span class="keyword">return</span> m[s];</span><br><span class="line"> <span class="built_in">vector</span><<span class="built_in">string</span>> res;</span><br><span class="line"> <span class="keyword">if</span>(dict.count(s))</span><br><span class="line"> res.push_back(s);</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>; i<s.size(); i++)</span><br><span class="line"> {</span><br><span class="line"> <span class="built_in">string</span> word=s.substr(i);</span><br><span class="line"> <span class="keyword">if</span>(dict.count(word))</span><br><span class="line"> {</span><br><span class="line"> <span class="built_in">string</span> rem=s.substr(<span class="number">0</span>,i);</span><br><span class="line"> <span class="built_in">vector</span><<span class="built_in">string</span>> pre=combineword(word, wordBreak(rem,dict));</span><br><span class="line"> res.insert(res.end(), pre.begin(), pre.end());</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> m[s]=res;</span><br><span class="line"> <span class="keyword">return</span> res;</span><br><span class="line"> }</span><br><span class="line"> <span class="function"><span class="keyword">void</span> <span class="title">print_out_m</span><span class="params">()</span></span><br><span class="line"> </span>{</span><br><span class="line"> <span class="comment">//unordered_map<string, vector<string>>::iterator mit=m.begin();</span></span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">auto</span> x: m)</span><br><span class="line"> {</span><br><span class="line"> <span class="built_in">cout</span><<x.first<<endl;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">auto</span> y:x.second)</span><br><span class="line"> {</span><br><span class="line"> <span class="built_in">cout</span><<<span class="string">" "</span><<y<<endl;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span><br><span class="line"></span>{</span><br><span class="line"> <span class="built_in">string</span> s =<span class="string">"ilikefacebooks"</span>;</span><br><span class="line"> <span class="built_in">unordered_set</span><<span class="built_in">string</span>> dict({<span class="string">"i"</span>,<span class="string">"like"</span>,<span class="string">"face"</span>,<span class="string">"book"</span>,<span class="string">"s"</span>,<span class="string">"facebook"</span>});</span><br><span class="line"> Solution sol;</span><br><span class="line"> <span class="built_in">vector</span><<span class="built_in">string</span>> res=sol.wordBreak(s,dict);</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">auto</span> x:res)</span><br><span class="line"> {</span><br><span class="line"> <span class="built_in">cout</span><<x<<endl;</span><br><span class="line"> }</span><br><span class="line"> sol.print_out_m();</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>]]></content>
<summary type="html">
<![CDATA[<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="preprocessor">#<span class="keyword">include</span> <iostream></span></span><br><span class="line"><span class="preprocessor">#<span class="keyword">include</span> <string></span></span><br><span class="line"><span class="preprocessor">#<span class="keyword">include</span> <vector></span></span><br><span class="line"><span class="preprocessor">#<span class="keyword">include</span> <stack></span></span><br><span class="line"><span class="preprocessor">#<span class="keyword">include</span> <unordered_set></span></span><br><span class="line"><span class="preprocessor">#<span class="keyword">include</span> <unordered_map></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> Solution{</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line"> <span class="built_in">unordered_map</span><<span class="built_in">string</span>, <span class="built_in">vector</span><<span class="built_in">string</span>>> m;</span><br><span class="line"> <span class="built_in">vector</span><<span class="built_in">string</span>> combineword(<span class="built_in">string</span> word, <span class="built_in">vector</span><<span class="built_in">string</span>> pre)</span><br><span class="line"> { </span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i<pre.size(); i++)</span><br><span class="line"> {</span><br><span class="line"> pre[i]+=<span class="string">" "</span>+word; </span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> pre;</span><br><span class="line"> }</span><br></pre></td></tr></table></figure>]]>
</summary>
<category term="LeetCode" scheme="http://jssu.github.io/tags/LeetCode/"/>
<category term="学习" scheme="http://jssu.github.io/categories/%E5%AD%A6%E4%B9%A0/"/>
</entry>
<entry>
<title><![CDATA[How to commit in git]]></title>
<link href="http://jssu.github.io/2015/08/01/git_commit/"/>
<id>http://jssu.github.io/2015/08/01/git_commit/</id>
<published>2015-08-02T01:40:53.000Z</published>
<updated>2015-08-01T18:18:28.000Z</updated>
<content type="html"><![CDATA[<figure class="highlight nginx"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="title">git</span> init</span><br></pre></td></tr></table></figure>
<a id="more"></a>
<figure class="highlight vhdl"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">git add .</span><br><span class="line"></span><br><span class="line"># Adds the files <span class="keyword">in</span> the local repository <span class="keyword">and</span> stages them <span class="keyword">for</span> commit. <span class="keyword">To</span> unstage a <span class="keyword">file</span>, <span class="keyword">use</span> <span class="attribute">'git</span> reset HEAD YOUR-<span class="keyword">FILE</span>'.</span><br></pre></td></tr></table></figure>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">git <span class="operator"><span class="keyword">commit</span> -m <span class="string">'First commit'</span></span><br><span class="line"># Commits the tracked changes <span class="keyword">and</span> prepares them <span class="keyword">to</span> be pushed <span class="keyword">to</span> a remote repository. <span class="keyword">To</span> remove this <span class="keyword">commit</span> <span class="keyword">and</span> modify the file, <span class="keyword">use</span> <span class="string">'git reset --soft HEAD~1'</span> <span class="keyword">and</span> <span class="keyword">commit</span> <span class="keyword">and</span> <span class="keyword">add</span> the file again.</span></span><br></pre></td></tr></table></figure>
<figure class="highlight livecodeserver"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">git remote <span class="built_in">add</span> origin remote repository <span class="built_in">URL</span></span><br><span class="line"><span class="comment"># Sets the new remote</span></span><br><span class="line">git remote -v</span><br><span class="line"><span class="comment"># Verifies the new remote URL</span></span><br></pre></td></tr></table></figure>
<figure class="highlight nginx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="title">git</span> push origin master</span><br><span class="line"><span class="comment"># Pushes the changes in your local repository up to the remote repository you specified as the origin</span></span><br></pre></td></tr></table></figure>]]></content>
<summary type="html">
<![CDATA[<figure class="highlight nginx"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="title">git</span> init</span><br></pre></td></tr></table></figure>]]>
</summary>
<category term="Github" scheme="http://jssu.github.io/tags/Github/"/>
<category term="学习" scheme="http://jssu.github.io/categories/%E5%AD%A6%E4%B9%A0/"/>
</entry>
<entry>
<title><![CDATA[傲慢-狗镇]]></title>
<link href="http://jssu.github.io/2015/07/16/%E7%8B%97%E9%95%87/"/>
<id>http://jssu.github.io/2015/07/16/狗镇/</id>
<published>2015-07-17T01:40:53.000Z</published>
<updated>2015-07-17T00:41:24.000Z</updated>
<content type="html"><![CDATA[<p>这是一个真正的恐怖片。</p>
<p>它的恐怖之处在于,它是一个锐利的、寒光闪闪的追问。在这个追问之下,我们自以为是的道德变得弱不经风:如果你是狗镇人,你会对一个无辜者施暴吗?再假如,暴力是民主化的,是被允许的,安全的、正当的,没有后顾之忧的,你也能从中获得某种满足的,满城皆如是、无人是例外的,你还敢信心满满地说不么?</p>
<p>在看完《狗镇》之后,我长久地处于一种颤栗状态,是真实的肉体颤栗,而非修辞。它不由分说地,把我推到人性的法庭,拷问我的良知,审判我的德行,以至于后来,它们统统站不住脚,变成了可疑的存在。</p>
<p>我会吗?我敢和整个镇子唱反调吗?</p>
<p>我无法自信。</p>
<p>也许会?也许不会?又也许我只会冷眼旁观,不跟从恶行,也不违良知,远离集体,明哲保身。但,沉默何尝不是另一种认可,冷暴力何尝不是另一种形式的暴力。</p>
<a id="more"></a>
<p>1,史上最令人发指的故事</p>
<p>“我只看到崇山峻岭中的一个美丽村落,大家面对艰苦环境,还是有梦有希望,还有七个一点儿也不丑的小瓷人。”</p>
<p>格蕾丝出现以前,我们看到的狗镇,堪称世外桃源。它遗世独立,土地平旷,屋舍俨然,有良田美池桑竹之属,在某种约定俗成的律令下,人们谨言慎行,相敬如宾,同时也不辞辛苦地耕种,没有任何旁逸斜出的矛盾,一如电脑程序般秩序井然。</p>
<p>“他们都是好人……”</p>
<p>“他们都是好孩子……”</p>
<p>他们的话语勾勒出一个人性的天堂——所有人都是好人,所有孩子都是好孩子,善良在这里一统天下,人性劣迹无处寻觅,贪婪与邪恶无处容身。</p>
<p>然而,洛基山脚这帮农民们真是如此天真吗?</p>
<p>测验总是突然到来。</p>
<p>作为一种矛盾的诱因,格蕾丝出现了。她伴着隐约的枪声到来,打破狗镇的平衡与寂静。这声枪响代表了狗镇人对她的理解:隐约的威胁,外来的异端。所以当她走入狗镇,百计千方地试图融入其中的时候,他们的善意一直有所保留。</p>
<p>经过商量后,他们决定将格蕾丝留镇查看两周,以观后效。为了讨好他们,格蕾丝去每户人家帮忙干活,打理土地,陪伴老人,为孩子做家教……两周之后,他们答应下来,让这个陌生女子留在镇中。</p>
<p>电影看到这里,我几乎要以为,格蕾丝最终会与这个群体打成一片,嫁给汤姆,成为狗镇的一员,从此以后,过上幸福美满的生活,拥有一个童话式的收梢。</p>
<p>但狗镇是一个斯芬克斯式的秘密,美人的容颜,兽的身心,它充满诱惑,让人陷入、迷乱和恐惧。谁将它解开,它就在谁眼前狼狈而死;谁无力看穿,谁就变成斯芬克斯的猎物!</p>
<p>“狗镇已经骗过你了吗?这个村子从里烂到外,就算它堕崖我也无动于衷,我看不到它的魅力……”</p>
<p>在狗镇人对格蕾丝集体施虐之前,有一个人,一个同样来自都市,和格蕾丝有着相似追求和背景,但后来第一个强暴她的人——查克——提前预告了悲剧。</p>
<p>他说:“你爱上狗镇了,树木、高山、纯朴的人,再不然那些肉桂也能骗过你,那些醋栗派里的肉桂。狗镇完全符合都市人的梦想……”</p>
<p>“你也来自都市对不对?”</p>
<p>“那是很久以前的事了。我没那么笨了,人都一样贪婪。只是乡下人比较失败,有的吃就把肚子撑破……”</p>
<p>可是,格蕾丝不相信这个人人都循规蹈矩、有信仰、有民主、有希望的地方会是一个阿鼻地狱,尤其是同意她留下来的钟声响起15声之后,她觉得,这就是她的乌托邦,是她向往的彼岸,是与她父亲的黑社会相对应的一种天堂,是贫穷但其乐融融的伊甸园。她将在这里修行,获知善良的人性密码。</p>
<p>但她的天真愿望与谨慎的为人处世,并没有为她赢得永恒的宁静。</p>
<p>当通辑令又一次贴到了狗镇,警察来盘查,人们觉得格蕾丝是一种危险,她应该付出更多,才能与收留她的风险持平。她开始做更多的苦活,这一次,大家再也没有客套,把脏活累活全交给她。这个美丽善良如圣女的女孩,开始替整个狗镇人承担,受苦,逐渐肮脏。</p>
<p>但人性的贪婪,从来就没有满足之时。</p>
<p>只要你有所求,受制于人是难免的事。格蕾丝的危境,也成为格蕾丝最大的软肋。果然,当警察再次来临的时候,她被查克要胁,要么别出声,要么他出声。</p>
<p>她沉默下来,被查克强暴。</p>
<p>罪恶都不是忽然到来的,它一点一点地发生,试探着,犹疑着,欲行又止,如果受害者沉默,它就被那点软弱鼓励着,无所顾忌地为非作歹、兴风作浪。</p>
<p>格蕾丝无力反抗。于是她悲剧的序曲拉开,噩运正式上演。</p>
<p>她成为勾引有妇之夫的荡妇,成了婊子,成为破鞋,成为贱货,她被查克老婆带着人痛打,当着她的面,摔碎7个至爱的瓷娃娃。</p>
<p>“如果你能忍着不哭,我就给你留下一二个。”</p>
<p>瓷娃娃破碎的时候,格蕾丝对狗镇人脆弱、单薄及易逝的信任,也跟着四分五裂了。她和汤姆一起合谋,要搭本的货车逃离狗镇。但车子行到半路,本停了下来,他阻拦在她逃离的前方,开始勒索,要钱,要么要她的身体。</p>
<p>她没有钱。卡车司机扯开她的衣服,在她的肉体上运动。</p>
<p>这是一个俯拍的镜头,脏污的本蠕动着,而格蕾丝一脸木然,看不出悲喜。关于卡车司机行暴的电影片断,我印象极深的有两个,一个是《雾中风景》,一个是《狗镇》。女主角都压抑而隐忍,但每一个都让我触目惊心。在纯真的人们奔往希望的途中,恶用最粗暴的方式,把她们践踏,告诉她们残酷的现实。</p>
<p>她第一次被强奸,是为了留下。</p>
<p>第二次被强奸,是为了离开。</p>
<p>目的的不同,代表了狗镇的性质的变化。从前是乐园,现在是地狱。但悲剧没有完。当车子停下的时候,她从苹果堆中爬出,又听到熟悉的狗叫声,狗镇的15位居民站在车边,等着这一个逃离的罪人,开始集体对她进行迫害。</p>
<p>为了防止格蕾丝的逃跑,他们打造了一个沉重的铸铁车轮,像一个狗链,拴在她的脖子上。他们把她当成狗,一只没有尊严、没有人格,甚至没有意识的狗。她在榆树街上往返的时候,必须拚尽全力,才能将那个巨大的桎梏移动半步。</p>
<p>她成为狗镇的囚徒,也成为狗镇的公共妓女。所有男人晚上都会爬到她的阁楼去,拉下裤子,发泄兽欲,他们沉默地动作着,完事后提起裤子离开,一句话都不会多讲。后来,狗镇盲眼的老人、十来岁的幼童,也以猥亵她为乐。</p>
<p>格蕾丝不是Vasumitra,强暴过她的男人没有变成虔诚的佛教徒,而是愈加麻木地作恶,往着无底线的深渊,步步紧驱。</p>
<p>汤姆也想占有他,可是,他的“爱情”阻止他无底线地纵欲。但他不甘心,觉得自己的权利没有被满足,想置格蕾丝于死地。他举报了她。当黑色小轿车再次来到狗镇的时候,格蕾丝见到了自己的父亲,一个恶的化身,一个杀人不眨眼的权力在握者。</p>
<p>父女二人久别重逢,但此时的格蕾丝,已经把人性之诡谲、现实之炎凉统统阅尽了。</p>
<p>“你说我傲慢……”</p>
<p>“你视掠夺为天经地义!”</p>
<p>“我就是讨厌你这点,你才是傲慢!”</p>
<p>“我原谅别人,所以我傲慢?”</p>
<p>“人犯了错,你必须惩罚、指责他们。你不惩罚,是不给他们知错的机会;你原谅他们,是因为你自以为你的道德高于他人,这就是傲慢!”</p>
<p>来到狗镇之前,她一直在行善积德,克服私心,自诩“慈悲为怀”,原谅强暴犯和杀人犯,宽恕猪狗不如的行径。因为在基督教中,一个罪人和一个修道士,在上帝面前都是平等的,信仰的超越性要求宽容一切罪恶。</p>
<p>但当她亲历所有罪恶,她又是怎么样的呢?</p>
<p>“如果我答应重作你的女儿,我何时才能得到权势?”</p>
<p>“现在。”</p>
<p>“好,那我不想再见到这个狗镇”。</p>
<p>这才是真相。</p>
<p>当血洗狗镇的命令从她的嘴巴中发出,格蕾丝的伪善土崩瓦解,她不再是受苦的、承担人间所有罪孽的圣徒,她成了另一个恶毒的狗镇人。</p>
<p>而狗镇人,则成了另一群无助的格蕾丝。</p>
<p>她用变本加厉的方式,发泄内心的怨恨。她要赶尽杀绝,斩草除根,任何生灵都不放过,老人、妇女、小孩,都成为陪葬品。狗镇的人们惊恐地叫喊着,在格蕾丝的怒火中一个接一个死去。善在恶中消失,恶在更强大的恶中泯灭,最大的恶因为无懈可击,变成权力,甚至神。</p>
<p>“有个家庭有好几个小孩,如果当着她(维拉)的面杀她2个孩子,她能忍住不哭,那就停止……她欠我的!”</p>
<p>最后,所有人都杀光了,只剩下汤姆,这个自称爱她的男人。格蕾丝提着枪走出车厢,指着他的后脑,了结他的性命。她亲自染指罪孽,而且是从最爱的人开始,人间最美好的东西,真、善、美,甚至爱,都成了培养恶的祭品。</p>
<p>从此以后,格蕾丝成为无情而狠毒的人,她终于可以接手那个庞大的罪恶帝国,成为优秀的继承者,成为自己当初最嫌弃的人。</p>
<p>最后,狗镇只剩下一只狂吠的小狗,再无人幸存——犬兽当道,人何存焉?</p>
<p>当一切情节都已经脱落,真相水落石出。我们才惊觉,这就是一个谎言充斥的世界,这就是一个谎言充斥的村庄。</p>
<p>狗镇不是镇。</p>
<p>榆树街没有树。</p>
<p>好人不是好人,好孩子不是好孩子。</p>
<p>而Grace不是德行,不是慈悲,更不是恩典和救赎。</p>
<p>2,无处不在的狗镇</p>
<p>虚空的中央,忽然出现了一个舞台,人迹屑屑,光影憧憧,地板上用粉笔画着街道和房屋,天空是一块毛叽叽的布,一切都是抽象的,虚拟的,没有地域,没有门窗,没有墙垣,没有山河日月,也没有花草树木、鸟兽鱼虫。</p>
<p>因为是舞台剧,拉斯·冯·提尔摈弃多余的道具,布景精简到了极至,一切都是象征性的。所以,《狗镇》成为一个寓言,失去一切具体限制,变得无往不利,无孔不入,无处不在。</p>
<p>这是极简主义的又一个试验品。</p>
<p>它拆解了空间的约束,切断了对环境的依赖,让故事直击本质。</p>
<p>拉斯·冯·提尔说,“这种技术就像心理放大镜,更贴近人物”,他相信这种叙述方式能够强化观众的观影体验,缩短观众和电影叙事间的距离,使批判与意义,在一片模糊的背景之中跃出,独特、鲜明、入木三分。</p>
<p>房屋是假的,街道是假的,狗镇是假的,美德与恩典是假的,宽恕与赦免是假的,民主与人权是假的,一切皆是浮光幻影,当不得真的。但一切又都是真的。善良与美的牺牲是真的,傲慢与恶是真的,摧毁与绝望是真的,指桑骂槐的影射是真的。</p>
<p>就这点真的东西,它所向披靡,像针尖一样,直戳我们不敢直视的痛处;像水一样,曲径通幽地渗入人性的罅隙;像蚂蚁一样,能动摇色厉内荏的外部建设;像病毒一样,能在任何繁华或蛮荒的地方落地生根。</p>
<p>无论美国,还是中国,无论城市,或者乡村,无论是公共场所,还是你的家,都不是狗镇遗毒的避风港。它可能在下一秒就在你身边粉墨登场,演员是任何人——你父母可以是狗镇人,你的孩子可以是格蕾丝,你爱人可以是汤姆,你可以是一条狗。</p>
<p>就在上学年,我所任教的班级里,发生过一起全班男生集体欺负一个人的事件。被欺负的男孩叫王天林,他是留守儿童,被托付给一个老师看管,生得高大,脏兮兮的,曾经翻过厕所的围墙,去偷看女生。也曾经为了满足周围男生的要求,在课堂上解开裤子打手枪,涂在手上给人看,加上又嗜好阴阳怪气地嘲弄他人,因此许多孩子有些厌恶他。</p>
<p>不知从哪天起,我就看到他三天两头血浸浸地来上课。有一回晚自习,他忽然站起来,仰着头,捏着鼻子,鼻孔边蜿蜒着两道血流,衣领上是星星点点的红斑。</p>
<p>他说:老师,我流血了。</p>
<p>我赶紧把他领出去,去清理和看医生。出去之后,发现他的嘴唇都往外翻了出来,像一条被折磨过的肉虫。</p>
<p>问他怎么回事,不愿说,只是说没事。我坚持着,要他告诉我实情,终于,他说了原委:全班男生都团结起来对付他,他经常被打,频率极高,有时一周竟高达三次。有几回,他们在教室里围殴他,无缘无故的,只因为他们认为他贱。</p>
<p>他不敢反抗,只是恶狠狠地说:等我不读书了,我就叫人把他们全都打残来!</p>
<p>至此,我才惊觉,就在此地,就在当下,《狗镇》正在我的身边发生,它改头换面,取名为《校园》。</p>
<p>然而,哪里才是净土呢?</p>
<p>“格蕾丝,听说你在这里有麻烦?”父亲说。</p>
<p>“家里才最麻烦!”</p>
<p>这无疑也成了譬喻,它暗示着我们所目睹的,并非恶的最高级,更令人惊骇的,还在狗镇之外,在这个空荡荡的舞台的他方。</p>
<p>3,恶的传承性</p>
<p>《在缅甸寻找乔治欧威尔》这本书里,曾提到一个缅甸传说。</p>
<p>有一个村庄,出没着一条恶龙,它神通广大,但无恶不作,每年要求村庄献祭一个处女,每年这个村庄都会有一个英雄去与恶龙搏斗,但无人生还。又一个少年英雄出发时,有人悄悄尾随。英雄用剑刺死恶龙,然后坐在尸身上,看着龙穴内闪烁的珠宝,慢慢地长出鳞片、尾巴和触角,最终变成恶龙。</p>
<p>当一种力量大到无穷,他者无法与之抗衡,成为极权,都是极度危险的事。</p>
<p>因为他可以决定他人的命运,这个村庄、这个世界,都成了他的人性的试验场。在他的翻覆无常中,世界有如惊弓之鸟,动荡不安。</p>
<p>在这个故事里,少年英雄正义、勇敢、无私,但纵然是这样一个毫无破绽的人,也有作恶的潜质。毕竟人非圣贤,总有卑劣之心。而绝对权力的拥有,又为他提供了作恶的条件。</p>
<p>于是,他的恶念被唤醒、放出、张牙舞爪、呼风唤雨,然后,他变成另一条恶龙。</p>
<p>格蕾丝和少年英雄一样,在故事开始,她也是一个圣徒。但在狗镇这个人性课堂,恶通过施恶的过程,一点一点地教授给了受害者。</p>
<p>当格雷丝得到权力,她的角色大逆转,原本酣眠于心的恶像苏醒的兽,张开嗜血的眼睛。她发出血洗狗镇的命令,用她刚刚学会的残忍方式,连本带息地回报她的“老师们”,成为更大的作恶者。</p>
<p>此时我们才惊觉,格蕾丝其实就是一个没有狗镇户口的狗镇人。</p>
<p>“人之性恶,其善者伪也。”</p>
<p>我是一个人性的悲观论者,一直禀持人性恶的理念。在我看来,恶一直存在,只是缘于道德和伦理的规范,法律和警察的威慑而没有泛滥成灾。所谓好人,不是因为通体无瑕,而是十分清楚自己的缺陷而自我警惕,用良知自我教化,用美德自我加持,从而做到人畜无害。</p>
<p>在日常生活中,我们都在心中编了一个笼子,看管着那头恶之兽。但在某些不必担责之时,这些笼门自动开启,恶念集体复苏,对弱者进行凌辱和杀戮。</p>
<p>但我又忍不住作出一个假设。</p>
<p>在故事之初,格蕾丝是纯真的,从善如流,排斥杀戮,宁愿远走他乡,也不愿意接受父亲的权力。你可以说她伪善,但伪善一生,之于他人而言,也就是真正的善了。但到了狗镇,当她一点点见识了人性之恶,亲历种种不公时,她终于有了疯狂的报复欲。</p>
<p>假如狗镇没有发生这一切,假如狗镇的法律可以确保每个人的人权,作恶者的行为得到一定的抑制,那么,格蕾丝式的悲剧是不是就会少一些发生。</p>
<p>“狗只能遵从天性,为什么不该原谅他们?”</p>
<p>“狗可以学会很多事。但每次原谅他们就不行了。”</p>
<p>以暴制暴,用惩罚来提醒,用鞭鞑来告诫,甚至,用死亡来洗刷罪孽。这是恶的逻辑。它与基督的绝对宽容的理念完全相悖。可是,在狗镇以及狗镇之外的世界里,这种逻辑却是畅通无阻的。</p>
<p>4,傲慢就是一种恶</p>
<p>所有的狗镇人,都不觉得自己是在作恶,更不觉得自己是罪人。</p>
<p>他们认为,冒着风险收留格蕾丝,是一种莫大的恩德,于情于理,格蕾丝必须付出回报。至于用狗链拴住格蕾丝,和后来对她的奸淫,都只是对她逃跑的一种惩罚。总而言之,他们认为一切都是正当的,无可挑剔的。因为,他们是施舍者。接受施舍的人,理当感恩,理当无怨言,理当加倍回报。</p>
<p>而这种心态,就是傲慢。</p>
<p>真正的施舍,不是一种交易式的交换,不是用基督的名义做着买卖,不是高人一等的怜悯和赏赐,而是平等的尊重、包容和帮助。</p>
<p>但他们自觉恩重如山,高高在上,心安理得地享用格蕾丝的祭祀。如果惩罚过度,也没什么大不了。因为,他们是她的恩公。他们为所欲为,犯下罪过而不自知。</p>
<p>人的诸种恶行中,骄傲为最,认为自己比他人更优越,然后变质为对他人的轻视,甚至憎恨,这是一切罪行的开端。基督教提倡谦逊,不仅仅是行为上的,更要灵魂中的自知和谦卑。</p>
<p>而格蕾丝,也有更大的傲慢。</p>
<p>来到狗镇之前,她是主张宽恕的基督徒,她占据道德高地,批评父亲,批评恶,主张原谅强奸犯和杀人狂。</p>
<p>可是,是谁给予了她宽恕的权利,所有罪行的受害者答应了吗?切肤的痛苦,和剜心的绝望她感受过吗?她凭什么替他们代言,凭什么去为他人的惨剧盖棺定论,凭什么将他人余生的痛苦敷衍了事?</p>
<p>谁有终极审判的权利?只有上帝。</p>
<p>试图僭越本分,去用道德审判他人的人,都是傲慢的罪人。</p>
<p>当父女重逢,在车厢里,二人的对话很有意思。</p>
<p>格蕾丝:我为什么不该慈悲为怀?</p>
<p>父亲:不,有时候应该慈悲为怀,但你的标准要一致,这才公平。你有错应该受罚,他们也一样。</p>
<p>格蕾丝:他们是人。</p>
<p>父亲:人要不要为自己的行为负责?当然要,但你不给他们机会。这是极端的傲慢。我爱你,我爱你,但我没见过比你更傲慢的人,而你还说我傲慢。</p>
<p>从格蕾丝的回答里,我们可以看出她的自我定位,是一种与众不同的、超越人类的存在。父亲一语中的,说出格蕾丝自以为是的伪善,以及宽容一切的主张背后的致命弊端:无以伦比的傲慢。</p>
<p>她和狗镇人在这一点上,又有着惊人的一致:傲慢、自以为是、行恶而不自知。</p>
<p>5,多数人暴政</p>
<p>让人觉得意外的是,在这场对格蕾丝的迫害中,所有的决策,都是通过投票的方法产生,这符合我们对西方民主社会的想象:公共事务通过投票来决策,少数服从多数,保证更多人的利益。并且,在这个电影中,所有的民主程序都合法,似乎无可挑剔。</p>
<p>但,这到底是民主,还是多数人暴政?</p>
<p>我想起土改和文革时期,贫下中农分了浮财与土地之后,便开始瓜分或轮奸地主小老婆,甚至他们的女儿。此类现象比比皆是,和《狗镇》一样,这种罪行也是公开或半公开的,也符合民主原则:多数人同意,至少不反对。</p>
<p>还有,苏格拉底以“妖言惑众”被定罪,人民热烈拥护这一判决;</p>
<p>袁崇焕被诬通敌卖国,在北京西市问斩,市民争食其肉;</p>
<p>希特勒高票当选总统,随后民主地解散其他政党,接着合法地通过了迫害犹太人的法令;</p>
<p>而离我最近的例子,是2012年,万宁中学初三8班以全班学生投票的方式,开除了一名“有点吵”的学生;</p>
<p>……</p>
<p>所有这些决策背后,都站着无数个支持的人。</p>
<p>但他们有着共同的漏洞:违背人权。</p>
<p>真正的民主,必须建立在宪政和法制的基础上,多数人权利被满足的同时,必须保障少数人的基本人权,否则,这样的民主是多数人暴政。</p>
<p>在《狗镇》里,狗镇人是自行其是,无视格蕾丝的自由与权利的,他们禁锢她的自由,侵犯她的肉体,剥削她的金钱,损害她的私产……他们靠着人多势众,剥夺少数人的基本权利包括财产权和生命权。这就是多数人暴政。</p>
<p>于是,他们便犯下罪行,成了作恶者。</p>
<p>拉斯·冯·提尔不喜欢美国,曾经有记者问他:怎么样才能让你来美国呢?</p>
<p>冯提尔说:除非你用核武器摧毁了整个欧洲。</p>
<p>他通过美国三部曲,来表达他的反思。在这一部中,对民主的质疑力度是最强烈的。</p>
<p>人民无论就智力,还是德行,大都庸常。他们有保守、愚钝的一面,习惯站在新事物的对立面,或打击异端,或排斥外来者,常常不辨是非,甚至助纣为虐。因此,许多时候,民主会成为一种迫害异端的合法武器。</p>
<p>于是有人提出哲人王制度。</p>
<p>中国人永远渴望明君圣主,遗憾的是,哲人王、明君出现的几率极低。一旦我们将所有权力赋予某个人,结果总是让人失望,掌握至高权力者要么昏庸,要么残暴,要么将个人好恶看得高于公众利害。如同格蕾丝的父亲,以及后来的格蕾丝。</p>
<p>6,不存在的乌托邦</p>
<p>在奉行集体主义的社会里,民主的实行步步维艰。</p>
<p>而在西方社会里,实行哲人王制度,又过于天真和一厢情愿。</p>
<p>我们该往何处去?</p>
<p>“格蕾丝,听说你在这里有麻烦?”</p>
<p>“家里才最麻烦!”</p>
<p>正如格蕾丝,她心怀悲悯,她选择站在石头的对立面——鸡蛋的一方,有良知,也有行动,为了避开父亲的权力帝国,她来到狗镇,她觉得普通民众之中,才可能有大美与大善。但孰不知,她进入的是另一种恶,甚至在与恶的交战中,她自己也成为恶的本身。</p>
<p>这使我想到了司汤达的秩事,在他11岁时,有一天从家里溜出去参加一个革命者的集会,却意外地受到了震动。他发现无产者不仅衣衫褴褛,浑身臭气,而且粗俗不堪,满嘴脏话。</p>
<p>“总之,我那时就像我现在一样,热爱人民,憎恶压迫他们的人,但是如果要我和人民生活在一起,那我觉得简直是一种不忍受的折磨……为了人民的幸福,我可以做任何事情,但我得承认,我宁愿每月在监狱里蹲两个星期,也不愿去和那些店主一起生活!”</p>
<p>和司汤达一样,格蕾丝最终也觉得狗镇无法容忍,她离开狗镇,回到那个充满杀戮的权力中心。</p>
<p>乌托邦是不存在的,正如无菌的世界不存在一样。此地即彼地,每一个地方都是欲念丛林,人性的豺狼虎豹深藏其中,并没有大不同。</p>
<p>从我记事开始,我就厌恶小地方的传统与愚昧,一直渴望离开,离开村庄,离开镇子,离开县城,到更远的地方去,因为我总觉得一个更好的世界,定然会在远方。</p>
<p>但当我南来北往,辗转许多城市之后,却发现世界本身就是不完美的存在。走到天涯海角,都要面对理想的坠落,人性与道德之间的挣扎。就好像我们试图远离《1942》的恐惧,千回百转之后,寻到《美丽新世界》,却发现这个乌托邦同样荒谬。</p>
<p>鲁迅说,从来就没有至善至美,极境就是绝境。</p>
<p>一种又一种寻觅的可能相继关闭,此地或远方,权贵或平民,城市或乡村……皆是残缺,皆藏污浊。而那个更好的世界触不可及,成为夸父一直追赶,却永远不能抵达的太阳。</p>
<p>与《狗镇》相提并论的,是陈冲导的《天浴》,同样是无辜少女最终沦为所有男人泄欲工具的故事。两者不同的是,《天浴》女主角最终含辱而死。</p>
<p>她没有权力的扶持,所以无法逆袭,成为权力的牺牲品。</p>
<p>这是中国版的《狗镇》,也是《狗镇》的后续故事。</p>
<p>它使我想到,当格蕾丝获得极端权力以后,她定然会持续作恶。任何一个受虐者,也是施虐者。而那些相继而来的格蕾丝们,大抵只能一个接一个地,像李小璐一样,在极权下受辱而死。</p>
<p>这是更普遍的现实。</p>
<p>7,知识的无力</p>
<p>亚里斯多德在他著名的《诗学》中是这样说的:俄狄浦斯之所以陷于厄运,不是由于他为非作恶,而是由于他犯了错误;</p>
<p>犯了错误,在这里指的是人的无知,而不是指道德上的缺陷。</p>
<p>那么,无知致恶,有知便可以使人免于厄运么?</p>
<p>我们想到了《狗镇》的男主角,汤姆,这是一个读书人,一个理想主义者,一个有良知的说教犯。他一直致力于小镇的道德建设,希望狗镇能变成一个福地。</p>
<p>“汤姆是作家,至少他这么认为,他到目前为止,只写了‘伟大’和‘渺小’,外加一个问号……”</p>
<p>在狗镇所有的施虐者中,汤姆是最有戏剧性的一个,因为在故事的开始,他是狗镇的良心。他年轻,相信理想,热爱阅读,有善心,乐于助人,有反省能力。当惊慌失措的格蕾丝到来,他毫不犹豫地,对格蕾丝施以援手,并且尽全力说服大家接纳她;在矛盾初发的时候,他帮她笼络人心,并且用爱情来温暖这只外来的惊弓之鸟。</p>
<p>可是,他的软弱,成为他的阿喀琉斯之踵。后来,他无力反抗整个集体的暴力,渐渐沉默,麻木,又加上私欲作祟,他最后倒戈相向,对格蕾丝实施迫害。</p>
<p>一次次出卖格蕾丝的,是他;</p>
<p>想致格蕾丝于死地的,也是他。</p>
<p>张雨生有一首歌,是这样唱的:</p>
<p>我最深爱的人伤我却是最深<br>教人无助的深刻<br>点亮一盏灯温暖我无悔青春<br>燃尽我所有无怨的认真</p>
<p>……</p>
<p>真是说尽了这一对怨侣的来来回回。</p>
<p>于是格蕾丝可以原谅所有人的加害,却不能宽宥汤姆的背叛。它成为她最无法释然的在喉之鲠,她走出旁观的车厢,亲自站在杀场上,叩动扳机,击毙了这个伪善者。</p>
<p>她说:“有些人,要自己去解决!”</p>
<p>生活中往往出现一些人,他们自诩智者,或人类博爱者,立志一生品行端正,合乎理智。像一个楷模,一个榜样,或者一本活的处世学。可结果呢?众所周知,许多自我圣化的人,或早或晚都背叛了自己,做出一些荒诞的、无理性的,甚至是最不成体统的事。</p>
<p>“人的最好的定义是:两条腿的忘恩负义的动物。”</p>
<p>这是陀思妥耶夫斯基说的。他还说,这不是人类的主要缺点,它最主要的缺点是:一贯品行恶劣。是一贯的,从太古的大洪水直到人类历程上的石勒苏益格—荷尔斯泰因时期。</p>
<p>品行恶劣,恶念将人冲撞得东倒西歪。因此也就不明智。</p>
<p>尽管汤姆有着救世的意图,有着深厚的学识,头脑也好使,但由于怯懦的性格,和贪婪好色的私欲,他成为一个阴狠的懦夫,和可怜的叛徒。</p>
<p>格雷丝说:“我们会在爱与自由中重逢,而不是在这种情形下苟合。”</p>
<p>汤姆答应了。</p>
<p>但当他在求欢被拒后,终于露出真面目:“所有的男人都占有了你,只有我还不曾!”</p>
<p>不过一个色欲熏心的小人,出于虚荣而卖弄真理,却不可能有永恒的理性、正确的意识。知识与疯狂的对阵,又一次败下阵来。</p>
<p>8,我们都是格蕾丝</p>
<p>就在我写下这篇影评的今天,我听到了一则关于我的流言。我开始不相信是真的,近些年独来独往,自闭得几乎抑郁,不太可能有缺陷和漏洞留下来培养谣言。但朋友一再强调流言确凿存在:“是不是这样?”</p>
<p>我说:“没有,我干干净净地单着。”</p>
<p>我大概明白是怎么回事,大概缘于上班时蹭过一个同路的家长的车,就滋生这些非议。明白以后,我对这个小县城又增加了一份彻骨的失望,在它的闭塞与愚昧之外,是随处可见的恶。无论是成人世界,还是校园。</p>
<p>“这就逼得我在这里一个人都不能说话了。”</p>
<p>“你说不说,都会有人传。”</p>
<p>我又无奈又生气,恨不得生出一种巨大的力量,可以把谣言制造者和传播者揪到面前,狠狠地扇她们几个大耳刮。但看了《狗镇》后,我忽然惊觉,原来我和格蕾丝并没什么两样。</p>
<p>一切都可以被解释了。</p>
<p>人非上帝,没有万无一失的美德。人性恶是存在的,如同基督教义中所言的原罪,它们平时屏气息声,埋伏于一隅,但当身处集体,并觉得作恶没有后顾之忧时,潘多拉的盒子就会自动开启。</p>
<p>然而,在这样的一个大狗镇中,人微言轻如我,所能做的又有什么呢?不过是在人性的斜坡上,尽可能只走五十步,不迈出跨向第一百步的脚。虽然在本质上,它们并没什么不同。</p>
]]></content>
<summary type="html">
<![CDATA[<p>这是一个真正的恐怖片。</p>
<p>它的恐怖之处在于,它是一个锐利的、寒光闪闪的追问。在这个追问之下,我们自以为是的道德变得弱不经风:如果你是狗镇人,你会对一个无辜者施暴吗?再假如,暴力是民主化的,是被允许的,安全的、正当的,没有后顾之忧的,你也能从中获得某种满足的,满城皆如是、无人是例外的,你还敢信心满满地说不么?</p>
<p>在看完《狗镇》之后,我长久地处于一种颤栗状态,是真实的肉体颤栗,而非修辞。它不由分说地,把我推到人性的法庭,拷问我的良知,审判我的德行,以至于后来,它们统统站不住脚,变成了可疑的存在。</p>
<p>我会吗?我敢和整个镇子唱反调吗?</p>
<p>我无法自信。</p>
<p>也许会?也许不会?又也许我只会冷眼旁观,不跟从恶行,也不违良知,远离集体,明哲保身。但,沉默何尝不是另一种认可,冷暴力何尝不是另一种形式的暴力。</p>]]>
</summary>
<category term="文章" scheme="http://jssu.github.io/tags/%E6%96%87%E7%AB%A0/"/>
<category term="生活" scheme="http://jssu.github.io/categories/%E7%94%9F%E6%B4%BB/"/>
</entry>
<entry>
<title><![CDATA[你未必是人好,你只是没机会放荡]]></title>
<link href="http://jssu.github.io/2015/07/08/%E7%BB%8F%E5%8E%86/"/>
<id>http://jssu.github.io/2015/07/08/经历/</id>
<published>2015-07-09T01:40:53.000Z</published>
<updated>2015-07-08T23:43:11.000Z</updated>
<content type="html"><![CDATA[<p>作者 : 杨奇函 <a href="http://blog.renren.com/share/258366974/17933740255" target="_blank" rel="external">http://blog.renren.com/share/258366974/17933740255</a></p>
<p>毕业后自己要承担自己的一切开销,便不得不精打细算了起来。每买东西或出去玩都会很省。我常常以节俭自诩,各种秀自己的勤俭节约。某天我跟我妈说我如何是一个勤俭节约的人。我妈呵呵,说:“儿子你不是勤俭节约,你只不过现在拮据了。当年你花我们钱的时候你怎么就没有节俭呢?”我无言以对。</p>
<p>小时候认识邻居一叔是公务员。十年前他处于边缘岗位。总听见他谩骂贪官,咒骂腐败。家里也确实经济条件一般。我们觉得他肯定是一个好干部。后来这叔被提拔为了某实权部门的副处长。然后前几天落马了。我伙呆。听他妻子说,原来叔“靠边站”的时候真的对腐败恨之入骨。但是后来有了权力尝到了腐败的甜头,一样吃拿卡要。邻居听后,不胜唏嘘。<br> <a id="more"></a><br>《圣经》中记载了一个故事:彼得三次不认主。大意就是耶稣跟门徒说完自己将要被刑拘以及判刑。彼得立即表示誓死捍卫耶稣。耶稣告诉彼得:“你虽然现在信誓旦旦,但夜里鸡叫前你将三次不承认认识我。”彼得坚决表示对自己的忠诚和勇敢有信心。结果,鸡叫之前彼得果然就有因为惧怕承认认识耶稣而惹祸上身,进而对卫兵否认认识耶稣。</p>
<p>以上三个故事告诉我:没有经历考验的“人品”,不能放心被视为“人品”。</p>
<p>就像我们一直回避我们的硬伤的频率一样,我们常常炫耀我们自身并不一定存在的美德。我们总是喜欢向亲友们展示我们自认为的高尚的道德情操,经常在朋友圈以获取点赞为目的来炫耀自身的一些操守,我们常常通过寄希望于感动他人而自我感动,我们总是在迷失在一个幻想中的高尚自我而不能自拔。</p>
<p>单身多年的我们,可能会觉得我们是负责任的“好男人”,不爱“骗女孩子感情,耽误女孩子青春”;熬夜工作的我们,可能会觉得我们是有使命感的“好员工”,为了好的工作成果可以“抛头颅洒热缺”;生活朴素的我们,可能会觉得我们是“安贫乐道,不慕荣华”的从容生活的人。如此种种,不胜枚举。每当我们承受一些世俗意义上的负担,我们往往会将其同某种道德品行联系起来。</p>
<p>但是,我们真的如我们所想的那样吗?</p>
<p>长年的单身,有多少是真的因为我们“怕耽误女孩”而导致的?可能只是因为“我们自己丑,还嫌弃别人丑”。与其说我们自己选择当一个“好人”,毋宁说是我们更多是因为长年在爱情狩猎场被淘汰而不得不选择当一个“好人”。</p>
<p>熬夜通宵的工作,有多少是出于强烈的使命感和责任心?可能只是因为我们单位时间内完成任务的能力有限,以及面临着强大裁员压力和生存压力而不得不卖命干活。试问如果说现在我们熬夜的工作即便不干我们依旧可以领取工资,我们有多少人的“事业心”将会顷刻瓦解?</p>
<p>至于我们简朴的生活,有多少是真的因为我们乐于恪守朴素的生活?可能只是因为我们并没有机会去奢侈。正是因为我们惨淡的银行卡余额,我们才不得不将自己的目光从琳琅满目的奢侈品店移开,而投向廉价的衣食住行。如果我们一夜变土豪,我们能够保证自己还是会对着淘宝款含情脉脉,对着国际一线呵呵呵呵吗?</p>
<p>我们会相信甚至炫耀自己的一些具有苦难色彩的经历,进而标榜并炫耀自身的一些品质。而事实上,我们很多自以为是的种种品行并没有经过可靠检验,而我们却把它夸夸其谈。当我们远离检验的时候,我们往往高歌猛进;当我们面临检验的时候,我们往往默不作声;当我们经过检验的时候,我们往往百口莫辩。我们对自身的一些“道德判断”,与其说是一种判断,勿宁说是一种期待,甚至是一种想象。</p>
<p>最近有一部电影叫《道士下山》。讲了一个小道士的故事。大意就是一个生长在山上不谙世事的小道士一心修道。被师父赶下山之后,在世俗社会经历了恩怨,仇杀,淫乱,生死等等人性的最阴暗和丑恶,然后才悟出道理重新回到山上修道的故事。若要修道,不在山上,而在山下。没有经历过拷问和磨砺的“道”,都是虚假的,脆弱的。道如此,德亦如此。</p>
<p>都不曾坐过宝马,却说自己不会喜欢阿斯顿马丁的生活,这并不靠谱;不曾有女神对你表达爱意,却信誓旦旦说自己不会被美色所诱惑,这并不靠谱。试问,没有经历过种种欲望的拖累,如何能说自己能做到无欲则刚?再问,没有经受过种种诱惑,如何能放言自己已然六根清净?又问,这红尘你都没有走过一场,你如何能说你已看破了红尘?</p>
<p>以最标榜禁欲和德行的佛教为例。佛祖出身王子,从小过着麻辣小龙虾任吃,长腿妹子任啪的快活日子。所以他对欲望的认识也最深刻,对欲望的危害也最了解,他同红尘有着最强的革命性,最终主张种种禁欲。而对于广大和尚而言,他们的革命性则不一定如释迦牟尼强大。因为他们不曾经历红尘,恰恰容易为红尘所扰,即便他们标榜自身不屑红尘。《水浒传》中言及裴如海和潘巧云通奸便讲过“和尚是色中饿鬼”的戏谑说法。玄奘的首席爱徒辩机禅师同高阳公主私通便是一例。</p>
<p>所以,在没有基本的检验下,我们对自身道德的自信甚至自负并非如我们想象的那样可靠。很多时候,维系我们的言行合乎道德规范的,往往不是我们的道德情操高人一等或者精神境界俯瞰众生,而是仅仅因为我们的种种短板。我们可能忽视了这样一个事实:支撑我们“人品”的未必是我们的人品,而恰恰是那些用来证明我们人品的苦难。我们未必是人好,我们只是没有机会去放荡。</p>
<p>维持我们节俭的,可能是我们的贫穷;维持我们检点的,可能是我们的丑陋;维持我们的低调的,可能是我们的平庸;维持我们的钻研的,可能是我们的笨拙。促成我们“义无反顾”的,可能是我们的“走投无路”;激发我们“看淡一切”的,可能是我们的“一无所有”。我们“心无旁骛”,可能只是因为我们没有能力去从容;我们“刚正不阿”,可能只是因为我们没有资格被诱惑。</p>
<p>在没有经历过检验之前,一旦我们对自己的人品太自负,就会进而丧失了对我们自身人性弱点的警惕,从而放纵自我。我们在未曾有过丝毫检验的基础上对自身得来的结论及其导致的言行逻辑,在精神上是有残缺的,在实践中是有风险的。</p>
<p>这种不可靠的道德自负的潜在的危险就是:一旦我们具备了摆脱苦难的能力,我们可能就会失去了维系我们“道德品行”的束缚,而我们却相信我们有足够的强大的品行,我们便会挟持道德自负为所欲为,我们更会缺乏对自身短板开展有针对性的自我提升。</p>
<p>尝试第一口K粉的吸毒者没有不觉得自己足够坚强,可以抵制住毒瘾的,而忽视了对交友的谨慎;跟别有用心的“商人朋友”吃第一顿饭时候的落马官员没有不相信自己的立场和原则,能够保持廉洁奉公的,而忽视了对人民的责任;所有刷夜玩DOTA的学渣都会在十一点高喊“这是最后一盘”然后一直玩到一点以后的,而忽视了对自控力的提高;每一个爱逛淘宝的萌妹子都会一边相信自己的价值判断能力,一边买着一大堆三天就后悔的杂七杂八的东西的,而忽视了对个人理财能力的提升。</p>
<p>行文至此,我们可能会觉得“我们太过妄自菲薄,道德虚无”。这里必须强调,我们毫不质疑种种美德存在于我们身上的可能性,我们更绝不否认任何一个有美好德行的人,而是突出强调要警惕我们种种美德的可靠性。我们不是要说我们都是“不道德”或者“没有检验就不能说自己有道德”,而是想强调我们“未必如我们想的那么有道德”以及“我们要对自己的道德判断更谨慎”。就像对个人能力的高估一样,对个人品行的高估一样会对自己和他人造成伤害。</p>
<p>当我们对自身的道德品行有警惕的时候,我们做事情还会心存顾虑,投鼠忌器;当我们对自身道德品行盲目自负的时候,我们则很容易肆无忌惮,伤痕累累。一旦缺乏对自身的德行操守的警惕和对自我提升的敬畏,我们会做出更多的傻事,一些让我们大叫三声,后悔一生的傻事。</p>
<p>质疑自我的目的不是自我否定,而是自我提升。对自身道德的拷问,旨在提示一种谦卑,一种对自身道德品行的谦卑,从而存留对社会,对人生的谦卑。似如《圣经》中《彼得前书》(5:5-6)所言:</p>
<p>“就是你们众人,都要以谦卑束腰、彼此顺服。因为神阻挡骄傲的人、赐恩给谦卑的人。”</p>
]]></content>
<summary type="html">
<![CDATA[<p>作者 : 杨奇函 <a href="http://blog.renren.com/share/258366974/17933740255">http://blog.renren.com/share/258366974/17933740255</a></p>
<p>毕业后自己要承担自己的一切开销,便不得不精打细算了起来。每买东西或出去玩都会很省。我常常以节俭自诩,各种秀自己的勤俭节约。某天我跟我妈说我如何是一个勤俭节约的人。我妈呵呵,说:“儿子你不是勤俭节约,你只不过现在拮据了。当年你花我们钱的时候你怎么就没有节俭呢?”我无言以对。</p>
<p>小时候认识邻居一叔是公务员。十年前他处于边缘岗位。总听见他谩骂贪官,咒骂腐败。家里也确实经济条件一般。我们觉得他肯定是一个好干部。后来这叔被提拔为了某实权部门的副处长。然后前几天落马了。我伙呆。听他妻子说,原来叔“靠边站”的时候真的对腐败恨之入骨。但是后来有了权力尝到了腐败的甜头,一样吃拿卡要。邻居听后,不胜唏嘘。<br>]]>
</summary>
<category term="文章" scheme="http://jssu.github.io/tags/%E6%96%87%E7%AB%A0/"/>
<category term="生活" scheme="http://jssu.github.io/categories/%E7%94%9F%E6%B4%BB/"/>
</entry>
<entry>
<title><![CDATA[Search in Rotated Sorted Array]]></title>
<link href="http://jssu.github.io/2015/06/28/SearchinRotatedSortedArray/"/>
<id>http://jssu.github.io/2015/06/28/SearchinRotatedSortedArray/</id>
<published>2015-06-28T18:04:26.000Z</published>
<updated>2015-11-08T03:54:16.000Z</updated>
<content type="html"><![CDATA[<p>Search in Rotated Sorted Array</p>
<blockquote>
<p>Suppose a sorted array is rotated at some pivot unknown to you beforehand.<br>(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).<br>You are given a target value to search. If found in the array return its index, otherwise return -1.<br>You may assume no duplicate exists in the array.</p>
</blockquote>
<a id="more"></a>
<figure class="highlight vbscript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> Solution {</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line"> <span class="built_in">int</span> search(vector<<span class="built_in">int</span>>& nums, <span class="built_in">int</span> target) {</span><br><span class="line"> <span class="keyword">if</span>(nums.size()==<span class="number">0</span>)</span><br><span class="line"> return -<span class="number">1</span>;</span><br><span class="line"> <span class="built_in">int</span> <span class="built_in">left</span> = <span class="number">0</span>, <span class="built_in">right</span>= nums.size()-<span class="number">1</span>, middle;</span><br><span class="line"> <span class="keyword">while</span>(<span class="built_in">left</span><=<span class="built_in">right</span>)</span><br><span class="line"> {</span><br><span class="line"> middle = (<span class="built_in">right</span>+<span class="built_in">left</span>)/<span class="number">2</span>;</span><br><span class="line"> <span class="keyword">if</span>(nums[middle]==target)</span><br><span class="line"> return middle;</span><br><span class="line"> <span class="keyword">if</span>(nums[middle]>=nums[<span class="built_in">left</span>])</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(nums[<span class="built_in">left</span>]<=target && target<nums[middle])</span><br><span class="line"> <span class="built_in">right</span> = middle-<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="built_in">left</span> = middle+<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(nums[middle]<target && target<=nums[<span class="built_in">right</span>])</span><br><span class="line"> <span class="built_in">left</span> = middle+<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="built_in">right</span> = middle-<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> return -<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line">};</span><br></pre></td></tr></table></figure>
<blockquote>
<p>Follow up for “Search in Rotated Sorted Array”:<br>What if duplicates are allowed?<br>Would this affect the run-time complexity? How and why?<br>Write a function to determine if a given target is in the array.</p>
</blockquote>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> Solution {</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line"> <span class="function"><span class="keyword">bool</span> <span class="title">search</span><span class="params">(<span class="built_in">vector</span><<span class="keyword">int</span>>& nums, <span class="keyword">int</span> target)</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> left = <span class="number">0</span>, right = nums.size()-<span class="number">1</span>, mid;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">while</span>(left<=right)</span><br><span class="line"> {</span><br><span class="line"> mid = (left + right) >> <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">if</span>(nums[mid] == target) <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line"></span><br><span class="line"> <span class="comment">// the only difference from the first one, trickly case, just updat left and right</span></span><br><span class="line"> <span class="keyword">if</span>( (nums[left] == nums[mid]) && (nums[right] == nums[mid]) ) </span><br><span class="line"> {++left; --right;}</span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">if</span>(nums[left] <= nums[mid])</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>( (nums[left]<=target) && (nums[mid] > target) )</span><br><span class="line"> right = mid-<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">else</span> left = mid + <span class="number">1</span>; </span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>((nums[mid] < target) && (nums[right] >= target) ) </span><br><span class="line"> left = mid+<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">else</span> right = mid-<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line"> }</span><br><span class="line">};</span><br></pre></td></tr></table></figure>
]]></content>
<summary type="html">
<![CDATA[<p>Search in Rotated Sorted Array</p>
<blockquote>
<p>Suppose a sorted array is rotated at some pivot unknown to you beforehand.<br>(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).<br>You are given a target value to search. If found in the array return its index, otherwise return -1.<br>You may assume no duplicate exists in the array.</p>
</blockquote>]]>
</summary>
<category term="学习" scheme="http://jssu.github.io/categories/%E5%AD%A6%E4%B9%A0/"/>
</entry>
<entry>
<title><![CDATA[wordbreak]]></title>
<link href="http://jssu.github.io/2015/06/24/wordbreak/"/>
<id>http://jssu.github.io/2015/06/24/wordbreak/</id>
<published>2015-06-25T06:10:55.000Z</published>
<updated>2015-08-09T16:00:42.000Z</updated>
<content type="html"><![CDATA[<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> Solution {</span><br><span class="line"> <span class="built_in">unordered_map</span><<span class="built_in">string</span>, <span class="built_in">vector</span><<span class="built_in">string</span>>> m;</span><br><span class="line"></span><br><span class="line"> <span class="built_in">vector</span><<span class="built_in">string</span>> combine(<span class="built_in">string</span> word, <span class="built_in">vector</span><<span class="built_in">string</span>> prev){</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i<prev.size();++i){</span><br><span class="line"> prev[i]+=<span class="string">" "</span>+word;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> prev;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line"> <span class="built_in">vector</span><<span class="built_in">string</span>> wordBreak(<span class="built_in">string</span> s, <span class="built_in">unordered_set</span><<span class="built_in">string</span>>& dict) {</span><br><span class="line"> <span class="keyword">if</span>(m.count(s)) <span class="keyword">return</span> m[s]; <span class="comment">//take from memory</span></span><br><span class="line"> <span class="built_in">vector</span><<span class="built_in">string</span>> result;</span><br><span class="line"> <span class="keyword">if</span>(dict.count(s)){ <span class="comment">//a whole string is a word</span></span><br><span class="line"> result.push_back(s);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<s.size();++i){</span><br><span class="line"> <span class="built_in">string</span> word=s.substr(i);</span><br><span class="line"> <span class="keyword">if</span>(dict.count(word)){</span><br><span class="line"> <span class="built_in">string</span> rem=s.substr(<span class="number">0</span>,i);</span><br><span class="line"> <span class="built_in">vector</span><<span class="built_in">string</span>> prev=combine(word,wordBreak(rem,dict));</span><br><span class="line"> result.insert(result.end(),prev.begin(), prev.end());</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> m[s]=result; <span class="comment">//memorize</span></span><br><span class="line"> <span class="keyword">return</span> result;</span><br><span class="line"> }</span><br><span class="line">};</span><br></pre></td></tr></table></figure>
<a id="more"></a>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> Solution {</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line"> <span class="built_in">vector</span><<span class="built_in">string</span>> wordBreak(<span class="built_in">string</span> s, <span class="built_in">unordered_set</span><<span class="built_in">string</span>> &dict) {</span><br><span class="line"> <span class="built_in">vector</span><<span class="built_in">string</span>> res;</span><br><span class="line"> <span class="keyword">if</span> (dict.size() == <span class="number">0</span>) <span class="keyword">return</span> res;</span><br><span class="line"></span><br><span class="line"> <span class="built_in">vector</span><<span class="built_in">vector</span><<span class="keyword">int</span>> > spaceIdx;</span><br><span class="line"> spaceIdx.resize(s.size() + <span class="number">1</span>);</span><br><span class="line"> spaceIdx[<span class="number">0</span>].push_back(-<span class="number">1</span>);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i <= s.size(); i++) {</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j < i; j++) {</span><br><span class="line"> <span class="keyword">if</span> (spaceIdx[j].size() && dict.count(s.substr(j, i - j))) {</span><br><span class="line"> spaceIdx[i].push_back(j);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> backTrack(s, spaceIdx, s.size());</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="built_in">vector</span><<span class="built_in">string</span>> backTrack(<span class="built_in">string</span>& s, <span class="built_in">vector</span><<span class="built_in">vector</span><<span class="keyword">int</span>> >& spaceIdx, <span class="keyword">int</span> idx) {</span><br><span class="line"> <span class="built_in">vector</span><<span class="built_in">string</span>> res;</span><br><span class="line"> <span class="keyword">if</span> (idx <= <span class="number">0</span>) {res.push_back(<span class="string">""</span>); <span class="keyword">return</span> res;}</span><br><span class="line"></span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < spaceIdx[idx].size(); i++) {</span><br><span class="line"> <span class="built_in">string</span> str = s.substr(spaceIdx[idx][i], idx - spaceIdx[idx][i]);</span><br><span class="line"> <span class="built_in">vector</span><<span class="built_in">string</span>> subRes = backTrack(s, spaceIdx, spaceIdx[idx][i]);</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j < subRes.size(); j++) {</span><br><span class="line"> <span class="keyword">if</span> (subRes[j] != <span class="string">""</span>) subRes[j] = subRes[j] + <span class="string">" "</span> + str;</span><br><span class="line"> <span class="keyword">else</span> subRes[j] = str;</span><br><span class="line"> res.push_back(subRes[j]);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> res;</span><br><span class="line"> }</span><br><span class="line">};</span><br></pre></td></tr></table></figure>
]]></content>
<summary type="html">
<![CDATA[<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> Solution {</span><br><span class="line"> <span class="built_in">unordered_map</span><<span class="built_in">string</span>, <span class="built_in">vector</span><<span class="built_in">string</span>>> m;</span><br><span class="line"></span><br><span class="line"> <span class="built_in">vector</span><<span class="built_in">string</span>> combine(<span class="built_in">string</span> word, <span class="built_in">vector</span><<span class="built_in">string</span>> prev){</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i<prev.size();++i){</span><br><span class="line"> prev[i]+=<span class="string">" "</span>+word;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> prev;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line"> <span class="built_in">vector</span><<span class="built_in">string</span>> wordBreak(<span class="built_in">string</span> s, <span class="built_in">unordered_set</span><<span class="built_in">string</span>>& dict) {</span><br><span class="line"> <span class="keyword">if</span>(m.count(s)) <span class="keyword">return</span> m[s]; <span class="comment">//take from memory</span></span><br><span class="line"> <span class="built_in">vector</span><<span class="built_in">string</span>> result;</span><br><span class="line"> <span class="keyword">if</span>(dict.count(s)){ <span class="comment">//a whole string is a word</span></span><br><span class="line"> result.push_back(s);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<s.size();++i){</span><br><span class="line"> <span class="built_in">string</span> word=s.substr(i);</span><br><span class="line"> <span class="keyword">if</span>(dict.count(word)){</span><br><span class="line"> <span class="built_in">string</span> rem=s.substr(<span class="number">0</span>,i);</span><br><span class="line"> <span class="built_in">vector</span><<span class="built_in">string</span>> prev=combine(word,wordBreak(rem,dict));</span><br><span class="line"> result.insert(result.end(),prev.begin(), prev.end());</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> m[s]=result; <span class="comment">//memorize</span></span><br><span class="line"> <span class="keyword">return</span> result;</span><br><span class="line"> }</span><br><span class="line">};</span><br></pre></td></tr></table></figure>]]>
</summary>
</entry>
<entry>
<title><![CDATA[心灵鸡汤都是假的]]></title>
<link href="http://jssu.github.io/2015/06/22/%E5%BF%83%E7%81%B5%E9%B8%A1%E6%B1%A4%E9%83%BD%E6%98%AF%E5%81%87%E7%9A%84/"/>
<id>http://jssu.github.io/2015/06/22/心灵鸡汤都是假的/</id>
<published>2015-06-22T23:07:05.000Z</published>
<updated>2015-06-22T20:09:52.000Z</updated>
<content type="html"><![CDATA[<p>心灵鸡汤都是假的。。。反心灵鸡汤也是假的。。。<br>作者:江寒园</p>
<p>今早打开豆瓣刷到一条广播:</p>
<p>这两份名单你认识多少?第一份名单:傅以渐、王式丹、毕沅、林召堂、王云锦、刘子壮、陈沆、刘福姚、刘春霖。第二份名单:李渔、洪昇、顾炎武、金圣叹、黄宗羲、吴敬梓、蒲松龄、洪秀全、袁世凯。哪份名单上你认识的人多一些?答案揭晓:前者全是清朝科举状元;后者全是当时落第秀才。</p>
<p>第一眼看到这条广播,感觉挺有道理的,论据充足,论证有力,实在是安慰今天高考孩子的一剂良药:即使你这次高考没考好,也不代表你这一辈子就完蛋了。看看这份名单上的人,后者全都落榜,但现在流传千古,我们永远怀念他;前者虽然是当年的状元,可现在谁还知道他们。所以,成功和学历并没有半毛钱关系!安心去考,落榜了也能有大好前程!</p>
<p>后来感觉有点不太对。思索了一下,我写了第三份名单:<br><a id="more"></a><br>1.欧阳修为避嫌曾巩,不然苏轼本是状元;2. 唐宋八大家的曾巩是状元;3. 文天祥是状元;4. 王维是状元;5. 柳公权是状元;6. 郭子仪是状元;7. 贺知章是状元;8. 三国演义开头曲,写二十一史弹词的杨慎,明朝三大才子,他也是状元;9. 张居正更是出名的神童,顾璘怕他年少得意,故意压制了几年,即使如此也在嘉靖二十六年中了进士;10. 其继任者申时行也是状元。</p>
<p>po主怎么不举这些例子?</p>
<p>以这个例子出发,可以略谈几点我反感成功学和鸡汤文的原因:</p>
<p>第一、幸存者偏差</p>
<p>第三份名单上的十个例子即是死亡的数据。他们并不会开口说话,告诉你,你看我也是状元不也流传千古了。这就是幸存者偏差。<br>他们并不会告诉你事实的全部,总是以偏概全,只采纳对他们有利的数据,从而诱导出他们希望你得到的结论。</p>
<p>第二、故意含混的因果逻辑</p>
<p>成功学把事实隐藏,逻辑简化,只留下简单的因果联系:<br>A、考中状元——无人知晓;<br>B、考不上——流传千古;<br>C、你即使考不上也会流传千古</p>
<p>这种思维实在是很害人的。尤其是如果今年参加高考的孩子看到这这种话,在还没有足够阅读面和理智判断的时候会产生什么后果?他告诉你高考压根和成功关系不大:第二份名单,不都没考中,不还是流传千古。第一份都是状元却籍籍无名。</p>
<p>举个例子,不是咬人耳朵和强奸才能成为拳王,泰森成为拳王是因为他有足够的实力和技巧,是他日复一日的刻苦训练。如果不是因为强奸他只会更成功。</p>
<p>不是只要吸毒就能成功。宁财神成功是因为他有足够的才华和编剧技巧,是因为他看书,不断练习写作。如果他不吸毒只会更成功。</p>
<p>同样地,黄宗羲、蒲松龄他们流传千古是他们本就有才,如果他们中了状元,只会成为他们人生的助力,会更加的名满天下,流传千古。就像张居正,是锦上添花。</p>
<p>我们后来者看待历史,经常会犯一个以果导因的毛病,只要是成功了,那他们身上不管什么都可当作经验来学习,不管适不适合自己,也不管他这个因素到底和他的成功有多大关系、甚至有时候是反作用,只不过很多时候这些因素并不如强奸、吸毒那么一目了然罢了。</p>
<p>第三、简单的因果线条、单维度的价值判断<br>这份名单隐含的结论是仅以在民众流传的名气来衡量成功与否。而这世上很多事情都是多因一果。可鸡汤文只列举一因一果。</p>
<p>第一份名单里仅举毕沅、傅以渐为例</p>
<p>毕沅:经史小学金石地理之学,无所不通,续司马光书,成《续资治通鉴》。梁启超对之评价极高,以为:“有毕《鉴》,则各家续《鉴》皆可废也。”</p>
<p>傅以渐:学识广博,精通经史,工于诗文,学者称星岩先生。是一位竭诚尽忠的贤臣良相。有史料记载:顺治破格提拔傅以渐为兵部尚书,众皆赞同,一致认为,选择得当,用人适时,宫廷和睦,天下太平。</p>
<p>第二份名单里,如李渔,蒲松林流传是因为文学作品受众广,而毕沅所作《续资治通鉴》除了对史学研究感兴趣的人谁会注意到?</p>
<p>你没听说过人家的名字并不代表人家不厉害,只能说明你现在的思维、格局、学术水平离他们还太远。</p>
<p>而且这个例子只简单把成功与考上状元简单地连接起来,他们也大多是身后留名,比如蒲松龄生前就过的很困窘。洪尚秀要不是因为吃不上饭,哪会铤而走险去造反?这把一个个活生生的人的背后故事,和他们之所以出名的种种因素统统忽略,只简单地把考上或考不上状元同青史留名等同起来,再蛮横地把青史留名和成功等同起来。事实上,反而是那些没考上的人生前困窘,也只是身后留名,而那些没留名的状元你怎么知道他们活得不幸福,而简单地以青史留名来衡量成功?</p>
<p>与之类似还有几个幼稚的小故事,那都是我们小时候写作文经常用到的:</p>
<p>比如:</p>
<ol>
<li>华盛顿小的时候时候,砍掉他父亲的两棵樱桃树。后来主动承认错误,得到了他父亲的原谅。</li>
<li>爱迪生在列车上作试验,列车突然倾斜,出了事故,列车员打了他一拳,鼓膜破裂,爱迪生的耳朵从此再也听不到声音了。</li>
<li>爱因斯坦小学数学不好,每次考试都是一分。后来努力学习,成为了最伟大的物理学家。</li>
</ol>
<p>这样的故事还可以一直列下去,第一个故事教育我们要诚实守信,这才是伟大人物所必须的品质,如果你想要和华盛顿一样伟大,就要勇敢承认错误。第二个故事告诉我们成功并不是一帆风顺的,爱迪生真是穷且益坚,实在是我们的榜样啊!第三个爱因斯坦的故事安慰我们,即使现在学习不好,未来依然可以有所作为,成为大科学家!</p>
<p>然而这些并不是故事的全部。</p>
<p>甚至于有些故事并不存在,比如第一个《华盛顿和樱桃树》,后来其传记作者坦白,因为史料太少,这个故事是为了凸显华盛顿的优秀品质而虚构的,很类似于我们现在史书上常见的帝王传记,刘邦是其母与龙交合,已而有娠,遂产高祖。或者某些帝王刚生下来,满室红光。</p>
<p>至于第二个故事,后来据爱迪生本人说,其真实版本则与之恰恰相反:</p>
<p>“有一天,我上火车晚了,两手抱着一大捆报纸,勉强抓住已开动的车的把手,但力气不足,渐渐下滑。列车员赶忙伸手来拉,不凑巧抓住了我的耳朵,就这样硬把我拉了上来,当时我的耳朵嗡嗡作响,我的生命得救了,但耳朵却聋了。”</p>
<p>这就是说,列车员不仅不坏,反而是他的救命恩人。至于后来的这个流传(因为车内失火,他被勒令把试验器具全部拿下车),是确有其事,不过它发生在1862年。而他变成聋子,则发生在两年前。另外爱迪生8岁得了重猩红热病,高烧之后,耳朵就一直不太好,不能全怪人家列车员。</p>
<p>还有经常说到的,爱迪生8岁时到附近一家小学读书,但听不清老师讲话,理解能力也差,因此有人嘲笑他,最后终于休学,跟着母亲学习。这和第三个故事爱因斯坦小时候得低分的故事一样,用于激励我们。但这只不过是因为他周围人,甚至他自己都没有注意到他的耳朵听不清吧——并不是脑子不好。</p>
<p>至于第三个,谣传太久。德国考试成绩评分为6分制——1分:sehr gut(优秀);2分:gut(良好);3分:befriedigend(中等);4分:ausreichend(及格);5 分:mangelhaft(不及格);6分:ungenuegend(差)。爱因斯坦人家从小就很聪明。</p>
<p>即使出发点是教育孩子向善,可如果本身这就是一个虚假的故事,等长大后明白过来又会很可能反而会产生相反的效果:</p>
<p>原来我从小就生活在一个谎言的世界。甚至连官方的语文教科书都是假的,这世上还有真的可信的事情吗?</p>
<p>这对儿童的三观是一次崩塌。</p>
<p>这几个小学时代的故事对我们来说已经太过幼稚了,现在以我们的阅读面完全可以轻易辨别出他们的真假。然而世上还有其他很多类似的故事,有着更为精妙的包装。很多人孜孜不倦地阅读着名人传记,妄图从中得到些成功的真谛。但这些传记背后的作者不会告诉你他们成功的全部真相。</p>
<p>他们不会告诉你,王石的父亲当年是王震王胡子的部下;任志强不会告诉你他父亲是曾经的商业部副部长;潘石屹不会告诉你他的发迹是和女富豪张欣结婚后开始的;原铁道部部长刘志军,他的发迹和他的三次婚姻有关,嗯,男版邓文迪,一次跳得比一次高。</p>
<p>或者还可以再加上某条一直很火的微博,</p>
<p>“盖茨的书不会告诉你他母亲是IBM董事,是她给儿子促成了第一单大生意,巴菲特的书只会告诉你他8岁就知道去参观纽交所,但不会告诉你他国会议员的父亲带他去的,是高盛的董事接待的。”</p>
<p>文青们别得意,与之类似的还有你们的穷游女神Molly,你们最喜欢最受感动的这些话:</p>
<p>身体和心灵总要有一个在路上。</p>
<p>人的一生中,总要有一次为了理想而奋不顾身抛下一切,许多年后回想起来才不会后悔。</p>
<p>现在火爆异常,一直高烧不退的去拉萨“净化灵魂”,骑行318成为纯粹的人,全世界穷游旅行。</p>
<p>旅游能净化个屁的灵魂啊。</p>
<p>风景一会就看腻了,主要还是要与人文景观,历史地理所学的知识结合起来才能达到真正的知行合一。你没一点历史文化知识积淀,看到伦勃朗也只是匆匆拍个照,还能像人家一样,一看三个小时,感动到流泪还晕倒?没一点积淀看到1825年帕里斯发明的幻盘你能跟电影的产生原理,视觉暂留和似动现象联系起来?来到柏林墙只是傻傻看几块砖头和墙上的涂鸦,你啥也不知道,怎么能和当年历史背景联系起来感受历史的厚重?逛到但泽也只是看看风景而已,没看过《铁皮鼓》《狗年月》《猫与鼠》,不知道君特格拉斯,不知道但泽支离破碎的历史,你说你啥都不知道,风景一看就忘,只会傻呵呵拍个风景,你能受到什么震撼,能有什么知行合一?</p>
<p>但文青的市场需求就在这,Molly的出现完美地满足了他们的幻想,颜美身材好还能任性地到处穷游。不知多少女生因此吵着和男朋友分了手。</p>
<p>直到后来曝出Molly不是穷游,一直都有赞助,住的也是豪华大酒店,每天几千。这样的行为已经是一种欺骗。你告诉他们说走就走的旅行,却没告诉他们你背后有着企业进行赞助,所以才能走得这么潇洒。</p>
<p>还有,不好意思,可能牵扯到你们的男神。大冰。</p>
<p>一开始我也很迷他,觉得简直是我理想中的生活啊!太让人羡慕了!看了几篇The One上的故事,觉得写的很感人,后来渐渐感觉有点不对。他在书里竭力宣扬一种世外桃源,抛弃世俗名利的生活。看起来很潇洒很美好,可如前所述,他只展示给你了他最梦幻的一部分,其他的并没有给你看,或只是匆匆一笔带过。比如他路上的艰辛,因摔伤肋骨折断,小脚趾切除。还有后来去他微博里逛了一圈,里面众人高举书籍的热闹世俗场景和书里所描绘的拉漂,净化灵魂,世外桃源实在相差太远。</p>
<p>不能说鸡汤,说迷魂汤可能更贴切一点。一剂灌给困在繁琐无聊的都市生活中的文青男女的虚幻的迷幻汤,完美地满足了他们的精神需要。</p>
<p>你在书里宣扬的远离世俗的桃源生活和当下简直截然想反,你没告诉他们故事的全部却引得一大批文青男女抛弃身上对家人的责任,而离家出走,四处搭车穷游陷入并不安全的境地。</p>
<p>这也是我反感这类迷魂汤一个原因——他们只强调个人的任性的自由而放弃承担责任。</p>
<p>孔子说,父母在,不远游,游必有方。</p>
<p>这庶几是孝、是仁的一种表现。</p>
<p>我并不认为辞职旅行,拦路搭车,到处蹭吃蹭喝比兢兢业业认真工作,背负生活重担的人更高尚或更出色。</p>
<p>你走之后留给的是家人无尽的担心,他们只告诉你穷游,告诉你搭车,没告诉你这背后安不安全,父母会不会因此白了头。你被灌了迷幻汤一走了之之后正在上升的职业生涯很可能也会中断,而回来后发现存款没了,女友跟人跑了,灵魂似乎也没得到什么提升。</p>
<p>他们编造这些鸡汤,这些成功学无非是出于背后的利益,好像我国伟大领袖Chair Mao为了赶英超美而鼓励全国人民,“天才就是1%的天分加上99的汗水”。可Chair Mao 没有告诉你后半句是,“但那1%的灵感是最重要的,甚至比那99%的汗水都要重要。”</p>
<p>另一个方面,这些成功学,鸡汤文本身能够流传是因为:</p>
<p>人民群众喜闻乐见。<br>你要真相还是心理安慰?<br>人民群众选择心理安慰。</p>
<p>这个世界的大部分真相是很残酷的:</p>
<p>家境贫寒的你的努力奋斗,在现在这个时代,不仅是中国,也包括社会阶层固化的英国,很多时候比不上别人显赫家族的一句话。</p>
<p>你从小就被告知,每个人的智商差别都不大,重要的是努力与否。然而后来你却渐渐发现。这世上确实是有智商这种东西的,小学初中题目简单不觉得,上了高中有人不听课期末稍微复习一下照样考第一,你每天晚自习学到11点依然是倒数。</p>
<p>这世界从来没公平过,真相永远这么残酷。</p>
<p>人民群众既然无力改变,只能编造谎言,给自己制造麻醉剂。</p>
<p>这些鸡汤文、成功学的背后有广泛的群众基础,市场需求。</p>
<p>最后,不好意思我也骗了你,如果你能看到这里。在反鸡汤的过程中也出现了很多谣言。比如我在之前举得比尔盖茨和巴菲特的例子:</p>
<p>“盖茨的书不会告诉你他母亲是IBM董事,是她给儿子促成了第一单大生意,巴菲特的书只会告诉你他8岁就知道去参观纽交所,但不会告诉你他国会议员的父亲带他去的,是高盛的董事接待的。”</p>
<p>我在写作过程中查阅了百科和维基,对他们的父母都有介绍:</p>
<p>盖茨的母亲</p>
<p>玛丽·麦克斯韦尔·盖茨 (英语:Mary Maxwell Gates,1929年7月5日-1994年6月10日)是一名美国商业界人士。</p>
<p>盖茨曾于1975年至1993年任华盛顿大学校董事。她是金县联合劝募协会(King County’s United Way)的首名女性总裁,还是全国联合劝募协会(United Way of America)执行理事会的首名女性主席。值得注意的是,她在任全国联合劝募协会执行理事会主席期间与IBM的首席执行官约翰埃克斯(John Fellows Akers)共事。她还是第一洲际银行公司(First Interstate Bank)的首名女性主管。玛丽之子威廉·亨利·盖茨三世(比尔·盖茨)是微软公司的创建者之一。1</p>
<p>也就是说,盖茨的母亲并不是IBM的董事,她与IBM唯一的交集,就是与其执行官共事过。</p>
<p>巴菲特的父亲:</p>
<p>Howard Homan Buffett (August 13, 1903 – April 30, 1964) was an American businessman, investor, and politician. He was a four-term Republican United States Representative. He was the father of Warren Buffett, a billionaire businessman and investor.2</p>
<p>沃伦-巴菲特生于1930年,他的父亲霍华德-巴菲特的确是美国国会众议员,但他是在1942年才第一次当选的,当时巴菲特已经12岁了。也就是说,当巴菲特“在8岁的时候参观纽约证交所”时,根本不可能受到国会议员一般的待遇。</p>
<p>显然盖茨和巴菲特家境是不错,他们的父母也都能给他们以一定的帮助,但并没达到如此夸张的地步。</p>
<p>还有爱迪生那后半句话,与前半句谣传流传的以一样广,如果有爱迪生说过这句话一定有对应的原文,可在英文世界里并没有。唯一对的上号的是这么两句话:</p>
<p>None of my inventions came by accident. I see a worthwhile need to be met and I make trial after trial until it comes. What it boils down to is one per cent inspiration and ninety-nine per cent perspiration.3</p>
<p>我没有一项发明是碰巧得来的。当看到了一个值得人们投入精力、物力的社会需求有待满足后,我就一次又一次地做实验,直到它化为现实。这得最终得归于百分之一的灵感和百分之九十九的汗水。</p>
<p>Genius is one percent inspiration and ninety-nine per cent perspiration. Accordingly, a ‘genius’ is often merely a talented person who has done all of his or her homework.</p>
<p>这和中文版本的前半句一样,后面多的这半句大意是“因此,天才不过是一个经常能完成自己工作的聪明人而已”。也没有说“灵感更重要”。</p>
<p>而且这中文世界这广为流传的后半句,和爱迪生一贯为自己打造的努力勤勉的形象不符。</p>
<p>这种为反而反的“反鸡汤”本质上和鸡汤也没什么区别,都是一次对民众精神需求的精准把握。只不过前者是“正能量”的虚幻安慰,后者可以算作是对现实不满的一次感情的宣泄。最后不知会把人引到什么地方去。</p>
<p>原谅我骗了你,这世界本就是一个罗生门,人民要的也不是真相。</p>
]]></content>
<summary type="html">
<![CDATA[<p>心灵鸡汤都是假的。。。反心灵鸡汤也是假的。。。<br>作者:江寒园</p>
<p>今早打开豆瓣刷到一条广播:</p>
<p>这两份名单你认识多少?第一份名单:傅以渐、王式丹、毕沅、林召堂、王云锦、刘子壮、陈沆、刘福姚、刘春霖。第二份名单:李渔、洪昇、顾炎武、金圣叹、黄宗羲、吴敬梓、蒲松龄、洪秀全、袁世凯。哪份名单上你认识的人多一些?答案揭晓:前者全是清朝科举状元;后者全是当时落第秀才。</p>
<p>第一眼看到这条广播,感觉挺有道理的,论据充足,论证有力,实在是安慰今天高考孩子的一剂良药:即使你这次高考没考好,也不代表你这一辈子就完蛋了。看看这份名单上的人,后者全都落榜,但现在流传千古,我们永远怀念他;前者虽然是当年的状元,可现在谁还知道他们。所以,成功和学历并没有半毛钱关系!安心去考,落榜了也能有大好前程!</p>
<p>后来感觉有点不太对。思索了一下,我写了第三份名单:<br>]]>
</summary>
<category term="文章" scheme="http://jssu.github.io/tags/%E6%96%87%E7%AB%A0/"/>
<category term="生活" scheme="http://jssu.github.io/categories/%E7%94%9F%E6%B4%BB/"/>
</entry>
<entry>
<title><![CDATA[FB,后面发面经]]></title>
<link href="http://jssu.github.io/2015/06/20/FB%E5%BA%94%E8%AF%A5%E5%BD%95%E4%BA%86%E5%90%A7/"/>
<id>http://jssu.github.io/2015/06/20/FB应该录了吧/</id>
<published>2015-06-20T19:15:55.000Z</published>
<updated>2015-06-22T20:09:26.000Z</updated>
<content type="html"><![CDATA[<p>我很满意自己的面试。加油!</p>
]]></content>
<summary type="html">
<![CDATA[<p>我很满意自己的面试。加油!</p>
]]>
</summary>
<category term="学习" scheme="http://jssu.github.io/categories/%E5%AD%A6%E4%B9%A0/"/>
</entry>
<entry>
<title><![CDATA[来FB]]></title>
<link href="http://jssu.github.io/2015/06/17/FBInterview/"/>
<id>http://jssu.github.io/2015/06/17/FBInterview/</id>
<published>2015-06-18T04:24:02.000Z</published>
<updated>2015-06-18T01:26:20.000Z</updated>
<content type="html"><![CDATA[<p>一句话!加油!</p>
]]></content>
<summary type="html">
<![CDATA[<p>一句话!加油!</p>
]]>
</summary>
<category term="生活" scheme="http://jssu.github.io/categories/%E7%94%9F%E6%B4%BB/"/>
</entry>
</feed>