zoukankan      html  css  js  c++  java
  • Gym 100733G No Negations

    题目:

      

    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
  • 相关阅读:
    js学习笔记
    Bootstrap学习笔记
    css学习任务二:切图写代码
    九宫格改进
    js学习笔记
    XHTML复习笔记
    html基础知识复习笔记
    css学习任务一:绘制九宫格
    如何不使用第三个变量来交换两个数的值
    算术右移与逻辑右移
  • 原文地址:https://www.cnblogs.com/forwin/p/4807015.html
Copyright © 2011-2022 走看看