Uvs: 185-Roman Numerals (dfs)

Source: Internet
Author: User

Question: Ultraviolet A: 185-Roman Numerals


Question: Give a string equality and ask if this string is a Rome equality? Are there any conforming Arabic equations? The former outputs correct or incorrect, and the latter scores:

A combination of letters that can form an Arabic equation must be greater than or equal to 2,
There is only one combination of valid letters that can form an Arabic equation.
Impossible does not have a combination of letters that match the Arabic equation.
Solution:
1. Rule of making up the Rome equation: if the value of the letter (I-1) on the left of each current letter (I) is smaller than the value corresponding to I, the value of the I-1 is negative, otherwise it is positive. Finally, determine whether or not it is just about or not.
2. If an Arabic equation can be formed, each letter can be replaced by a number between 0 and 9 to see if there is an equation between the left and right sides.
Note:
0 is displayed separately, and 0 is not used in the case of leading 0, and must be excluded. Then the same letter indicates the same value. Here we will enumerate the situations of each letter, and then remove the case where 0 is wrong, and dfs () will find the correct combination.

Code:
#include 
   
    #include 
    
     const int N = 10;char str[N * N], s1[3][N];int s[N * N];char vis[N * N], head[N * N];const char *letter = "IVXLCDM";intcount, size;void init () {s['I'] = 1;s['X'] = 10;s['C'] = 100;s['M'] = 1000;s['V'] = 5;s['L'] = 50;s['D'] = 500;size = 0;}int trans (char * a) {int num = 0, i;for (i = 1; i <= strlen (a); i++) {if (s[a[i - 1]] >= s[a[i]])num += s[a[i- 1]];elsenum -= s[a[i - 1]];}return num;}bool is_roman () {int n1 = trans(s1[0]);int n2 = trans(s1[1]);int n3 = trans(s1[2]);if (n1 + n2 == n3)return true;return false;}int tran_num (char * a) {int num = 0;for (int i = 0; i < strlen (a); i++)num = num * 10 + s[a[i]];return num;}void is_numerals (int n, int v) {if (n == size) {int n1 = tran_num (s1[0]);int n2 = tran_num (s1[1]);int n3 = tran_num (s1[2]);if (n1 + n2 == n3) count++;return ;}if (v >= 7)return;while(!vis[letter[v]]) v++;if (v < 7) {for (int j = 0; j < 10; j++) {if (j == 0 && head[letter[v]])continue;s[letter[v]] = j;is_numerals (n + 1, v + 1);if (count >= 2)return;}} }int main () {int k, j;while (scanf ("%s", str) , str[0] != '#') {init();k = j = 0;memset (vis, 0, sizeof (vis));memset (head, 0, sizeof (head));for (int i = 0; i < strlen (str); i++) {if (str[i] != '+' && str[i] != '=' ) {s1[k][j++] = str[i];if (!vis[str[i]])size++;vis[str[i]] = 1;if (j == 1)head[str[i]] = 1;} else {s1[k][j] = '\0';k++;j = 0;}}s1[2][j] = '\0';printf ("%s ", is_roman() ?"Correct":"Incorrect");count = 0;is_numerals(0, 0);if (!count)printf ("impossible\n");else {if (count == 1)printf ("valid\n");elseprintf ("ambiguous\n");}}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.