Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<vector>
- #include <string>
- #include<algorithm>
- #include <iostream>
- #include <queue>
- #include<set>
- #include<stack>
- #include<cmath>
- #include<math.h>
- #include<map>
- using namespace std;
- #define ll long long
- #define pll pair<long long, long long>
- #define mp make_pair
- #define all(a) a.begin(), a.end()
- #define rall(a) a.rbegin(), a.rend()
- #define fs first
- #define sd second
- const ll inf = 1e9 + 123, llinf = 1e18 + 123;
- void xru() {
- setlocale(LC_ALL, "rus");
- /*freopen(".in", "r", stdin);
- freopen(".out", "w", stdout);*/
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- cout.tie(NULL);
- }
- void run(){
- cout<<endl;
- system("pause");
- }
- struct vertex{
- vertex* to[26];
- bool term;
- vertex(){
- for(ll i = 0; i<26; i++ ) to[i] = nullptr;
- term = false;
- }
- };
- vertex* root = new vertex();
- void push(string& s){
- vertex* v = root;
- for(char c: s){
- c-='a';
- if(v->to[c] == nullptr) v->to[c] = new vertex();
- v = v->to[c];
- }
- v->term = true;
- }
- bool check(string& s){
- vertex* v = root;
- for(char c: s){
- c-='a';
- if(v->to[c] == nullptr) return false;
- v = v->to[c];
- }
- return v->term;
- }
- string str = "";
- void write(vertex* v = root){
- if(v->term) cout<<str<<endl;
- for(ll i = 0; i<26; i++){
- if(v->to[i] != nullptr){
- str.push_back(i + 'a');
- write(v->to[i]);
- str.pop_back();
- }
- }
- }
- int main(){
- while(true){
- char c;
- cin>>c;
- if(c == '+'){
- string s;
- cin>>s;
- push(s);
- } else if (c == '?'){
- string s;
- cin>>s;
- if(check(s)) cout<<"Yes\n";
- else cout<<"No\n";
- } else if (c == '#'){
- write();
- } else{
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement