Because I'm recently ready to start learning to do some small Android projects practiced hand, I have a system-level three applications, dials, contacts and text messages, ready to start from the simplest dial-up, but because these applications are inevitably automatically prompted, I think the design is a string matching problem, It is intended to be implemented in C and will be integrated into the application by JNI in the future.
1. The first is a simple match, which is actually poor lifting:
Functions implemented in C language I put here:
1 voidNaive_string_match (Char*t,Char*p) {2 intn =strlen (t);3 intm =strlen (p);4 ints;5 for(s=0; s<=n-m;++s) {6 if(!STRNCMP (p,t+s,m)) {7printf"OK,%d", s);8 }9 }Ten}
(Note that for future porting, all functions in my implementation are included in the POSIX standard)
Here's a description of the two methods I'm not familiar with.
1.strlen (string.h) computes the string length passed in: A pointer to the C-type string (char */char []) is returned: A string length of non-2.strncmp (string.h) compares the first n characters of two strings in the same pass: Two strings to compare, and N returns: Same return 0, different returns other values depending on the situation
2.rabin-karp algorithm
The concept of basic number theory is not very well understood, but simple implementation is still possible,
Because this algorithm describes comparisons against numeric strings, my implementation is mainly for numbers such as mobile phone numbers to match, of course, the description also depends on the introduction of the algorithm description
(1) Qin Jiushao/horner Law
1 intHornerChar*p,intDintm) {2 intresult =0;3 inti;4 5 for(i=0; i<m-1;++i) {6result = ((p[i]- -) + result) *D;7 }8 9result+= (p[m-1]- -);Ten returnresult; One}
(2) Calculate the numeric size of the T string by offset
1 int* CAL (Char*t,intMintd) {2 inti;3 intSub = strlen (t)-m;4 Char*tmp = (Char*)malloc(M *sizeof(Char) +1);5 int*result = (int*)malloc(Sub +1) *sizeof(int));6 7TMP =strncpy (tmp,t,m);8result[0]=Horner (tmp,d,m);9 Ten for(i=1; i<sub+1;++i) { OneResult[i] = (result[i-1]-POW (d,m-1) * (t[i-1] - -)) *d + (t[i-1+m]- -); A } - - Free(TMP); the returnresult; -}
Note that the POW function is a simple function written by itself rather than a function in MATH.H:
1 int Pow ( int d,int m) { 2 int result=1 ; 3 while (m> 0 4 result *= D; 5 --M; 6 7 return result; 8 }
(3) Main algorithm:
1 voidRabin_karp_matcher (Char*t,Char*p,intDintq) {2 ints,i,c;3 intm=strlen (p);4 intn=strlen (t);5 6 intPx=horner (P,d,strlen (p))%Q;7 int*tx=cal (t,m,d);8 9C=n-m +1;Ten One for(i=0; i<c;++i) { ATx[i]%=Q; - } - the for(s=0; s<c;++s) { - if(px==Tx[s]) { - if(!STRNCMP (p,t+s,m)) { -printf"OK,%d\n", s); + } - } + } A at Free(TX); -}
(Note that the TX location is the space I allocated earlier, so it's OK to use it after it's gone)
Here is a description of a function that I am unfamiliar with in implementing the above code:
3.strncpy (string.h) copies the first m characters of a string to another character array (must be at least a storage location larger than m) (note: This last bit must be taken into account, but when you output the target character array by%s, Show on Windows The last one is '? ' Incoming: Target character array, original character array, count m output: pointer to the first address of the target character array
Test Case:
(no accident, this is the matching algorithm I used to do the dial)
3 ... to be continued
C language implementation of string matching algorithm--Introduction to Algorithms third Edition (1)