본문 바로가기
백준

백준 단계 5. 문자열

by 템닉___ 2024. 3. 17.

이 포스팅은 템닉의 개발저장소의 레거시 글입니다.

 

템닉의 개발저장소

기억 못할 수도 있는 지식들을 덤프해버리는 공간입니다.

tempnixk.tistory.com

 

1. 문자와 문자열 (문제번호 27866)

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

using namespace std;

int main() {
    string S;
    int i;
    cin >> S;
    cin >> i;
    cout << S.at(i-1) << "\n";
    return 0;
}

2. 단어 길이 재기 (문제번호 2743)

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

using namespace std;

int main() {
    string S;
    cin >> S;
    cout << S.size() << "\n";
    return 0;
}

3. 문자열 (문제번호 9086)

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

using namespace std;

int main() {
    string S;
    unsigned short T, i;
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> T;
    while(T-->0) {
        cin >> S;
        cout << *S.begin() << *(S.end()-1) << "\n";
    }
    return 0;
}

4. 아스키 코드 (문제번호 11654)

#include <iostream>

using namespace std;

int main() {

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    char a;
    cin >> a;
    int b = a;
    cout << b;
    return 0;
}

5. 숫자의 합 (문제번호 11720)

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

int main() {

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    
    string str;
    vector<short> vec;
    unsigned short N, sum=0;
    cin >> N;
    cin >> str;
    for(int i=0;i<N;i++) {
        vec.push_back(str[i]-48);
    }

    sum = accumulate(vec.begin(), vec.end(),0);
    cout << sum;
    return 0;
}

6. 알파벳 찾기 (문제번호 10809)

#include <iostream>
#include <string>
#include <vector> 
#include <algorithm>

using namespace std;

int main()
{
    vector<char> vec1, vec2;
    string S;
    cin >> S;
    //cout << S.length() <<endl;
    
    
    for(int i=0;i<S.length()+i;i++) {
        vec1.push_back(S[S.length()-1]);
        S.pop_back();
    }
    reverse(vec1.begin(), vec1.end());
    // for(int i=0; i<vec1.size(); i++) {
    //     cout << vec1[i] << " ";
    // }
    // cout << S << endl;
    
    for(int i=0; i<26; i++) {
        vec2.push_back(char(97+i));
        //cout << vec2[i] << " ";
    }
    //cout << "\n";
    for(int i=0; i<26; i++) {
        int n=0;
        for(int j=0; j<vec1.size(); j++) {
            if(vec1[j]==vec2[i]) {
                cout << j << " ";
                n++;
            } 
        }
        if(n==0)
            cout << "-1" << " ";
        else
            n--;
    }
    
    return 0;
}


//벡터에 넣어준 뒤 문자열 popback 하고 벡터 reverse 걸어주는게 나은지, 
//for 돌려서 i번째 알파벳이 문자열 인덱스랑 일치하는지 비교하는게 나은지

처음에 생각했을때는 벡터 사용해서 연산하려고 했는데 중복된 원소가 있을 경우 처리가 복잡해지는 것 같아서.... 배열로 때려넣었다.

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


int main()
{
    string S;
    cin >> S;
    int idx = 0;
    int arr[26]={-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,-1};
    for(int i=0; i<S.size(); i++) {
        idx = (int)S[i] -97;
        if(arr[idx]==-1) {
            arr[idx] = i;   
        }
        else {
            arr[idx] = min(arr[idx], i);
        }
    }
    for(int i = 0; i<26;i++) {
        cout << arr[i] << " ";
    }
    return 0;
}

이렇게 코드를 작성하니 통과했다.

 

++) 2024.03.18 지금 와서 블로그 정리하면서 문제를 봤는데 vector를 사용했으면 편했을 것 같다. unique 사용했다면 어땠을까?

'백준' 카테고리의 다른 글

[백준] 7287번 - 등록  (0) 2024.03.18
[백준] 10699번 - 오늘 날짜  (0) 2024.03.18
백준 단계 4. 배열  (0) 2024.03.17
백준 단계 3. 반복문  (0) 2024.03.17
백준 단계 2. 조건문  (0) 2024.03.17