题解:P13192 [GCJ 2016 #1B] Getting the Digits

· · 题解

思路:

通过观察可以发现有一些字母在所有数字中只出现过一次,我们可以先处理这些字母对应的单词。
比如字母 Z 只出现在 ZERO 中。

在处理完这些后,我们可以发现有一些字母在这些单词中只出现过两次,而其中一个单词已经处理过了,这时我们就可以直接处理另外一个单词。
例如 SIXSEVENSIX 中的 X 只在它自己里面出现,已经被处理了,而 S 只在这两个单词中出现,所以剩下的 S 都是 7 的了。

以此类推,我们便可以完成此题。

代码:

#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define itn int
#define ull unsigned long long
int T;
string s;
int main(){
    cin>>T;
    for(int i=1;i<=T;i++){
        cin>>s;
        unordered_map<char,int>cnt;
        int bj[12];memset(bj,0,sizeof bj);
        for(int i=0;i<s.size();i++)cnt[s[i]]++;
        while(cnt['Z']){// ZERO   0   Z 只在 0 中出现
            cnt['Z']--;cnt['E']--;cnt['R']--;
            cnt['O']--;
            bj[0]++;
        }
        while(cnt['W']){// TWO   2
            cnt['T']--;cnt['W']--;cnt['O']--;
            bj[2]++;
        }
        while(cnt['G']){// EIGHT 8
            cnt['E']--;cnt['I']--;cnt['G']--;
            cnt['H']--;cnt['T']--;
            bj[8]++;
        }
        while(cnt['X']){// SIX   6
            cnt['S']--;cnt['I']--;cnt['X']--;
            bj[6]++;
        }
        while(cnt['U']){// FOUR  4
            cnt['F']--;cnt['O']--;cnt['U']--;
            cnt['R']--;
            bj[4]++;
        }
        while(cnt['S']){// SEVEN 7 因为有 S 出现的只有 6 和 7,6 已经处理完了,剩下的 S 都是 7 的了
            cnt['S']--;cnt['E']--;cnt['V']--;
            cnt['E']--;cnt['N']--;
            bj[7]++;
        }
        while(cnt['F']){// FIVE  5
            cnt['F']--;cnt['I']--;cnt['V']--;
            cnt['E']--;
            bj[5]++;
        }
        while(cnt['H']){// THREE 3
            cnt['T']--;cnt['H']--;cnt['R']--;
            cnt['E']--;cnt['E']--;
            bj[3]++;
        }
        while(cnt['I']){// NINE 9
            cnt['N']--;cnt['I']--;cnt['N']--;
            cnt['E']--;
            bj[9]++;
        }
        while(cnt['O']){// ONE 1
            cnt['O']--;cnt['N']--;cnt['E']--;
            bj[1]++;
        }
        cout<<"Case #"<<i<<": ";
        for(int i=0;i<=9;i++){
            while(bj[i])cout<<i,bj[i]--;
        }
        cout<<"\n";
    }
    return 0;
}