UVA11787 Numeral Hieroglyphs【数制】

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 (aspiral), 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.
    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.
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.
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

问题链接UVA11787 Numeral Hieroglyphs
问题简述:(略)
问题分析
    埃及数制判定计算问题。首先每种数字不得超过9个;另外,数字必须是升序或降序的。

程序说明
    程序中,用数组a2d[]把字母“BUSPFTM”分别映射为1-7,数组v[]将1-7映射为那个字母相应的值。变量dir1和dir2分别表示数字是降序或升序,用1表示降序或升序,它们不能同时为1。
参考链接:(略)
题记:查表法是好东西,用数组实现的查表法是最好的东东!

AC的C++语言程序如下:

/* UVA11787 Numeral Hieroglyphs */

#include <bits/stdc++.h>

using namespace std;

const int D = 7;
const int v[] = {0, 1 ,10, 100, 1000, 10000, 100000, 1000000};
int a2d[26];

void init()
{
    memset(a2d, 0, sizeof(a2d));
    a2d['B' - 'A'] = 1;
    a2d['U' - 'A'] = 2;
    a2d['S' - 'A'] = 3;
    a2d['P' - 'A'] = 4;
    a2d['F' - 'A'] = 5;
    a2d['T' - 'A'] = 6;
    a2d['M' - 'A'] = 7;
}

int main()
{
    init();

    int t, cnt[D + 1];
    cin >> t;
    while(t--) {
        string s;
        int dir1 = 0, dir2 = 0;
        memset(cnt, 0, sizeof(cnt));

        cin >> s;
        for(int i = 0; s[i]; i++) {
            cnt[a2d[s[i] - 'A']]++;
            if(i >= 1) {
                if(a2d[s[i- 1] - 'A'] > a2d[s[i] - 'A'])
                    dir1 = 1;
                else if(a2d[s[i- 1] - 'A'] < a2d[s[i] - 'A'])
                    dir2 = 1;
            }
        }

        for(int i = 1; i <= D; i++)
            if(cnt[i] > 9) {
                dir1 = dir2 = 1;
                break;
            }
        if(dir1 && dir2)
            printf("error\n");
        else {
            int sum = 0;
            for(int i = 1; i <= D; i++)
                sum += cnt[i] * v[i];
            printf("%d\n", sum);
        }
    }

    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值