1. 程式人生 > >ZOJ 3957: Knuth-Morris-Pratt Algorithm

ZOJ 3957: Knuth-Morris-Pratt Algorithm

ofstream ext cin -s names fst algo text math

Knuth-Morris-Pratt Algorithm

[email protected] Sycamore, ZJNU
[email protected] 4/9/2017
#include <iostream>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <string>
#include <algorithm>
#include <numeric>
#include <functional>
#include <vector> #include <set> #include <list> #include <stack> #include <queue> #include <map> #include <algorithm> #include <cctype> #include<fstream> //#define cin fin //#define cout fout //ifstream fin("in.txt"); //ofstream fout("out.txt"); using namespace
std; typedef vector<int> VI; typedef pair<int, int> PII; int main() { ios::sync_with_stdio(false); int T; cin >> T; while (T--) { string s; cin >> s; int c = 0, pos = 0; while (true) { int ppos = min(s.find("cat", pos), s.find("dog", pos)); if (ppos < pos)
break; else { pos = ppos + 3; c++; } } cout << c << endl; } return 0; }

ZOJ 3957: Knuth-Morris-Pratt Algorithm