Question link: http://acm.nyist.net/JudgeOnline/problem.php? PID = 1, 542
Solution:
It was stuck here during the provincial competition and could not be made.
I thought about it again, and then I quickly typed out the code .... String processing is still not feasible. Please try again later ......
My idea is to use the dictionary to store chemicals and check whether there are all kinds of reagents. If there is a complete set of reagents, check whether there is a reaction. If not, add a result array. Then sort it.
The Code is as follows:
# Include <iostream> # include <cstring> # include <cstdio> # include <algorithm> # include <cmath> # include <string> using namespace STD; char word [410] [110]; bool visit [410]; string ans [10010]; // product int top; struct trie {int flag, size; // trie * Next [65]; trie (): flag (0), size (-1) {memset (next, null, sizeof (next) ;}} * head; void insert (string S, int type) {int temp, Len; trie * cur = head; Len = S. length (); For (int I = 0; I <Len; ++ I) {If (s [I]> = 'A' & S [I] <= 'Z ') temp = s [I]-'A'; else if (s [I]> = 'A' & S [I] <= 'Z') temp = (INT) (s [I]-'A') + 26; elsetemp = (INT) (s [I]-'0') + 52; if (cur-> next [temp] = NULL) cur-> next [temp] = new trie; cur = cur-> next [temp];} cur-> flag = 1; if (type = 0) cur-> size = 0; // reaction agent else {If (cur-> size =-1) {cur-> size = 1; ans [top ++] = s ;}} int find (char * s) {int I, Len = strlen (s ); for (I = 0; I <Len; ++ I) if (s [I] = ') return I;} bool query (string s) {int Len, temp; Len = S. length (); trie * cur = head; For (INT I = 0; I <Len; ++ I) {If (s [I]> = 'A' & S [I] <= 'Z') temp = s [I]-'A '; else if (s [I]> = 'A' & S [I] <= 'Z') temp = (INT) (s [I]-'A ') + 26; elsetemp = (INT) (s [I]-'0') + 52; If (cur-> next [temp] = NULL) return false; cur = cur-> next [temp];} If (cur-> flag = 1) return true; elsereturn false;} int m Ain () {int num, have; string now; int Pos; // equal sign position string temp; while (scanf ("% d", & num )! = EOF) {head = new trie; memset (word, '\ 0', sizeof (Word); memset (visit, false, sizeof (visit); Top = 0; for (INT I = 0; I <num; ++ I) scanf ("% s", word [I]); scanf ("% d", & have ); for (INT I = 0; I