Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- struct node{
- bool term = false;
- int ind;
- node* lett[26];
- node(int ind){
- this->ind = ind;
- for(auto &l :lett)
- l = nullptr;
- }
- };
- node* root = new node(0);
- vector<node*>nodes = {root};
- int cnter = 1;
- void add_to_bor(string s){
- node* curr = nodes[0];
- for(int i = 0; i < s.size(); i++){
- char a = s[i];
- if(curr->lett[a -'a'] == nullptr){
- curr->lett[a -'a'] = new node(cnter);
- curr = curr->lett[a -'a'];
- nodes.push_back(curr);
- cnter++;
- }
- }
- curr->term = true;
- }
- bool check(string s){
- node* curr = nodes[0];
- for(int i = 0; i < s.size(); i++){
- if(curr->lett[s[i] - 'a'] == nullptr){
- return false;
- }
- curr = curr->lett[s[i] - 'a'];
- }
- return curr->term;
- }
- bool check_substr(string s){
- node* curr = nodes[0];
- for(int i = 0; i < s.size(); i++){
- if(curr->lett[s[i] - 'a'] == nullptr){
- return false;
- }
- curr = curr->lett[s[i] - 'a'];
- }
- return true;
- }
- int main() {
- string text;
- cin >> text;
- for(int i = 0; i < text.size(); i++){
- // string s = string(text.begin() + i, text.end());
- // add_to_bor(s);
- node* curr = nodes[0];
- for(int j = i; j < text.size(); j++){
- char a = text[j];
- if(curr->lett[a -'a'] == nullptr){
- curr->lett[a -'a'] = new node(cnter);
- curr = curr->lett[a -'a'];
- nodes.push_back(curr);
- cnter++;
- }
- }
- curr->term = true;
- }
- int m;
- cin >> m;
- for(int i = 0; i < m; i++){
- string s;
- cin >> s;
- if(check_substr(s))
- cout << "Yes" << endl;
- else
- cout << "No" << endl;
- }
- // string s;
- // cin >> s;
- // add_to_bor(s);
- // while(cin >> s){
- // cout << bool(check(s)) << endl;
- // }
- // string s1 = string(s.begin()+1, s.end());
- // add_to_bor(s1);
- // node * curr = new node(1);
- // nods.push_back(curr);
- //
- // curr->lett[0] = new node(2);
- // curr = curr->lett[0];
- // nods.push_back(curr);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement