【链接】 我是链接,点我呀:)
【题意】
【题解】
迭代加深搜索。 枚举最大层数。(也即改变的数字个数 然后枚举第一个改哪个数字,第二个改哪个数字。。 一定要注意字典序问题。 每次优先改成较小的字典序(也即顺序枚举 然后注意这个字符不改的情况。 不要算改变数。 最后改完之后。 只需要枚举a和b的情况。 看看a*b是不是等于c就好 ->查看这样的a*b数量是不是1 如果是1的话.就说明是正确的。直接输出那个ans就好(我们已经是从小到大枚举了,找到的一定是答案【代码】
/*
1.Shoud it use long long ?
2.Have you ever test several sample(at least therr) yourself?
3.Can you promise that the solution is right? At least,the main ideal
4.use the puts("") or putchar() or printf and such things?
5.init the used array or any value?
6.use error MAX_VALUE?
7.use scanf instead of cin/cout?
8.whatch out the detail input require
*/
/*
一定在这里写完思路再敲代码!!!
*/
#include <bits/stdc++.h>
using namespace std;
string x,y,z,s,ans;
int belong[10];
int maxdep,lenx,leny,lenz,tot;
string inttostring(int x){
string s = "";
while (x > 0){
s = (char)(x%10+'0')+ s;
x/=10;
}
return s;
}
int stringtoint(string s){
int x = 0;
int len = s.size();
for (int i = 0;i < len;i++){
x = x *10 + s[i]-'0';
}
return x;
}
void ok(int dep){
if (tot>1) return;
if (dep==lenx+leny){
string v[3];
for (int i = 0;i < 3;i++) v[i]="";
for (int i = 0;i < lenx+leny+lenz;i++) v[belong[i]]+=s[i];
int tx = stringtoint(v[0]),ty = stringtoint(v[1]);
tx = tx*ty;
string tz = inttostring(tx);
if ((int)tz.size()!=(int)v[2].size()) return;
for (int i = 0;i < (int) v[2].size();i++)
if (tz[i]!=v[2][i] && v[2][i]!='*') return;
tot++;
return;
}
if (s[dep]=='*'){
int qidian = 0;
if (dep==0 || dep == lenx || dep==lenx+leny) qidian = 1;
for (int i = qidian;i <= 9;i++){
s[dep] = i+'0';
ok(dep+1);
s[dep] = '*';
}
}else ok(dep+1);
}
bool dfs1(int dep,int nex){
if (dep==maxdep){
tot = 0;
string tans = s;
ok(0);
if(tot==1) {
ans = s;
return true;
}
return false;
}
if (nex>=lenx+leny+lenz) return false;
char temp;
temp = s[nex];
s[nex] = '*';
int cnt = 1;
if (s[nex]==temp) cnt = 0;
if (dfs1(dep+cnt,nex+1)) return true;
s[nex] = temp;
int qidian = 0;
if (nex==0 || nex == lenx || nex == lenx+leny) qidian = 1;
for (int i = qidian;i <= 9;i++){
temp = s[nex];
s[nex] = i+'0';
int cnt = 1;
if (s[nex]==temp) cnt = 0;
if (dfs1(dep+cnt,nex+1)) return true;
s[nex] = temp;
}
return false;
}
int main(){
#ifdef LOCAL_DEFINE
freopen("rush_in.txt", "r", stdin);
#endif
ios::sync_with_stdio(0),cin.tie(0);
int kase = 0;
while (cin >> x && x[0]!='0'){
ans.clear();
cin >> y >> z;
lenx = x.size(),leny = y.size(),lenz = z.size();
s = x + y + z;
for (int i = 0;i < lenx;i++) belong[i] = 0;
for (int i = lenx;i < lenx+leny;i++) belong[i] = 1;
for (int i = lenx+leny;i < lenx+leny+lenz;i++) belong[i] = 2;
for (maxdep = 0;;maxdep++)
if (dfs1(0,0)) break;
cout <<"Case "<<++kase<<": ";
for (int i = 0;i < lenx;i++) cout << ans[i];cout <<' ';
for (int i = lenx;i < lenx+leny;i++) cout << ans[i];cout <<' ';
for (int i = lenx+leny;i < lenx+leny+lenz;i++) cout << ans[i];
cout << endl;
}
return 0;
}