POJ 3548 Restoring the digits
2015-07-26 17:02
316 查看
Description
Let's consider arithmetic expressions (addition or subtraction) over non-negative decimal integers. The expression syntax is as follows:
the first operand;
the operator sign ('+' or '‑');
the second operand;
the character '=';
the result of the operation (sum or difference, according to the operator).
The operands don't exceed 999 999 999 . In case of subtraction the first operand should be greater than or equal to the second one. There are no spaces in the expression.
Upper-case Latin letters are substituted for some digits (possibly including insignificant zeroes) so that identical letters correspond to identical digits and different letters correspond to different digits. It is guaranteed that at least one such substitution
is made.
The task is to restore the substituted digits.
Input
The input contains only one line with the encoded arithmetic expression.
Output
The output consists of several lines. Each line describes one substitution and contains a letter and the corresponding digit. The letter and the digit should be separated by exactly one space. The strings should be sorted in the ascending order of letters.
Letters not used in the substitution should not be listed.
Sample Input
Sample Output
Let's consider arithmetic expressions (addition or subtraction) over non-negative decimal integers. The expression syntax is as follows:
the first operand;
the operator sign ('+' or '‑');
the second operand;
the character '=';
the result of the operation (sum or difference, according to the operator).
The operands don't exceed 999 999 999 . In case of subtraction the first operand should be greater than or equal to the second one. There are no spaces in the expression.
Upper-case Latin letters are substituted for some digits (possibly including insignificant zeroes) so that identical letters correspond to identical digits and different letters correspond to different digits. It is guaranteed that at least one such substitution
is made.
The task is to restore the substituted digits.
Input
The input contains only one line with the encoded arithmetic expression.
Output
The output consists of several lines. Each line describes one substitution and contains a letter and the corresponding digit. The letter and the digit should be separated by exactly one space. The strings should be sorted in the ascending order of letters.
Letters not used in the substitution should not be listed.
Sample Input
103K+G0G1=CG36
Sample Output
C 1 G 0 K 5 数据挺小的,直接暴力dfs#include<cstdio> #include<cmath> #include<algorithm> #include<iostream> #include<cstring> #include<string> #include<vector> using namespace std; const int maxn=205; char s[maxn],a[maxn],c; int b[maxn],tot,v[maxn],f[maxn]; bool check() { for (int i=0;i<tot;i++) v[a[i]]=b[i]; for (int i=0;i<10;i++) v[i+'0']=i; int x1,x2,x3,i,j,k; for (x3=x2=x1=i=0;s[i]!='+'&&s[i]!='-';i++) x1=x1*10+v[s[i]]; for (j=i+1;s[j]!='=';j++) x2=x2*10+v[s[j]]; for (k=j+1;s[k];k++) x3=x3*10+v[s[k]]; if (s[i]=='+'&&x1+x2==x3) return true; if (s[i]=='-'&&x1-x2==x3) return true; return false; } bool dfs(int x) { if (x==tot) return check(); for (int i=0;i<10;i++) if (!f[i]) { b[x]=i; f[i]=1; if (dfs(x+1)) return true; f[i]=0; } return false; } int main() { while (~scanf("%s",s)) { tot=0; memset(f,0,sizeof(f)); for (char i='A';i<='Z';++i) for (int j=0;s[j];j++) if (i==s[j]) {a[tot++]=i; break;} dfs(0); for (int i=0;i<tot;i++) printf("%c %d\n",a[i],b[i]); } }
相关文章推荐
- 抛开flash,自己开发实现C++ RTMP直播流播放器
- Android 自定义ViewGroup(自定义布局容器)
- 01_GIT基础、安装
- 【YII2学习笔记】20150726-1
- 01_GIT基础、安装
- DQ锁相
- 7.1 虚拟机直接IO原理与架构
- I/O完成端口模型
- [HDOJ 4524] 郑厂长系列故事――逃离迷宫
- linux_c 网络开发日记(2)GCC简介
- 7、二种 为二个不同的子网配置DHCP服务器(中继代理服务器)
- [leedcode 136] Single Number
- 字符串
- Number Guessing
- 随便写写
- 自学opencv过程中轮廓学习时遇到的几个问题
- POJ 2570 Fiber Network
- iOS 数组遍历删除元素的问题
- Mybatis动态Sql
- [HDOJ 4520] 小Q系列故事――最佳裁判