HWOJ 删除字符串中重复出现的字符

HWOJ 删除字符串中重复出现的字符

题目:删除字符串中重复出现的字符
样例:
输入:google
输出:gole
题目分析:
算法思路:
哈希表过滤法。
①初始化一个哈希表,用以存储字符(key)及字符出现的次数;
②遍历哈希表,进行统计计数;
③输出统计次数为1及统计次数多余1的(输出1次)。如果多于1次则只显示一次,时间复杂度:O(n)。这里考虑多定义加一个新数组来存放输出。
学习笔记:
注意学习采用pOutputStr[outcnt++]=*pHashKey 和 pOutputStr[outcnt++]='\0'

===============================================================================
参考代码:

//.cpp
#include <iostream>  
using namespace std;  

char DeleteRepeating(char *InputStr, char *pOutputStr)  
{  
    //输入不合法  
    if(InputStr == NULL)  
        return 0;  

    //创建一个哈希表  
    const int tableSize = 256;  
    unsigned int hashTable[tableSize];  
    int outcnt = 0;  
    for(int i = 0;i < tableSize; i++)  
        hashTable[i] = 0;  

    char *pHashKey = InputStr;  
    while(*pHashKey)  
    {  
        hashTable[*pHashKey]++;  
        pHashKey++;  
    }  

    pHashKey = InputStr;  

    //只对出现一次的一次储存,出现两次的也只储存一次  
    while(*pHashKey)  
    {  
        if(hashTable[*pHashKey] == 1)  
        {  
            pOutputStr[outcnt++] = *pHashKey;  
        }  
        else if(hashTable[*pHashKey] > 1)  
        {  
            pOutputStr[outcnt++] = *pHashKey;  
            hashTable[*pHashKey] = 0;  
        }  
        pHashKey++;  
    }  
        pOutputStr[outcnt++] = '\0';  
}  

int main()  
{  
    char str[100];  
    char str_out[100];  
    cin >> str;  

    DeleteRepeating(str,str_out);  
    cout << str_out << endl;  

    return 0;  
}  

  
  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值