[ACM] poj 3295 tautology (constructor)

Source: Internet
Author: User

Tautology
Time limit:1000 ms   Memory limit:65536 K
Total submissions:9302   Accepted:3549

Description

WFF 'n' proof is a logic game played with dice. each die has six faces representing some subset of the possible symbols K, A, N, C, E, P, Q, R, S, T. A well-formed formula (WFF) is any string of these symbols obeying the following rules:

  • P, Q, R, S, and t are wffs
  • IfWIs a WFF, nWIs a WFF
  • IfWAndXAre wffs, KWX,WX, CWX, And EWXAre wffs.
The meaning of a WFF is defined as follows:
  • P, Q, R, S, and t are logical variables that may take on the value 0 (false) or 1 (true ).
  • K, A, N, C, E meanAnd, or, not, implies,AndEqualsAs defined in the truth table below.
Definitions of K, A, N, C, and E
W x KWX AWX NW CWX EWX
1 1 1 1 0 1 1
1 0 0 1 0 0 0
0 1 0 1 1 1 0
0 0 0 0 1 1 1

ATautologyIs a WFF that has value 1 (true) regardless of the values of its variables. For example,ApnpIs a tautology because it is true regardless of the valueP. On the other hand,ApnqIs not, because it has the value 0P = 0, q = 1.

You must determine whether or not a WFF is a tautology.

Input

Input consists of several test cases. Each test case is a single line containing a WFF with no more than 100 symbols. A line containing 0 follows the last case.

Output

For each test case, output a line containingTautologyOrNotAs appropriate.

Sample Input

ApNpApNq0

Sample output

tautologynot

Source

Waterloo local contest, 2006.9.30


Solution:

Calculate the value of the logical expression based on different input strings. The stack is used here, which is similar to the expression evaluate idea. The string entered from the back to the front is written into the stack if it is a number (P, Q, R, S, T in the question, if it is an operator, the number is obtained from the stack, and then the operation is performed on the stack. The numbers P, Q, R, S, and T in the question are only 0, 1 values. Therefore, the result is a 5-round loop and the obtained expression is evaluated. Exit when 0 is encountered.

Code:

# Include <iostream> # include <string. h ># include <stack> using namespace STD; string WFF; int P, Q, R, S, T; bool OK; int compute (string Str) // stack operation {stack <int> st; int Len = Str. length (); For (INT I = len-1; I> = 0; I --) {If (STR [I] = 'P') ST. push (p); else if (STR [I] = 'q') ST. push (Q); else if (STR [I] = 'R') ST. push (r); else if (STR [I] = 's') ST. push (s); else if (STR [I] = 'T') ST. push (t); else if (STR [I] = 'k') {int x = ST. top (); St. pop (); int y = ST. top (); ST. pop (); ST. push (X & Y);} else if (STR [I] = 'A') {int x = ST. top (); ST. pop (); int y = ST. top (); ST. pop (); ST. push (X | Y);} else if (STR [I] = 'n') {int x = ST. top (); ST. pop (); ST. push (! X);} else if (STR [I] = 'C') {int x = ST. top (); ST. pop (); int y = ST. top (); ST. pop (); ST. push (! X | Y);} else if (STR [I] = 'E') {int x = ST. top (); ST. pop (); int y = ST. top (); ST. pop (); ST. push (x = y) ;}} return St. top () ;}int main () {While (CIN >>> WFF & WFF! = "0") {OK = 1; for (P = 0; P <2; p ++) // The enumerated result. If it is 0, the loop for (q = 0; q <2; q ++) for (r = 0; r <2; r ++) for (S = 0; S <2; s ++) for (t = 0; t <2; t ++) {If (compute (WFF) = 0) {OK = 0; goto label ;}} label: if (OK) cout <"tautology" <Endl; else cout <"not" <Endl;} 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.