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

 

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

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

 

21347번: Keyboardd

Oh no! Joanna just spilled some syrup on her keyboard and now some of the keys are sticky. This causes her considerable frustration, as every time she presses one of the sticky keys, the corresponding character gets entered twice on her computer. This coul

www.acmicpc.net

입력한 원문 자체에 같은 입력을 연속으로 한 경우가 있을 수 있다. 이 점을 유의하여 풀이를 생각해내자.

 

글쓴이의 경우 지금 순서의 문자가 무엇인지를 먼저 알아내고 해당 문자가 앞으로 연속해서 몇 개 있는지를 세어 그 개수가 다르면 해당 문자를 sticky하다고 처리하는 것으로 문제를 해결했다.

 

각 문자를 중복출력하지 않기 위해 bool 배열등을 이용하자.

 

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

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

bool ans[128];

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

	string s1, s2; getline(cin, s1); getline(cin, s2);
	int s1len = s1.length(), s2len = s2.length();
	s1 += '?', s2 += '?';

	int iter1 = 0, iter2 = 0;
	while (iter1 < s1len) {
		char c = s1[iter1];
		int cnt1 = 0, cnt2 = 0;
		while (s1[iter1] == c) iter1++, cnt1++;
		while (s2[iter2] == c) iter2++, cnt2++;
		if (cnt1 != cnt2) ans[c] = 1;
	}

	for (char i = 0; i < 127; i++) {
		if (ans[i]) cout << i;
	}
}
728x90

+ Recent posts