티스토리 뷰

반응형

백준 2037 문자메세지

해설

ABC, DEF, GHI, JKL, MNO, PQRS, TUV, WXYZ 가 각각 같은 번호를 눌러 입력해야하므로 이전에 동일한 번호를 눌렀다면 추가로 대기시간 W시간을 요구한다.
공백에 대해선 대기시간 W 이 필요하지 않다.

BC, EF, HI .. 등에 대해선 여러번 눌러야하므로 P*required[알파벳]을 이용해 구할 수 있게 하였음.

입력 받는 것이 조금 까다로울 수 있다.

````C++

#include <stdio.h>

#include

#include <string.h>

using namespace std;

int a[26] =
{ 2, 2, 2, 3, 3, 3,
4, 4, 4, 5, 5, 5,
6, 6, 6, 7, 7, 7, 7,
8, 8, 8, 9, 9, 9, 9
};

int required[26] =
{ 1, 2, 3, 1, 2, 3,
1, 2, 3, 1, 2, 3,
1, 2, 3, 1, 2, 3, 4,
1, 2, 3, 1, 2, 3, 4
};

char input[ 1002 ];
int main()
{ int p, w;
cin >> p >> w;
getchar();
fgets(input, 1002, stdin);

int len = strlen(input);
if ( input[len-1] == '\n') len--;

int before = -1;
int ans = 0;

for (int i =0; i<len; i++)
{
    if ( input[i] == ' ')
    {
        int key = 1;
        ans += p;
        before = key;

        continue;
    }

    int al = input[i]-'A';  
    int key = a[ al ]; 

    // 같은 키일 때 
    if ( key == before )
    {
        ans += w;
    }
    ans += required[ al ]*p;
    before = key;
}
printf("%d\n", ans);

} `

반응형

'알고리즘 문제 > implementation' 카테고리의 다른 글

leetcode. 2 Add Two Numbers  (0) 2022.09.05
백준 2108 통계학  (0) 2022.09.05
CF 1092 C Prefixes and Suffixes  (0) 2018.12.19
908B New Year and Buggy Bot  (0) 2017.12.30
892B Wrath  (0) 2017.11.18