Ultraviolet A 185 Roman Numerals

Source: Internet
Author: User

UVA_185

The first part is not detailed. You can translate the string into an integer. The key lies in the second part. This is like a mathematical problem when you are a small user.

First, we may analyze the characteristics of the question and try to find as many backtracing conditions as possible.

Obviously, we should calculate from the single-digit addition, which not only conforms to our calculation habits, but also facilitates us to evaluate the expression. We may consider that the analysis of each column in the vertical form is in the form of x + y + s = z, where s is the remainder of the first digit, then, of course, we choose to enumerate the values of x and y, so that z is naturally determined. If we enumerate the values of z, it will be much more troublesome.

X and y are enumerated. The first case is that the character has been assigned a value in the previous operation, the second case is that this character has not been assigned a value, and the third case is that this character does not exist. (in this case, we can guarantee that the string that represents and represents is the longest during preprocessing, otherwise, there is no solution ). If a value has been assigned, you can only take that value. If this value does not exist, it is similar to the value already assigned. It can be regarded as 0. If the value is not assigned, We need to assign this character a value that is not repeated with the previous one.

Several backtracing conditions are provided in this step of determining z. Like the preceding analysis on x and y, we also need to divide z into two States: confirmed and unconfirmed. If z has been determined, we need to check whether the single position of x + y + s is equal to the value of z. If not, we do not need to continue searching; otherwise, we will continue to perform further searching. If z is not determined, check whether the single-digit value of x + y + s has been assigned to other characters. If it has been assigned to another character, you do not need to continue searching, if other characters are not assigned, the value is assigned to the z character and further searches are performed.

After all the characters are assigned a value, we need to determine whether the remainder is 0 and whether the maximum bit of a string is 0, if both of them meet the requirements, add the Count cnt to 1. If cnt is changed to 2 after that, there must be multiple solutions. At the same time, we do not need to find more solutions, so we can directly return and end all the dfs.

#include<stdio.h>
#include<string.h>
char str[50], a[15], b[15], c[15];
int A, B, C, v[128], num[128], cnt, vis[15][128], hash[15];
void init()
{
int i, j, k, x, y, z;
A = strchr(str, '+') - str;
B = strchr(str, '=') - strchr(str, '+') - 1;
C = strlen(str) - (strchr(str, '=') - str) - 1;
x = y = z = 0;
for(i = 0, j = A - 1; str[i] != '+'; i ++, j --)
{
a[j] = str[i];
if(str[i + 1] != '+' && v[str[i + 1]] > v[str[i]])
x -= v[str[i]];
else
x += v[str[i]];
}
for(++ i, j = B - 1; str[i] != '='; i ++, j --)
{
b[j] = str[i];
if(str[i + 1] != '=' && v[str[i + 1]] > v[str[i]])
y -= v[str[i]];
else
y += v[str[i]];
}
for(++ i, j = C - 1; str[i]; i ++, j --)
{
c[j] = str[i];
if(str[i + 1] && v[str[i + 1]] > v[str[i]])
z -= v[str[i]];
else
z += v[str[i]];
}
if(x + y == z)
printf("Correct ");
else
printf("Incorrect ");
}
int dfs(int cur, int s)
{
int i, j, k, x, y, z, unx, uny, unz;
if(cur == C)
{
if(s == 0 && num[a[A - 1]] != 0 && num[b[B - 1]] != 0 && num[c[C - 1]] != 0)
{
++ cnt;
if(cnt >= 2)
return 1;
}
return 0;
}
z = 0;
unx = 1;
if(cur >= A)
{
x = 0;
unx = 0;
}
else if(num[a[cur]] != -1)
{
x = num[a[cur]];
unx = 0;
}
for(i = (unx ? 0 : 9); i < 10; i ++)
{
if(unx && hash[i])
continue;
if(unx)
{
hash[i] = 1;
x = num[a[cur]] = i;
}
uny = 1;
if(cur >= B)
{
y = 0;
uny = 0;
}
else if(num[b[cur]] != -1)
{
y = num[b[cur]];
uny = 0;
}
for(j = (uny ? 0 : 9); j < 10; j ++)
{
if(uny && hash[j])
continue;
if(uny)
{
hash[j] = 1;
y = num[b[cur]] = j;
}
unz = 1;
if(num[c[cur]] != -1)
unz = 0;
z = (x + y + s) % 10;
k = (x + y + s) / 10;
if(unz)
{
if(!hash[z])
{
hash[z] = 1;
num[c[cur]] = z;
if(dfs(cur + 1, k))
return 1;
hash[z] = 0;
num[c[cur]] = -1;
}
}
else
{
if(z == num[c[cur]])
{
if(dfs(cur + 1, k))
return 1;
}
}
if(uny)
{
hash[j] = 0;
num[b[cur]] = -1;
}
}
if(unx)
{
hash[i] = 0;
num[a[cur]] = -1;
}
}
return 0;
}
void solve()
{
int i, j, k;
if(A > C || B > C)
{
printf("impossible\n");
return ;
}
memset(num, -1, sizeof(num));
memset(hash, 0, sizeof(hash));
cnt = 0;
dfs(0, 0);
if(cnt == 0)
printf("impossible\n");
else if(cnt == 1)
printf("valid\n");
else
printf("ambiguous\n");
}
int main()
{
v['I'] = 1, v['X'] = 10, v['C'] = 100, v['M'] = 1000;
v['V'] = 5, v['L'] = 50, v['D'] = 500;
for(;;)
{
gets(str);
if(str[0] == '#')
break;
init();
solve();
}
return 0;
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.