0. 写在前面

本文记录了一个AC自动机的诞生!

之前看过有人用C++写过AC自动机,也有用C#写的,还有一个用nodejs写的。。

感觉他们的代码过于冗长,而且AC率也不是很理想。

刚好在回宿舍的路上和学弟聊起这个事

随意想了想思路,觉得还是蛮简单的,就顺手写了一个,效果,还可以接受。

先上个图吧:

最后应该还可以继续刷,如果修改代码或者再添加以下其他搜索引擎可以AC更多题,

不过我有意控制在3000这个AC量,也有意跟在五虎上将之后。

1. 爬虫思路

思路其实非常清晰:

  1. 模拟登录HDU
  2. 针对某一道题目
    • 搜索AC代码

      • 通过正则表达式进行代码的提取
      • 通过htmlparser进行代码的处理
    • 提交
      • 若AC,返回2
      • 否则,继续提交代码(这里最多只提交10份代码)
      • 10次提交后还未AC,放弃此题

2. 简单粗暴的代码

#coding='utf-8'
import requests, re, os, HTMLParser, time, getpass host_url = 'http://acm.hdu.edu.cn'
post_url = 'http://acm.hdu.edu.cn/userloginex.php?action=login'
sub_url = 'http://acm.hdu.edu.cn/submit.php?action=submit'
csdn_url = 'http://so.csdn.net/so/search/s.do'
head = { 'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36' }
html_parser = HTMLParser.HTMLParser()
s = requests.session() def login(usr,psw):
s.get(host_url);
data = {'username':usr,'userpass':psw,'login':'Sign In'}
r = s.post(post_url,data=data) def check_lan(lan):
if 'java' in lan:
return '5'
return '0' def parser_code(code):
return html_parser.unescape(code).encode('utf-8') def is_ac(pid,usr):
tmp = requests.get('http://acm.hdu.edu.cn/userstatus.php?user='+usr).text
accept = re.search('List of solved problems</font></h3>.*?<p align=left><script language=javascript>(.*?)</script><br></p>',tmp,re.S)
if pid in accept.group(1):
print '%s was solved' %pid
return True
else:
return False def search_csdn(PID,usr):
get_data = { 'q':'HDU ' + PID, 't':'blog', 'o':'', 's':'', 'l':'null' }
search_html = requests.get(csdn_url,params=get_data).text
linklist = re.findall('<dd class="search-link"><a href="(.*?)" target="_blank">',search_html,re.S)
for l in linklist:
print l
tm_html = requests.get(l,headers=head).text;
title = re.search('<title>(.*?)</title>',tm_html,re.S).group(1).lower()
if PID not in title:
continue
if 'hdu' not in title:
continue
tmp = re.search('name="code" class="(.*?)">(.*?)</pre>',tm_html,re.S)
if tmp == None:
print 'code not find'
continue
LAN = check_lan(tmp.group(1))
CODE = parser_code(tmp.group(2))
if r'include' in CODE:
pass
elif r'import java' in CODE:
pass
else:
continue
print PID, LAN
print '--------------'
submit_data = { 'check':'0', 'problemid':PID, 'language':LAN, 'usercode':CODE }
s.post(sub_url,headers=head,data=submit_data)
time.sleep(5)
if is_ac(PID,usr):
break if __name__ == '__main__':
usr = raw_input('input your username:')
psw = getpass.getpass('input your password:')
login(usr,psw)
pro_cnt = 1000
while pro_cnt <= 5679:
PID = str(pro_cnt)
if is_ac(PID,usr):
pro_cnt += 1
continue
search_csdn(PID,usr)
pro_cnt += 1

代码不长,仅仅只有78行,是的,就是这样!

3. TDDO

目前没有打算完善这篇博客,也不推荐去研究这个东西,推荐的是去学习真正的算法,哈哈!

很久很久以前自己写过的AC自动机,,,,贴一发:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
#define clr( a, b ) memset( a, b, sizeof(a) )
const int SIGMA_SIZE = 26;
const int NODE_SIZE = 500000 + 10; struct ac_automaton{
int ch[ NODE_SIZE ][ SIGMA_SIZE ];
int f[ NODE_SIZE ], val[ NODE_SIZE ], last[ NODE_SIZE ];
int sz;
void init(){
sz = 1;
clr( ch[0], 0 ), clr( val, 0 );
}
void insert( char *s ){
int u = 0, i = 0;
for( ; s[i]; ++i ){
int c = s[i] - 'a';
if( !ch[u][c] ){
clr( ch[sz], 0 );
val[sz] = 0;
ch[u][c] = sz++;
}
u = ch[u][c];
}
val[u]++;
}
void getfail(){
queue<int> q;
f[0] = 0;
for( int c = 0; c < SIGMA_SIZE; ++c ){
int u = ch[0][c];
if( u ) f[u] = 0, q.push(u), last[u] = 0;
}
while( !q.empty() ){
int r = q.front(); q.pop();
for( int c = 0; c < SIGMA_SIZE; ++c ){
int u = ch[r][c];
if( !u ){
ch[r][c] = ch[ f[r] ][c];
continue;
}
q.push( u );
int v = f[r];
while( v && !ch[v][c] ) v = f[v];
f[u] = ch[v][c];
last[u] = val[ f[u] ] ? f[u] : last[ f[u] ];
}
}
}
int work( char* s ){
int res = 0;
int u = 0, i = 0, e;
for( ; s[i]; ++i ){
int c = s[i] - 'a';
u = ch[u][c];
e = u;
while( val[e] ){
res += val[e];
val[e] = 0;
e = last[e];
}
}
return res;
}
}ac;

python爬虫学习(11) —— 也写个AC自动机的更多相关文章

  1. python爬虫学习 —— 总目录

    开篇 作为一个C党,接触python之后学习了爬虫. 和AC算法题的快感类似,从网络上爬取各种数据也很有意思. 准备写一系列文章,整理一下学习历程,也给后来者提供一点便利. 我是目录 听说你叫爬虫 - ...

  2. python爬虫学习(1) —— 从urllib说起

    0. 前言 如果你从来没有接触过爬虫,刚开始的时候可能会有些许吃力 因为我不会从头到尾把所有知识点都说一遍,很多文章主要是记录我自己写的一些爬虫 所以建议先学习一下cuiqingcai大神的 Pyth ...

  3. Python爬虫学习:二、爬虫的初步尝试

    我使用的编辑器是IDLE,版本为Python2.7.11,Windows平台. 本文是博主原创随笔,转载时请注明出处Maple2cat|Python爬虫学习:二.爬虫的初步尝试 1.尝试抓取指定网页 ...

  4. 《Python爬虫学习系列教程》学习笔记

    http://cuiqingcai.com/1052.html 大家好哈,我呢最近在学习Python爬虫,感觉非常有意思,真的让生活可以方便很多.学习过程中我把一些学习的笔记总结下来,还记录了一些自己 ...

  5. python爬虫学习笔记(一)——环境配置(windows系统)

    在进行python爬虫学习前,需要进行如下准备工作: python3+pip官方配置 1.Anaconda(推荐,包括python和相关库)   [推荐地址:清华镜像] https://mirrors ...

  6. [转]《Python爬虫学习系列教程》

    <Python爬虫学习系列教程>学习笔记 http://cuiqingcai.com/1052.html 大家好哈,我呢最近在学习Python爬虫,感觉非常有意思,真的让生活可以方便很多. ...

  7. Python爬虫学习02--pyinstaller

    Python爬虫学习02--打包exe可执行程序 1.上一次做了一个爬虫爬取电子书的Python程序,然后发现可以通过pyinstaller进行打包成exe可执行程序.发现非常简单好用 2.这是上次写 ...

  8. Python爬虫学习第一记 (翻译小助手)

    1 # Python爬虫学习第一记 8.24 (代码有点小,请放大看吧) 2 3 #实现有道翻译,模块一: $fanyi.py 4 5 import urllib.request 6 import u ...

  9. Python爬虫学习:三、爬虫的基本操作流程

    本文是博主原创随笔,转载时请注明出处Maple2cat|Python爬虫学习:三.爬虫的基本操作与流程 一般我们使用Python爬虫都是希望实现一套完整的功能,如下: 1.爬虫目标数据.信息: 2.将 ...

随机推荐

  1. 广州的小伙伴福利-由微软组织的在广州SQL Server线下活动

    请按照如下格式报名.

  2. javaScript笔记

    两边符号是tab键上面的那个键,不是单引号 即在sort内置的函数中先将各字符串转化为统一的大写或者小写,再进行比较即可. -------------------------------------- ...

  3. 没有神话,聊聊decimal的“障眼法”

    0x00 前言 在上一篇文章<妥协与取舍,解构C#中的小数运算>的留言区域有很多朋友都不约而同的说道了C#中的decimal类型.事实上之前的那篇文章的立意主要在于聊聊使用二进制的计算机是 ...

  4. Win10桌面预览版14316更新内容大全

    下载更新: 安装之后右下角: Win10桌面预览版14316更新内容:       Windows上运行乌班图Bash:通过设置开启开发者模式,更新和安全>面向开发人员.然后搜索"Wi ...

  5. 联想 Thinkpad X230 SLIC 2.1 Marker

    等了好久,终于等到了 X230 的 SLIC 2.1 的 Marker !特发帖备份... 基本情况 笔记本:Lenovo X230(i5+8G+500G) 操作系统:Windows 7 Pro x6 ...

  6. EF Code First学习系列

    EF Model First在实际工作中基本用不到,前段时间学了一下,大概的了解一下.现在开始学习Code First这种方式.这也是在实际工作中用到最多的方式. 下面先给出一些目录: 1.什么是Co ...

  7. Entity Framework实现多列排序

    aList.OrderBy(a => a.WIndex).ThenBy(a=>a.KIndex) 类似sql:order by WIndex,KIndex

  8. iOS学习笔记——滚动视图(scrollView)

    滚动视图:在根视图中添加UIScrollViewDelegate协议,声明一些对象属性 @interface BoViewController : UIViewController<UIScro ...

  9. C#开发微信门户及应用(37)--微信公众号标签管理功能

    微信公众号,仿照企业号的思路,增加了标签管理的功能,对关注的粉丝可以设置标签管理,实现更加方便的分组管理功能.开发者可以使用用户标签管理的相关接口,实现对公众号的标签进行创建.查询.修改.删除等操作, ...

  10. spring源码:核心组件(li)

    一.AOP实现 Spring代理对象的产生:代理的目的是调用目标方法时我们可以转而执行InvocationHandler类的invoke方法,所以如何在InvocationHandler上做文章就是S ...