首页 > 其他分享 >Known Notation

Known Notation

时间:2022-11-22 19:03:08浏览次数:54  
标签:ch string Notation ++ num Known mul RPN


 


Known Notation

 

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:

There is a non-empty string consists of asterisks and non-zero digits. The length of the string will not exceed 1000.

<h4< dd="">

Output

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

<h4< dd="">

Sample Input

3
1*1
11*234**
*

<h4< dd="">

Sample Output

1
0
2

 

 

题意:给出一个字符串,求将它变为后序表达式最少要几步。方法很简单,贪心求解。符号的数量少于数字的数量,若数字是最后一个,则与前面的符号交换。

Code:

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int T,len;
char ch[10000];
int number[10000];
int main(){
scanf("%d",&T);
while (T--){
int k=0,ans=0,mul=0,num=0;
memset(number,0,sizeof(number));
scanf("%s",ch);
int len=strlen(ch);
for (int i=0;i<len;i++)
if (ch[i]=='*') mul++; else num++;
if (len==num){
printf("0\n");
continue;
}
if (num>mul) num=0,mul=0;
else num=mul+1-num,ans+=num,mul=0;
for (int i=0;i<len;i++)
if (ch[i]!='*') number[++k]=i;
for (int i=0;i<len;i++)
if (ch[i]!='*') num++;
else{
mul++;
if (mul>=num){
swap(ch[i],ch[number[k]]);
k--,ans++,mul--,num++;
}
}
if (ch[len-1]!='*') ans++;
printf("%d\n",ans);
}
return 0;
}

 

标签:ch,string,Notation,++,num,Known,mul,RPN
From: https://blog.51cto.com/u_15888102/5878358

相关文章

  • Mysql数据库连接失败SSLException: Unsupported record version Unknown-0.0
    问题描述:mysql版本:5.7.27jdk版本:1.8.0_201tomcat日志中报错,显示连接数据库失败,报错信息如下:Thelastpacketsuccessfullyreceivedfromtheserverwas152millisecon......
  • original: Error: Unknown storage engine 'InnoDB'
    问题Nodejs工程下,用sequelize向一个现有的MySQL数据库中初始化数据时报错,如题:original:Error:Unknownstorageengine'InnoDB'ENVMySQL5.6Sequelize:^6......
  • Java中的自定义注解Annotation
    与注释不同,注解可以被其他程序读取。内置注解:@SuppressWarnings参数:   元注解:用来注解其它注解的注解。1.@Target:使用的位置。包括:TYPE意味着,它能标注"类......
  • Vue中使用Mock,devSever中before方法弃用>webpack新版本出现的vue.config.js配置问题:op
    话不多说直接上代码:1、mock相关配置(mock/index.js),这里仅使用 setupMiddlewares其余旧版级过渡版本方法见官网1//引入mock2constMock=require('mockjs');......
  • Angular报错:Error: Unknown argument: spec
    解决方案使用--skip-tests代替效果展示可以看到spec.ts消失了参考链接https://stackoverflow.com/questions/62228834/angular-cli-command-issue-unknown-option-s......
  • CF1292E Rin and The Unknown Flower 题解
    IO交互题fflush(stdout)害人不浅!!1注意到如果我们发起询问C和O就可以花费2的代价知道整个串,不过代价过高,所以我们考虑减小点代价。考虑发起询问CO,CH,CC,这样我......
  • 【EF Core】常用的 DataAnnotations
    DataAnnotations验证常用的DataAnnotationsRequired:属性值必须非空或者不能只是空格,如果允许全空格可以[Required(AllowEmptyStrings=true)]DisplayName:显示名–......
  • JAVA注解(Annotation)
    注解(Annotation)什么是注解Annotation的作用:不是程序本身,可以对程序做出解释,这一点和注释(comment)没什么区别。可以被其他程序(比如编译器)读取Annotation的格式:注......
  • 解决Android手机连接Charles Unknown问题
    最近很多同事反馈使用Charles抓包出现了很多unknown的问题,现象如下图查看右侧的原因,给出的结果是这样的这里将讲解如何解决这个问题,但是开始阅读之前,请确认符合如下的条件本......
  • 解决: Annotation processors must be explicitly declared now
    今天看着项目,想着使用黄油刀省点事儿,配置好黄油刀之后,悠哉的点击了一下运行,突然报了一个异常,如下:Error:java.lang.RuntimeException:Annotationprocessorsmustbeexplic......