①所谓兄弟字符串,就是指两个字符串中相同的字符串出现的个数相同。通常我们要判断两个字符串是否为兄弟字符串。
实现算法:
//judge two string is brother string or not
bool IsBrotherString(const char *src,const char *dst)
{
if( strlen(src) != strlen(dst) )
return false;
//usually we only have 256 chars
unsigned int hashTable[256];
memset(hashTable,0,sizeof(hashTable));
//compute the num of every char in the src
const char *pSrc = src;
while(*pSrc != '\0')
{
++ hashTable[*pSrc ++];
}
//minus the num of every char in the dst
const char *pDest = dst;
while(*pDest != '\0')
{
-- hashTable[*pDest ++];
}
//at last,if all the num in hashTable is zero, it is brother string.
pSrc = src;
while(*pSrc != '\0')
{
if(hashTable[*pSrc ++] != 0)
{
return false;
}
}
return true;
}
②在一个较长的字符串中,单词与单词之间用空格隔开。算法实现字符串中是否有单词在字典中出现,已知单词字典是给出的。(迅雷的笔试题目)
一)简化版本
#include <stdio.h>
#include <string.h>
//judge two string is brother string or not
bool IsBrotherString(const char *src,const char *dst)
{
if( strlen(src) != strlen(dst) )
return false;
//usually we only have 256 chars
unsigned int hashTable[256];
memset(hashTable,0,sizeof(hashTable));
//compute the num of every char in the src
const char *pSrc = src;
while(*pSrc != '\0')
{
++ hashTable[*pSrc ++];
}
//minus the num of every char in the sdst
const char *pDest = dst;
while(*pDest != '\0')
{
-- hashTable[*pDest ++];
}
//at last,if all the num in hashTable is zero, it is brother string.
pSrc = src;
while(*pSrc != '\0')
{
if(hashTable[*pSrc ++] != 0)
{
return false;
}
}
return true;
}
inline char *DeleteSpace(char *pString)
{
while(' ' == *pString)
pString ++;
return pString;
}
bool IsMatching(const char* dictionary[],const int dictLen, char *pSrc)
{
if(NULL == pSrc)
return false;
unsigned int i, nLen, nIndex = 0;
char *pBegin = pSrc;
char *pEnd = pSrc;
char *pDst = NULL;
pBegin = DeleteSpace( pBegin );
while('\0' != *pBegin)
{
pEnd = strstr(pBegin," ");
if(NULL != pEnd)
{
nLen = pEnd - pBegin;
pDst = new char[nLen + 1];
strncpy(pDst, pBegin, nLen);
pDst[nLen] = '\0';
for(i = 0; i < dictLen; ++ i)
{
if(IsBrotherString(dictionary[i], pDst))
return true;
}
pBegin = DeleteSpace( pEnd );
}
else
{ //the end of string.
nLen = strlen(pBegin);
pDst = new char[nLen + 1];
strncpy(pDst, pBegin, nLen);
pDst[nLen] = '\0';
for(i = 0; i < dictLen; ++ i)
{
if(IsBrotherString(dictionary[i], pDst))
return true;
}
break;
}
//free memory
delete [] pDst;
pDst = NULL;
}
return false;
}
int main()
{
unsigned int i, nCount;
const char *dictionary[] = {"adadad","bbb","abcd","tianmo"};
char *sentence = " adadada I am tianmo abcde adadada thank you very much ";
int dictLen = sizeof(dictionary) / sizeof(char *);
if(IsMatching(dictionary, dictLen,sentence))
{
printf("Successful !\n");
}
else
{
printf("Bad !\n");
}
return 0;
}
二)完整版本
#include <stdio.h>
#include <string.h>
//judge two string is brother string or not
bool IsBrotherString(const char *src,const char *dst)
{
if( strlen(src) != strlen(dst) )
return false;
//usually we only have 256 chars
unsigned int hashTable[256];
memset(hashTable,0,sizeof(hashTable));
//compute the num of every char in the src
const char *pSrc = src;
while(*pSrc != '\0')
{
++ hashTable[*pSrc ++];
}
//minus the num of every char in the sdst
const char *pDest = dst;
while(*pDest != '\0')
{
-- hashTable[*pDest ++];
}
//at last,if all the num in hashTable is zero, it is brother string.
pSrc = src;
while(*pSrc != '\0')
{
if(hashTable[*pSrc ++] != 0)
{
return false;
}
}
return true;
}
inline char *DeleteSpace(char *pString)
{
while(' ' == *pString)
pString ++;
return pString;
}
int GetNumOfWords(char* pString)
{
if(NULL == pString)
return 0;
unsigned int nCount = 0;
char *pSrc = pString;
pSrc = DeleteSpace( pSrc );
while('\0' != *pSrc)
{
pSrc = strstr(pSrc," ");
++ nCount;
if(NULL != pSrc)
pSrc = DeleteSpace( pSrc );
else
break;//the end of the string
}
return nCount;
}
bool IsMatching(const char* dictionary[],const int dictLen, char *pSrc)
{
if(NULL == pSrc)
return false;
unsigned int i, nLen, nIndex = 0;
char *pBegin = pSrc;
char *pEnd = pSrc;
unsigned int nCount = GetNumOfWords(pSrc);
char **pDst = new char *[ nCount ]();
pBegin = DeleteSpace( pBegin );
while('\0' != *pBegin)
{
pEnd = strstr(pBegin," ");
if(NULL != pEnd)
{
nLen = pEnd - pBegin;
pDst[ nIndex ] = new char[nLen + 1];
strncpy(pDst[ nIndex ], pBegin, nLen);
pDst[nIndex][nLen] = '\0';
for(i = 0; i < dictLen; i ++)
{
if(IsBrotherString(dictionary[i], pDst[nIndex]))
return true;
}
++ nIndex;
pBegin = DeleteSpace( pEnd );
}
else
{ //the end of string.
nLen = strlen(pBegin);
pDst[ nIndex ] = new char[nLen + 1];
strncpy(pDst[ nIndex ], pBegin, nLen);
pDst[nIndex][nLen] = '\0';
for(i = 0; i < dictLen; i ++)
{
if(IsBrotherString(dictionary[i], pDst[nLen]))
return true;
}
break;
}
}
//free memory
for(i = 0; i < nCount; ++ i)
{
delete [] pDst[i];
}
pDst = NULL;
return false;
}
int main()
{
unsigned int i, nCount;
const char *dictionary[] = {"adadad","bbb","abcd","tianmo"};
char *sentence = " adadada I am tianmo abcde adadada thank you very much ";
int dictLen = sizeof(dictionary) / sizeof(char *);
if(IsMatching(dictionary, dictLen,sentence))
{
printf("Successful !\n");
}
else
{
printf("Bad !\n");
}
return 0;
}