哈希表對字符串的高效處理

哈希表對字符串的高效處理

        哈希表(散列表)是一種非常高效的查找數據結構,在原理上也與其他的查找不盡相同,它迴避了關鍵字之間反覆比較的繁瑣,而是直接一步到位查找結果。當然,這也帶來了記錄之間沒有任何關聯的弊端。應該說,散列表對於那些查找性能要求高,記錄之間關係無要求的數據有非常好的適用性。注意對散列函數的選擇處理衝突的方法

        Hash表是使用 O(1) 時間進行數據的插入、刪除和查找,但是 hash 表不保證表中數據的有序性,這樣在 hash 表中查找最大數據或者最小數據的時間是 O(N) 。

 

/* 字符串中完成過濾重複字符的功能,

【輸入】:1.常字符串;2.字符串長度;3.【out】用於輸出過濾後的字符串.

【輸出】:過濾後的字符串。

*/

思路1, 循環判定法。第1步,先記錄字符串中第1個字符;第2步,然後從第2個字符開始,判定其和其前面的字符是否相同,不相同的話,則統計進去;相同的話則繼續遍歷,直到字符串末尾(遇到’\0’)。時間複雜度:On2

思路2, 哈希表過濾法。第1步,初始化一個哈希表,用以存儲字符(key)及字符出現的次數;第2步,遍歷哈希表,進行統計計數;第3步,輸出統計次數爲1及統計次數多餘1的(輸出1次)。時間複雜度:On)。

//循環判定法過濾掉重複字符

  1. void stringFilter(const char*pInputStr, long lInputLen, char *pOutputStr)  
  2. {  
  3.        if(pInputStr== NULL || lInputLen == 0 || pOutputStr == NULL)  
  4.        {  
  5.               return;  
  6.        }  
  7.         
  8.        intnCnt = 0;  
  9.        *pOutputStr= pInputStr[0];            //先處理第一個  
  10.        ++nCnt;  
  11.         
  12.        intnNotEqualCnt = 0;                 //統計計數  
  13.        for(inti = 1; i < lInputLen; i++)  
  14.        {  
  15.               nNotEqualCnt= 0;  
  16.               for(intj = i-1; j >=0; j--)  
  17.               {  
  18.                      if(pInputStr[i]!= pInputStr[j])  
  19.                      {  
  20.                             ++nNotEqualCnt;  
  21.                      }  
  22.               }  
  23.                
  24.               if(nNotEqualCnt== i)  //和前面的都不一樣.  
  25.               {  
  26.                      pOutputStr[nCnt++]= pInputStr[i];  
  27.               }  
  28.                
  29.        }//endfor  
  30.        pOutputStr[nCnt]= '\0';  
  31. }  

//哈希表法過濾字符串中的重複字符

  1. void stringFilterFast(const char*pInputStr, long lInputLen, char *pOutputStr)  
  2. {  
  3.        charrstChar = '\0';  
  4.        boolbNotRepeatFound = false;  
  5.        constunsigned int size = 256;  
  6.        unsignedint hashTable[size];  
  7.        constchar* pHashKey = pInputStr;  
  8.        intoutPutCnt = 0;  
  9.         
  10.        if(pInputStr== NULL)  
  11.        {  
  12.               return;  
  13.        }  
  14.         
  15.        //初始化哈希表  
  16.        for(unsignedint i = 0; i < size; i++)  
  17.        {  
  18.               hashTable[i]= 0;  
  19.        }  
  20.         
  21.        //將pString讀入到哈希表中  
  22.        while(*pHashKey!= '\0')  
  23.        {  
  24.               cout<< *pHashKey << "\t";  
  25.               hashTable[*pHashKey]++;    //統計計數  
  26.               pHashKey++;  
  27.        }      
  28.    
  29.        //讀取哈希表,對只出現1次的進行存儲,對出現多次的進行1次存儲。  
  30.        pHashKey= pInputStr;  
  31.        while(*pHashKey!= '\0')  
  32.        {  
  33.               if((hashTable[*(pHashKey)])== 1)   //僅有一次,  
  34.               {  
  35.                      pOutputStr[outPutCnt++]= *pHashKey;  
  36.               }  
  37.               elseif((hashTable[*(pHashKey)]) > 1) // 多餘一次,統計第一次  
  38.               {  
  39.                      pOutputStr[outPutCnt++]= *pHashKey;  
  40.                      hashTable[*(pHashKey)]= 0;  
  41.               }  
  42.               pHashKey++;  
  43.        }  
  44.        pOutputStr[outPutCnt]= '\0';  
  45.    
  46. }  
  47.    
  48. int main()  
  49. {  
  50.        constchar* strSrc = "desdefedeffdsswwwwwwwwwwdd";//"desdefedeffdssw";  
  51.        char*strRst =new char[strlen(strSrc)+1];  
  52.        stringFilter(strSrc,strlen(strSrc), strRst);  
  53.        cout<< strRst << endl;  
  54.        return0;  
  55. }  

//哈希表法查找字符串中第一個不重複的字符

【功能】:查找字符串中第一個不重複的字符。

【輸入】:字符串。

【輸出】:第一個不重複的字符。

時間複雜度O(n),思路類似於上面的哈希表過濾法

  1. char FirstNotRepeatingChar(constchar* pString)  
  2. {  
  3.        charrstChar = '\0';  
  4.        boolbNotRepeatFound = false;  
  5.        constunsigned int size = 256;  
  6.        unsignedchar hashTable[size];  
  7.        constchar* pHashKey = pString;  
  8.    
  9.        if(pString== NULL)  
  10.        {  
  11.               returnrstChar;  
  12.        }  
  13.    
  14.        //初始化哈希表  
  15.        for(unsignedint i = 0; i < size; i++)  
  16.        {  
  17.               hashTable[i] = 0;  
  18.        }  
  19.         
  20.        //將pString存入到哈希表中  
  21.        while(*pHashKey!= '\0')  
  22.        {  
  23.               hashTable[*(pHashKey++)]++;    //統計計數  
  24.        }  
  25.    
  26.        //讀取哈希表,找到第一個=1的字符,bNotRepeatFound用於查找。.  
  27.        pHashKey= pString;  
  28.        while(*pHashKey!= '\0')  
  29.        {  
  30.               if((hashTable[*(pHashKey)]) == 1)  
  31.               {  
  32.                      bNotRepeatFound= true;  
  33.                      rstChar= *pHashKey;  
  34.                      break;  
  35.               }  
  36.               pHashKey++;  
  37.        }  
  38.    
  39.        if(bNotRepeatFound)  
  40.        {  
  41.               cout<< "The first not Repeate char is " << rstChar <<endl;  
  42.        }  
  43.        else  
  44.        {  
  45.               cout<< "The first not Repeate char is not Exist " << endl;  
  46.        }  
  47.    
  48.        returnrstChar;  
  49. }  
  50.    
  51. int main()  
  52. {  
  53.        constchar* strSrc = "google";  
  54.        constchar* strSrc2 = "[email protected]";  
  55.        constchar* strSrc3 = "aabbccddeeff";  
  56.        constchar* strsrc4 = "11111111";    
  57.                                                                                                                      
  58.        constchar* strArray[4] = {strSrc, strSrc2, strSrc3, strsrc4};  
  59.    
  60.       for(inti = 0; i < 4; i++)  
  61.       {  
  62.               FirstNotRepeatingChar(strArray[i]);  
  63.        }  
  64.    
  65.        return0;  
  66. }  

舉一反三:【百度面試題】對於一個海量的文件中存儲着不同的URL,用最小的時間複雜度去除重複的URL。可借鑑字符串處理的哈希表過濾法。不過,這裏的大文件等價於之前的字符串,這裏的URL等價於之前的字符。
發佈了34 篇原創文章 · 獲贊 4 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章