免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2101 | 回复: 3
打印 上一主题 下一主题

《程序员》第9期编程擂台,请高手指点如何写一个跑得快的。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-08-21 13:10 |只看该作者 |倒序浏览
昨晚看到这个题目,用C++写了一个,结果比人家java写的跑得还慢!请教高手如何很好的完成这个任务:
面对浩瀚的信息海洋,找到想要的资源有时真的是不容易。在大量文字中搜索高频词汇是信息搜索和数据压缩的共通课题。
  这次智慧擂台请大家在一个比较庞大的英文文本中找出M个数量最多的短语(由N个单词组成)。统一处理相同的文本文件,该文本只包含英文单词、空格和回行符,比较谁的程序效率最高,这个文本近日发布。

  程序输入:M,N,文本文件路径(M不超过20,N不超过8)
  程序输出:高频短语及其数量清单
  
  擂台规则:提交符合以上要求的可执行程序,语言招式不限,点到为止;
       我们将在统一的环境下对每位选手的作品进行公平的测试,
       比较出综合用时最少的程序。

文本文件链接:
http://www.codechina.net/down//2 ... amp;spaces-5666.rar

[ 本帖最后由 2eye 于 2006-8-21 20:46 编辑 ]

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
2 [报告]
发表于 2006-08-22 15:06 |只看该作者
原帖由 2eye 于 2006-8-21 13:10 发表
昨晚看到这个题目,用C++写了一个,结果比人家java写的跑得还慢!请教高手如何很好的完成这个任务:

感觉关键就是hash函数
  1. #include <cstddef>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <list>
  5. #include <iterator>
  6. #include <map>

  7. using namespace std;

  8. struct WordNode
  9. {
  10.     string word;
  11.     WordNode *pre_word, *pre_match, *pre_node, *next_node;
  12.     bool first_match;

  13.     WordNode() : pre_word(NULL), pre_match(NULL), pre_node(NULL), next_node(NULL), first_match(true) { }
  14.     ~WordNode() { }
  15. };

  16. bool MatchWord(const WordNode *first, const WordNode *second, int M)
  17. {
  18.     const WordNode *ptr = second;
  19.     for(int i=0; i<M; i++) {
  20.         if(first->word != second->word || first == ptr) return false;
  21.         first = first->next_node;
  22.         second = second->next_node;
  23.     }
  24.     return true;
  25. }

  26. int hashfunc(WordNode *node, int M)
  27. {
  28.     unsigned short v = 0;
  29.     for(int i=0; i<M && node != NULL; i++) {
  30.         for(int j=0; j<node->word.size(); j++)
  31.             v = 5*v + node->word[j];
  32.         node = node->next_node;
  33.     }
  34.     return int(v);
  35. }

  36. int main()
  37. {
  38.     ifstream inf("words.txt");
  39.     if (!inf) {
  40.         cerr << "Error open file words.txt" << endl;
  41.         exit(-1);
  42.     }

  43.     WordNode *head, *node, *tail = NULL, *ptr, *match;
  44.     int M, N, i=0, cnt;

  45.     cin >> M >> N;
  46.     WordNode **preM = new WordNode*[M];
  47.     int index=0;
  48.     memset(preM,  0, sizeof(WordNode*) * M);
  49.     node = new WordNode;
  50.     head = node;
  51.     map<int, WordNode *> wordMap;
  52.     map<int, WordNode *>::iterator map_itr;
  53.     int hashval;
  54.     while(inf >> node->word) {
  55.         preM[index] = node;
  56.         index = (index+1)%M;
  57.         ptr = preM[index];
  58.         hashval = hashfunc(ptr, M);
  59.         if(hashval > 0) {
  60.             if((map_itr = wordMap.find(hashval)) == wordMap.end()) {
  61.                 wordMap[hashval] = node;
  62.             }
  63.             else {
  64.                 node->pre_word = map_itr->second;
  65.                 map_itr->second = node;
  66.             }
  67.         }
  68.         if(ptr != NULL) {
  69.             for(match=ptr->pre_word; match != NULL; match = match->pre_word) {
  70.                 if(MatchWord(match, ptr, M)) {
  71.                     ptr->pre_match = match;
  72.                     match->first_match = false;
  73.                     break;
  74.                 }
  75.             }
  76.         }

  77.         tail = node;
  78.         node = new WordNode;
  79.         tail->next_node = node;
  80.         node->pre_node = tail;
  81.     }
  82.     list<pair<WordNode *, int> > toplist;
  83.     list<pair<WordNode *, int> >::iterator itr;
  84.     for(; ptr != NULL; ptr=ptr->pre_node) {
  85.         if(ptr->first_match) {
  86.             cnt = 1;
  87.             for(match = ptr->pre_match; match != NULL; match = match->pre_match) cnt++;
  88.             for(itr=toplist.begin(); itr!=toplist.end() && itr->second > cnt; ++itr);
  89.             if(itr!=toplist.end()) {
  90.                 toplist.insert(itr, make_pair(ptr, cnt));
  91.                 if(toplist.size() > N)
  92.                     toplist.pop_back();
  93.             }
  94.             else if(toplist.size() < N)
  95.                 toplist.push_back(make_pair(ptr, cnt));
  96.         }
  97.     }
  98.     for(itr=toplist.begin(), i=1; itr!=toplist.end(); ++itr, i++) {
  99.         cout << "No." << i << ":";
  100.         ptr = itr->first;
  101.         for(int j=0; j<M; j++, ptr=ptr->next_node) {
  102.             cout << ptr->word << " ";
  103.         }
  104.         cout << endl << "Count:" << itr->second << endl;
  105.     }
  106.     while(head != NULL) {
  107.         ptr = head->next_node;
  108.         delete head;
  109.         head = ptr;
  110.     }
  111. }

复制代码

论坛徽章:
0
3 [报告]
发表于 2006-08-22 15:46 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
39
2017金鸡报晓
日期:2017-02-08 10:39:4219周年集字徽章-周
日期:2023-04-15 12:02:2715-16赛季CBA联赛之深圳
日期:2023-02-16 14:39:0220周年集字徽章-年
日期:2022-08-31 14:25:28黑曼巴
日期:2022-08-17 18:57:0919周年集字徽章-年
日期:2022-04-25 13:02:5920周年集字徽章-20	
日期:2022-03-29 11:10:4620周年集字徽章-年
日期:2022-03-14 22:35:1820周年集字徽章-周	
日期:2022-03-09 12:51:3220周年集字徽章-年
日期:2022-02-10 13:13:4420周年集字徽章-周	
日期:2022-02-03 12:09:4420周年集字徽章-20	
日期:2022-01-25 20:14:27
4 [报告]
发表于 2006-08-22 16:15 |只看该作者
No.1:Following are
Count:711
No.2:are the
Count:619
No.3ec Xinhua
Count:514
No.4:BEIJING Dec
Count:486
No.5:Jan Xinhua
Count:394
No.6:BEIJING Jan
Count:382
No.7:US dollars
Count:355
No.8:US dollars
Count:350
No.9:United States
Count:350
No.10:matter II
Count:346


7,8是重复的,速度也不太快,不过我写不出更好的。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP