※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※

 

이번에 볼 문제는 백준 13939번 문제인 Imena이다.
문제는 아래 링크를 확인하자.

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

 

13939번: Imena

Little Mirko likes to type and often gets bored during class, which is why his teacher assigned him a task. Mirko must retype a book​ that contains N space-separated sentences. In this book, a sentence​ is an array of one or more space-separated words,

www.acmicpc.net

주어지는 각 문자열이 (1)'.', '?', '!'중 하나로 끝나는지의 여부, (2) 대문자로 시작하고 나머지 문자들이 소문자로 이루어져있는지의 여부(단, '.', '?', '!'로 끝나는 경우 그 마지막 문자 제외) 두 가지를 조사해 조건을 만족하는 문자열의 개수를 세고 출력하는 것으로 문제를 해결할 수 있다.

 

각 문자의 종류에 따라 다른 수를 대응시켜주는 아래 구현의 arr과 같은 배열을 만들면 구현을 간단히 할 수 있다.

 

아래는 제출한 소스코드이다.

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

int T;
int cnt;

int arr[128];

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

	for (int i = 'A'; i <= 'Z'; i++) arr[i] = 1;
	for (int i = 'a'; i <= 'z'; i++) arr[i] = 2;
	arr['.'] = arr['?'] = arr['!'] = 3;

	cin >> T;
	while (T) {
		string s;  cin >> s;
		if (arr[s.back()] == 3) {
			s.pop_back();
			bool chk = 1;
			int slen = s.length();
			if (arr[s[0]] != 1) chk = 0;
			for (int i = 1; i < slen; i++) {
				if (arr[s[i]] != 2) chk = 0;
			}

			if (chk) cnt++;

			cout << cnt << '\n';
			cnt = 0, T--;
		}
		else {
			bool chk = 1;
			int slen = s.length();
			if (arr[s[0]] != 1) chk = 0;
			for (int i = 1; i < slen; i++) {
				if (arr[s[i]] != 2) chk = 0;
			}

			if (chk) cnt++;
		}
	}
}
728x90

'BOJ' 카테고리의 다른 글

[BOJ 2562 // C++] 최댓값  (0) 2023.02.05
[BOJ 2566 // C++] 최댓값  (0) 2023.02.04
[BOJ 9776 // C++] Max Volume  (0) 2023.02.03
[BOJ 24622 // C++] Blocks  (0) 2023.02.03
[BOJ 22937 // C++] 교수님 계산기가 고장났어요!  (0) 2023.02.03

+ Recent posts