Topic:
It is late at night and you found a logical expression on the blackboard, which you believe are the secret to figure out if Your gang is going to be attacked tomorrow.
Your immediate reaction is-copy the expression and take it to the big bosses. Luckily, you remembered this mafia bosses don ' t enjoy surprises, and they want your expression to is simple and clean.
Given the logical expression in the board, have variables represented by 中文版 letters A-Z, and the operators OR and A ND, simplify it using Morgan's law, which is:
To help you, we'll use the following format:
- [ A + b ] = ( A * b ) or
- [ A + b ] = ( A * b ) or
- [ a + b ] = ( a * b ) or
- [ a * b ] = ( a + b ) or
- [ a * b ] = ( a + B ) or
- [ a + B ] = ( a + B ) /span>
This is also valid for three or more variables. The expression'll has variables, represented by letters A-Z: if it's a lower case letter, it's a negated variable. It may also has parenthesis that group expressions, so you must solve the expressions between the parenthesis first. There may also is square brackets, in which case you must apply the rules above.
Don ' t simplify the expression beyond applying the rules described in the problem.
Input
The input is a logical expression has at most characters. Each character can is a variable (A-Z), a negated variable (A-Z), the operator or (+) or the operator and (*). Also, it can have parenthesis. A negation is represented by a lowercase character or a logic block between square brackets ([]).
Output
Print the simplified logic expression.
Sample Input
Input
[J+b+v*a]
Output
(J*b*v+a)
Input
I+i+g+m* ((f))
Output
I+i+g+m* ((f))
Input
v+a* (M*[t*b*n*u*g*g+x+o])
Output
v+a* (m* (T+b+n+u+g+g*x*o))
Input
G*[q+w+[l* (j)]
Output
g* (q*w* (l* (j)))
Test instructions
Probably is to give you a string, when you find [] make, the inside of the uppercase letter into lowercase, lowercase letters to uppercase, * change the plus, + sign multiplication sign, otherwise output
Analysis:
Get a stack and press test instructions to make it.
1#include <cstdio>2#include <iostream>3#include <algorithm>4#include <stack>5#include <cstring>6#include <cctype>7 using namespacestd;8 Chars[201];9stack<Char>K;Ten intMain () One { Ascanf"%s", s); - intn=strlen (s); - intnum=0; the for(intI=0; i<n;i++) - { - if(!k.empty ()) - { + if(s[i]=='[') - { +num++; As[i]='('; at } - Else if(s[i]==']') - { -num--; -s[i]=')'; - if(num==0) in { - K.pop (); to } + } - Else if(num%2!=0) the { * if(s[i]>='A'&&s[i]<='Z') $s[i]=ToLower (S[i]);Panax Notoginseng Else if(s[i]>='a'&&s[i]<='Z') -s[i]=ToUpper (S[i]); the Else if(s[i]=='*') +s[i]='+'; A Else if(s[i]=='+') thes[i]='*'; + } - } $ Else $ { - if(s[i]=='[') - { thenum++; -s[i]='(';Wuyi K.push (S[i]); the } - } Wu } -printf"%s\n", s); About return 0; $}
View Code
Gym 100733G No negations