【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:\Users\hooh\Desktop\st.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_prj\hdoj\data.in", "w") as fout:
241         fout.write(line)
242     logging.debug("acm submit end...")
243     

 

转载于:https://www.cnblogs.com/bombe1013/p/4973307.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值