문제
Some computer programs have problems in displaying Arabic text, especially when mixed with English words in the same line, because Arabic is written from right to left and English is written from left to right. In this problem we will try to fix a text with some corrupted lines which consist of a mixture of Arabic and English words. For simplicity, all Arabic letters will be replaced with the letter '#'.
Each line will contain at most one English word. For a line containing an English word, the program that will fix the text will swap the words before the English word with the words after the English word. The words before the English word will remain in the same order. The words after the English word will also remain in the same order. For example, if the line is "# #### ### abc ##", it will be fixed to become "## abc # #### ###".
Please note that a line that contains words only of the same language is not corrupt.
입력
Your program will be tested on one or more test cases. The first line of the input will be a single integer T, the number of test cases (1 ≤ T ≤ 100). Next 2T lines contain the test cases, each on a pair of lines.
The first line of each case contains a single integer N, the number of words in the line to fix (1 ≤ N ≤ 100). The second line contains N words, separated by single spaces, with no leading or trailing spaces, and each word will be at least 1 character and at most 10 characters long.
Each word will be either Arabic or English. Arabic words will consist of one to ten '#' letters, and English words will consist of one to ten English lower case letters. Each line contains at most one English word.
출력
For each test case, output, on a single line, the fixed line of input text.
풀이
항상 느끼지만 영어로 된 문제는 해석이 제일 어렵다..
첫 번째 줄에는 test_case의 수를, 그 다음 두 줄씩 단어의 수와 단어를 입력 받는다.
단어는 #으로만 구성이 되어 있거나, 최대 한 개의 단어만 소문자로 구성되어 있다.
이 때, 소문자를 기준으로 좌우를 반전하여 출력하는 문제이다. 이 부분이 예제에 잘 안 나와 있어서 헷갈렸던 문제이다.
예를 들어,
5
# ## abc ### ####
의 경우는 다음과 같이 출력하면 된다.
### #### abc # ##
코드
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
int t;
cin >> t;
for (int i = 0; i < t; i++) {
int n;
int index = -1;
cin >> n;
vector<string> word;
string input;
for (int j = 0; j < n; j++) {
cin >> input;
word.push_back(input);
if ('a' <= input[0] && input[0] <= 'z')
index = j;
}
if (index != -1) {
for (int j = index + 1; j < word.size(); j++) {
printf("%s ", word[j].c_str());
}
printf("%s ", word[index].c_str());
for (int j = 0; j < index; j++) {
printf("%s ", word[j].c_str());
}
printf("\n");
}
else {
for (int j = 0; j < word.size(); j++) {
printf("%s ", word[j].c_str());
}
printf("\n");
}
}
}
'Old > 백준' 카테고리의 다른 글
백준 11403번 경로 찾기 // C++ (0) | 2020.02.18 |
---|---|
백준 9558번 Between the Mountains // C++ (0) | 2020.02.18 |
백준 14888번 연산자 끼워넣기 // C++ (0) | 2020.02.18 |
백준 14889번 스타트와 링크 // C++ (0) | 2020.02.18 |
백준 5766번 할아버지는 유명해! // C++ (0) | 2020.02.18 |