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的更多相关文章

  1. 【解题报告】zju-1145 Dreisam Equations

    原题地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=145 题目大意:在给定的等式右边数字之间加上加.减.乘运算符,使等式成 ...

  2. 狗狗40题~(Volume B)

    H - Sorting Slides 应该是个二分匹配的模板题的,但我还不会写 = = 其实数据规模很小,就用贪心的方法就水过了(没加vis判冲突wa了几发,从此开始艰难的没有1A 的生活) #inc ...

  3. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  4. A.Kaw矩阵代数初步学习笔记 5. System of Equations

    “矩阵代数初步”(Introduction to MATRIX ALGEBRA)课程由Prof. A.K.Kaw(University of South Florida)设计并讲授. PDF格式学习笔 ...

  5. [家里蹲大学数学杂志]第269期韩青编《A Basic Course in Partial Differential Equations》 前五章习题解答

    1.Introduction 2.First-order Differential Equations Exercise2.1. Find solutons of the following inti ...

  6. Represent code in math equations

    Introduce The article shows a way to use math equations to represent code's logical. Key ideas logic ...

  7. EM basics- the Maxwell Equations

    All the two important problems that the EM theory trys to describe and explain are propogation and r ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. Zookeeper源码编译为Eclipse工程(win7下Ant编译)

    前言 ZooKeeper是雅虎的.用Ant进行软件构建. 千里之行,始于足下.想看源码的第一步,是下载源码并导入某个IDE工具. Ant http://ant.apache.org/ Windows: ...

  2. Spark Streaming和Kafka整合保证数据零丢失

    当我们正确地部署好Spark Streaming,我们就可以使用Spark Streaming提供的零数据丢失机制.为了体验这个关键的特性,你需要满足以下几个先决条件: 1.输入的数据来自可靠的数据源 ...

  3. [BZOJ 2759] 一个动态树好题

    [BZOJ 2759] 一个动态树好题 题目描述 首先这是个基环树. 然后根节点一定会连出去一条非树边.通过一个环就可以解除根的答案,然后其他节点的答案就可以由根解出来. 因为要修改\(p_i\),所 ...

  4. 渐进式迭代教学法--PHP

    渐进式迭代教学法--PHP 目前常见的课程体系大致情况如下: 阶段1:前端基础(html+css+js) 阶段2:PHP&MySQL基础 + 框架 (PHP基本语法,面向对象,mvc,sql基 ...

  5. Linux:Day4(下) 用户及组管理

    Linux用户:Username/UID 管理员:root,0 普通用户: 1-65535 系统用户:1-499 对守护进程获取资源进行权限分配: 登陆用户:500+ 交互式登录: Linux组:Gr ...

  6. Python:Day52 urls

    Django的2.0版本之后使用新的规则,之前的url变成了path,如果要使用正则还需要引入re_path from django.urls import path,re_path urlpatte ...

  7. ROS的launch文件

    ROS中可以把很多的命令以描述的形式写成launch文件,然后用roslaunch命令执行launch文件.它的使用方法如下: roslaunch [package] [filename.launch ...

  8. ajax如何增加请求头

    代码如下(主要关键就是headers,大家可以根据需要来增加请求头): $.ajax({ type: "POST", timeout: , // 超时时间 10 秒 headers ...

  9. OpenCV3计算机视觉Python语言实现笔记(五)

    图像的几何变换主要包括:平移.扩大与缩小.旋转.仿射.透视等等.图像变换是建立在矩阵运算基础上的,通过矩阵运算可以很快的找到对应关系. 1. 图像的平移 图像的平移,沿着x方向tx距离,y方向ty距离 ...

  10. Feature Extractor[inception v2 v3]

    0 - 背景 在经过了inception v1的基础上,google的人员还是觉得有维度约间的空间,在<Rethinking the Inception Architecture for Com ...