2012-06-17 17:29:21Morris

[UVA] 11787 - Numeral Hieroglyphs

 B - Numeral Hieroglyphs 

Background

Egyptians had a writing system based on hieroglyphs from around 3000 BC. Hieroglyphs are small drawings representing words. Besides, Egyptians had a base 10 system of hieroglyphs for numerals. That is, they had separate symbols for one unit (a bar), ten units (an inverted ‘U’), one hundred (a spiral), one thousand (a paper plant), ten thousand (a finger), one hundred thousand (a tadpole) and one million (a man kneeling).

These are the numeral hieroglyphs:

To make up number 276, for example, fifteen symbols were required: two "hundred" symbols, seven "ten" symbols, and six "unit" symbols. Number 276 would appear as:

Number 4622 would be represented as:

As you can see, Egyptians wrote ordered symbols, according to its value, from left to right as well as from right to left.

The Problem

You have to convert numeral hieroglyphs into numbers. For that, we will use the following code:

So, we could represent 276 as SSUUUUUUUBBBBBB or BBBBBBUUUUUUUSS.

You cannot write more than nine times each character.

The Input

The first line of the input contains an integer, n, indicating the number of test cases. For each test case, one line appears, that contains a combination of m characters belonging to the following set {‘B’, ‘U’, ‘S’, ‘P’, ‘F’, ‘T’, ’M’}, where 1<=m<=500, representing, or not, possible numeral hieroglyphs.

The Output

For each combination of characters you must write either the corresponding number or the word "error" if one of the two following cases occurs: (a) the input is not ordered, or (b) there are more than nine equal characters.

Sample Input

8
PPPSUB
BUSPPP
PPPUUUPPP
BUSPFTM
MMMMMMMMMM
MMMMTTTUBBBBB
BBPPPPPPPFTTT
MMMMMMMMMTTTTTTTTTFFFFFFFFFPPPPPPPPPSSSSSSSSSUUUUUUUUUBBBBBBBBB

Sample Output

3111
3111
error
1111111
error
4300015
317002
9999999



#include <stdio.h>

int main() {
int t;
char s[501];
scanf("%d ", &t);
while(t--) {
gets(s);
bool flag1 = false, flag2 = false;
char ch[] = {'M','T','F','P','S','U','B'};
long nu[] = {1000000,100000,10000,1000,100,10,1};
int i, j, k, ans1 = 0, ans2 = 0;
for(i = 0, j = 0, k = 0; i < 7; i++) {
int cnt = 0;
while(s[j] == ch[i]) {
cnt++, j++;
ans1 += nu[i];
if(cnt > 9) flag1 = true;
}
cnt = 0;
while(s[k] == ch[6-i]) {
cnt++, k++;
ans2 += nu[6-i];
if(cnt > 9) flag2 = true;
}
}
if(s[j] != '\0') flag1 = true;
if(s[k] != '\0') flag2 = true;
if(flag1 && flag2)
puts("error");
else
printf("%d\n", flag2 ? ans1 : ans2);
}
return 0;
}