标签:效率 题目 拷贝 ++ mil 避免 简单的 第一个 null
/*
从第一个字符串中删除第二个字符串中出现的全部字符
by Rowandjj
2014/8/18
*/
#include<iostream>
#include<string.h>
using namespace std;
#define MAX_SIZE 256
//在串1中删除全部出如今串2中的字符
void DeleteChar(char *str1,char *str2)
{
if(str1 == NULL || str2 == NULL)
{
return;
}
char table[MAX_SIZE];//准备一个哈希数组
int i,j;
for(i = 0; i < MAX_SIZE; i++)//初始化哈希数组
{
table[i] = 0;
}
for(i = 0; *(str2+i)!='\0';i++)//以每一个字符的asc码相应的数字作为数组下标。值为1代表出现过
{
table[*(str2+i)] = 1;
}
for(i = 0; *(str1+i) != '\0'; i++)//遍历串1。删除在串2中出现的每一个字符
{
if(table[*(str1+i)] == 1)
{
for(j = i+1;*(str1+j) != '\0';j++)//这里须要移动字符
{//假设同意,能够使用空间换时间
*(str1+j-1) = *(str1+j);
}
*(str1+j-1) = '\0';
}
}
}
int main()
{
char str1[] = "We are students.";
char *str2 = "aeiou";
DeleteChar(str1,str2);
cout<<str1<<endl;
return 0;
}
/*
推断两个单词是否为变位词
by Rowandjj
2014/8/18
*/
#include<iostream>
#include<string.h>
using namespace std;
#define MAX_WORD_SIZE 256
bool isAnagram(char *str1,char *str2)
{
if(str1 == NULL || str2 == NULL)
{
return false;
}
int len = strlen(str1);
if(len != strlen(str2))
{
return false;
}
int i;
int table[MAX_WORD_SIZE];//准备一个hash数组
for(i = 0; i < MAX_WORD_SIZE; i++)
{
table[i] = 0;
}
for(i = 0; i < len; i++)//为hash表相应每一个字母出现次数增1
{
table[*(str1+i)]++;
}
for(i = 0; i < len; i++)//为hash表相应每一个字母出现次数减1
{
table[*(str2+i)]--;
}
for(i = 0; i < MAX_WORD_SIZE; i++)//假设hash每一个值都为0,那么就是变位词
{
if(table[i] != 0)
{
return false;
}
}
return true;
}
int main()
{
char *str1 = "evil";
char *str2 = "live";
if(isAnagram(str1,str2))
{
cout<<"YES"<<endl;
}else
{
cout<<"NO"<<endl;
}
return 0;
}/*
删除字符串中全部反复出现的字符
by Rowandjj
2014/8/18
*/
#include<iostream>
#include<string.h>
using namespace std;
#define MAX 256
//去掉反复字符
void deleteRepeatChar(char *str)
{
if(str == NULL)
{
return;
}
int table[MAX];
int i;
for(i = 0; i < MAX; i++)//初始化哈希表
{
table[i] = 0;
}
int len = strlen(str);
char *temp = (char*)malloc(sizeof(char)*len);//存放不带反复字符的新串。避免移动原串
if(!temp)
{
exit(-1);
}
int j = 0;
for(i = 0; i < len; i++)
{
if(table[*(str+i)] == 1)//该字符反复
{
continue;
}else//该字符不反复
{
table[*(str+i)] = 1;
temp[j++] = *(str+i);//拷贝到新串
}
}
for(i = 0; i < j; i++)//将新串中的字符拷贝到原始串中
{
str[i] = temp[i];
}
*(str+i) = '\0';//别忘了还要复制结束符
free(temp);
}
int main()
{
char str[] = "google";
deleteRepeatChar(str);
cout<<str<<endl;
return 0;
}标签:效率 题目 拷贝 ++ mil 避免 简单的 第一个 null
原文地址:http://www.cnblogs.com/slgkaifa/p/7236427.html