【HDOJ】3337 Guess the number
神一样的题目。简言之,利用手段获得测试用例的第一行,输出结果。
很显然利用wa, TLE, OLE等judge status可以获得测试用例。
因此,果断Python写一个acm提交机器人。依赖lxml库。
测试用例是The Ascii of H.
#!/usr/env python import urllib2
import urllib
import cookielib
import logging
from urlparse import *
from time import sleep
from lxml import etree
import string global url, n_vol
url = "http://acm.hdu.edu.cn"
n_vol = 46
judgeStatus_xpath = "/html/body/table/tr[4]/td/div/table/tr[2]/td[3]/font" def initLog():
# os.chdir(parentDirPath)
suffix = 'hdu'
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S',
filename=__file__[:-3]+'_'+suffix+'.log',
filemode='w') def acmHdu_Login(user, password):
login_page = urljoin(url, "userloginex.php?action=login")
try:
'create cookie'
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
opener.addheaders = [('User-agent',\
'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)')]
data = urllib.urlencode({"username":user, "userpass":password})
urllib2.install_opener(opener)
urllib2.urlopen(login_page, data) except Exception, e:
print "Login unsucceed"
print e def acmHdu_Submit(pid, lang, code):
submit_url = urljoin(url, "submit.php?action=submit")
data = urllib.urlencode({"problemid":pid, "language":lang, "usercode":code})
req = urllib2.Request(submit_url, data)
response = urllib2.urlopen(req)
content = response.read() def acmHdu_Status(user, pid, lang):
sleep(5)
status_url = urljoin(url, "status.php?first=&pid=%s&user=%s&lang=%s&status=0" % (str(pid), user, str(lang+1)))
# print status_url
content = urllib2.urlopen(status_url).read()
with open("C:\Users\hooh\Desktop\st.html", "w") as fout:
fout.write(content) def acmHdu_Status_Parse(user, pid, lang):
sleep(5)
status_url = urljoin(url, "status.php?first=&pid=%s&user=%s&lang=%s&status=0" % (str(pid), user, str(lang+1)))
# print status_url
content = urllib2.urlopen(status_url).read()
tree = etree.HTML(content)
infoList = map(
lambda ele: ele.text, tree.xpath(judgeStatus_xpath)
)
return infoList[0] if infoList else "" class constForSubmit:
can = ' ' + string.digits + string.uppercase + string.lowercase
pid = 3337
lang = 2
template = string.Template(\
'''
/* 3337 */
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 char pattern[105];
int len; void init() {
len = 0;
pattern[len++] = ' ';
rep(i, 0, 10)
pattern[len++] = '0'+i;
rep(i, 0, 26)
pattern[len++] = 'A'+i;
rep(i, 0, 26)
pattern[len++] = 'a'+i;
puts(pattern);
} void WA() {
puts("Wa");
} void TLE() {
while (true);
} void OLE() {
while (true)
puts("123");
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif init(); char line[20]; gets(line);
int len = strlen(line);
int index = $index;
int th = $th; if (index >= len)
OLE(); if (pattern[th] == line[index]) {
WA();
} else {
while (true)
;
} #ifndef ONLINE_JUDGE
printf("time = %d.\\n", (int)clock());
#endif return 0;
} ''') class CFS(constForSubmit):
pass def acmHdu_Submit3337(user, pid, lang, template):
maxl = 20
varDict = dict()
cn = len(CFS.can)
ret = ""
for index in xrange(maxl):
flag = False
for th in xrange(cn):
varDict["index"] = index
varDict["th"] = th
code = template.safe_substitute(varDict)
acmHdu_Submit(pid, lang, code)
while True:
status = acmHdu_Status_Parse(user, pid, lang).strip()
if status=="Running" or status=="Queuing" or status=="Compiling":
continue
print status
if status == "Wrong Answer":
### find the correct char
ret += CFS.can[th]
flag = True
break
elif status == "Output Limit Exceeded":
### find the last char
logging.debug("Result is %s" % (ret))
return ret
elif status == "Time Limit Exceeded":
break
else:
continue
if flag:
break
logging.debug("%d: ret = %s" % (index, ret))
return ret if __name__ == "__main__":
# user = "Bombe16"
# password = "496528674"
# initLog();
# logging.debug("acm submit begin...")
# acmHdu_Login(user, password)
# acmHdu_Submit(CFS.pid, CFS.lang, CFS.code)
# acmHdu_Status(user, CFS.pid, CFS.lang)
# logging.debug("acm submit end...") user = "Bombe16"
password = ""
initLog();
logging.debug("acm submit begin...")
acmHdu_Login(user, password)
line = acmHdu_Submit3337(user, CFS.pid, CFS.lang, CFS.template)
with open("F:\Qt_prj\hdoj\data.in", "w") as fout:
fout.write(line)
logging.debug("acm submit end...")
【HDOJ】3337 Guess the number的更多相关文章
- 【HDOJ】1394 Minimum Inversion Number
逆序数的性质.1. 暴力解 #include <stdio.h> #define MAXNUM 5005 int a[MAXNUM]; int main() { int n; int i, ...
- 【贪心】codeforces D. Minimum number of steps
http://codeforces.com/contest/805/problem/D [思路] 要使最后的字符串不出现ab字样,贪心的从后面开始更换ab为bba,并且字符串以"abbbb. ...
- 【LeetCode】9、Palindrome Number(回文数)
题目等级:Easy 题目描述: Determine whether an integer is a palindrome. An integer is a palindrome when it rea ...
- 【leetcode】668. Kth Smallest Number in Multiplication Table
题目如下: 解题思路:几乎和[leetcode]719. Find K-th Smallest Pair Distance 的方法一样.只不过一个是减法一个是乘法,还有一点区别是[leetcode]7 ...
- 【HDOJ】3948 The Number of Palindromes
后缀数组求不重复回文子串数目.注意dp数组. /* 3948 */ #include <iostream> #include <sstream> #include <st ...
- 【HDOJ】4162 Shape Number
循环串的最小表示法. /* */ #include <iostream> #include <string> #include <map> #include < ...
- 【HDOJ】3509 Buge's Fibonacci Number Problem
快速矩阵幂,系数矩阵由多个二项分布组成.第1列是(0,(a+b)^k)第2列是(0,(a+b)^(k-1),0)第3列是(0,(a+b)^(k-2),0,0)以此类推. /* 3509 */ #inc ...
- 【HDOJ】1018 Big Number
数学题,还是使用log避免大数,但是不要忘记需要+1,因为0也是1位,log(100)= 2,但却是3位. #include <stdio.h> #include <math.h&g ...
- 【HDOJ】3006 The Number of set
数据量这么小,果断状态压缩+dp. /* 3006 */ #include <iostream> #include <string> #include <map> ...
随机推荐
- 在jsp中获取服务器端的时间
以前只知道在js中这样写:var date = new Date() : 但是这得到的是客户浏览器的时间,并不是服务器的时间. 当然可以通过在Java文件中添加属性,再在js中通过document.g ...
- 使用 vmstat 监测系统性能
在linux/unix下,vmstat是常用的系统性能监测工具.常用用法如下 vmstat 1 10 表示以1秒为间隔,做相关参数的采样,一共10次.输出范例如下: procs ----------- ...
- Oracle笔记(三)单行函数
-函数 函数像一个黑盒子一样(看不到里边的构造),有参数返回值,可以为我们完成一定的功能. -单行 这种函数会对结果中的每一行计算一次,每行返回一个结果,单行概念区别于分组函数. 单行函数主要分为以下 ...
- 选择结构if语句和switch语句的区别
------- android培训.java培训.期待与您交流! ---------- 1.选择结构if语句格式及其使用 A:if语句的格式: if(比较表达式1) { 语句体1; }else if( ...
- 用函数的地址调用函数 C++ MFC
先说一段废话,幸亏我汇编基础(基础 基础 基础)扎实,否则这个问题真的恶心到我了正题 因为有特殊需求, 需要写一个类似于接口的功能,但是因为用到的次数不多再加上C++写接口各种麻烦所以想通过函数的地 ...
- Arrays 标准库算法
Binary Search public static int binarySearch0(Object[] a, int fromIndex, int toIndex, Object key) { ...
- PHP页面间参数传递的四种方法详解
2016-04-16 定义page01.php和page02.php两个php文件,将page01中的内容想办法传递到page02,然后供我们继续使用.------------------------ ...
- remastersys修改默认选项
1.vim /etc/remastersys/isolinux/isolinux.cfg.vesamenu default vesamenu.c32prompt 0timeout 100 menu t ...
- Git-it:一个学习Git和Github的教程(软件)
Git-it https://github.com/jlord/git-it 2016-08-01 在FreeCodeCamp的引导下了解到的Git-it.OSC有收录. Git-it是一个指导使用G ...
- head 头标签(转发)
HTML head 头标签 paddingme | 04 Oct 2014 HTML head 头部分的标签.元素有很多,涉及到浏览器对网页的渲染,SEO 等等,而各个浏览器内核以及各个国内浏览器厂商 ...