ZJU_1145 OR POJ_1100 Dreisam Equations
Dreisam Equations
{ 两个网站的题有点不一样(ZJH有特判)POJ时间卡得紧,建议去POJ过 }
题目大意:
给你一个字符串:是一个等式,等式左边是一个数,右边由若干个数和()构成,要求加入(+、- 或 *)来使得等式成立,注意:这道题抛弃了原本的优先级,除了括号一律采用从左到右的计算顺序。
题目陷阱:
测试数据中可能有 2= (1)(1) 或者 2=((1)1) ,不一定只是在空格处加符号。
题目思路:
可以对原字符串进行加工,也就是先预先处理空格,不过这样子比较麻烦,可以考虑到,符号只能加在以下四种情况:
1. 数字 和 数字 之间
2. 数字 和 ( 之间
3. ) 和 数字 之间
4. ) 和 ( 之间
这样子分情况以后就容易了许多,可以遍历一遍字符串,将数字存到num数组,( 当作 -1,)当作 -2,有可能加符号的地方当作 -3;
然后深入优先搜索,遍历所有可能,因为最多等号右边12个数,也就是最多11个符号,也就是3的11次方,基本不会超时。
AC 代码:(两个网站的题 long long 的格式不一样,所以%I64d 可能需要改下)
#include<stdio.h>
typedef long long LL;
LL sum;
LL num[],no;
LL st[],so;
char tt[];
LL to; void push(LL x)
{
if(to==||to>&&tt[to-]=='(') st[so++]=x;
else
{
switch(tt[to-])
{
case '+':st[so-]=st[so-]+x;break;
case '-':st[so-]=st[so-]-x;break;
case '*':st[so-]=st[so-]*x;break;
default: break;
}
to--;
}
} // -4 +
// -5 -
// -6 * bool dfs(int pos)
{
if(pos>=no)
{
if(st[so-]==sum) return true;
else return false;
}
if(num[pos]==-)
{
LL Sst[],Sso=so;
for(int i=;i<so;i++)
Sst[i]=st[i];
char Stt[];
LL Sto=to;
for(int i=;i<to;i++)
Stt[i]=tt[i];
num[pos]=-;
tt[to++]='+';
if(dfs(pos+)) return true;
so=Sso;
for(int i=;i<so;i++)
st[i]=Sst[i];
to=Sto;
for(int i=;i<to;i++)
tt[i]=Stt[i];
num[pos]=-;
tt[to++]='-';
if(dfs(pos+)) return true;
so=Sso;
for(int i=;i<so;i++)
st[i]=Sst[i];
to=Sto;
for(int i=;i<to;i++)
tt[i]=Stt[i];
num[pos]=-;
tt[to++]='*';
if(dfs(pos+)) return true;
num[pos]=-;
}
else if(num[pos]==-)
{
tt[to++]='(';
if(dfs(pos+)) return true;
}
else if(num[pos]==-)
{
to--;
so--;
push(st[so]);
if(dfs(pos+)) return true;
}
else
{
push(num[pos]);
if(dfs(pos+)) return true;
}
return false;
}
int main()
{
char s[];
int cas=;
while(gets(s))
{
if(s[]==''&&s[]==) break;
int i=;
no=;
for(;s[i];i++)
{
if(s[i]<=''&&s[i]>='')
{
if(no>&&num[no-]!=-) num[no++]=-;
num[no]=;
for(;s[i]<=''&&s[i]>='';i++)
{
num[no]=num[no]*+s[i]-'';
}
no++;
i--;
}
else if(s[i]=='(')
{
if(no>&&num[no-]!=-) num[no++]=-;
num[no++]=-;
}
else if(s[i]==')')
{
num[no++]=-;
}
}
sum=num[];
so=to=;
printf("Equation #%d:\n",cas++);
if(!dfs())
{
printf("Impossible\n\n");
}
else
{
printf("%I64d=",sum);
for(int i=;i<no;i++)
{
if(num[i]>=) printf("%I64d",num[i]);
else
{
switch(num[i])
{
case -:printf("(");break;
case -:printf(")");break;
case -:printf("+");break;
case -:printf("-");break;
case -:printf("*");break;
default:break;
}
}
}
printf("\n\n");
}
}
return ;
}
ZJU_1145 OR POJ_1100 Dreisam Equations的更多相关文章
- 【解题报告】zju-1145 Dreisam Equations
原题地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=145 题目大意:在给定的等式右边数字之间加上加.减.乘运算符,使等式成 ...
- 狗狗40题~(Volume B)
H - Sorting Slides 应该是个二分匹配的模板题的,但我还不会写 = = 其实数据规模很小,就用贪心的方法就水过了(没加vis判冲突wa了几发,从此开始艰难的没有1A 的生活) #inc ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- A.Kaw矩阵代数初步学习笔记 5. System of Equations
“矩阵代数初步”(Introduction to MATRIX ALGEBRA)课程由Prof. A.K.Kaw(University of South Florida)设计并讲授. PDF格式学习笔 ...
- [家里蹲大学数学杂志]第269期韩青编《A Basic Course in Partial Differential Equations》 前五章习题解答
1.Introduction 2.First-order Differential Equations Exercise2.1. Find solutons of the following inti ...
- Represent code in math equations
Introduce The article shows a way to use math equations to represent code's logical. Key ideas logic ...
- EM basics- the Maxwell Equations
All the two important problems that the EM theory trys to describe and explain are propogation and r ...
- FITTING A MODEL VIA CLOSED-FORM EQUATIONS VS. GRADIENT DESCENT VS STOCHASTIC GRADIENT DESCENT VS MINI-BATCH LEARNING. WHAT IS THE DIFFERENCE?
FITTING A MODEL VIA CLOSED-FORM EQUATIONS VS. GRADIENT DESCENT VS STOCHASTIC GRADIENT DESCENT VS MIN ...
- ACM题目————Equations
Description Consider equations having the following form: a*x1^2+b*x2^2+c*x3^2+d*x4^2=0 a, b, c, d a ...
随机推荐
- 别老扯什么hadoop,你的数据根本不够大
本文原名“Don't use Hadoop when your data isn't that big ”,出自有着多年从业经验的数据科学家Chris Stucchio,纽约大学柯朗研究所博士后,搞过 ...
- Linux系统在信息社会的发展
Linux系统在信息社会的发展 随着信息技术的高速发展并迅速渗透到社会生活的各个方面,Linux日益成为人们学习.工作.生活不可缺少的基本工具,再过不了几年,不会使用Linux,就会象不识字一样使人举 ...
- maven项目引入依赖之后,jar包没有自动导入报错
解决这个问题可run maven-intall 一下 ,需要在联网的情况下去下载jar包. 我这样做了一下就可以了.
- Linux的目录结构--一切从根开始
Linux目录结构的特点 # 举例-linux下面使用光盘 ###.把光盘放入到光驱中 ###.linux中使用光盘 /dev/cdrom [root@oldboyedu- ~]# ll /dev/c ...
- 【vue】vue-router的用法
依赖安装:(c)npm install vue-router 过程: import Vue from 'vue'; import Router from 'vue-router'; Vue.use(R ...
- classmethod 和 staticmethod
我一般很少用到. Talk is cheap, show you the code. #!/usr/bin/env python # -*- coding: utf-8 -*- ########### ...
- Java的错误类型
程序的错误分为:编译期语法错误.运行期异常错误和运行期逻辑错误 (1)编译期语法错误可以借助Eclipse的帮助方便地定位错误,并进行修改 如: (2)运行期异常,即 没有语法错误,编译可以通过,但运 ...
- Jmeter自定义Java请求,继承AbstractJavaSamplerClient
首先,使用Eclipse新建一个项目,然后从Jmeter的lib/ext目录下中拷贝ApacheJMeter_java.jar和ApacheJMeter_core.jar两个文件,然后引入这两个JAR ...
- kafka模型理解
1.消息发送至一个topic,而这个topic可以由多个partition组成,每条消息在partition中的位置称为offset 2.消息存在有效期,如果设置为2天,则消息2天后会被删除 3.每个 ...
- Java Statement PK PrepareStatement
PreparedStatement是用来执行SQL查询语句的API之一,Java提供了 Statement.PreparedStatement 和 CallableStatement三种方式来执行查询 ...