카테고리 없음

[백준/c++] 20154번: 이 구역의 승자는 누구야?

녕이 2022. 7. 17. 17:36
728x90

 

 

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

 

20154번: 이 구역의 승자는 누구야?!

첫째 줄에 알파벳 대문자로만 이루어진 길이 K(1 ≤ K ≤ 1,000,000)인 문자열 S가 주어진다.

www.acmicpc.net

 

 

다른 사람 코드를 보니 좀 더 단순하게 풀었더라.. 대단하네,,

나는 그냥 주어진 대로 했는데,,, 흑흑 

 

생각해보니 진짜 굳이 2개씩 더할 필요가 없었다..ㅋㅋㅋㅋㅋㅋㅋㅋㅋ

앞에서부터 cnt를 누적하면서 10을 넘어섰는지 체크만 해주면 되는... 다음부턴 더 생각하고 하기..^^

 

 

[원래 코드]

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

int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int num[26] = {3,2,1,2,3,3,3,3,1,1,3,1,3,3,1,2,2,2,1,2,1,1,2,2,2,1};
    string s;
    int cnt = 0;
    int n;
    cin >> s;
    for(int i=0; i<s.size(); i+=2){
        if(i == s.size()-1){ //last element
            n = num[abs('A'-s[i])];
        }else{
            int a = num[abs('A' - s[i])];
            int b = num[abs('A' - s[i+1])];
            n = a + b;
        }
        if(cnt + n > 10) cnt = (cnt+n)%10;
        else             cnt = (cnt+n);
    }
    if(cnt % 2 == 0) cout << "You're the winner?\n";
    else             cout << "I'm a winner!\n";
    return 0;
}

 

 

[다시 짠 코드]

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

int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int num[26] = {3,2,1,2,3,3,3,3,1,1,3,1,3,3,1,2,2,2,1,2,1,1,2,2,2,1};
    string s;
    int cnt = 0;
    cin >> s;
    for(int i=0; i<s.size(); i++){
        cnt += num[s[i] - 'A'];
        if(cnt > 10) cnt %= 10;
    }
    if(cnt % 2 == 0) cout << "You're the winner?\n";
    else             cout << "I'm a winner!\n";
    return 0;
}

 

근데 둘 다 메모리와 시간이 비슷하니까.. ㅎㅎ...

 

 

 

 

728x90