P2336 ac自动机方法挂掉

P2336 [SCOI2012] 喵星球上的点名

emmm 我又发错了 ```cpp #include<iostream> #include<algorithm> #include<vector> #include<stack> #include<queue> #include<string> #include<cstring> #include<climits> #include<cmath> #include<map> #include<set> using namespace std; bool judge[100010]; int ans_teacher[100010]; int ans_cat[100010]; struct tire { static const int maxn = 26; struct node { map<int, node *>nex; node *fail = nullptr; int exist = -1; int exist_num = 0; }; node *root = new node; void insert(vector<int>&name, int ord) { node *now = root; for (int id : name) { if (now->nex.find(id) == now->nex.end()) now->nex[id] = new node; now = now->nex[id]; } now->exist = ord; now->exist_num++; } void init_fail() { node *now; queue<node *>que; que.push(root); while (!que.empty()) { now = que.front(); que.pop(); for (auto ite = now->nex.begin(); ite != now->nex.end(); ite++) { if (now == root) ite->second->fail = root; else { node *t = now->fail; while (t != nullptr) { if (t->nex.find(ite->first) != t->nex.end()) { ite->second->fail = t->nex[ite->first]; break; } t = t->fail; } if (t == nullptr) ite->second->fail = root; } que.push(ite->second); } } } int query(vector<int>&name) { int cal = 0; node *now = root; for (int id : name) { while (now->nex.find(id) == now->nex.end() && now != root) now = now->fail; if (now->nex.find(id) == now->nex.end()) continue; now = now->nex[id]; node *t = now; while (t != root) { if (t->exist != -1 && !judge[t->exist]) { cal += t->exist_num; judge[t->exist] = true; ans_teacher[t->exist]++; } t = t->fail; } } return cal; } }tire; vector<int>name[10010][2]; vector<int>call; int main() { ios_base::sync_with_stdio(false); int N, M; cin >> N >> M; for (int i = 0; i < N; i++) { for (int k = 0; k < 2; k++) { int t; cin >> t; for (int j = 0; j < t; j++) { int n; cin >> n; name[i][k].push_back(n); } } } for (int i = 0; i < M; i++) { vector<int>().swap(call); int t; cin >> t; for (int j = 0; j < t; j++) { int n; cin >> n; call.push_back(n); } tire.insert(call, i); } tire.init_fail(); for (int i = 0; i < N; i++) { memset(judge, 0, sizeof(judge)); ans_cat[i] += tire.query(name[i][0]); ans_cat[i] += tire.query(name[i][1]); } for (int i = 0; i < M; i++) cout << ans_teacher[i] << "\n"; for (int i = 0; i < N; i++) cout << ans_cat[i] << " "; } ```
by 孤月 @ 2019-03-08 16:33:41


|