defaultK

[프로그래머스] (문자열, 정렬) 카카오 파일명 정렬 c++ 본문

알고리즘 ( C++ )/프로그래머스

[프로그래머스] (문자열, 정렬) 카카오 파일명 정렬 c++

kwoss2341 2021. 1. 24. 01:17

https://programmers.co.kr/learn/courses/30/lessons/17686

 

코딩테스트 연습 - [3차] 파일명 정렬

파일명 정렬 세 차례의 코딩 테스트와 두 차례의 면접이라는 기나긴 블라인드 공채를 무사히 통과해 카카오에 입사한 무지는 파일 저장소 서버 관리를 맡게 되었다. 저장소 서버에는 프로그램

programmers.co.kr

기본적인 아이디어.

 

구조체를 정의하여 정렬하는 방식으로 진행.

정렬은 head, number, index 순으로 정렬  

 

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

struct myfile
{
    string head;
    int num;
    string tail;
    int ind;
};

int cmpbigs(string a, string b)
{
    int n=min(a.length(),b.length());
    
    int sw=0;
    for(int i=0; i<n; i++)
    {
        if(a[i]>b[i])
        {
            return 0;
        }
        else if(a[i]<b[i])
        {
            return 1;   
        }
    }
    
    if(a.length()==b.length())
    {
        return 2;
    }
    else if(a.length()<b.length())
    {
        return 1;
    }
    else
    {
        return 0;
    }
    
}

bool cmp(myfile p1,myfile p2)
{
    int i=cmpbigs(p1.head,p2.head);
    
    if(i==2)
    {
        if(p1.num>p2.num)
        {
            return 0;
        }
        else if(p1.num<p2.num)
        {
            return 1;
        }
        else
        {
            if(p1.ind>p2.ind)
            {
                return 0;
            }
            else
            {
                return 1;
            }
        }
    }
    else
    {
        return i;
    }
}

bool ischar(char c)
{
    return c>96&&c<123;
}

bool isnum(char c)
{
    return c>47&&c<58;
}

void mypush(myfile m[],string str,int ind)
{
    int i,j,k;
    for(i=0; i<str.length(); i++)
    {
        if(isnum(str[i]))
        {
            break;
        }
    }
    m[ind].head=str.substr(0,i);
   
    for(j=i; i<str.length(); j++)
    {
        if(!isnum(str[j]))
        {
            break;
        }
    }
    m[ind].num=stoi(str.substr(i,j-i));
    m[ind].tail=str.substr(j);
    m[ind].ind=ind;
    
    transform(m[ind].head.begin(), m[ind].head.end(), m[ind].head.begin(), ::tolower);
  
    return;
}

//https://programmers.co.kr/learn/courses/30/lessons/17686
vector<string> solution(vector<string> files) {
    vector<string> answer;
    int n=files.size();
    
    myfile m[n];
    
    for(int i=0; i<n; i++)
    {
        mypush(m,files[i],i);
    }
    
    sort(m,m+n,cmp);
    
    for(int i=0; i<n; i++)
    {
        answer.push_back(files[m[i].ind]);
    }
    
    
    return answer;
}

 

파일명 정렬 실행시간

Comments