哈希表对字符串的高效处理

哈希表对字符串的高效处理

        哈希表(散列表)是一种非常高效的查找数据结构,在原理上也与其他的查找不尽相同,它回避了关键字之间反复比较的繁琐,而是直接一步到位查找结果。当然,这也带来了记录之间没有任何关联的弊端。应该说,散列表对于那些查找性能要求高,记录之间关系无要求的数据有非常好的适用性。注意对散列函数的选择处理冲突的方法

        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)。

//循环判定法过滤掉重复字符

[cpp]  view plain copy
  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. }  

//哈希表法过滤字符串中的重复字符

[cpp]  view plain copy
  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),思路类似于上面的哈希表过滤法

[cpp]  view plain copy
  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 = "yyy@163.com";  
  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等价于之前的字符。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值