在Linux下玩ACM的一个实用工具


如果你想稍微提高一下做ACM题目的效率的话,下面这个工具应该很适合你。我的这个工具名称叫做 sgxiao_acm.pl ,是一个perl写成的脚本,用于生成一个简单的代码C/C++/Java代码,并且自动添加上注释,注释的内容包括代码建立的日期,作者,以及那个题目的title。生成的结果如下所示:
  1. /*  
  2.  * Auther : sgxiao  
  3.  * Title : Unit Fraction Partition  
  4.  * Date : 2011-03-20  
  5.  */  
  6. #include<iostream>   
  7. using namespace std;   
  8. int main(void)   
  9.     int i, j, k;   
  10.     int n;   
  11.     return 0;   
  12. }   

    上述代码是我输入了POJ上面的1980号题目之后生成的结果,这个结果将保存为1980_PKU.cpp。值得注意的是,1980这个题目的title已经自动在注释里面了,这就省却了我们到每个题目的page上面去拷贝题目的时间了。

    要完成上述功能,可以将该工具放置在linux的某个目录下面,我先假设你是放置在自己的home目录下面,然后输入如下命令:

$ ./sgxiao_acm.pl

please input the id of the problem: 1980

please input the school (PKU ZJU UVa Ural): pku

please input the LANG (c c++ Java): C++

回车之后,会在当前目录生成 1980_PKU.cpp 的文件了。如果你希望一步到位,也可以以参数的形式输入:

$ ./sgxiao_acm.pl --id=1980 --lang=c++ school=pku

    如果你经常游荡于各地的OnlineJudge,例如国内著名的北大POJ、浙大ZJU,又或者是国外的UVa 或者是Ural的话。用了这个工具能够稍微提高一下你的A题效率,当然我认为该工具至少还能够再更强大一点点。如果你有兴趣,你可以在日常使用的过程中,再帮忙进行必要的修改。让这个工具支持更多的OJ系统,支持更多的语言生成的代码,以及支持更多方便提高工作效率的各种其他特性。目前这个工具还缺乏一个比较好的--help,不知道谁能帮忙添加上去。修改过后希望你也能把修改过后的版本发布出来供大家分享。我真心希望这个工具能成为每个ACMer都必用的工具。

    该工具使用了perl的一个外部库,叫做LWP,用作抓取problem页面的title。但是可能在某些发行版上的perl版本并不默认安装这这个库,所以你必须自行添加。

   同时我也希望这个工具能能在大家的共同努力下面,移植到更多的平台下面。我目前是在Ubuntu 8.04的版本下面使用的。

当前版本的代码如下:

  1. # file : sgxiao_acm.pl   
  2. #!/usr/bin/perl   
  3. use LWP::Simple;   
  4. use LWP;   
  5. use strict;   
  6. use HTML::TreeBuilder 3;   
  7. use Getopt::Long;   
  8. $ENV{"LANG"} = "C";   
  9. $ENV{"LC_ALL"} = "C";   
  10. ##-----------------------------   
  11. my $browser = LWP::UserAgent->new;   
  12. sub do_GET {   
  13.     my $resp = $browser->get(@_);   
  14.   return ($resp->content, $resp->status_line, $resp->is_success, $resp)   
  15.       if wantarray;   
  16.     return unless $resp->is_success;   
  17.     return $resp->content;   
  18. }   
  19. ##-----------------------------   
  20. sub do_POST {   
  21.     my $resp = $browser->post(@_);   
  22.     return ($resp->content, $resp->status_line, $resp->is_success, $resp)   
  23.         if wantarray;   
  24.     return unless $resp->is_success;   
  25.     return $resp->content;   
  26. }   
  27.   
  28. sub get_PKU_title {   
  29.     my $num = shift @_;   
  30.     my $content = do_GET('http://poj.org/problem?id='.$num);   
  31.        
  32.     my $root = HTML::TreeBuilder->new_from_content($content);   
  33.        
  34.        
  35.     my @inputs = $root -> find_by_attribute("class""ptt");   
  36.     my $title = $inputs[0] -> as_text;   
  37.        
  38.     return $title;   
  39. }   
  40. sub get_ZJU_title {   
  41.     my $num = shift @_;   
  42.     my $content = do_GET('http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode='.$num);   
  43.        
  44.     my $root = HTML::TreeBuilder->new_from_content($content);   
  45.        
  46.     my @inputs = $root -> find_by_attribute("class""bigProblemTitle");   
  47.     my $title = $inputs[0] -> as_text;   
  48.        
  49.     return $title;   
  50. }   
  51. ## TODO   
  52. sub get_UVa_title {   
  53.     my $num = shift @_;   
  54.     my $content = do_GET('http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode='.$num);   
  55.     my $root = HTML::TreeBuilder->new_from_content($content);   
  56.     my @inputs = $root -> find_by_attribute("class""bigProblemTitle");   
  57.     my $title = $inputs[0] -> as_text;   
  58.     return $title;   
  59. }   
  60. sub get_Ural_title {   
  61.     my $num = shift @_;   
  62.     my $content = do_GET('http://acm.timus.ru/problem.aspx?space=1&num='.$num);   
  63.     my $root = HTML::TreeBuilder->new_from_content($content);   
  64.     my @inputs = $root -> find_by_attribute("class""problem_title");   
  65.     my $title = $inputs[0] -> as_text;   
  66.     return $title;   
  67. }   
  68. # param  $auther $title   
  69. # return $text   
  70. sub source_java {   
  71.     my $auther = shift @_;   
  72.     chomp $auther;   
  73.     my $title = shift @_;   
  74.     chomp $title;   
  75.     my $date = `date "+%F"`;   
  76.     chomp $date;   
  77.     my $text ="/**   
  78.  * Auther : $auther   
  79.  * Title : $title   
  80.  * Date : $date   
  81.  */   
  82. import java.util.*;   
  83. import java.math.*;   
  84. class Main {   
  85.     public static void main(String argv[]) {   
  86.         Scanner scanner = new Scanner(System.in);   
  87. // coding here   
  88.     }   
  89. }   
  90. ";   
  91.     return $text;   
  92. }   
  93. # param  $auther $title   
  94. # return $text   
  95. sub source_c {   
  96.     my $auther = shift @_;   
  97.     chomp $auther;   
  98.     my $title = shift @_;   
  99.     chomp $title;   
  100.     my $date = `date "+%F"`;   
  101.     chomp $date;   
  102.     my $text ="/*   
  103.  * Auther : $auther   
  104.  * Title : $title   
  105.  * Date : $date   
  106.  */   
  107. #include<stdio.h>   
  108. int main(void)   
  109. {   
  110.     int i, j, k;   
  111.     int n;   
  112.     return 0;   
  113. }   
  114. ";   
  115.     return $text;   
  116. }   
  117. # param  $auther $title   
  118. # return $text   
  119. sub source_cpp {   
  120.     my $auther = shift @_;   
  121.     chomp $auther;   
  122.     my $title = shift @_;   
  123.     chomp $title;   
  124.     my $date = `date "+%F"`;   
  125.     chomp $date;   
  126.     my $text ="/*   
  127.  * Auther : $auther   
  128.  * Title : $title   
  129.  * Date : $date   
  130.  */   
  131. #include<iostream>   
  132. using namespace std;   
  133. int main(void)   
  134.     int i, j, k;   
  135.     int n;   
  136.     return 0;   
  137. }   
  138. ";   
  139.     return $text;   
  140. }   
  141. ## main   
  142. my $SCHOOL;                     # PKU ZOJ UVa Ural   
  143. my $LANG;                       # Java C C++ ## TODO  add pascal   
  144. my $auther = `whoami`;          # auther   
  145. my $id;   
  146. my $title = '';                 # Problem 's title   
  147. #print STDERR "Welcome to use sgxiao's tools.\n";   
  148. GetOptions ('school=s' => \$SCHOOL,   
  149.             'lang=s' => \$LANG,   
  150.             'id=i' => \$id);   
  151. if(!defined($id)) {   
  152.     print "please input the id of the problem: ";   
  153.     $id=<>;   
  154.     chomp $id;   
  155. }   
  156. if(!defined($SCHOOL)) {   
  157.     print "please input the school (PKU ZJU UVa Ural): ";   
  158.     $SCHOOL=<>;   
  159.     chomp $SCHOOL;   
  160. }   
  161. if(!defined($LANG)) {   
  162.     print "please input the LANG (c c++ Java): ";   
  163.     $LANG=<>;   
  164.     chomp $LANG;   
  165. }   
  166. if($SCHOOL =~ m/PKU|Poj/i) {   
  167.     $title = get_PKU_title($id);   
  168.     $SCHOOL = 'PKU';   
  169. }   
  170. elsif($SCHOOL =~ m/ZJU|Zoj/i) {   
  171.         $title = get_ZJU_title($id);   
  172.         $SCHOOL = 'ZJU';   
  173. }   
  174. elsif($SCHOOL =~ m/UVa/i) {   
  175.         # TODO $title = get_UVa_title($id);   
  176. }   
  177. elsif($SCHOOL =~ m/Ural/i) {   
  178.         $title = get_Ural_title($id);   
  179.         $SCHOOL = 'Ural';   
  180. }   
  181. else {   
  182.     die "Error!!please input the School(PKU ZJU Ural UVa!!)";   
  183. }   
  184. my $text;   
  185. my $postfix;   
  186. if($LANG =~ m/c\+\+/i) {   
  187.     $text = source_cpp($auther, $title);   
  188.     $postfix = '.cpp';   
  189. } elsif($LANG =~ m/java/i) {   
  190.     $text = source_java($auther, $title);   
  191.     $postfix = '.java';   
  192. } elsif($LANG =~ m/c/i) {   
  193.     $text = source_c($auther, $title);   
  194.     $postfix = '.c';   
  195. }   
  196. my $file = "ACM/$id\_$SCHOOL$postfix";   
  197. open TEXT, ">$file" or die $!;   
  198. print TEXT $text;   
  199. close TEXT;  

相关内容