2 solutions

  • 0
    @ 2024-9-5 9:48:38
    #include<iostream>
    #include <string> 
    #include<vector>
    #include<unordered_map>
    #include<algorithm>
    
    using namespace std;
    
    int main(){
        string s;
        cin>>s;
        int k;
        cin>>k;
    
        int n=s.length();
        vector<int> nums;
        for(char schar:s){
            int a=schar;
            nums.push_back(a);
        }
        unordered_map<int,int> hash;
        for(int i=0;i<n;i++){
            if(!hash.count(nums[i])){
                hash[nums[i]]=i;
            }
        }
        sort(nums.begin(),nums.end());
        if(k>n){
            //int result=nums[n-1];
            cout<<hash[nums[n-1]];
        }else{
            cout<<hash[nums[k-1]];
        }
    
        return 0;
    }
    
    • 0
      @ 2024-8-28 20:29:21
      while True:
          s = list(input("str:"))
          k = int(input("k:"))
          nums = []
          result = float('inf')
      
          for index, val in enumerate(s):
              nums.append([index, val])
          nums.sort(key = lambda x: ord(x[1]))
         
          if k > len(s):
              result = len(s)
          else:
              result = nums[k - 1][0]
              while k >= 2 and nums[k - 1][1] == nums[k - 2][1]:
                  result = nums[k - 2][0]
                  k -= 1
      
          print(result)
      
      • 1

      Information

      ID
      25
      Time
      1000ms
      Memory
      256MiB
      Difficulty
      6
      Tags
      # Submissions
      44
      Accepted
      14
      Uploaded By