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

 

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

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

 

25894번: Phoneme Palindromes

The first input line contains a positive integer, n, indicating the number of test cases to process. Each test case starts with an integer, p (1 ≤ p ≤ 13), indicating the count for pairs of letters that sound the same. Each of the following p input lin

www.acmicpc.net

주어지는 두 문자의 쌍은 서로 같은 취급을 하면 되므로, 두 문자중 하나를 다른 하나로 모두 바꾼 뒤 해당 문자열이 팰린드롬(회문) 문자열이 되는지를 판별하는 것으로 문제를 해결할 수 있다.

 

reverse를 이용하면 문자열을 간편히 뒤집을 수 있고, 뒤집은 문자열과 원래의 문자열이 같은지를 이용해 팰린드롬 문자열 판별을 쉽게 할 수 있다.

 

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

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

int arr[128];

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

	int T; cin >> T;
	for (int t = 1; t <= T; t++) {
		memset(arr, 0, sizeof(arr));
		cout << "Test case #" << t << ":\n";
		int K; cin >> K;
		while (K--) {
			char x, y; cin >> x >> y;
			arr[x] = y;
		}
		int Q; cin >> Q;
		while (Q--) {
			string s; cin >> s; cout << s << ' ';
			for (auto& l : s) {
				if (arr[l]) l = arr[l];
			}
			string ss = s; reverse(ss.begin(), ss.end());
			if (s == ss) cout << "YES\n";
			else cout << "NO\n";
		}
		cout << '\n';
	}
}
728x90

+ Recent posts