亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 2118 | 回復(fù): 3
打印 上一主題 下一主題

《程序員》第9期編程擂臺,請高手指點(diǎn)如何寫一個跑得快的。 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2006-08-21 13:10 |只看該作者 |倒序?yàn)g覽
昨晚看到這個題目,用C++寫了一個,結(jié)果比人家java寫的跑得還慢!請教高手如何很好的完成這個任務(wù):
面對浩瀚的信息海洋,找到想要的資源有時真的是不容易。在大量文字中搜索高頻詞匯是信息搜索和數(shù)據(jù)壓縮的共通課題。
  這次智慧擂臺請大家在一個比較龐大的英文文本中找出M個數(shù)量最多的短語(由N個單詞組成)。統(tǒng)一處理相同的文本文件,該文本只包含英文單詞、空格和回行符,比較誰的程序效率最高,這個文本近日發(fā)布。

  程序輸入:M,N,文本文件路徑(M不超過20,N不超過8)
  程序輸出:高頻短語及其數(shù)量清單
  
  擂臺規(guī)則:提交符合以上要求的可執(zhí)行程序,語言招式不限,點(diǎn)到為止;
       我們將在統(tǒng)一的環(huán)境下對每位選手的作品進(jìn)行公平的測試,
       比較出綜合用時最少的程序。

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

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

論壇徽章:
1
榮譽(yù)會員
日期:2011-11-23 16:44:17
2 [報(bào)告]
發(fā)表于 2006-08-22 15:06 |只看該作者
原帖由 2eye 于 2006-8-21 13:10 發(fā)表
昨晚看到這個題目,用C++寫了一個,結(jié)果比人家java寫的跑得還慢!請教高手如何很好的完成這個任務(wù):

感覺關(guān)鍵就是hash函數(shù)
  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. }

復(fù)制代碼

論壇徽章:
0
3 [報(bào)告]
發(fā)表于 2006-08-22 15:46 |只看該作者
提示: 作者被禁止或刪除 內(nèi)容自動屏蔽

論壇徽章:
39
2017金雞報(bào)曉
日期:2017-02-08 10:39:4219周年集字徽章-周
日期:2023-04-15 12:02:2715-16賽季CBA聯(lián)賽之深圳
日期: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 [報(bào)告]
發(fā)表于 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是重復(fù)的,速度也不太快,不過我寫不出更好的。
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP