力扣 423 超出时间限制

给你一个字符串 s ,其中包含字母顺序打乱的用英文单词表示的若干数字(0-9)。按 升序 返回原始的数字。

输入:s = “owoztneoer”
输出:“012”

输入:s = “fviefuro”
输出:“45”

#include <iostream>
using namespace std;
#include <string>
#include <unordered_map>
#include <algorithm>

string func(vector<string>& temp, vector <int>& num1,string & result1)
{
    
    char count = 0, count2 = 0;;
    for (auto& ch : temp) {
        vector <int> num2(26, 0);
        for (auto& chr : ch) {
            num2[chr - 'a']++;
        }
        cout << "num2的元素为:";
        for (int i = 0; i < num2.size(); i++) {
            cout << num2[i];
        }
        cout << endl;

        for (int i = 0; i < num2.size(); i++) {
            if ((num1[i] - num2[i]) < 0)
            {
                count2 = 1;
                break;
            }
        }
        if (count2 == 0)
        {
            vector <int> moment;
            for (int i = 0; i < num2.size(); i++) {
                moment.push_back(num1[i] - num2[i]);
            }
            cout << "num1 - num2的元素为:";
            for (int i = 0; i < num2.size(); i++) {
                cout << moment[i];
            }
            cout << endl;
            num1 = moment;  //geng xin
            result1+=(to_string(count));
        }
        count2 = 0;
        count++;
    }
    return result1;
}


int main()
{
    string s;
    s = "zorezerothreethreezero";
    vector <int> num1(26, 0);
    for (auto& ch : s) {  
        num1[ch - 'a']++;  //按照字母出现频率进行整理
    }
    cout << "num1的元素为:";
    for (int i = 0; i < num1.size(); i++) {
        cout << num1[i];
    }
    cout << endl;


    vector<string>temp;
    temp.push_back("zero");
    temp.push_back("one");
    temp.push_back("two");
    temp.push_back("three");
    temp.push_back("four");
    temp.push_back("five");
    temp.push_back("six");
    temp.push_back("seven");
    temp.push_back("eight");
    temp.push_back("nine");

    cout << "temp的元素为: ";
    for (int i = 0; i < temp.size(); i++) {
        cout << temp[i]<<" ";
    }
    cout << endl;


    string result1;
    int q = 0;
    while (q==0) {  //防止有重复的字母
        result1 = func(temp, num1, result1);
        vector <int> qqq;
        qqq = num1;

        for (int i = 0; i < qqq.size(); i++) {
            cout << qqq[i];
        }
        cout << endl;

        sort(qqq.begin(), qqq.end());       
        if (qqq[qqq.size()-1] <= 0) {
            q = 1;
        }        
    }
    cout << result1 << endl;


	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值