julycoding/The-Art-Of-Programming-By-July-2nd

1.2 字符串包含 新方法

pjbruce opened this issue · 0 comments

#define NO_OF_CHARS 256

char b[] = "ABGAA67";
char a[] = "AG7";


int aASCII[NO_OF_CHARS];
int bASCII[NO_OF_CHARS];
for (int i = 0; i < NO_OF_CHARS; i++) 
{
	aASCII[i] = -1;
	bASCII[i] = -1;
}

for (int i = 0; i < strlen(a); i++)
{
     aASCII[(int) a[i]] = i;
}

for (int i = 0; i < strlen(b); i++)
{
	bASCII[(int) b[i]] = i;
}

int findCount = 0;

for (int i = 0; i < NO_OF_CHARS; i++)
{
	if (aASCII[i] != -1 && bASCII[i]!=-1)
	{
		findCount++;
	}

}

if (findCount == strlen(a))
{
	printf("find");
}

Reference:https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_string_search_algorithm
http://www.geeksforgeeks.org/pattern-searching-set-7-boyer-moore-algorithm-bad-character-heuristic/