March 24th thought of a string matching new algorithm time complexity self n't counted

xiaoxiao2021-03-06  21

/ ************************************************** ************** * String Match Uty @ uaty * Algorithm: First, the location of each character in the string is first recorded in the list array ANSI, characters The ASCII value is a * array index. When the same character occurs once, the corresponding linked list adds a node * matching, each character of the target is used as an index to find the location of the search string *, if The value of each location is incremented in turn, indicating that the destination string appears in the string to be searched, ************************************* **************************************************** / # include #include DWORD LOOKUPTHESOURCE (CHAR * SOURCE, INT LENGTH); DWORD DOTHESEARCHING (Char * Str, Int Length); Bool Havenext (CHAR Char, INT POSITION);

TypedEf struct _node {int value; // Location struct _node * next; bool isnext; // of the character is located in the destination string of the next} node, * pnode; pnode ANSI [256 ]; // ------------------------------------------------------------------------------------------------------------------------ ---------------------- DWord main (void) {char SourceString [1024] = "Aabbccdfffff"; char searching [1024] = "b"; int i ; Int Ret; for (i = 0; i <256; i ) ANSI [i] = null;

Lookupthesource (SourceString, 1024); // Positions to the position of each character in the string to appear in the list array ANSI RET = DOTHESEARCHING (Searching, Strlen (Searching)); // Matching Process Printf ("% S / N% S / N ", SourceString, Searching; Printf ("% D / N ", RET); return 0;} // ------------------------ ------------------------------------------ DWORD LOOKUPTHESOSOURCE (Char * SOURCE , intleth) {INT I; PNODE TEMP; for (i = 0; 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 -> next = null; Temp-> Next -> isnext = false;}} return 0;} // --------------- -------------------------------------------------- --- DWord Dothesearch (CHAR * STR, INT Length) {INT I; PNODE TEMP; for (i = 0; I Next) {if (length == 1) Return Temp-> value; // Consider only one character of the destination only, special IF (i == 0 || TEMP-> isnext == true) {// The first character of the destination string is False if (Havenext (STR [i 1], TEMP-> Value 1) && i 1 == Length -1) {// Whether the current word // The next character in the position in the string is the next character in the destination string (TEMP-> Value 1 - (length-1)); // This can only return the first location // TEMP-> Value 1 Current characters are the first character; Return destination string first character appears}}}}} Return -1;} // -------------------------------------------------- ---------------- Bool Havenext (Char char, int position) {pnode temp; for (Temp = ANS I [char]; Temp! = Null; Temp = Temp-> Next) {if (Temp-> value == position) {TEMP-> isnext = true; return true;} if (Temp-> Value> Position Return False; // Link table node The order in the character appears} Return False;} // ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------ There is a problem, change new, the efficiency is not very good, Reproduce

/ ************************************************** ************** * String Match Uty @ uaty * Algorithm: First, the location of each character in the string is first recorded in the list array ANSI, characters The ASCII value is a * array index. When the same character occurs once, the corresponding linked list adds a node * matching, each character of the target is used as an index to find the location of the search string *, if The value of each location is incremented in turn, indicating that the destination string appears in the string to be searched, ************************************* **************************************************** / # include #include DWORD lookupthesource (char * source, int length); DWORD dotheseArching (char * str, int length, char * sourcestring); BOOL hAvenext (char chAr, int position); typedef struct _node {int vAlue; / / In the position htruct _node * next; bool isnext; / / The location of the character is the next} node, * pnode; pnode ANSI [256]; // --- -------------------------------------------------- --------------- DWORD main (void) {// "dsfserwfgdfgertertertreterterrrrrrrrrrrrrrrrrtrertfgdfgdfgddd" "fgdfgd" char sourcestring [1024] = "AAAAAAAABBDEAAAAAAAAAAAAAAAAAAAAAAA"; char seArching [1024] = "ABBDE"; int I; int Ret; for (i = 0; I <256; i ) ANSI [I] = NULL;

For (i = 0; I Value = I; ANSI [Source [i]] -> Next = NULL; ANSI [Source [i]] -> isnext = false;

转载请注明原文地址:https://www.9cbs.com/read-42916.html

New Post(0)