首页 > 其他 > 详细

Gym 100733G No Negations

时间:2015-09-14 15:26:11      阅读:223      评论:0      收藏:0      [点我收藏+]

题目:

  

It is late at night and you found a logical expression on the blackboard, which you believe is the secret to figure out if your gang is going to be attacked tomorrow.

Your immediate reaction is to copy the expression and take it to the big bosses. Luckily, you remembered that mafia bosses don‘t enjoy surprises, and they want your expression to be simple and clean.

Given the logical expression in the board, having variables represented by english letters A-Z, and the operators OR and AND, simplify it using Morgan‘s law, that is:

  • 技术分享
  • 技术分享

To help you, we will 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)

This is also valid for three or more variables. The expression will have variables, represented by letters A-Z: if it is a lower case letter, it is a negated variable. It may also have parenthesis that group expressions, so you must solve the expressions between the parenthesis first. There may also be 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 having at most 200 characters. Each character can be 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)))
题意:
大概就是给你个字符串,当你发现[]使,将里面的大写字母变成小写,小写字母变为大写,*号变加号,+号变乘号,否则原样输出
分析:
弄个栈按题意来就可以。
技术分享
 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<stack>
 5 #include<cstring>
 6 #include<cctype>
 7 using namespace std;
 8 char s[201];
 9 stack<char> k;
10 int main()
11 {
12     scanf("%s",s);
13     int n=strlen(s);
14     int num=0;
15     for(int i=0;i<n;i++)
16     {
17         if(!k.empty())
18         {
19             if(s[i]==[)
20             {
21                num++;
22                s[i]=(;
23             }
24             else if(s[i]==])
25             {
26                 num--;
27                 s[i]=);
28                 if(num==0)
29                 {
30                     k.pop();
31                 }
32             }
33             else if(num%2!=0)
34             {
35                 if(s[i]>=A&&s[i]<=Z)
36                     s[i]=tolower(s[i]);
37                 else if(s[i]>=a&&s[i]<=z)
38                     s[i]=toupper(s[i]);
39                 else if(s[i]==*)
40                     s[i]=+;
41                 else if(s[i]==+)
42                     s[i]=*;
43             }
44         }
45         else
46         {
47             if(s[i]==[)
48             {
49                 num++;
50                 s[i]=(;
51                 k.push(s[i]);
52             }
53         }
54     }
55     printf("%s\n",s);
56     return 0;
57 }
View Code

 

Gym 100733G No Negations

原文:http://www.cnblogs.com/forwin/p/4807015.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!