Title Link: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4917
Test instructions: Every word has some tips words. Enter n words and their tips first. Then the M group queries, each query some words, the dictionary order output these words public tips. (each word contains only lowercase uppercase letters)
Idea: For the first word, use the vector array G,g[i] to save all tips for this word. For all words, create a dictionary tree, and at the end of the word, save the tips of the word in a one-dimensional subscript i in the G array. Finally, a map is used to count each of the tips in each set of queries. In each set of queries, all the tips count values in all the query words are recorded as the number of words, eventually sorted by dictionary order, and finally output.
Code:
#include <iostream> #include <stdio.h> #include <string.h> #include <string> #include <map > #include <set> #include <algorithm> #include <vector>using namespace std;const int N = 3e3 + 10;const int SIZE = 60;const int Max_word = 305;struct Trie {int val[size];int w;}; int Sz;char Str[max_word];char St[max_word]; Trie pn[n];map<string, int> m;vector<string> g[n];vector<string> res;int newnode () {memset (Pn[sz]. Val, 0, sizeof (pn[sz].val));p N[SZ].W = -1;return sz++;} void init () {sz = 0;newnode ();} void Insert (char *s, int j) {int u = 0;int len = strlen (s); for (int i = 0; i < len; i++) {int idx = s[i]-' A '; if (!pn[ U].VAL[IDX]) Pn[u].val[idx] = NewNode (); u = Pn[u].val[idx];} PN[U].W = j;string t;gets (str), len = strlen (str), for (int i = 0; i < len; i++) {if (str[i] = = ") {g[j].push_back (t); t . Clear ();} Elset.push_back (Str[i]);} if (!t.empty ()) G[j].push_back (t);} int findstr (char *s) {int u = 0;int len = strlen (s); for (int i = 0; I < Len i++) {int idx = s[i]-' A '; if (!pn[u].val[idx]) return-1;u = Pn[u].val[idx];} if (G[pn[u].w].empty ()) return-1;for (int i = 0; i < g[pn[u].w].size (); i++) M[g[pn[u].w][i]]++;return u;} int main () {int n, q;while (scanf ("%d", &n)! = EOF) {init (); for (int i = 0; i < n; i++) {scanf ("%s", str); GetChar () ; G[i].clear (); Insert (str, i);} scanf ("%d", &q); GetChar (); int k, t;for (int i_q = 1; i_q <= q; i_q++) {m.clear (); gets (str); int y = 0;t = 0;int len = strlen (str); for (int i = 0; i < len; i++) {st[y++] = str[i];if (str[i + 1] = = ' + ' | | str[i + 1] = = ') {St[y] = ' '; k = findstr (ST); if (k = =-1) break;t++;y = 0;i++;}} if (k = =-1) {puts ("NO"); continue;} Res.clear (); for (int i = 0; i < g[pn[k].w].size (); i++) if (m[g[pn[k].w][i]] = = t) res.push_back (g[pn[k].w][i]); if (RES.E Mpty ()) puts ("NO"), else {sort (Res.begin (), Res.end ()); for (int i = 0; i < res.size ()-1; i++) cout << Res[i] <& Lt ""; cout << res[res.size ()-1] << Endl;}}} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
ZOJ 3674 Search in the Wiki (dictionary tree + map + vector)