Simple string comparison function easy to understand complexity

Source: Internet
Author: User
Tags comparison strlen

1. Simple string matching function

Simple string matching is simple, it is a dual loop.

Calculate the hair One:

#include <stdio.h>

#include <string.h>

Char s[51],t[11];

int next[11];

int cnt[11];

void index (Char*s,char *t,int POS)

{

int i,j,k;

int x = 0;

int len1 = strlen (s);

int len2 = strlen (t);

for (i=pos;i<len1;i++)

{

K = i;

for (j=0;j<len2;j++)

{

if (S[k++]!=t[j])

Break

}

if (J>=LEN2)

{

cnt[x++] = i;

}

}

}

int main (void)

{

memset (cnt,-1,11*sizeof (int));

Gets (s);

Gets (t);

int POS;

scanf ("%d", &pos);

Index (S,T,POS);

for (int i=0;cnt[i]!=-1;i++)

{

printf ("%d", cnt[i]);

}

return 0;

}

The following analysis of its complexity:

In addition to index2,index4,index6,index10 around the string comparison several times, in other places the string is only compared once, the total number of comparisons is (n-m+1) + (m-1) +others,others is the red part, This means that its complexity is close to O (n+m).

Assuming that the length of the main string is n, the length of the pattern string is M, and in the worst case, for each character in the main string (n-m+1), the characters in the pattern are matched m times, followed by a total number of matches (1+2+...+ (m-1)) =m (m-1)/2, the total number of matches is (n-m+ 1) *m+ m (m-1)/2, that is, the complexity of the calculation is O (n*m).

The above method is more intuitive and simple, but the complexity is very high, inefficient, difficult to apply to the actual, there is an improved version, the function is the output of the first occurrence of the sub-string index.

Calculate Hair Two:

#include <stdio.h>

#include <string.h>

Char s[51],t[11];

int next[11];

int index (char *s,char *t,int POS)

{

int i =pos-1;

int j = 0;

int len1= strlen (s);

int len2 =strlen (t);

while (I<LEN1&&J<LEN2)

{

if (S[i]==t[j])

{

i++;

j + +;

}

Else

{

I-=j-1;

j = 0;

}

}

if (J>=LEN2)

{

Returni-len2;

}

Else

return-1;

}

int main (void)

{

Gets (s);

Gets (t);

int POS;

scanf ("%d", &pos);

int no =index (s,t,pos);

if (no>0)

printf ("String s contains substring T at index of%d~~\n", no);

Else

printf ("String s does not contain substring t~~\n");

return 0;

}

Let's analyze the complexity of the

In addition to the index2,index4,index6,index10 around the string comparison several times, in other places the string is only compared once, assuming that the final find to the substring position is index, in a single lookup process, the final comparison is (index+len2-1) + Others, this others is the result of many comparisons that have been made before. Its complexity is close to O (n+m). In the worst case, its complexity is also O (n*m).

Calculation of the third:

#include <stdio.h>

#include <string.h>

Char s[51],t[11];

int next[11];

int cnt[11];

void index (char *s,char *t,int POS)

{

intx=0;

int i = pos-1;

int j = 0;

int len1 = strlen (s);

int len2 = strlen (t);

for (i=pos-1;i<len1;)

{

while (I<LEN1&&J<LEN2)

{

if (S[i]==t[j])

{

i++;

j + +;

}

Else

{

I-= j-1;

j = 0;

}

}

if (J>=LEN2)

{

cnt[x++] = I-len2;

I-= len2-1;

j = 0;

}

}

}

int main (void)

{

memset (cnt,-1,11*sizeof (int));

Gets (s);

Gets (t);

int POS;

scanf ("%d", &pos);

Index (S,T,POS);

for (int i=0;cnt[i]!=-1;i++)

{

printf ("%d", cnt[i]);

}

printf ("\ n");

return 0;

}

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.