Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <string>
- using namespace std;
- ifstream fin("C:\\clionfolder\\prog4\\input.txt");
- ofstream fout("C:\\clionfolder\\prog4\\output.txt");
- int N;
- class Letter {
- char symbol;
- int *map;
- public:
- Letter(char a) : symbol(a), map(new int[N]) {};
- ~Letter() {
- delete[] map;
- }
- Letter(const Letter &orig) {
- this->map = new int[N];
- for (int i = 0; i < N; i++) {
- this->map[i] = orig.map[i];
- }
- }
- char getchar() {
- return this->symbol;
- }
- void addstate(int from, int to) {
- this->map[from] = to;
- }
- int getstate(int from) {
- return this->map[from];
- }
- };
- class DKA {
- bool *ending;
- int start;
- vector<Letter> funcmap;
- public:
- DKA(int k) : ending(new bool[N]), start(k), funcmap({}) {
- for (int i = 0; i < N; i++) {
- ending[i] = false;
- }
- }
- ~DKA() {
- delete[] ending;
- }
- DKA(const DKA &orig) {
- for (int i = 0; i < N; i++) {
- this->ending[i] = orig.ending[i];
- }
- }
- int searchfor(char a) {
- for (int i = 0; i < this->funcmap.size(); i++) {
- if (this->funcmap[i].getchar() == a) {
- return i;
- }
- }
- return -1;
- }
- void end(int k) {
- this->ending[k] = true;
- }
- void readfunc() {
- int from, to;
- char a;
- fin >> from >> to >> a;
- bool unseenbefore = true;
- for (int i = 0; i < this->funcmap.size(); i++) {
- if (this->funcmap[i].getchar() == a) {
- unseenbefore = false;
- }
- }
- int index;
- if (unseenbefore) {
- this->funcmap.emplace_back(a);
- index = this->funcmap.size() - 1;
- } else {
- index = this->searchfor(a);
- }
- this->funcmap[index].addstate(from, to);
- }
- int step(int from, char a) {
- int index = this->searchfor(a);
- if (index == -1) {
- throw "Error";
- }
- return this->funcmap[index].getstate(from);
- }
- bool stringscan(string str, int k) {
- for (int i = 0; i < str.length(); i++) {
- k = step(k, str[i]);
- }
- bool l = this->ending[k];
- return l;
- }
- };
- void dothething() {
- int k, f, p, temp, T;
- fin >> N >> k >> f;
- DKA dka(k);
- for (int i = 0; i < f; i++) {
- fin >> temp;
- dka.end(temp);
- }
- fin >> p;
- for (int i = 0; i < p; i++) {
- dka.readfunc();
- }
- fin >> T;
- string str;
- for (int i = 0; i < T; i++) {
- fin >> str;
- if (dka.stringscan(str, k)) {
- fout << "YES\n";
- } else {
- fout << "NO\n";
- }
- }
- }
- int main() {
- if (!fin.is_open())
- return 1;
- dothething();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement