题意:给出一个数字组成的字符串,然后在字符串内添加三种运算符号 * + - ,要求输出所有添加运算符并运算后结果等于2000的式子。 所有数字不能有前导0,

且式子必须是合法的。

析:这个题很明显的暴力,因为最长才9位数字,也就是最多有8个位置位置可能插符号,当然实际并没有那么多,所以直接暴力就行,也不用优化,直接暴就行。

就是DFS,在每个位置考虑四种情况,*,+,-,或者不放,最后再一个一个的判断是不是等于2000就好,注意这个题有一个坑,我也不知道是哪个数据,

也没有想到,就是一个没有用运算符也没有的时候,是不成立的,比如2000,这个是不成立,我并没有找到其他的数据,但是如果我只判2000,是WA,

如果哪位大神知道,告诉一下,感激不尽。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <stack>
using namespace std ; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 10 + 5;
const int mod = 1e9 + 7;
const char *mark = "+-*";
const int dr[] = {0, 0, -1, 1};
const int dc[] = {-1, 1, 0, 0};
int n, m;
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
vector<string> ans; void dfs(int idx, string s){
if(idx == s.size()){
ans.push_back(s);
return ;
} for(int i = 0; i < 3; ++i){
string t = s;
t.insert(idx, 1, mark[i]);
dfs(idx+2, t);
}
dfs(idx+1, s);
} inline bool before0(const string &s){
int cnt = 0;
for(int i = 1; i < s.size()-1; ++i){
if(!isdigit(s[i])) ++cnt;
if(!isdigit(s[i-1]) && s[i] == '0' && isdigit(s[i+1])) return true;
} return !(cnt > 0);
} bool judge(const string &s){
int i = 0;
int ans = 0, tmp = 0;
char ch = '+';
while(i < s.size() && isdigit(s[i])) tmp = tmp * 10 + s[i++] - '0';
while(i < s.size()){
if(s[i] == '*'){
int t = 0; ++i;
while(i < s.size() && isdigit(s[i])) t = t * 10 + s[i++] - '0';
tmp *= t;
}
else{
ans += ch == '+' ? tmp : -tmp;
ch = s[i]; ++i; tmp = 0;
while(i < s.size() && isdigit(s[i])) tmp = tmp * 10 + s[i++] - '0';
}
}
ans += ch == '+' ? tmp : -tmp;
return ans == 2000;
} int main(){
string s;
int kase = 0;
while(cin >> s && s[0] != '='){
printf("Problem %d\n", ++kase);
if(s == "2000=" || s.size() < 4){ puts(" IMPOSSIBLE"); continue; }
s.pop_back();
ans.clear();
dfs(1, s);
bool ok = false;
for(int i = 0; i < ans.size(); ++i)
if(!before0(ans[i]) && judge(ans[i]))
cout << " " << ans[i] << "=\n", ok = true;
if(!ok) puts(" IMPOSSIBLE");
}
return 0;
}

UVa 817 According to Bartjens (暴力,DFS)的更多相关文章

  1. UVA - 817 According to Bartjens

    Description  According to Bartjens  The wide dissemination of calculators and computers has itsdisad ...

  2. UVA.129 Krypton Factor (搜索+暴力)

    UVA.129 Krypton Factor (搜索+暴力) 题意分析 搜索的策略是:优先找长串,若长串不合法,则回溯,继续找到合法串,直到找到所求合法串的编号,输出即可. 注意的地方就是合法串的判断 ...

  3. hihoCoder 1185 连通性·三(Tarjan缩点+暴力DFS)

    #1185 : 连通性·三 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 暑假到了!!小Hi和小Ho为了体验生活,来到了住在大草原的约翰家.今天一大早,约翰因为有事要出 ...

  4. Strange Country II 暴力dfs

    这题点的个数(<=50)有限, 所以可以纯暴力DFS去搜索 //#pragma comment(linker, "/STACK:16777216") //for c++ Co ...

  5. UVA.10986 Fractions Again (经典暴力)

    UVA.10986 Fractions Again (经典暴力) 题意分析 同样只枚举1个,根据条件算出另外一个. 代码总览 #include <iostream> #include &l ...

  6. UVA.839 Not so Mobile ( 二叉树 DFS)

    UVA.839 Not so Mobile ( 二叉树 DFS) 题意分析 给出一份天平,判断天平是否平衡. 一开始使用的是保存每个节点,节点存储着两边的质量和距离,但是一直是Runtime erro ...

  7. UVA129 暴力dfs,有许多值得学习的代码

    紫书195 题目大意:给一个困难的串,困难的串的定义就是里面没有重复的串. 思路:不需要重新对之前的串进行判重,只需要对当前的加入的字符进行改变即可. 因为是判断字典序第k个的字符串,所以要多一个全局 ...

  8. 2018杭电多校第五场1002(暴力DFS【数位】,剪枝)

    //never use translation#include<bits/stdc++.h>using namespace std;int k;char a[20];//储存每个数的数值i ...

  9. A. The Fault in Our Cubes 暴力dfs

    http://codeforces.com/gym/101257/problem/A 把它固定在(0,0, 0)到(2, 2, 2)上,每次都暴力dfs检查,不会超时的,因为规定在这个空间上,一不行, ...

随机推荐

  1. 函数xdes_get_offset

    /********************************************************************//** Returns page offset of the ...

  2. JS调用客户端EXE

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. 物联网操作系统HelloX V1.78测试版正式发布

    经过HelloX开发团队近四个月的努力,在HelloX V1.77版本基础上,增加许多功能特性,并对V1.77版本的一些特性进行了进一步优化之后,正式形成HelloX V1.78测试版本,经相对充分的 ...

  4. Java [Leetcode 137]Single Number II

    题目描述: Given an array of integers, every element appears three times except for one. Find that single ...

  5. FFmpeg解码H264及swscale缩放详解

    本文概要: 本文介绍著名开源音视频编解码库ffmpeg如何解码h264码流,比较详细阐述了其h264码流输入过程,解码原理,解码过程.同时,大部分应用环境下,以原始码流视频大小展示并不是最佳方式,因此 ...

  6. ffmpeg Windows下采集摄像头一帧数据,并保存为bmp图片

    这里请注意,在编译ffmpeg时,不要使用--disable-devices选项. 使用 --enable-encoder=rawvideo --enable-decoder=rawvideo 启用r ...

  7. jQuery避免$符和其他JS库冲突的方法对比

    1.如果jquery库在第三方库之后引用.这个时候jquery库会占用$. 解决办法:剔除$符号的使用权. <script type="text/javascript" sr ...

  8. 【转】iOS类似Android上toast效果

    原文网址:http://m.blog.csdn.net/article/details?id=50478737 做过Android开发的人都知道toast,它会在界面上显示一排黑色背景的文字,用于提示 ...

  9. 【转】c++内存泄露检测,长文慎入!

    原文网址:http://blog.csdn.net/zengraoli/article/details/8905334 关于内存泄露的,今天无意想到,网上找了一下   本篇blog附带的所有工具和代码 ...

  10. View.VISIBLE、INVISIBLE、GONE的区别

    android中UI应用的开发中经常会使用view.setVisibility()来设置控件的可见性,其中该函数有3个可选值,他们有着不同的含义: View.VISIBLE--->可见View. ...