Original question: zoj 3674 http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemcode = 3674.
It is not hard to understand the meaning of the question. It is easy to think of brute force, but you have no idea how to implement it. After reading the code on the Internet, we directly used vector and map to use the set_intersection () function. I have heard of this function before, but I have never written it, so I typed it according to his code. I can see this function.
The code can be understood at a Glance. The key is whether you can write the code.
Code:
# Include <iostream> # include <cstdio> # include <cstring> # include <cmath> # include <algorithm> # include <string> # include <vector> # include <map> # include <set> # include <cstdlib> using namespace STD; # define n 10007 char name [35], ss [210], tips [210]; int check [110]; int main () {int n, m, I, J, a, B; int cword, ctips, IND, ccheck; while (scanf ("% d", & N )! = EOF) {cword = ctips = 0; Map <string, int> word; Map <string, int> tip; Map <int, string> ATIP; vector <int> G [110], K1, K2; For (j = 1; j <= N; j ++) {scanf ("% s", name ); if (! Word [name]) word [name] = ++ cword; // hash A = word [name]; getchar (); gets (SS); ind = 0; for (I = 0; SS [I]; I ++) {If (ss [I] = '') {tips [ind] = '\ 0 '; if (! Tip [tips]) {tip [tips] = ++ ctips; ATIP [ctips] = tips;} B = tip [tips]; G [A]. push_back (B); ind = 0;} else tips [ind ++] = ss [I];} tips [ind] = 0; If (! Tip [tips]) {tip [tips] = ++ ctips; ATIP [ctips] = tips;} B = tip [tips]; G [A]. push_back (B); sort (G [A]. begin (), g [A]. end ();} scanf ("% d", & M); getchar (); While (M --) {ccheck = 0; gets (SS); ind = 0; for (I = 0; SS [I]; I ++) {If (ss [I] = '') {name [ind] = 0; check [ccheck ++] = word [name]; ind = 0;} else name [ind ++] = ss [I];} name [ind] = 0; check [ccheck ++] = word [name]; k1.clear (); vector <int >:: iterator it; For (IT = G [Check [0]. begin (); It <G [Check [0]. end (); It ++) k1.push _ back (* It); for (I = 1; I <ccheck; I ++) {k2.clear (); set_intersection (k1.begin (), k1.end (), g [Check [I]. begin (), g [Check [I]. end (), back_inserter (K2); I ++; if (I> = ccheck) // last one {k1.clear (); For (IT = k2.begin (); it <k2.end (); It ++) k1.push _ back (* It); break;} k1.clear (); set_intersection (k2.begin (), k2.end (), G [Check [I]. begin (), g [Check [I]. end (), Back_inserter (K1);} // the final result is K1 if (! K1.size () {puts ("no"); continue;} set <string> ans; For (IT = k1.begin (); It <k1.end (); It ++) ans. insert (ATIP [* It]); set <string >:: iterator st; ST = ans. begin (); cout <* st; For (ST ++; st! = Ans. End (); ST ++) cout <"" <* st; puts ("") ;}} return 0 ;}
View code