• 【HDOJ】3337 Guess the number


    神一样的题目。简言之,利用手段获得测试用例的第一行,输出结果。
    很显然利用wa, TLE, OLE等judge status可以获得测试用例。
    因此,果断Python写一个acm提交机器人。依赖lxml库。
    测试用例是The Ascii of H.

      1 #!/usr/env python
      2 
      3 import urllib2
      4 import urllib
      5 import cookielib
      6 import logging
      7 from urlparse import *
      8 from time import sleep
      9 from lxml import etree
     10 import string
     11 
     12 global url, n_vol
     13 url  = "http://acm.hdu.edu.cn"
     14 n_vol = 46
     15 judgeStatus_xpath = "/html/body/table/tr[4]/td/div/table/tr[2]/td[3]/font"
     16 
     17 def initLog():
     18     # os.chdir(parentDirPath)
     19     suffix = 'hdu'
     20     logging.basicConfig(
     21                 level=logging.DEBUG,
     22                 format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
     23                 datefmt='%a, %d %b %Y %H:%M:%S',
     24                 filename=__file__[:-3]+'_'+suffix+'.log',
     25                 filemode='w')
     26     
     27     
     28 def acmHdu_Login(user, password):
     29     login_page = urljoin(url, "userloginex.php?action=login")
     30     try:
     31         'create cookie'
     32         cj = cookielib.CookieJar()
     33         opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
     34         opener.addheaders = [('User-agent',
     35                  'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)')]
     36         data = urllib.urlencode({"username":user, "userpass":password})
     37         urllib2.install_opener(opener)
     38         urllib2.urlopen(login_page, data)
     39         
     40     except Exception, e:
     41         print "Login unsucceed"
     42         print e
     43     
     44 def acmHdu_Submit(pid, lang, code):
     45     submit_url = urljoin(url, "submit.php?action=submit")
     46     data = urllib.urlencode({"problemid":pid, "language":lang, "usercode":code})
     47     req = urllib2.Request(submit_url, data)
     48     response = urllib2.urlopen(req)
     49     content = response.read()
     50     
     51     
     52 def acmHdu_Status(user, pid, lang):
     53     sleep(5)
     54     status_url = urljoin(url, "status.php?first=&pid=%s&user=%s&lang=%s&status=0" % (str(pid), user, str(lang+1)))
     55     # print status_url
     56     content = urllib2.urlopen(status_url).read()
     57     with open("C:UsershoohDesktopst.html", "w") as fout:
     58         fout.write(content)
     59         
     60         
     61 def acmHdu_Status_Parse(user, pid, lang):
     62     sleep(5)
     63     status_url = urljoin(url, "status.php?first=&pid=%s&user=%s&lang=%s&status=0" % (str(pid), user, str(lang+1)))
     64     # print status_url
     65     content = urllib2.urlopen(status_url).read()
     66     tree = etree.HTML(content)
     67     infoList = map(
     68         lambda ele: ele.text, tree.xpath(judgeStatus_xpath)
     69     )
     70     return infoList[0] if infoList else ""
     71     
     72 
     73 class constForSubmit:
     74     can = ' ' + string.digits + string.uppercase + string.lowercase
     75     pid = 3337
     76     lang = 2
     77     template = string.Template(
     78 '''
     79 /* 3337 */
     80 #include <iostream>
     81 #include <string>
     82 #include <map>
     83 #include <queue>
     84 #include <set>
     85 #include <stack>
     86 #include <vector>
     87 #include <deque>
     88 #include <algorithm>
     89 #include <cstdio>
     90 #include <cmath>
     91 #include <ctime>
     92 #include <cstring>
     93 #include <climits>
     94 #include <cctype>
     95 #include <cassert>
     96 #include <functional>
     97 #include <iterator>
     98 #include <iomanip>
     99 using namespace std;
    100 //#pragma comment(linker,"/STACK:102400000,1024000")
    101 
    102 #define sti                set<int>
    103 #define stpii            set<pair<int, int> >
    104 #define mpii            map<int,int>
    105 #define vi                vector<int>
    106 #define pii                pair<int,int>
    107 #define vpii            vector<pair<int,int> >
    108 #define rep(i, a, n)     for (int i=a;i<n;++i)
    109 #define per(i, a, n)     for (int i=n-1;i>=a;--i)
    110 #define clr                clear
    111 #define pb                 push_back
    112 #define mp                 make_pair
    113 #define fir                first
    114 #define sec                second
    115 #define all(x)             (x).begin(),(x).end()
    116 #define SZ(x)             ((int)(x).size())
    117 #define lson            l, mid, rt<<1
    118 #define rson            mid+1, r, rt<<1|1
    119 
    120 char pattern[105];
    121 int len;
    122 
    123 void init() {
    124     len = 0;
    125     pattern[len++] = ' ';
    126     rep(i, 0, 10)
    127         pattern[len++] = '0'+i;
    128     rep(i, 0, 26)
    129         pattern[len++] = 'A'+i;
    130     rep(i, 0, 26)
    131         pattern[len++] = 'a'+i;
    132     puts(pattern);
    133 }
    134 
    135 void WA() {
    136     puts("Wa");
    137 }
    138 
    139 void TLE() {
    140     while (true);
    141 }
    142 
    143 void OLE() {
    144     while (true)
    145         puts("123");
    146 }
    147 
    148 int main() {
    149     ios::sync_with_stdio(false);
    150     #ifndef ONLINE_JUDGE
    151         freopen("data.in", "r", stdin);
    152         freopen("data.out", "w", stdout);
    153     #endif
    154     
    155     init();
    156     
    157     char line[20];
    158     
    159     gets(line);
    160     int len = strlen(line);
    161     int index = $index;
    162     int th = $th;
    163     
    164     if (index >= len)
    165         OLE();
    166     
    167     if (pattern[th] == line[index]) {
    168         WA();
    169     } else {
    170         while    (true)
    171             ;
    172     }
    173     
    174     #ifndef ONLINE_JUDGE
    175         printf("time = %d.\n", (int)clock());
    176     #endif
    177     
    178     return 0;
    179 }
    180 
    181 
    182 ''')
    183 
    184 class CFS(constForSubmit):
    185     pass
    186     
    187     
    188 def acmHdu_Submit3337(user, pid, lang, template):
    189     maxl = 20
    190     varDict = dict()
    191     cn = len(CFS.can)
    192     ret = ""
    193     for index in xrange(maxl):
    194         flag = False
    195         for th in xrange(cn):
    196             varDict["index"] = index
    197             varDict["th"] = th
    198             code = template.safe_substitute(varDict)
    199             acmHdu_Submit(pid, lang, code)
    200             while True:
    201                 status = acmHdu_Status_Parse(user, pid, lang).strip()
    202                 if status=="Running" or status=="Queuing" or status=="Compiling":
    203                     continue
    204                 print status
    205                 if status == "Wrong Answer":
    206                     ### find the correct char
    207                     ret += CFS.can[th]
    208                     flag = True
    209                     break
    210                 elif status == "Output Limit Exceeded":
    211                     ### find the last char
    212                     logging.debug("Result is %s" % (ret))
    213                     return ret
    214                 elif status == "Time Limit Exceeded":
    215                     break
    216                 else:
    217                     continue
    218             if flag:
    219                 break
    220         logging.debug("%d: ret = %s" % (index, ret))
    221     return ret
    222     
    223     
    224 if __name__ == "__main__":
    225     # user = "Bombe16"
    226     # password = "496528674"
    227     # initLog();
    228     # logging.debug("acm submit begin...")
    229     # acmHdu_Login(user, password)
    230     # acmHdu_Submit(CFS.pid, CFS.lang, CFS.code)
    231     # acmHdu_Status(user, CFS.pid, CFS.lang)
    232     # logging.debug("acm submit end...")
    233     
    234     user = "Bombe16"
    235     password = "496528674"
    236     initLog();
    237     logging.debug("acm submit begin...")
    238     acmHdu_Login(user, password)
    239     line = acmHdu_Submit3337(user, CFS.pid, CFS.lang, CFS.template)
    240     with open("F:Qt_prjhdojdata.in", "w") as fout:
    241         fout.write(line)
    242     logging.debug("acm submit end...")
    243     
  • 相关阅读:
    根据时间类型查询
    @Autowired注解
    如何将jar包引入到本地maven仓库
    org.springframework.beans.factory.BeanDefinitionStoreException
    几个常用的 lombok 注解
    String类底层源码(随时更新)
    如何判断单链表有环
    指定java中Date日期格式化,实现字符串转Date
    vue学习记录
    小说,漫画
  • 原文地址:https://www.cnblogs.com/bombe1013/p/4973307.html
Copyright © 2020-2023  润新知