指定字符的索引

输出指定字母在字符串中的索引

给定一个字符串,把字符串按照大写在前小写在后排序,输出排好后的第 K 个字母在原来字符串的索引。相同字母输出第一个出现的位置。

例如:

输入

1 hAkDAjByBq 4 输出

1 6 说明:

排好序后 AABBDhjkqy,第 4 个是 B,第一个出现的在原字符串 6 这个位置。(索引是从 0 开始)

#include <iostream>
#include <string>
#include <algorithm>
 
using namespace std;
 
int FindSourcePos(string source_string, int new_pos) {
    string string_tempUp;
    string string_tempLow;
    for (char i : source_string) {
        if ((i >= 'A') && (i <= 'Z')) {
            string_tempUp += i;
        } else {
            string_tempLow += i;
        }
    }
    sort(string_tempUp.begin(), string_tempUp.end());
    sort(string_tempLow.begin(), string_tempLow.end());
    string new_string = string_tempUp + string_tempLow;
    auto old_pos = source_string.find(new_string[new_pos - 1]);
    return old_pos;
}
 
int main(int argc, char **argv) {
    while (1) {
        string source_string;
        int new_pos;
        cin >> source_string >> new_pos;
        cout << FindSourcePos(source_string, new_pos) << endl;
    }
    return 0;
}
strs='hAkDAjByBq'
n=4
res=[]
res2=[]
for i in range(len(strs)):
    if strs[i].isupper():
        res.append((strs[i],i))
    else:
        res2.append((strs[i],i))
print(res)
print(res2)

a=res+res2
print(a)
print(a[4-1][1])
全部评论

相关推荐

码农索隆:传音老登来也。 但是这个我不知道怎么回答,不仅仅传音吧,很多公司在候选人不第一时间接受offer或主动将报道时间延期时,都会再从池子里面捞人,直到l捞到满足公司所有要求的人。
秋招的第一个offer,...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务