Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include<stdio.h>
- #include<string.h>
- using namespace std;
- #define NO_OF_CHARS 256
- int getNextState(string pat, int M, int state, int x)
- {
- if (state < M&& x == pat[state]) //jesli znak jest taki sam jak nastepny to zwieksz stan
- return state + 1;
- int ns, i;
- for (ns = state; ns > 0; ns--)
- {
- if (pat[ns - 1] == x)
- {
- for (i = 0; i < ns - 1; i++)
- if (pat[i] != pat[state - ns + 1 + i])
- break;
- if (i == ns - 1)
- return ns;
- }
- }
- return 0;
- }
- void computeTF(string pat, int M, int** TF)
- {
- int state, x;
- for (state = 0; state <= M; ++state)
- for (x = 0; x < NO_OF_CHARS; ++x)
- TF[state][x] = getNextState(pat, M, state, x);
- }
- void search(string pat, string txt)
- {
- int M = pat.size();
- int N = txt.size();
- int** TF = new int* [M + 1];
- for (int i = 0; i < M + 1; ++i)
- TF[i] = new int[NO_OF_CHARS];
- computeTF(pat, M, TF);
- int i, state = 0;
- for (i = 0; i < N; i++)
- {
- state = TF[state][txt[i]];
- if (state == M)
- cout << " Pattern found at index " << i - M + 1 << endl;
- }
- }
- int main()
- {
- string txt = "AABCABBABABABABAAABA";
- string pat = "AABA";
- search(pat, txt);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement