문제

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

걸린 시간

-

풀이

TypeScript

const fs = require('fs');
const filePath = process.platform === 'linux' ? '/dev/stdin' : './input.txt';
const stdin = fs.readFileSync(filePath).toString().trim().split('\n').map((s: string) => s.trim());
const input = (() => {
  let line = 0;
  return () => stdin[line++];
})();

const s: string = input();

const regexp = new RegExp(/<[a-z0-9 ]{1,}>| /, 'g');

const tag = s.match(regexp);
const word = s.split(regexp).map(e => e.split('').reverse().join(''));

let ans: string = '';

for(let i = 0; i < word.length; i++){
  ans += word[i];
  if(tag && i < tag.length) ans += tag[i];
}

console.log(ans);

정규 표현식을 공부하고 사용해봤다.

그런데 더 효율적인 방법이 존재했다.

const regexp = new RegExp(/(<\w\s]+>|\s)/, 'g');
  • a-z0-9_ = \w
  • = \s
  • {1,} = +

까지는 표현의 차이지만 소괄호로 전체를 감싸주면 split 메소드를 사용할 때 구분자 마저도 배열에 담겨 리턴된다.

'Baekjoon' 카테고리의 다른 글

Baekjoon 1912번 연속합  (0) 2022.05.11
Baekjoon 15486번 퇴사 2  (0) 2022.05.10
Baekjoon 1699번 제곱수의 합  (0) 2022.04.17
Baekjoon 3372번 보드 점프  (0) 2022.04.16
Baekjoon 14501번 퇴사  (0) 2022.04.13

댓글