Can someone tell me why this is getting WA’d? thanks!
Problem link: NPLFLF Problem - CodeChef
/*
https://www.codechef.com/problems/NPLFLF
*/
#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define vin(v) for (auto& x : v) cin >> x
#define pb(x) push_back(x);
#define mp(x, y) make_pair(x, y);
#define sz(x) (int)x.size()
#define log(x) cout << x << '\n'
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
template<typename T>
void print(vector<T>& v) { cout << "--DBG--\n"; for (auto& x : v) cout << x << ' '; cout << "--DBG--\n"; }
typedef unsigned long long ull;
typedef long double ld;
typedef long long ll;
typedef double dd;
const int MOD = 1000000007;
// const int MOD = 998244353;
// #define TESTCASE
void solve()
{
}
struct TrieNode {
TrieNode* children[26] = {nullptr};
bool isWord = false;
};
class Trie {
private:
TrieNode* root;
vector<map<string, int>> v;
public:
Trie() { root = new TrieNode(); v.resize(100005); }
bool insert(const string& s)
{
string t;
auto tempMap = v;
TrieNode* tmp = root;
for (int i = sz(s) - 1; i >= 0; --i) {
t.pb(s[i]);
++v[sz(t)][t];
if (!tmp->children[s[i] - 'a'])
tmp->children[s[i] - 'a'] = new TrieNode();
tmp = tmp->children[s[i] - 'a'];
}
if (tmp->isWord) {
v = tempMap;
return false;
}
return tmp->isWord = true;
}
bool suffCount(int k, int len)
{
for (auto& avto : v[len]) {
debug(avto.first, avto.second);
if (avto.second == k)
return true;
}
return false;
}
bool deleteN(string s)
{
string t;
auto tempMap = v;
TrieNode* tmp = root;
for (int i = sz(s) - 1; i >= 0; --i) {
t.pb(s[i]);
--v[sz(t)][t];
tmp = tmp->children[s[i] - 'a'];
}
if (tmp->isWord) {
tmp->isWord = false;
return true;
}
return false;
}
};
int32_t main()
{
ios_base::sync_with_stdio(false); cin.tie(nullptr);
#ifdef TESTCASE
int t; cin >> t; while (t--) solve();
#endif
int q; cin >> q;
Trie sufftree;
map<int, string> insop;
for (int i = 1; i <= q; ++i) {
int t; cin >> t;
if (t == 1) {
string s; cin >> s;
insop.insert({i, s});
sufftree.insert(s);
}
else if (t == 2) {
int k, len; cin >> k >> len;
if (sufftree.suffCount(k, len)) log("YES");
else log("NO");
}
else {
int ith; cin >> ith;
sufftree.deleteN(insop[ith]);
}
}
return 0;
}