题意:给出一个数字组成的字符串,然后在字符串内添加三种运算符号 * + - ,要求输出所有添加运算符并运算后结果等于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. linux delete files older than 3 days

    4 down vote accepted This is easy enough (although note that this goes by a modification time more t ...

  2. centos的版本和内核查看

    查看linu的内核信息 查看distrubution,centos属于哪个release

  3. iOS开发:mac使用svn管理项目

    记录mac下常用的svn命令: 1.检出项目: svn checkout .../svn/projectName --username=xxx --password=xxx //将ip换成svn服务器 ...

  4. UVa 10294 (Pólya计数) Arif in Dhaka (First Love Part 2)

    Burnside定理:若一个着色方案s经过置换f后不变,称s为f的不动点,将置换f的不动点的数目记作C(f).等价类的数目等于所有C(f)的平均值. 一个项链,一个手镯,区别在于一个能翻转一个不能,用 ...

  5. UVa 1635 (唯一分解定理) Irrelevant Elements

    经过紫书的分析,已经将问题转化为求组合数C(n-1, 0)~C(n-1, n-1)中能够被m整除的个数,并输出编号(这n个数的编号从1开始) 首先将m分解质因数,然后记录下每个质因子对应的指数. 由组 ...

  6. TIOBE 2015年5月编程语言排行榜 Visual Studio系列在上升

    TIOBE 编程语言社区排行榜是编程语言流行趋势的一个指标,每月更新,这份排行榜排名基于互联网上有经验的程序员. 课程和第三方厂商的数量.排名使用著名的搜索引擎(诸如 Google.MSN.Yahoo ...

  7. LeetCode Contains Duplicate II (判断重复元素)

    题意:如果有两个相同的元素,它们之间的距离不超过k,那么返回true,否则false. 思路:用map记录每个出现过的最近的位置,扫一边序列即可.扫到一个元素就判断它在前面什么地方出现过.本题数据有点 ...

  8. Maximum Product Subarray JAVA实现

    题目描述: Find the contiguous subarray within an array (containing at least one number) which has the la ...

  9. linux-LINUX试题

    ylbtech-doc:linux-LINUX试题 LINUX试题 1.A,LINUX试题返回顶部 01.{Linux题目}在使用匿名登录ftp时,用户名为(  )? (选择1项) A) login ...

  10. Delphi 操作word 表格

    var wordApp, WordDoc, WrdSelection, wrdtable: variant; strAdd: string; wdPar,wdRange:OleVariant; iCo ...