zoukankan      html  css  js  c++  java
  • ZOJ 3829 Known Notation 贪心

    Known Notation

    Time Limit: 1 Sec  

    Memory Limit: 256 MB

    题目连接

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829

    Description

    Do you know reverse Polish notation (RPN)? It is a known notation in the area of mathematics and computer science. It is also known as postfix notation since every operator in an expression follows all of its operands. Bob is a student in Marjar University. He is learning RPN recent days.

    To clarify the syntax of RPN for those who haven't learnt it before, we will offer some examples here. For instance, to add 3 and 4, one would write "3 4 +" rather than "3 + 4". If there are multiple operations, the operator is given immediately after its second operand. The arithmetic expression written "3 - 4 + 5" in conventional notation would be written "3 4 - 5 +" in RPN: 4 is first subtracted from 3, and then 5 added to it. Another infix expression "5 + ((1 + 2) × 4) - 3" can be written down like this in RPN: "5 1 2 + 4 × + 3 -". An advantage of RPN is that it obviates the need for parentheses that are required by infix.

    In this problem, we will use the asterisk "*" as the only operator and digits from "1" to "9" (without "0") as components of operands.

    You are given an expression in reverse Polish notation. Unfortunately, all space characters are missing. That means the expression are concatenated into several long numeric sequence which are separated by asterisks. So you cannot distinguish the numbers from the given string.

    You task is to check whether the given string can represent a valid RPN expression. If the given string cannot represent any valid RPN, please find out the minimal number of operations to make it valid. There are two types of operation to adjust the given string:

    1. Insert. You can insert a non-zero digit or an asterisk anywhere. For example, if you insert a "1" at the beginning of "2*3*4", the string becomes "12*3*4".
    2. Swap. You can swap any two characters in the string. For example, if you swap the last two characters of "12*3*4", the string becomes "12*34*".

    The strings "2*3*4" and "12*3*4" cannot represent any valid RPN, but the string "12*34*" can represent a valid RPN which is "1 2 * 34 *".

    Input

    There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

    The first line contains an integer N (1 <= N <= 100) and a string S. The string S is one of "bit", "nat" or "dit", indicating the unit of entropy.

    In the next line, there are N non-negative integers P1P2, .., PNPi means the probability of the i-th value in percentage and the sum of Piwill be 100.

    Output

    For each test case, output the minimal number of operations to make the given string able to represent a valid RPN.

    Sample Input

    3
    1*1
    11*234**
    *
     

    Sample Output

    1
    0
    2

    HINT

    题意

    就给你一个后缀表达式,然后问你这个表达式最多需要修改几次

    1.插入一个数字或者符号

    2.交换俩符号的位置  

    题解:

    先插入,然后再跑交换就好了……

    每次交换就别真的交换,打个标记就好了,因为你已经插完了

    代码:

    //qscqesze
    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <set>
    #include <bitset>
    #include <vector>
    #include <sstream>
    #include <queue>
    #include <typeinfo>
    #include <fstream>
    #include <map>
    #include <stack>
    typedef long long ll;
    using namespace std;
    //freopen("D.in","r",stdin);
    //freopen("D.out","w",stdout);
    #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
    #define maxn 100006
    #define mod 1000000007
    #define eps 1e-9
    #define e exp(1.0)
    #define PI acos(-1)
    const double EP  = 1E-10 ;
    int Num;
    //const int inf=0x7fffffff;
    const ll inf=999999999;
    inline ll read()
    {
        ll x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    //*************************************************************************************
    
    string s;
    int main()
    {
        int t;scanf("%d",&t);
        while(t--)
        {
            cin>>s;
            int ans=0,flag1=0,flag2=0;
            for(int i=0;i<s.size();i++)
            {
                if(s[i]=='*')
                    flag2++;
                else
                    flag1++;
            }
            if(flag2+1>flag1)
                ans = (flag2+1)-flag1,flag1=(flag2+1)-flag1;
            else
                flag1=0;
            flag2=0;
            for(int i=0;i<s.size();i++)
            {
                if(s[i]=='*')
                {
                    flag2++;
                    if(flag1<flag2+1)
                    {
                        flag1++;
                        flag2--;
                        ans++;
                    }
                }
                else
                    flag1++;
            }
            cout<<ans<<endl;
        }
    }
  • 相关阅读:
    es-07-head插件-ik分词器插件
    es-01-简介
    es-02-elasticsearch安装及遇到的问题
    es-03-DSL的简单使用
    es-04-mapping和setting的建立
    lucene-01-简介
    Scipy---1.常数和特殊函数
    Pandas---12.数据加载和保存
    Pandas---11.移动窗口函数
    Pandas---10.DataFrame绘图
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4836632.html
Copyright © 2011-2022 走看看