Professional Documents
Culture Documents
5 6253808378267567498
5 6253808378267567498
Let n be the length of text and m be the total number characters in all words, i.e. m = length(arr[0]) +
length(arr[1]) + … + length(arr[k-1]). Here k is the total number of input words.
currentState = g[currentState][ch];
}
out[currentState] |= (1 << i);
}
for (int ch = 0; ch < MAXC; ++ch)
if (g[0][ch] == -1)
g[0][ch] = 0;
memset(f, -1, sizeof f);
queue<int> q;
for (int ch = 0; ch < MAXC; ++ch)
{
if (g[0][ch] != 0)
{
f[g[0][ch]] = 0;
q.push(g[0][ch]);
}
}
while (q.size())
{
int state = q.front();
q.pop();
for (int ch = 0; ch <= MAXC; ++ch)
{
if (g[state][ch] != -1)
{
int failure = f[state];
while (g[failure][ch] == -1)
failure = f[failure];
failure = g[failure][ch];
f[g[state][ch]] = failure;
out[g[state][ch]] |= out[failure];
q.push(g[state][ch]);
}
}
}
return states;
}
int findNextState(int currentState, char nextInput)
{
int answer = currentState;
int ch = nextInput - 'a';
while (g[answer][ch] == -1)
answer = f[answer];
return g[answer][ch];
}
void searchWords(string arr[], int k, string text)
{
buildMatchingMachine(arr, k);
int currentState = 0;
for (int i = 0; i < text.size(); ++i)
{
currentState = findNextState(currentState, text[i]);
if (out[currentState] == 0)
continue;
for (int j = 0; j < k; ++j)
{
if (out[currentState] & (1 << j))
{
cout << "Word " << arr[j] << " appears from "
<< i - arr[j].size() + 1 << " to " << i << endl;
}
}
}
}
int main()
{
/* string arr[] = {"he", "she", "hers", "his"};
string text = "ahishers";*/
int i,n;
cin>>n;
string arr[n],text;
for(i=0;i<n;i++)
cin>>arr[i];
cin>>text;
int k = sizeof(arr)/sizeof(arr[0]);
printf("cpp");
searchWords(arr, k, text);
printf("cpp");
return 0;
}
-------------
2nd solution:
import java.util.*;
class Test{
Arrays.fill(out, 0);
Arrays.fill(g[i], -1);
int states = 1;
int currentState = 0;
if (g[currentState][ch] == -1)
g[currentState][ch] = states++;
currentState = g[currentState][ch];
if (g[0][ch] == -1)
g[0][ch] = 0;
Arrays.fill(f, -1);
if (g[0][ch] != 0)
f[g[0][ch]] = 0;
q.add(g[0][ch]);
while (!q.isEmpty())
q.remove();
if (g[state][ch] != -1)
failure = f[failure];
failure = g[failure][ch];
f[g[state][ch]] = failure;
out[g[state][ch]] |= out[failure];
q.add(g[state][ch]);
return states;
answer = f[answer];
return g[answer][ch];
String text)
buildMatchingMachine(arr, k);
int currentState = 0;
for(int i = 0; i < text.length(); ++i)
currentState = findNextState(currentState,
text.charAt(i));
if (out[currentState] == 0)
continue;
(i - arr[j].length() + 1) +
int i,n;
n = Integer.parseInt(sc.nextLine());
for(i=0;i<n;i++) {
arr[i] = sc.nextLine();
searchWords(arr, n, text);
}
Given a text txt[0..n-1] and a pattern pat[0..m-1], write a function search(char pat[], char txt[]) that
prints all occurrences of pat[] in txt[]. You may assume that n > m.
#include<string.h>
#include<stdio.h>
#define d 256
int M = strlen(pat);
int N = strlen(txt);
int i, j;
int p = 0;
int t = 0;
int h = 1;
h = (h*d)%q;
p = (d*p + pat[i])%q;
t = (d*t + txt[i])%q;
if ( p == t )
if (txt[i+j] != pat[j])
break;
}
if (j == M)
if ( i < N-M )
if (t < 0)
t = (t + q);
int main()
scanf("%s",txt);
scanf("%s",pat);
int q = 101;
return 0;
Write a program to insert a set of strings into the trie and search for a word in it.
import java.io.*;
import java.util.*;
class Trie {
boolean isEndOfWord;
TrieNode(){
isEndOfWord = false;
children[i] = null;
};
int level;
int index;
if (pCrawl.children[index] == null)
pCrawl = pCrawl.children[index];
pCrawl.isEndOfWord = true;
int level;
int index;
TrieNode pCrawl = root;
if (pCrawl.children[index] == null)
return false;
pCrawl = pCrawl.children[index];
int i,n;
n = Integer.parseInt(sc.nextLine());
for(i=0;i<n;i++) {
keys[i] = sc.nextLine();
insert(keys[i]);
//System.out.println(search(word));
if(search(word) == true)
System.out.println(output[1]);
else System.out.println(output[0]);
Write a program to find that How many times a given pattern(p) is present in the given text(t).
Use KMP (Knuth Morris Pratt) Pattern Searching algorithm to solve this.
#include<stdio.h>
#include<string.h>
int c=0;
int M = strlen(pat);
int N = strlen(txt);
int lps[M];
computeLPSArray(pat, M, lps);
int i = 0;
int j = 0;
while (i < N) {
if (pat[j] == txt[i]) {
j++;
i++;
if (j == M) {
c++;
j = lps[j - 1];
if (j != 0)
j = lps[j - 1];
else
i = i + 1;
return c;
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) {
else
lps[i] = 0;
i++;
int main()
char txt[50],pat[25];
scanf("%s",txt);
scanf("%s", pat);
printf("%d",KMPSearch(pat, txt));
return 0;
Given a text txt[0..n-1] and a pattern pat[0..m-1], write a function search(char pat[], char txt[]) that
prints all occurrences of pat[] in txt[]. You may assume that n > m.
#include<stdio.h>
#include<string.h>
return state+1;
int ns, i;
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;
int state, x;
int M = strlen(pat);
int N = strlen(txt);
int TF[M+1][NO_OF_CHARS];
computeTF(pat, M, TF);
int i, state=0;
state = TF[state][txt[i]];
if (state == M)
int main()
char txt[50],pat[25];
scanf("%s",txt);
scanf("%s",pat);
search(pat, txt);
return 0;