Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PATTERN MATCHING #1

Open
0mehedihasan opened this issue Jan 23, 2023 · 3 comments
Open

PATTERN MATCHING #1

0mehedihasan opened this issue Jan 23, 2023 · 3 comments

Comments

@0mehedihasan
Copy link
Owner

https://www.camscanner.com/share/show?device_id=AD_AID-7D9B830855E0CB80&sid=CC4A3311745D436ByY9KL93K&pid=dsa&style=1&t=1674492397&share_link_style=2

@0mehedihasan
Copy link
Owner Author

@0mehedihasan
Copy link
Owner Author

@razitzanin
Copy link

Second Pattern Matching:
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
void compute(char* pat, int M, int* lps);
void secondpattern(char* pat,char* txt)
{
int M = strlen(pat);
int N = strlen(txt);
int lps[M];
compute(pat, M, lps);

int i = 0;
int j = 0;
while ((N - i) >= (M - j))//O(N)
{
    if (pat[j] == txt[i])
    {
        j++;
        i++;
    }

    if (j == M)
    {
        printf("Found pattern at index %d \n", i - j);
        j = lps[j - 1];
    }
    else if (i < N && pat[j] != txt[i])
    {
        if (j != 0)
            j = lps[j - 1];
        else
            i = i + 1;
    }
}

}
void compute(char* pat, int M, int* lps)
{
int len=0;
lps[0]=0;
int i=1;
while(i<M)
{
if (pat[i] == pat[len])
{
len++;
lps[i] = len;
i++;
}
else
{
if (len != 0)
{
len = lps[len - 1];
}
else
{
lps[i] = 0;
i++;
}
}
}
}
int main()
{
char txt[] = "ABABDABACDABABCABABABABCABABABABCABAB";
char pat[] = "ABABCABAB";
secondpattern(pat, txt);
return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants