백준: Silver1④ - 1254, 1263, 1276

1 분 소요


</br> 좀만 더하면 골드 5
</br>

1254: 팰린드롬 만들기

https://www.acmicpc.net/problem/1254

문자열 뒤에 추가해서 가장 짧은 팰린드롬 만들기

bool isPalin(string s){
    int sz = s.size();
    for(int i = 0; i < sz/2; i++){
        if( s[i] != s[sz - 1 - i] ) return false;
    }
    return true;
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    string s;
    cin >> s;

    if( isPalin(s) ){
        cout << s.size() << endl;
        return 0;
    }
    string reversed = s;
    reverse(reversed.begin(), reversed.end());

    int j = 1;
    for(int i = 2; i < s.size(); i++){
        cout << reversed.substr(0, i)<<endl;
        if( isPalin(reversed.substr(0, i)) ){
            cout<<"!"<<endl;
            j = max(j, i);
        }
    }

    cout << 2*s.size() - j << endl;
}

어떻게 할까 생각해 봤는데,

문자열: abcded
뒤집기: dedcba
따라서: ded  (cba)

니까 뒤집은 거에서 팰린드롬이 되는 가장 큰 부분을 찾으면, 남은 부분들을 더해주면 되겠다 싶더라.

최악의 경우 abcd와 같아도, 마지막 문자 d는 무조건 팰린드롬이므로 j는 1로 초기화 한다.
</br>

1263: 시간 관리

https://www.acmicpc.net/problem/1263

하루가 최대 100만 시간일 때 스케줄링 문제

bool cmp(tuple<int, int> &t1, tuple<int, int> &t2){
    return get<1>(t1) > get<1>(t2);
}

// in main()
    sort(v.begin(), v.end(), cmp);

    int ans = 10000000;
    for(int i = 0; i < n; i++){
        ans = min(ans, get<1>(v[i]));
        ans -= get<0>(v[i]);
    }
    if( ans < 0 ) cout << -1 << endl;
    else cout << ans << endl;

?? 하루라 해놓고 최대가 100만 시간인 건 뭔 경우지
조건 확인 잘 안 한 것도 문제긴 하지만 하루라 해놓고 100만 시간이라니 좀 너무하다

아무튼 일들을 입력 받으면, 제한 시각이 늦은 순으로 정렬하고, 최대한 늦게 처리하면 되는 간단한 그리디
</br>

1276: PLATFORME

https://www.acmicpc.net/problem/1276

쌓여진 플랫폼을 지을 때 다리 길이의 합 구하기

    sort(v.begin(), v.end());

    int ans = 0;
    for(int i = 0; i < n; i++){
        int y  = get<0>(v[i]);
        int x1 = get<1>(v[i]);
        int x2 = get<2>(v[i]);

        int a = y, b = y;
        for(int j = 0; j < i; j++){
            if( x1 >= get<1>(v[j]) && x1 < get<2>(v[j]) ){
                a = y - get<0>(v[j]);
            }
            if( x2 > get<1>(v[j]) && x2 <= get<2>(v[j]) ){
                b = y - get<0>(v[j]);
            }
        }
        ans += a+b;
    }
    cout << ans << endl;

y값 오름차순으로 정렬해 준다. 따라서 따로 함수가 필요 없었다.
다리 길이를 a = y, b = y라고 하고, 겹치는 게 있다면 확인해서 업데이트해 주면 된다.
</br>


알고 보니 학교 랭킹이 있던데 200등 안에 간당간당하게 있다ㅋㅋ
</br>

댓글남기기