Title Requirements:
Gives a string of very long strings, requiring the matching strings to be found.
For example, the destination string: 123, then 1*****3***2,12*****3 these are to find out.
is actually similar to some harmonious system ...
Topic Analysis:
1. If the destination string is: "423" and the input long string is: "4fsdfk2jfl3fd2jfksd3j4d4d4jkfd4jd3kdf2", then "4fsdfk2jfl3" should be output," 2jfksd3jld4 ","4jd3kdf2".
2. Using the map< character in C + +, the number of each character of the destination string is stored in map, and the number of each character is initialized to 1. Then the characters in the long string are traversed one after the other, if the character is stored in the map and the number is 1, then num plus 1, when num=n, The description has found the characters in the N destination string, can output, and then re-initializes the map and other variables after the output. Startflag=1 indicates that this traversal has not found the characters in the long string matched to the characters in the destination string, and when the first match is made, the startflag=0. Startpos is updated at the same time, and the startpos represents the position of the first time that a time has elapsed.
Code implementation:
#include <iostream>#include<map>using namespacestd;Const intN =3;voidHandle (Char*INPUTSTR,Char*desstr);intMainvoid){ CharInput[] = {"4FSDFK2JFL3FD2JFKSD3J4D4D4JKFD4JD3KDF2"}; CharDes[] ="423"; Handle (Input,des); return 0;}voidPRINTSTR (Char*start,Char*end) { while(Start! =end) {cout<< *start; Start++; } cout<< *end; cout<<Endl;}voidInithash (map<Char,int> &hash,Char*str) { while(*str! =' /') {hash[*STR] =1; STR++; }}voidHandle (Char*INPUTSTR,Char*desstr) { if(Inputstr==null | | desstr==NULL)return ; Map<Char,int>Hash; Inithash (HASH,DESSTR); intStartflag =1, num =0; Char*startpos; Map<Char,int>:: Iterator iter; while(*inputstr! =' /') {iter= Hash.find (*inputstr); if(ITER! =Hash.end ()) { if((*iter). Second = =1) { if(startflag) {startpos=Inputstr; Startflag=0; } ++hash[*INPUTSTR]; Num++; if(num==3) {printstr (STARTPOS,INPUTSTR); //re-initializenum=0; Inithash (HASH,DESSTR); Startflag=1; } } //when the first match character appears consecutively, select the last//If you choose to output "4JD3KDF2" instead of "4D4D4JKFD4JD3KDF2" because the first match is 4, there are multiple//4 Consecutive, choose the last one . Else if(*startpos = = *inputstr) {Startpos=Inputstr; }} inputstr++; }}
String matching algorithm "Microsoft interview 100 question 33rd"