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. 【Linux基础】文件处理实例

    1.文件拆分 //每4000行拆分一个文件 epms_t_ep_fx_stl_xy_20190129.dat 2.行处理 //查找第二列为711611且第三列为711100记录,打印行号和整行数据 a ...

  2. 【大数据技术】Sqoop

     1.Sqoop是什么 Sqoop:SQL-to-Hadoop,传统数据库与Hadoop间数据同步工具.(MySQL.Oracle <==> HDFS.HBase.Hive) Sqoop ...

  3. 数据合并处理concat

    var data = [ {name: '海门', value: 9}, {name: '鄂尔多斯', value: 12}, {name: '招远', value: 12}, {name: '舟山' ...

  4. SQL INNER JOIN 关键字

    SQL INNER JOIN 关键字 在表中存在至少一个匹配时,INNER JOIN 关键字返回行. INNER JOIN 关键字语法 SELECT column_name(s) FROM table ...

  5. vue的过滤器语发及应用案例

    1.使用地方: 双花括号插值处或  组件属性处   例: {{ message | capitalize }} <div v-bind:id="rawId | formatId&quo ...

  6. [tool] Visual Studio Code python配置

    语言设置 安装中文插件即可成为中文 选择一个Python解释器 Python是一种解释型语言,为了运行Python代码并获取Python IntelliSense,您必须告诉VS Code使用哪个解释 ...

  7. 关于xampp中无法启动mysql,Attempting to start MySQL service...的解决办法!!

    最近在学习服务器方面的知识,找到了这款功能强大的建站集成软件包——xampp.但是在开数据库服务器的时候,出现了这种情况.一直在Attemptng to start MySQL  service... ...

  8. 转://Oracle 高可用技术与云基础架构

    众所周知Oracle云基础架构已经在越来越多的行业里应用.大家了解云基础架构是如何演进的嘛?可能有人会说Oracle高可用技术是组成云架构的基础,那它们的关系是怎么样的?大家又了解Oracle高可用技 ...

  9. 【vue】vue +element 搭建及开发中项目中,遇到的错误提示

    1. import Layout from '@/views/layout/Layout'; export default [ { // 配置路由,当路径为'/activePublic',使用组件ac ...

  10. 破解windows版idea

    近期使用idea的时候提示已过试用期,需要购买,否则只能使用半小时 网上破解方法很全,记录一下 1.下载破解补丁 百度网盘:https://pan.baidu.com/s/15Wq9-c4SVwg_2 ...