在Linux下玩ACM的一个实用工具
在Linux下玩ACM的一个实用工具
如果你想稍微提高一下做ACM题目的效率的话,下面这个工具应该很适合你。我的这个工具名称叫做 sgxiao_acm.pl ,是一个perl写成的脚本,用于生成一个简单的代码C/C++/Java代码,并且自动添加上注释,注释的内容包括代码建立的日期,作者,以及那个题目的title。生成的结果如下所示:- /*
- * Auther : sgxiao
- * Title : Unit Fraction Partition
- * Date : 2011-03-20
- */
- #include<iostream>
- using namespace std;
- int main(void)
- int i, j, k;
- int n;
- return 0;
- }
上述代码是我输入了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的版本下面使用的。
当前版本的代码如下:
- # file : sgxiao_acm.pl
- #!/usr/bin/perl
- use LWP::Simple;
- use LWP;
- use strict;
- use HTML::TreeBuilder 3;
- use Getopt::Long;
- $ENV{"LANG"} = "C";
- $ENV{"LC_ALL"} = "C";
- ##-----------------------------
- my $browser = LWP::UserAgent->new;
- sub do_GET {
- my $resp = $browser->get(@_);
- return ($resp->content, $resp->status_line, $resp->is_success, $resp)
- if wantarray;
- return unless $resp->is_success;
- return $resp->content;
- }
- ##-----------------------------
- sub do_POST {
- my $resp = $browser->post(@_);
- return ($resp->content, $resp->status_line, $resp->is_success, $resp)
- if wantarray;
- return unless $resp->is_success;
- return $resp->content;
- }
- sub get_PKU_title {
- my $num = shift @_;
- my $content = do_GET('http://poj.org/problem?id='.$num);
- my $root = HTML::TreeBuilder->new_from_content($content);
- my @inputs = $root -> find_by_attribute("class", "ptt");
- my $title = $inputs[0] -> as_text;
- return $title;
- }
- sub get_ZJU_title {
- my $num = shift @_;
- my $content = do_GET('http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode='.$num);
- my $root = HTML::TreeBuilder->new_from_content($content);
- my @inputs = $root -> find_by_attribute("class", "bigProblemTitle");
- my $title = $inputs[0] -> as_text;
- return $title;
- }
- ## TODO
- sub get_UVa_title {
- my $num = shift @_;
- my $content = do_GET('http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode='.$num);
- my $root = HTML::TreeBuilder->new_from_content($content);
- my @inputs = $root -> find_by_attribute("class", "bigProblemTitle");
- my $title = $inputs[0] -> as_text;
- return $title;
- }
- sub get_Ural_title {
- my $num = shift @_;
- my $content = do_GET('http://acm.timus.ru/problem.aspx?space=1&num='.$num);
- my $root = HTML::TreeBuilder->new_from_content($content);
- my @inputs = $root -> find_by_attribute("class", "problem_title");
- my $title = $inputs[0] -> as_text;
- return $title;
- }
- # param $auther $title
- # return $text
- sub source_java {
- my $auther = shift @_;
- chomp $auther;
- my $title = shift @_;
- chomp $title;
- my $date = `date "+%F"`;
- chomp $date;
- my $text ="/**
- * Auther : $auther
- * Title : $title
- * Date : $date
- */
- import java.util.*;
- import java.math.*;
- class Main {
- public static void main(String argv[]) {
- Scanner scanner = new Scanner(System.in);
- // coding here
- }
- }
- ";
- return $text;
- }
- # param $auther $title
- # return $text
- sub source_c {
- my $auther = shift @_;
- chomp $auther;
- my $title = shift @_;
- chomp $title;
- my $date = `date "+%F"`;
- chomp $date;
- my $text ="/*
- * Auther : $auther
- * Title : $title
- * Date : $date
- */
- #include<stdio.h>
- int main(void)
- {
- int i, j, k;
- int n;
- return 0;
- }
- ";
- return $text;
- }
- # param $auther $title
- # return $text
- sub source_cpp {
- my $auther = shift @_;
- chomp $auther;
- my $title = shift @_;
- chomp $title;
- my $date = `date "+%F"`;
- chomp $date;
- my $text ="/*
- * Auther : $auther
- * Title : $title
- * Date : $date
- */
- #include<iostream>
- using namespace std;
- int main(void)
- int i, j, k;
- int n;
- return 0;
- }
- ";
- return $text;
- }
- ## main
- my $SCHOOL; # PKU ZOJ UVa Ural
- my $LANG; # Java C C++ ## TODO add pascal
- my $auther = `whoami`; # auther
- my $id;
- my $title = ''; # Problem 's title
- #print STDERR "Welcome to use sgxiao's tools.\n";
- GetOptions ('school=s' => \$SCHOOL,
- 'lang=s' => \$LANG,
- 'id=i' => \$id);
- if(!defined($id)) {
- print "please input the id of the problem: ";
- $id=<>;
- chomp $id;
- }
- if(!defined($SCHOOL)) {
- print "please input the school (PKU ZJU UVa Ural): ";
- $SCHOOL=<>;
- chomp $SCHOOL;
- }
- if(!defined($LANG)) {
- print "please input the LANG (c c++ Java): ";
- $LANG=<>;
- chomp $LANG;
- }
- if($SCHOOL =~ m/PKU|Poj/i) {
- $title = get_PKU_title($id);
- $SCHOOL = 'PKU';
- }
- elsif($SCHOOL =~ m/ZJU|Zoj/i) {
- $title = get_ZJU_title($id);
- $SCHOOL = 'ZJU';
- }
- elsif($SCHOOL =~ m/UVa/i) {
- # TODO $title = get_UVa_title($id);
- }
- elsif($SCHOOL =~ m/Ural/i) {
- $title = get_Ural_title($id);
- $SCHOOL = 'Ural';
- }
- else {
- die "Error!!please input the School(PKU ZJU Ural UVa!!)";
- }
- my $text;
- my $postfix;
- if($LANG =~ m/c\+\+/i) {
- $text = source_cpp($auther, $title);
- $postfix = '.cpp';
- } elsif($LANG =~ m/java/i) {
- $text = source_java($auther, $title);
- $postfix = '.java';
- } elsif($LANG =~ m/c/i) {
- $text = source_c($auther, $title);
- $postfix = '.c';
- }
- my $file = "ACM/$id\_$SCHOOL$postfix";
- open TEXT, ">$file" or die $!;
- print TEXT $text;
- close TEXT;
评论暂时关闭