- 从第一个字符串中删除在第二个字符串中出现的所有字符
“We are students” “aeiou” “W r studnts”
我们可以设置两个指针pfast和pslow,初始化时两个都指向字符串中的第一个字符,pfast碰到一个需要删除的字符直接跳过;pfast如果碰到不需要删除的字符,就把该字符复制给pslow,同时pfast后移(继续需找),pslow(指向下一个位置)。这样整个算法的时间复杂度就可以达到O(n)。而如何判断该字符是否需要删除呢,对于char型字符,我们可以用长度为256的哈希表来存储字符出现的次数。
#include<stdio.h>
#include<string>
#include<memory>
#include <iostream>
using namespace std;
void Del_chrs(char *src, char *del_chs)
{
if(src == NULL || del_chs==NULL)
return;
//建立并初始化哈希表
const int hashLength = 256;
unsigned int hashtable[hashLength];
for (int i = 0; i < hashLength; i++)
{
hashtable[i] = 0;
}
//hash表中存储第二个字符串中每个字符出现的次数
char *pdelch = del_chs;
while (*pdelch != ‘\0‘)
{
hashtable[*pdelch] = 1;
pdelch++;
}
//如果Pfast指向的字符不需要删除,就把其所指的值赋给pslow,两者都向后移动
//否则,pfast向后移动继续寻找,pslow不动。
char *p_slow = src;
char *p_fast = src;
/*
while (*p_fast != ‘\0‘)
{
if (hashtable[*p_fast] == 0)
{
*p_slow = *p_fast;
p_slow++;
}
p_fast++;
}
*/
while(*p_fast)
{
if(hashtable[*p_fast] == 1)
{
p_fast++;
}
*p_slow++ = *p_fast++;
}
*p_slow = ‘\0‘;
}
int main()
{
/*const int StrMaxLength =100;
char strFirst [StrMaxLength];
cin.getline(strFirst,sizeof(strFirst));
char strSecond[StrMaxLength];
cin.getline(strSecond,sizeof(strSecond));
*/
char strFirst[] = "They are students";
char strSecond[] = "aeiou";
Del_chrs(strFirst,strSecond);
cout << strFirst <<endl;
return 0;
}
- 定义一个函数,删除字符串中所有重复出现的字符。
“google” “gole”
#include <iostream>
#include <string.h>
using namespace std;
void stringFilterFast(const char*pInputStr)
{
char *pOutputStr= new char[strlen(pInputStr)+1];
char rstChar = ‘\0‘;
bool bNotRepeatFound = false;
const unsigned int size = 256;
unsigned int hashTable[size];
const char* pHashKey = pInputStr;
int outPutCnt = 0;
if(pInputStr== NULL)
{
return;
}
//初始化哈希表
for(unsigned int i = 0; i < size; i++)
{
hashTable[i]= 0;
}
//将pString读入到哈希表中
while(*pHashKey!= ‘\0‘)
{
cout<< *pHashKey << "\t";
hashTable[*pHashKey]++; //统计计数
pHashKey++;
}
//读取哈希表,对只出现1次的进行存储,对出现多次的进行1次存储。
pHashKey= pInputStr;
while(*pHashKey!= ‘\0‘)
{
if((hashTable[*(pHashKey)])== 1) //仅有一次,
{
pOutputStr[outPutCnt++]= *pHashKey;
}
else if((hashTable[*(pHashKey)]) > 1) // 多余一次,统计第一次
{
pOutputStr[outPutCnt++]= *pHashKey;
hashTable[*(pHashKey)]= 0;
}
pHashKey++;
}
pOutputStr[outPutCnt]= ‘\0‘;
cout << pOutputStr ;
}
int main()
{
const char* strSrc = "google";//"desdefedeffdssw";
//char*strRst =new char[strlen(strSrc)+1];
stringFilterFast(strSrc);
// cout<< strRst << endl;
//if (NULL != strRst){ delete[] strRst; strRst = NULL;}
return 0;
}
http://blog.csdn.net/laoyang360/article/details/8026579
3. 如果两个单词中出现的字母相同,并且每个字母出现的次数也相同,那么这两个单词互为变位词(Anagram)。例如:silent listen / evil live .
完成一个函数,判断输入字符串是不是为变位词。
#include <iostream>
using namespace std;
#define NO_OF_CHARS 256
bool areAnagram(const char *str1, const char *str2)
{
if (str1 == NULL || str2 == NULL)
{
return false;
}
int count[NO_OF_CHARS] = {0};
int i;
for (i = 0; str1[i] && str2[i]; i++)
{
count[str1[i]]++;
count[str2[i]]--;
}
if (str1[i] != ‘\0‘ || str2[i] != ‘\0‘)
{
return false;
}
for (i = 0; i < NO_OF_CHARS; i++)
{
if (count[i] != 0)
{
return false;
}
}
return true;
}
int main(int argc, char* argv[])
{
char str1[] = "geeksforgeeks";
char str2[] = "forgeeksgeeks";
if (areAnagram(str1, str2))
{
cout << "The two strings are anagram of each other" << endl;
}
else
{
cout << "The two strings are not anagram of each other" << endl;
}
return 0;
}
http://www.code123.cc/742.html
http://blog.csdn.net/lalor/article/details/7539717
时间: 2024-10-10 08:50:12