알파벳순으로 위쪽으로 정렬 한 다음 각각의 아래쪽으로 정렬하며 모든 항목이 정렬되어야합니다

샤 생크 두베이

입력 문자열에 중복 항목이 없습니다.

Ex AbBa 출력 = AaBb

예 = ACdac 출력 = AaCcd

예 = ADaEedb 출력 = AabDdEe

마지막 테스트 케이스에서 실행되지 않는 코드를 알려주세요. 다른 아이디어가있는 사람은 내가 이해하기 쉽도록 lang이 C ++이면 댓글을 달아주세요.

C ++

#include<bits/stdc++.h>
using namespace std;

void swap(char *a,char *b){
char temp = *a;
*a = *b;
*b =temp;
}
int main(){
string copy1;
string s ;
cin>>s;
int j=-1;

int left = 0;
int right = s.length()-1;

while(left<right){

    if(s[left] >= 'a'  && s[right]<='z'){
        swap(&s[left],&s[right]);
        right--;
    }
    else
        left++;

}

cout<<s<<endl;
priority_queue <char, vector<char>, greater<char> > pq;
    for(int i=0;i<s.length();i++){
            if(s[i]>='A' && s[i]<='Z'){
                pq.push(s[i]);
                }
    }

    for(int i=0;i<s.length();i++){
            if(pq.empty()==false){
                char m = pq.top();
                if(find(s.begin(),s.end(),(char)(m+32))!=s.end()){
                        copy1+=(char)m;
                        copy1+=(char)(m+32);
                        pq.pop();
                    }
                else{
                copy1+=(char)m;
                pq.pop();
                }
            }
    }

cout<<copy1<<endl;

}
*/
Stu

가능한 한 많은 표준 라이브러리를 사용하는 빠른 구현 :

#include <cctype>
#include <algorithm>
#include <string>
#include <iostream>

int main(){
    auto compare = [](char a, char b){
        char upperA = std::toupper(a);
        char upperB = std::toupper(b);
        if(upperA != upperB) return upperA < upperB;
        if(upperA == a) return true;
        return false;
    };

    std::string input = "ADaEedbaaaaa";

    std::sort(input.begin(), input.end(), compare);
    auto endOfUniques = std::unique(input.begin(), input.end());
    input.erase(endOfUniques, input.end());
    std::cout << input << std::endl;
}

std :: sort 대신 std :: priority_queue를 반드시 사용해야하는 경우이 버전은 약간 덜 우아한 버전이 작동하지만 중복 제거는 수행하지 않습니다 ( "문자열에 중복 허용 없음"이 입력 또는 출력에 대한 요구 사항) :

#include <cctype>
#include <string>
#include <iostream>
#include <queue>

int main(){
    struct compare{
        bool operator()(char a, char b){
            char upperA = std::toupper(a);
            char upperB = std::toupper(b);
            if(upperA != upperB) return upperA > upperB;
            if(upperA == a) return false;
            return true;
        }
    };

    std::string input = "ADaEedb";

    std::priority_queue<char, std::vector<char>, compare> queue;
    for(auto c : input) queue.push(c);

    std::string output;
    while(!queue.empty()){
        output += queue.top();
        queue.pop();
    }

    std::cout << output << std::endl;
}

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관