작성일 :

문제 링크

9093번 - 단어 뒤집기

설명

주어진 문장에서 각 단어의 단위는 그대로 유지하되, 각 단어내의 문자들만을 반대로 뒤집어 출력하는 문제입니다.

  • 입력으로 주어지는 문장은 공백으로 구분된 여러 단어들로 이루어져 있습니다.
  • 출력 시에는 단어들의 순서는 그대로 유지하고, 단어 내부의 문자 순서만 반전시킵니다.

접근법

  • 테스트케이스 수를 입력받은 뒤, 각 문장을 한 줄씩 읽습니다.
  • 한 줄을 공백 단위로 나누어 단어를 구분합니다.
  • 각 단어를 문자열 단위로 뒤집고, 공백으로 연결해 출력합니다.
  • 시간 복잡도는 입력 문자열의 총 길이를 n이라 할 때 O(n)이 됩니다.

Code

C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using System;
using System.Text;

class Program {
  static void Main() {
    var sr = new StreamReader(Console.OpenStandardInput());
    var sb = new StringBuilder();

    int t = int.Parse(sr.ReadLine());
    while (t-- > 0) {
      string[] words = sr.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);
        for (int i = 0; i < words.Length; i++) {
          char[] ch = words[i].ToCharArray();
          Array.Reverse(ch);
          sb.Append(ch);
          if (i < words.Length - 1) sb.Append(' ');
        }
        sb.Append('\n');
    }

      Console.Write(sb.ToString());
      sr.Close();
  }
}

C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int t; cin >> t;
  cin.ignore();
  while (t--) {
    string line, word;
    getline(cin, line);
    istringstream iss(line);
    bool isFirst = true;
    while (iss >> word) {
      if (!isFirst) cout << " ";
      reverse(word.begin(), word.end());
      cout << word;
      isFirst = false;
    }
    cout << "\n";
  }

  return 0;
}