NYOJ-542 test products [dictionary tree]

Source: Internet
Author: User

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 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.