39. 騰訊面試題:有一千萬條簡訊,有重複,以文字檔的形式儲存,一行一條,有重複。

來源:互聯網
上載者:User

有一千萬條簡訊,有重複,以文字檔的形式儲存,一行一條,有重複。

請用5分鐘時間,找出重複出現最多的前10條。


分析:

常規方法是先排序,在遍曆一次,找出重複最多的前10條。但是排序的演算法複雜度最低為nlgn。

可以設計一個hash_table, hash_map<string, int>,依次讀取一千萬條簡訊,載入到hash_table表中,並且統計重複的次數,與此同時維護一張最多10條的簡訊表。

這樣遍曆一次就能找出最多的前10條,演算法複雜度為O(n)。


實現如下:

#include<iostream>#include<map>#include<iterator>#include<stdio.h>using namespace std;#define HASH __gnu_cxx#include<ext/hash_map>#define uint32_t unsigned int#define uint64_t unsigned long intstruct StrHash{        uint64_t operator()(const std::string& str) const        {           uint32_t b    = 378551;           uint32_t a    = 63689;           uint64_t hash = 0;           for(size_t i = 0; i < str.size(); i++)           {              hash = hash * a + str[i];              a    = a * b;           }           return hash;        }        uint64_t operator()(const std::string& str, uint32_t field) const        {           uint32_t b    = 378551;           uint32_t a    = 63689;           uint64_t hash = 0;           for(size_t i = 0; i < str.size(); i++)           {              hash = hash * a + str[i];              a    = a * b;           }                hash = (hash<<8)+field;           return hash;        }};struct NameNum{        string name;        int num;        NameNum():num(0),name(""){}};int main(){        HASH::hash_map< string, int, StrHash > names;        HASH::hash_map< string, int, StrHash >::iterator it;        NameNum namenum[10];        string l = "";        while(getline(cin, l))        {                it = names.find(l);                if(it != names.end())                {                        names[l] ++;                }                else                {                        names[l] = 1;                        names[l] = 1;                }        }        int i = 0;        int max = 1;        int min = 1;        int minpos = 0;        for(it = names.begin(); it != names.end(); ++ it)        {                if(i < 10)                {                        namenum[i].name = it->first;                        namenum[i].num = it->second;                        if(it->second > max)                                max = it->second;                        else if(it->second < min)                        {                                min = it->second;                                minpos = i;                        }                }                else                {                        if(it->second > min)                        {                                namenum[minpos].name = it->first;                                namenum[minpos].num = it->second;                                int k = 1;                                min = namenum[0].num;                                minpos = 0;                                while(k < 10)                                {                                        if(namenum[k].num < min)                                        {                                                min = namenum[k].num;                                                minpos = k;                                        }                                        k ++;                                }                        }                }                i++;        }        i = 0;        cout << "maxlength (string,num): " << endl;        while( i < 10)        {                cout << "(" << namenum[i].name.c_str() << "," << namenum[i].num << ")" << endl;                i++;        }        return 0;}
使用g++編譯如下:

g++ main.cpp -o main

簡訊文字檔為:msg.txt

運行:./main < msg.txt

輸出結果為:

maxlength (string,num): 
(點點母嬰坊,4)
(農機配件維修,5)
(紅勝超市,6)
(龍溪大酒店,8)
(張記餃子館,3)
(友誼旅店,3)
(明珠通訊,3)
(金源旅館,3)
(洞庭山天然泉水,2)
(清源超市,3)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.