Implements a more advanced character matching algorithm, a string of very long characters, that requires the matching of strings that match the required characters

Source: Internet
Author: User
Tags hash strlen

A small algorithmic problem, the topic title. What do you mean?

is a very long pattern string such as "432a5234b5664c3243454", and then give a target string "ABC", as long as a string of strings in the pattern of colleagues containing each element of the target string can be.

Requirements are very low. It is very easy to make a violent match, but the time complexity of the brute-force matching algorithm is very low (O (n^m)). The KMP algorithm is difficult to deal with this fuzzy match.

How to improve efficiency, the use of hash table is easy to solve the problem, but also a fairly simple hash table, very helpful to the new understanding of the hash tables.

Because the string is only 255 bits, so this hash table only 0~255 these elements, according to the ASCII table code value as the subscript of the hash array, if the target string exists, the corresponding position element is 1,

The rest of the locations are 0. This allows us to construct an array to count whether the characters of the target string appear in the pattern string.


Code time, first make a hash table, because in the main function, I have the hash table of each element is initialized to 0, so long as the corresponding position is modified:

/* Function: Implements a more advanced character matching algorithm, which is a long string of characters, requiring a string name that matches the required character
: * * *
time: 0:10 2014/1/18
*/

#include < stdio.h>
#include <stdlib.h>
#include <string.h>

/* make hash table and initialize *
/void Mkhash (char T [],char hash[],int N)
{
	if (! T)
	{
		exit ( -1);
	}
	for (int i = 0; i< n; i++)
	{
		Hash[t[i]] = 1;		/* Subscript of the ASCII table as subscript for the hash array */
	}
}



The following function is whether a character appears in a hash.

int Isexit (char str,char hash[])
{
	if (!hash)
	{
		exit ( -1);
	}
	if (hash[str] = = 0)
	{
		return 0;
	}
	else
	{
		return 1;
	}
}


Next this is the main function, traversing the pattern string, the main match on the output of no difficulty is easy to read:

void GetPos (char s[], int n, char hash[])
{
	if (! S &&!hash)
	{
		exit ( -1);
	}
	for (int i=0;i<n;i++)
	{
		if (hash[s[i] = = 1)
		{
			printf ("%d", i+1);
			Hash[s[i]] = 0;}}
}

Then boot from main:

int main ()
{
	char s[255];
	Char t[255];

	Char hash[255] = {0};

	Gets (S);
	Gets (T);

	int slen = strlen (S);
	int Tlen = strlen (T);

	Mkhash (T,hash,tlen);

	GetPos (S,slen,hash);
	
	System ("pause");
	return 0;
}

OK, look at the output:



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.