Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 591 | Accepted: 181 | Special Judge |
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
103K+G0G1=CG36
Sample Output
C 1 G 0 K 5
原题
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
char s[1000],t[1000];
char cun[1000];
int C[1000];
int ans[1000];
int tot;
int flag;
int rt[15];
void DFS(int x)
{
int i;
if(x==tot)
{
t[0]='\0';
strcpy(t,s);
for(i=0;t[i];i++)
if(t[i]>='A'&&t[i]<='Z')
t[i]=ans[C[t[i]]]+'0';
int num1=0;
int num2=0;
int num3=0;
int num=0;
int jia=0;
int jian=0;
for(i=0;t[i];i++)
{
if(t[i]>='0'&&t[i]<='9') num=num*10+t[i]-'0';
if(t[i]=='+') num1=num,num=0,jia=1;
if(t[i]=='-') num1=num,num=0,jian=1;
if(t[i]=='=') num2=num,num=0;
}
num3=num,num=0;
if(jia==1)
if(num1+num2==num3)
flag=1;
if(jian==1)
if(num1-num2==num3)
flag=1;
return;
}
for(i=0;i<=9;i++)
{
if(rt[i]==0)
{
ans[x]=i;
rt[i]=1;
DFS(x+1);
if(flag) return;
rt[i]=0;
}
}
}
int main()
{
int i;
while(~scanf("%s",s))
{
memset(C,0,sizeof(C));
tot=1;flag=0;
for(i=0;s[i];i++)
{
if(s[i]>='A'&&s[i]<='Z')
{
if(C[s[i]]==0)
{
C[s[i]]=tot;
cun[tot]=s[i];
tot++;
}
}
}
memset(rt,0,sizeof(rt));
DFS(1);
int flag[1000];
int shuzi[1000];
memset(flag,0,sizeof(flag));
for(i=1;i<tot;i++)
{
flag[cun[i]]=1;
shuzi[cun[i]]=ans[i];
}
for(i='A';i<='Z';i++)
if(flag[i])
printf("%c %d\n",i,shuzi[i]);
}
return 0;
}