Poj 3295 tautology (simulation)

Source: Internet
Author: User

Question link: http://poj.org/problem? Id = 3295

Given a logical expression, you can determine whether the formula is permanent.

This is a simple question for children with a little ACM Foundation.

But I spent two hours wa several times.

Cause 1:

The test data mistaken for the question is to ensure that the values will be met before the meeting, and there will be a logical symbol waiting for them after the two values (N). It turns out that I am wrong, my Wa is sad.

Later I thought of this, but I was not sure. I tested the condition on default and used a while (1) for testing. If OJ returns timeout, it indicates that my conjecture is correct, return timeout;

Cause 2:

This error is detected quickly, but it is not obvious, that is, the switch statement. If it is not executed after the case statement, the following s -- that is, oh, no!

Switch (name [Len]) {s --; // This statement will never run case 'K ': ch = my_map [Rec [s] & my_map [Rec [s-1]? '1': '0'; s --; break; Case 'A': CH = my_map [Rec [s] | my_map [Rec [s-1]? '1': '0'; s --; break; Case 'E': CH = my_map [Rec [s] = my_map [Rec [s-1]? '1': '0'; s --; break; Case 'C': CH = (! My_map [Rec [s]) | my_map [Rec [s-1]? '1': '0'; s --; break; Case 'N': CH = my_map [Rec [s]? '0': '1'; break; // default: printf ("s: % d \ n", S );}

Then the problem is solved. We can simulate the stack according to the normal situation!

Here I use map to correspond to the true value of pqr. This is more convenient!

# Include <iostream> # include <stdio. h> # include <map> # include <string. h> using namespace STD; char STR [200]; char temp [200]; char rec [200]; Map <char, bool> my_map; // when this map is to be inserted, note that bool is_ OK (char * Name, int Len) {char ch; int S = 0; Len -- is also inserted at the time of 0 and 1 --; while (LEN> = 0) {If (name [Len]> = 'A' & name [Len] <= 'Z ') {rec [s ++] = Name [Len]; Len --;} else {s --; Switch (name [Len]) {Case 'K ': ch = my_map [Rec [s] & my_map [Rec [s-1]? '1': '0'; s --; break; Case 'A': CH = my_map [Rec [s] | my_map [Rec [s-1]? '1': '0'; s --; break; Case 'E': CH = my_map [Rec [s] = my_map [Rec [s-1]? '1': '0'; s --; break; Case 'C': CH = (! My_map [Rec [s]) | my_map [Rec [s-1]? '1': '0'; s --; break; Case 'N': CH = my_map [Rec [s]? '0': '1'; break;} rec [s ++] = CH; Len -- ;}return my_map [Rec [0];} int main () {int P, Q, R, S, T, Len; bool flag; while (scanf ("% s", STR) {flag = true; len = strlen (STR); If (strcmp (STR, "0") = 0) return 0; For (P = 0; P <2 & flag; P ++) for (q = 0; q <2 & flag; q ++) for (r = 0; r <2 & flag; r ++) for (S = 0; S <2 & flag; s ++) for (t = 0; t <2 & flag; t ++) {my_map ['0'] = false; my_map ['1'] = true; strcpy (temp, STR); my_map ['P'] = P; my_map ['q '] = Q; my_map ['R'] = r; my_map ['s'] = s; my_map ['T'] = T; If (! Is_ OK (temp, Len) Flag = false; my_map.clear ();} If (FLAG) printf ("tautology \ n"); else printf ("not \ 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.