AC代码

C++版本

/*
根据 定义的等式 的规则
判断给定串可以写成多少种二进制等式且成立的形式解法:
通过语法分析的形式将多个表达式定义进行 递归下降的解析。
此方法可以不用过多考虑多种表达式定义组合的合法和非法情况,只需要完整解析单个表达式,并递归调用。
*/#include<bits/stdc++.h>
using namespace std;
#define Result pair<char*,int>
#define FAIL make_pair((char*)NULL,0)
char srcExpr[32], expr[32], op[8] = {'0', '1', '+', '-', '*', '(', ')', '='};
struct Parser {Result Q(char* p) {Result res = E(p);if(res.first == NULL || *(res.first) != '=')    return FAIL;Result rgt = E(res.first+1);if(rgt.first == NULL || *(rgt.first) != 0 || rgt.second != res.second)  return FAIL;return rgt;}Result E(char *p) {Result ret = T(p);if(ret.first == NULL)   return FAIL;while(*(ret.first) == '+' || *(ret.first) == '-'){Result tmp = T(ret.first + 1);if(tmp.first == NULL)   return FAIL;if(*(ret.first) == '-') ret.second -= tmp.second;else    ret.second += tmp.second;ret.first = tmp.first;}return ret;}Result T(char *p) {Result ret = F(p);if(ret.first == NULL)   return FAIL;if(*(ret.first) == '*'){Result tmp = T(ret.first + 1);if(tmp.first == NULL)   return FAIL;ret.first = tmp.first,  ret.second *= tmp.second;}return ret;}Result F(char *p) {Result ret;if(*p == '-') {Result ret = F(p+1);ret.second = -ret.second;return ret;} else if(*p == '(') {Result ret = E(p+1);if(ret.first == NULL || *(ret.first) != ')')    return FAIL;ret.first++;return ret;} else {return N(p);}}Result N(char *p) {Result ret;if(!isdigit(*p))    return FAIL;if(*p == '0' && isdigit(*(p+1)))    return FAIL;while(isdigit(*p)){(ret.second *= 2) += (*p-'0');p++;}ret.first = p;return ret;}
};
int main()
{sort(op, op+8);scanf("%s", srcExpr);map<char, int> mp;int idx = 0;for(int i=0;srcExpr[i];i++){if(isalpha(srcExpr[i]) && mp.find( srcExpr[i] ) == mp.end())mp[ srcExpr[i] ] = ++idx;}if(idx > 8) {   printf("0\n");  return 0;   }int ans = 0;do {for(int i=0;srcExpr[i];i++)expr[i] = (isalpha(srcExpr[i]) ? op[ mp[srcExpr[i]]-1 ] : srcExpr[i]);if(Parser().Q(expr).first != NULL)  ans++;  } while(next_permutation(op, op+8));int factorial = 1;for(int i=1;i<=(8-idx);i++)factorial *= i;printf("%d\n", ans / factorial);
}

Python版本

# 解法:
# 总的有效符号为 8 种 + - * ( ) 0 1 =
# 全排列枚举将有效符合去替换字母,通过 Python 的 eval 函数去判断等式左边 = 右边? (...我选择用 Python 过此题的唯一原因)
# 同时题面中还有部分限制规则需要判断。
# Python eval 计算二进制数需形如 eval('0b101+0b10') 。lst = ['0', '1', '+', '-', '*', '(', ')', '=']
flg = [0 for i in range(8)]
ans = [0 for i in range(8)]
pos = [0 for i in range(256)]
chr = [0 for i in range(200)]
ok = list()
s = str()
cnt = 0
tot = [0]def jug(t):for i in range(len(t)):if t[i] in '+*':if i == 0 or t[i-1] in '(+*-':return Trueelif t[i] == '0':if (i > 0 and t[i-1] not in '01') and (i+1 < len(t) and t[i+1] in '01'):return Trueelif t[i] == '(':if i+1 < len(t) and t[i+1] == ')':return Truereturn Falsedef jugQ():tmp = s[:]t = s[:]for i in range(1, cnt+1):t = t.replace(chr[i], ans[i-1])while True:flag = Falsefor i in range(1, len(t)):if (t[i-1] in '(+-*=') and (t[i] in '01'):t = t[:i] + 'b' + t[i:]flag = Truebreakif flag == False:breakif t[0] in '01':t = 'b' + t[:]t = t.replace('b', '0b')try:t1, t2 = t.split('=')if jug(t1) or jug(t2):returna1 = eval(t1)a2 = eval(t2)if a1 == a2 and t not in ok:ok.append(t)# print(tmp)# print(t)tot[0] += 1except:returndef dfs(idx):if idx == 8:# print(ans)jugQ()for i in range(8):if flg[i]:continueelse:ans[idx] = lst[i]flg[i] = 1dfs(idx+1)flg[i] = 0if __name__ == "__main__":s = input()for c in s:if (c >= 'a' and c <= 'z') or (c >= 'A' and c <= 'Z'):if pos[ord(c)] > 0:continueelse:cnt+=1pos[ord(c)] = cntchr[cnt] = cif cnt > 8:print(0)else:dfs(0)print(tot[0])

Codeforces Gym 101158 E. Infallibly Crack Perplexing Cryptarithm (模拟 + 语法分析)相关推荐

  1. E:Infallibly Crack Perplexing Cryptarithm

    题目来源: CSUOJ 2295: Infallibly Crack Perplexing Cryptarithm Codeforces Gym 101158E Infallibly Crack Pe ...

  2. Codeforces Gym 101158E Infallibly Crack Perplexing Cryptarithm Gym [语法分析]

    题意:判断给定串可以写成多少种二进制等式且成立的形式. 题解:利用暴力+语法分析解题. AC代码: #include<bits/stdc++.h> using namespace std; ...

  3. 2019湖南多校第4场(codeforces gym 101158)部分题解

    目录 A题:题目链接 B题:题目链接 C题:题目链接 D题:题目链接 A题:题目链接 题意:给定一个整数 n,代表有一个序列 1,2,3,4,...,n ,然后给定一个整数m, 接下来有 m 个数,代 ...

  4. Codeforces Gym 101173 CERC 16 D BZOJ 4790 Dancing Disks

    Codeforces Gym 101173 CERC 16 D & BZOJ 4790 Dancing Disks 强烈安利这道构造题目,非常有意思. 这里用到的思想是归并排序! 多路归并排序 ...

  5. Codeforces Gym 101086 M ACPC Headquarters : AASTMT (Stairway to Heaven)

    Codeforces Gym 101086 M ACPC Headquarters : AASTMT (Stairway to Heaven) 题目来源: Codeforces 题意: 给出一些比赛, ...

  6. [Codeforces Gym 101651/100725B] Banal Tickets

    Codeforces Gym 100725 题解: 先分两种情况, 积为000与积非0" role="presentation" style="position ...

  7. Codeforces Gym 100513G G. FacePalm Accounting 暴力

    G. FacePalm Accounting Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513 ...

  8. Codeforces Gym 100269 Dwarf Tower (最短路)

    题目连接: http://codeforces.com/gym/100269/attachments Description Little Vasya is playing a new game na ...

  9. Codeforces Gym 100676G Training Camp 状压dp

    http://codeforces.com/gym/100676 题目大意是告诉你要修n门课,每门课有一个权值w[i], 在第k天修该课程讲获得k*w[i]的学习点数,给出了课程与先修课程的关系,要修 ...

最新文章

  1. SAP PM概念学习
  2. Spring Cloud Gateway中异常处理
  3. linux下获取线程号
  4. 那些年,我深爱着的PPT(一)
  5. pythonturtle画点的指令_简述python的turtle绘画命令及解释
  6. [iOS] ios的runtime
  7. swagger怎么看接口数量_vue编写的功能强大的swagger-ui
  8. lock.ReadWriteLock使用方法
  9. STC学习:八位数码管+流水灯
  10. 计算器与计算机小键盘的使用,会计神器!用上Cherry轴的计算器还能当小键盘用...
  11. python安装pyqt4_PyQt4 64位下载 PyQt4 for python2.7 v4.8.5 64位 windows 官方简体中文安装版 下载-脚本之家...
  12. 计算机桌面图标有阴影,电脑桌面图标标签出现阴影是怎么回事?桌面图标有背景解决方法...
  13. java 代码佛像_论面向组合子程序设计方法 之九 南无阿弥陀佛
  14. 5G智慧医疗全流程服务白皮书发布;2021年《故宫日历》青少版亮相 | 美通企业日报...
  15. 用C语言给自己的奖状
  16. 数据工具sqoop用法之mysql与hive数据导入导出
  17. 【PowerPoint】如何在PPT幻灯片添加“当前页/总页数”格式的页码
  18. 涂鸦智能进击南美取暖器市场
  19. 软件测试自我介绍 说什么才能让面试官一下子记住你!
  20. 拉格朗日四平方和定理c语言,拉格朗日四平方定理的证明

热门文章

  1. compose安装pgAdmin+postgis
  2. 淘集集秒杀活动需要隐身上架链接吗?
  3. 必需品、消费品与奢侈品
  4. java数组赋值后可以更改吗_我们可以为Java中的最终数组赋值吗?
  5. rt-thread http_ota通过WIFI下载固件文件的内存不足解决
  6. 实用:IP地址冲突的三大解决方式
  7. 2021最新 阿里云ECS建网站(建站)超详细全套完整图文教程
  8. 基于Go语言的网盘开发(GloudDisk)
  9. PHP基于thinkphp的在线机票销售系统#毕业设计
  10. 课程设计-图书信息管理系统