poj-3548 Restoring the digits(DFS枚举)

Restoring the digits
Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 591Accepted: 181Special 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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值