Ultraviolet A 565-Pizza Anyone? (Brute force enumeration + binary)

Source: Internet
Author: User

Question: Ultraviolet A 565-Pizza Anyone? (Brute force enumeration + binary)


The question is that a person wants to help his friends determine a pizza, and then each friend has his own taste requirements, asking if he can decide a pizza and then satisfy each friend's at least one requirement.

Can output the added items in the batch SA, and the output requirements are in the Lexicographic Order;

No pizza can satisfy these requests.

Solution: there are 16 kinds of materials in this question. Each type of material is only possible for obtaining and not obtaining, so there are 2 ^ 16 types (0-2 ^ 16-1 ), I will give examples of each situation and then let them know if they can meet at least one requirement of each friend. You don't have to try again later. However, the data volume for this question is not very small, 65535*100 (t) * 12 = 78642000. However, this situation is very rare, but the time is also very urgent, so we can use binary to optimize the time.


Code:

# Include
 
  
# Include
  
   
Const int N = 100; const int M = 16; char str [M * 2]; int t; int result [M]; bool flag; struct TOPPING {char need [M]; // required char omit [M]; // int cnt1, cnt2; // number of elements in the first two arrays} topping [N]; void init () {t = flag = 0; memset (topping, 0, sizeof (topping ));} // break down each string into void handle () {for (int I = 0; I <strlen (str); I ++) {if (str [I] = '+') {topping [t]. need [topping [t]. cnt1 ++] = str [I + 1]; I ++;} else if (Str [I] = '-') {topping [t]. omit [topping [t]. cnt2 ++] = str [I + 1]; I ++ ;}} t ++ ;} // determine whether the result can meet at least one request bool judge () {bool tag; for (int j = 0; j <t; j ++) of each friend) {tag = 0; for (int k = 0; k <topping [j]. cnt2; k ++) if (result [topping [j]. omit [k]-'a'] = 0) {tag = 1; break;} if (tag) continue; for (int k = 0; k <topping [j]. cnt1; k ++) if (result [topping [j]. need [k]-'a'] = 1) {tag = 1; break;} if (! Tag) return false;} return true;} // indicates the binary number corresponding to each 10-digit value as whether the void solve (int n) {int pos = 0; while (pos <M) {result [pos ++] = n & 1; n >>= 1;} if (judge () flag = 1;} int main () {while (scanf ("% s", str )! = EOF) {init (); while (str [0]! = '. ') {Handle (); scanf ("% s", str);} int m = 1 <17-1; int n = 0; // printf ("% d \ n", m); while (n <m) {solve (n); if (flag) break; n ++;} if (! Flag) printf ("No pizza can satisfy these requests. \ n "); else {printf (" Toppings: "); for (int I = 0; I <M; I ++) if (result [I]) printf ("% c", I + 'A'); printf ("\ n") ;}} return 0 ;}
  
 



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.