273. Integer to English Words

   



string T[10]={"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};
string H[10]={"","Ten","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"};
string K[10]={"Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};
string Hundred="Hundred",Thousand="Thousand",Million="Million",billion="Billion";
class Solution {
public:
    string getWords(int num)
    {
        string tmp="";
        if(num/100)
        {
            tmp=T[num/100]+" "+Hundred;
            num%=100;
        }
        if(num/10)
        {
            if(num>=10&&num<=19)
            {
                if(tmp!="")
                    return tmp+" "+K[num%10];
                return K[num%10];
            }
            if(tmp!="")
                tmp=tmp+" "+H[num/10];
            else 
                tmp=H[num/10];
            num%=10;
        }
        if(num)
        {
            if(tmp!="")
                tmp=tmp+" "+T[num];
            else 
                tmp=T[num];
        }
        return tmp;
    }
    string numberToWords(int num) {
        int cnt=0;
        string str="";
        if(num==0)
            return T[0];
        while(num)
        {
            int tmp=num%1000;
            num/=1000;
            string ans;
            if(tmp)
            {
                ans=getWords(tmp);
                if(cnt==1)
                    ans=ans+" "+Thousand;
                else if(cnt==2)
                    ans=ans+" "+Million;
                else if(cnt==3)
                    ans=ans+" "+billion;
                if(str=="")
                    str=ans;
                else 
                    str=ans+" "+str;
            }
            cnt++;
        }
        return str;
    }
};


Unlike English, Chinese words are not seperated by space. It's hard for computer to divide a sentence into words correctly. However, you can manage it by doing these steps. 1.Analyis a large number of articles, and count all the words and their ocurrence frequency. 2.Find a way to seperate the sentence into words, so that the probability product of all the words is largest. That means a sentence s is divided into w1/w2/w3.../wn, because p(w1)*p(w2)*p(w3)...*p(wn) is larger than any other p(v1)*p(v2)...p(vm). If a sentence is nihaoshijie, the best solution is nihao/shijie ,because p(nihao) * p(shijie) is largest of all solutions. You are given the words and their frequency. Do the division. The First part is the words and frequency. Every line is a wordi, string contains only lower case characters and a integer f(wordi). To simplify the problem, f(wordi) is not the real frequency. You can think the frequency is p(wordi), f(wordi) = (int)(log(p(wordi)). So the sum of fs should be largest, and all fs shouldn't be 0. The length of word is less than 50. The number of words is less than 40000. All the integers can be handled with int. The first part is ended with "#END". The sencond part is the sentences to be divided. Each line is a string contains only lower case characters. The length of the line is less than 5000. There are T sentences to be divided. (T<5) Each sentence a line. Print the line with "/" to seperate the words.The solution is unique. Print a blank line after each sentence. 输入样例 an 1 chu 1 chuan 1 huan 1 hu 1 hua 1 chuang 5 qi 1 an 1 qian 5 mi 1 min 1 ming 5 yu 1 yue 5 e 1 gu 1 gua 1 guan 1 guang 5 #END chuangqianmingyueguang 输出样例 chuang/qian/ming/yue/guang
最新发布
12-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值