/*************************************** ********************************
* String matching uty @ uaty
* Algorithm: first, enter the position of each character in the string to be searched into the ANSI array of the linked list. The ASCII value of the character is
* Array index. When the same character appears more than once, a node is added to the corresponding linked list.
* When matching, use each character of the target string as the index to locate the position of the string to be searched
* If the values of each position increase by 1, the target string appears in the string to be searched.
**************************************** *******************************/
# Include <stdio. h>
# Include <windows. h>
DWORD lookupthesource (char * Source, int length );
DWORD dothesearching (char * STR, int length );
Bool havenext (char, int position );
Typedef struct _ Node
{
Int value; // The Position of the string to be searched.
Struct _ node * next;
Bool isnext; // The Position of the character is the next character in the target string.
} Node, * pnode;
Pnode ANSI [256];
//--------------------------------------------------------------------
DWORD main (void)
{
Char sourcestring [1024] = "aabbccdffffc ";
Char searching [1024] = "B ";
Int I;
Int ret;
For (I = 0; I <256; I ++) ANSI [I] = NULL;
Lookupthesource (sourcestring, 1024); // enter the position of each character in the string to be searched into the list array ANSI
Ret = dothesearching (searching, strlen (searching); // matching process
Printf ("% s/n", sourcestring, searching );
Printf ("% d/N", RET );
Return 0;
}
//--------------------------------------------------------------------
DWORD lookupthesource (char * Source, int length)
{
Int I;
Pnode temp;
For (I = 0; I <length; I ++ ){
If (ANSI [source [I] = NULL ){
ANSI [source [I] = (pnode) malloc (sizeof (node ));
ANSI [source [I]-> value = I;
ANSI [source [I]-> next = NULL;
ANSI [source [I]-> isnext = false;
}
Else {
For (temp = ANSI [source [I]; temp-> next! = NULL; temp = temp-> next );
Temp-> next = (pnode) malloc (sizeof (node ));
Temp-> next-> value = I;
Temp-> next = NULL;
Temp-> next-> isnext = false;
}
}
Return 0;
}
//--------------------------------------------------------------------
DWORD dothesearching (char * STR, int length)
{
Int I;
Pnode temp;
For (I = 0; I <length; I ++) {// length-1?
// Next = 0;
For (temp = ANSI [STR [I]; temp! = NULL; temp = temp-> next ){
If (length = 1) return temp-> value; // special case when the destination string contains only one character
If (I = 0 | temp-> isnext = true) {// The isnext of the first character of the target string is false.
If (havenext (STR [I + 1], temp-> value + 1) & I + 1 = length-1) {// whether the current word is used
// The next character of the character in the string to be searched is the next character of the character in the target string
Return (temp-> value + 1-(length-1); // only the first position found can be returned.
// Temp-> value + 1 the current character is length-1; return the position where the first character of the target string appears
}
}
}
}
Return-1;
}
//--------------------------------------------------------------------
Bool havenext (char, int position)
{
Pnode temp;
For (temp = ANSI [char]; temp! = NULL; temp = temp-> next ){
If (temp-> value = position ){
Temp-> isnext = true;
Return true;
}
If (temp-> value> position) return false; // The linked list node is sequential by character
}
Return false;
}
//--------------------------------------------------------------------
If there is a problem, change it to a new one, and the efficiency is not very good, you can try again
/*************************************** ********************************
* String matching uty @ uaty
* Algorithm: first, enter the position of each character in the string to be searched into the ANSI array of the linked list. The ASCII value of the character is
* Array index. When the same character appears more than once, a node is added to the corresponding linked list.
* When matching, use each character of the target string as the index to locate the position of the string to be searched
* If the values of each position increase by 1, the target string appears in the string to be searched.
**************************************** *******************************/
# Include <stdio. h>
# Include <windows. h>
DWORD lookupthesource (char * Source, int length );
DWORD dothesearching (char * STR, int length, char * sourcestring );
Bool havenext (char, int position );
Typedef struct _ Node
{
Int value; // The Position of the string to be searched.
Struct _ node * next;
Bool isnext; // The Position of the character is the next character in the target string.
} Node, * pnode;
Pnode ANSI [256];
//--------------------------------------------------------------------
DWORD main (void)
{
// "Dsfserwfgdfgertertreterterrrrrrrrrrrrrrrtrertfgdfgdfgddd" "fgdfgd"
Char sourcestring [1024] = "aaaaaaaabbdeaaaaaaaaaaaaaaaaaaaaa ";
Char searching [1024] = "abbde ";
Int I;
Int ret;
For (I = 0; I <256; I ++) ANSI [I] = NULL;
For (I = 0; I <strlen (sourcestring); I ++ ){
Printf ("% C", sourcestring [I]);
If (I + 1) % 10 = 0) printf ("");
}
Printf ("/N ");
Printf ("% s/n", searching );
Lookupthesource (sourcestring, 1024); // enter the position of each character in the string to be searched into the list array ANSI
Ret = dothesearching (searching, strlen (searching), sourcestring); // matching process
Printf ("% d/N", RET );
Return 0;
}
//--------------------------------------------------------------------
DWORD lookupthesource (char * Source, int length)
{
Int I;
Pnode temp;
For (I = 0; I <length; I ++ ){
If (ANSI [source [I] = NULL ){
ANSI [source [I] = (pnode) malloc (sizeof (node ));
ANSI [source [I]-> value = I;
ANSI [source [I]-> next = NULL;
ANSI [source [I]-> isnext = false;
}
Else {
For (temp = ANSI [source [I]; temp-> next! = NULL; temp = temp-> next );
Temp-> next = (pnode) malloc (sizeof (node ));
Temp-> next-> value = I;
Temp-> next = NULL;
Temp-> next-> isnext = false;
}
}
Return 0;
}
//--------------------------------------------------------------------
DWORD dothesearching (char * STR, int length, char * sourcestring)
{
Int I;
Pnode temp;
For (I = 0; I <length; I ++) {// length-1?
// Next = 0;
For (temp = ANSI [STR [I]; temp! = NULL; temp = temp-> next ){
If (length = 1) return temp-> value; // special case when the destination string contains only one character
If (I = 0 | temp-> isnext = true) {// The isnext of the first character of the target string is false.
If (havenext (STR [I + 1], temp-> value + 1) & I + 1 = length-1) {// whether the current word is used
// The next character of the character in the string to be searched is the next character of the character in the target string
If (0 = strncmp (sourcestring + (temp-> value + 1-(length-1), STR, length )){
Return (temp-> value + 1-(length-1); // only the first position found can be returned.
// Printf ("% d/N", temp-> value + 1-(length-1 ));
}
// Printf ("% d/N", temp-> value + 1-(length-1 ));
// Temp-> value + 1 the current character is length-1; return the position where the first character of the target string appears
}
}
}
}
Return-1;
}
//--------------------------------------------------------------------
Bool havenext (char, int position)
{
Pnode temp;
For (temp = ANSI [char]; temp! = NULL; temp = temp-> next ){
If (temp-> value = position ){
Temp-> isnext = true;
Return true;
}
If (temp-> value> position) return false; // The linked list node is sequential by character
}
Return false;
}
//--------------------------------------------------------------------