0. 写在前面

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

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

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

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

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

先上个图吧:

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

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

1. 爬虫思路

思路其实非常清晰:

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

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

2. 简单粗暴的代码

  1. #coding='utf-8'
  2. import requests, re, os, HTMLParser, time, getpass
  3. host_url = 'http://acm.hdu.edu.cn'
  4. post_url = 'http://acm.hdu.edu.cn/userloginex.php?action=login'
  5. sub_url = 'http://acm.hdu.edu.cn/submit.php?action=submit'
  6. csdn_url = 'http://so.csdn.net/so/search/s.do'
  7. head = { 'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36' }
  8. html_parser = HTMLParser.HTMLParser()
  9. s = requests.session()
  10. def login(usr,psw):
  11. s.get(host_url);
  12. data = {'username':usr,'userpass':psw,'login':'Sign In'}
  13. r = s.post(post_url,data=data)
  14. def check_lan(lan):
  15. if 'java' in lan:
  16. return '5'
  17. return '0'
  18. def parser_code(code):
  19. return html_parser.unescape(code).encode('utf-8')
  20. def is_ac(pid,usr):
  21. tmp = requests.get('http://acm.hdu.edu.cn/userstatus.php?user='+usr).text
  22. accept = re.search('List of solved problems</font></h3>.*?<p align=left><script language=javascript>(.*?)</script><br></p>',tmp,re.S)
  23. if pid in accept.group(1):
  24. print '%s was solved' %pid
  25. return True
  26. else:
  27. return False
  28. def search_csdn(PID,usr):
  29. get_data = { 'q':'HDU ' + PID, 't':'blog', 'o':'', 's':'', 'l':'null' }
  30. search_html = requests.get(csdn_url,params=get_data).text
  31. linklist = re.findall('<dd class="search-link"><a href="(.*?)" target="_blank">',search_html,re.S)
  32. for l in linklist:
  33. print l
  34. tm_html = requests.get(l,headers=head).text;
  35. title = re.search('<title>(.*?)</title>',tm_html,re.S).group(1).lower()
  36. if PID not in title:
  37. continue
  38. if 'hdu' not in title:
  39. continue
  40. tmp = re.search('name="code" class="(.*?)">(.*?)</pre>',tm_html,re.S)
  41. if tmp == None:
  42. print 'code not find'
  43. continue
  44. LAN = check_lan(tmp.group(1))
  45. CODE = parser_code(tmp.group(2))
  46. if r'include' in CODE:
  47. pass
  48. elif r'import java' in CODE:
  49. pass
  50. else:
  51. continue
  52. print PID, LAN
  53. print '--------------'
  54. submit_data = { 'check':'0', 'problemid':PID, 'language':LAN, 'usercode':CODE }
  55. s.post(sub_url,headers=head,data=submit_data)
  56. time.sleep(5)
  57. if is_ac(PID,usr):
  58. break
  59. if __name__ == '__main__':
  60. usr = raw_input('input your username:')
  61. psw = getpass.getpass('input your password:')
  62. login(usr,psw)
  63. pro_cnt = 1000
  64. while pro_cnt <= 5679:
  65. PID = str(pro_cnt)
  66. if is_ac(PID,usr):
  67. pro_cnt += 1
  68. continue
  69. search_csdn(PID,usr)
  70. pro_cnt += 1

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

3. TDDO

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

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

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. #include <queue>
  5. using namespace std;
  6. #define clr( a, b ) memset( a, b, sizeof(a) )
  7. const int SIGMA_SIZE = 26;
  8. const int NODE_SIZE = 500000 + 10;
  9. struct ac_automaton{
  10. int ch[ NODE_SIZE ][ SIGMA_SIZE ];
  11. int f[ NODE_SIZE ], val[ NODE_SIZE ], last[ NODE_SIZE ];
  12. int sz;
  13. void init(){
  14. sz = 1;
  15. clr( ch[0], 0 ), clr( val, 0 );
  16. }
  17. void insert( char *s ){
  18. int u = 0, i = 0;
  19. for( ; s[i]; ++i ){
  20. int c = s[i] - 'a';
  21. if( !ch[u][c] ){
  22. clr( ch[sz], 0 );
  23. val[sz] = 0;
  24. ch[u][c] = sz++;
  25. }
  26. u = ch[u][c];
  27. }
  28. val[u]++;
  29. }
  30. void getfail(){
  31. queue<int> q;
  32. f[0] = 0;
  33. for( int c = 0; c < SIGMA_SIZE; ++c ){
  34. int u = ch[0][c];
  35. if( u ) f[u] = 0, q.push(u), last[u] = 0;
  36. }
  37. while( !q.empty() ){
  38. int r = q.front(); q.pop();
  39. for( int c = 0; c < SIGMA_SIZE; ++c ){
  40. int u = ch[r][c];
  41. if( !u ){
  42. ch[r][c] = ch[ f[r] ][c];
  43. continue;
  44. }
  45. q.push( u );
  46. int v = f[r];
  47. while( v && !ch[v][c] ) v = f[v];
  48. f[u] = ch[v][c];
  49. last[u] = val[ f[u] ] ? f[u] : last[ f[u] ];
  50. }
  51. }
  52. }
  53. int work( char* s ){
  54. int res = 0;
  55. int u = 0, i = 0, e;
  56. for( ; s[i]; ++i ){
  57. int c = s[i] - 'a';
  58. u = ch[u][c];
  59. e = u;
  60. while( val[e] ){
  61. res += val[e];
  62. val[e] = 0;
  63. e = last[e];
  64. }
  65. }
  66. return res;
  67. }
  68. }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. HTML5权威指南--Web Storage,本地数据库,本地缓存API,Web Sockets API,Geolocation API(简要学习笔记二)

    1.Web Storage HTML5除了Canvas元素之外,还有一个非常重要的功能那就是客户端本地保存数据的Web Storage功能. 以前都是用cookies保存用户名等简单信息.   但是c ...

  2. angular2系列教程(四)Attribute directives

    今天我们要讲的是ng2的Attribute directives.顾名思义,就是操作dom属性的指令.这算是指令的第二课了,因为上节课的components实质也是指令. 例子

  3. ES6环境搭建及react-router学习

    一.起因 ES6新纳入了很多振奋人心的新特性,真的很让人忍不住去尝试一下.不过,由于现在大部分的浏览器对ES6的支持程度都不是很好.所以如果想要放心地使用一些新特性,还需要用一些工具,将ES6或者ES ...

  4. 你真的会玩SQL吗?三范式、数据完整性

    你真的会玩SQL吗?系列目录 你真的会玩SQL吗?之逻辑查询处理阶段 你真的会玩SQL吗?和平大使 内连接.外连接 你真的会玩SQL吗?三范式.数据完整性 你真的会玩SQL吗?查询指定节点及其所有父节 ...

  5. Android重构与设计之路,从整理提示弹窗(SmartAlertPop)开始

    封装一个独立弹窗Module,这里的弹窗包括普通的Dialog方式弹框和WindowManager方式弹窗.提供一种管理项目里面弹窗的方案,便于后期修改和维护. 首先描述一个在大项目中普遍存在的一个现 ...

  6. JavaScript : 浅讲ajax

    1.ajax入门案例 1.1 搭建Web环境 ajax对于各位来说,应该都不陌生,正因为ajax的产生,导致前台页面和服务器之间的数据传输变得非常容易,同时还可以实现页面的局部刷新.通过在后台与服务器 ...

  7. 数百个 HTML5 例子学习 HT 图形组件 – 3D 建模篇

    http://www.hightopo.com/demo/pipeline/index.html <数百个 HTML5 例子学习 HT 图形组件 – WebGL 3D 篇>里提到 HT 很 ...

  8. 通过向页面写html代码导出excel

    //excel文件名 string filename = "考勤汇总"; StringBuilder ExcelHtml = new StringBuilder(); ExcelH ...

  9. C#语音朗读文本 — TTS的实现

    TTS, Text To Speech的缩写,是使用语音朗读文本的技术.目前,在国内应用较多的是排队叫号系统 Windows 平台的TTS,通常使用的是微软自带的 Speech API. Window ...

  10. python 数据类型---列表使用 之二 (增删改查)

    列表的操作 1.列表的修改 >>> name ['Frank', 'Lee', 2, ['Andy', 'Troy']] >>> name[0] = "F ...