L1-027 出租 分數 20

Frodnx發表於2024-08-01

用雜湊快速訪問找到下標

// 8'33"
#include <bits/stdc++.h>
using namespace std;
int arr[100];
int main()
{
    map<int,int> hash;
    string s;
    cin >> s;
    for(auto c : s)
        arr[c - '0'] = 1;
    int cnt = 0;
    vector<int> res1;
    
    cout << "int[] arr = new int[]{";
    for(int i = 9; i >= 0; -- i)
        if(arr[i] != 0)
        {
            res1.push_back(i);
            hash[i] = cnt ++;
        }
    for(int i = 0; i < res1.size(); ++ i)
    {
        cout << res1[i];
        if(i != res1.size() - 1) cout << ",";
    } 
    cout << "};\n";
    
    cout << "int[] index = new int[]{";
    for(int i = 0; i < s.size(); ++ i)
    {
    	cout << hash[s[i] - '0'];
    	if(i != s.size() - 1) cout << ",";
	}
    cout << "};";
    return 0;
}

相關文章