#include #include #include #include #include using namespace std; int delta[100][100]; set alphabet; string P, T; int low; bool isSuffix(string suff, string s) { return suff == s.substr(s.size() - suff.size(), s.size()); } void getAlphabet(string T) { for (int i = 0; i < T.size(); i++) { alphabet.insert(T[i]); } set::iterator it = alphabet.begin(); low = (int)*it; } void computeTransitionFunction() { int m = P.size(); string temp; for (int q = 0; q <= m; q++) { for (set ::iterator it = alphabet.begin(); it != alphabet.end(); it++) { char c = *it; int k = min(m + 1, q + 2); do { k--; } while (!isSuffix(P.substr(0, k), P.substr(0, q) + c)); delta[q][(int)c - low] = k; } } } void printTransitionFunction() { //cout << " "; //for (set ::iterator it = alphabet.begin(); it != alphabet.end(); it++) //{ // cout << *it << " "; //} //cout << "\n"; for (int i = 0; i <= P.size(); i++) { //cout << i << " "; for (set ::iterator it = alphabet.begin(); it != alphabet.end(); it++) { char c = *it; //cout << delta[i][(int)c - low] << " "; } //cout << "\n"; } } int finiteAutomationMatcher() { int br = 0; int q = 0; int n = T.size(); int m = P.size(); for (int i = 0; i < n; i++) { q = delta[q][(int)T[i] - low]; if (q == m) br++; //cout << i - m + 1 << " "; } return br; } int main() { fstream myfile; myfile.open("clown.in"); ofstream wfile("clown.out"); getline(myfile, T); int q; myfile >> q; getline(myfile, P); for (int i = 0; i < q; i++) { getline(myfile, P); getAlphabet(T); computeTransitionFunction(); printTransitionFunction(); wfile << finiteAutomationMatcher() << endl; } return 0; } //#include //#include //using namespace std; // //int main() { // // fstream myfile; // myfile.open("scourge.in"); // ofstream wfile("scourge.out"); // // int N; // int K; // // if (myfile.is_open()) { // while (!myfile.eof()) { // // // myfile >> N; // myfile >> K; // // unsigned how, i, j; // printf("%u = ", N); // i = 1; // while (N != 1) // { i++; // how = 0; // while (0 == N % i) // { // how++; // N = N / i; // } // // for (j = 0; j < how; j++) // printf("%u ", i); } // // if (wfile.is_open()) // { // wfile << "BUY"; // wfile.close(); // } // return 0; // } // } // myfile.close(); // //}