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. 【DB2基础】DB2编目和数据库连接

    1.编目节点和编目数据库 编目(Catalog),是在本地或远程建立客户端到服务器的数据库连接的过程.其目的在于获取编目信息,即生成用来访问数据库的目录. 系统数据库目录包含一个列表和指针,通过目录可 ...

  2. 【大数据技术】Sqoop

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

  3. Automatically migrating data to new machines kafka集群扩充迁移topic

    The partition reassignment tool can be used to move some topics off of the current set of brokers to ...

  4. Nginx使用教程(六):使用Nginx缓存之FastCGI缓存

    启用FastCGI缓存 <br\>编辑必须启用缓存的虚拟主机配置文件. nano /etc/nginx/sites-enabled/vhost 将以下行添加到server{}指令之外的文件 ...

  5. 【JXOI2018】守卫

    [JXOI2018]守卫 参考题解:https://blog.csdn.net/dofypxy/article/details/80196942 大致思路就是:区间DP.对于\([l,r]\)的答案, ...

  6. MacOS 10.12 Sierra 安全性与隐私没有任何来源选项解决方法

    MacOS 10.12 Sierra 安全性与隐私没有任何来源选项解决方法 来源: 时间:2016年09月21日 在升级了macOS Sierra (10.12)版本后在“安全性与隐私”中不再有“任何 ...

  7. 20175329 2018-2019-3《Java程序设计》第五周学习总结

    学号 20175329 2018-2019-3<Java程序设计>第五周学习总结 教材学习内容总结 第六章 接口与实现 使用关键字interface来定义一个接口.接口定义和类的定义十分相 ...

  8. BZOJ1064 NOI2008 假面舞会 图论

    传送门 将一组关系\((A,B)\)之间连一条边,那么显然如果图中存在环长为\(len\)的环,那么面具的种数一定是\(len\)的因数. 值得注意的是这里环的关系除了\(A \rightarrow ...

  9. TMS-规划图

    规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 ...

  10. npm install 安装报错:npm ERR! EPERM npm ERR! -4048 npm ERR! Error: EPERM: operation not permitted, unlink 'D:\test\demo\code\materialT\node_modules\.staging'

    更新项目依赖包,删除掉package-lock.json.node_modules,运行npm install,报如上错误信息,查询资料说是没有权限,本人用管理员身份打开powershell,运行np ...