#include #include #include using namespace std; map a; map cache; int calc_distance(string t, string c) { int len = t.length() < c.length() ? t.length() : c.length(); // min int i; for(i = 0; i < len; ++i) { if(t[i] != c[i]) { break; } } return t.length() - i + c.length() - i; } int main() { ifstream inp; inp.open("hipsters.in"); ofstream out; out.open("hipsters.out"); int q, t; string n; int d; inp >> q; map::iterator it; for(int i = 0; i < q; ++i) { inp >> t >> n; if(n == "0") { n = ""; } if(t == 1) { it = cache.find(n); if(it != cache.end()) { out << it->second << endl; } else { d = 0; for(it = a.begin(); it != a.end(); ++it) { d += calc_distance(n, it->first) * it->second; } cache.insert(pair(n, d)); out << d << endl; } } else if(t == 2) { it = a.find(n); if(it != a.end()) { it->second++; } else { a.insert(pair(n, 1)); } for(it = cache.begin(); it != cache.end(); ++it) { it->second += calc_distance(n, it->first); } } else // if(t == 3) { it = a.find(n); if(it->second == 1) { a.erase(it); } else { it->second--; } for(it = cache.begin(); it != cache.end(); ++it) { it->second -= calc_distance(n, it->first); } } } inp.close(); out.close(); return 0; }