알고리즘 공부
백준 a와 b (복습)
컴퓨터과학
2023. 10. 17. 21:18
https://www.acmicpc.net/problem/12904
12904번: A와 B
수빈이는 A와 B로만 이루어진 영어 단어가 존재한다는 사실에 놀랐다. 대표적인 예로 AB (Abdominal의 약자), BAA (양의 울음 소리), AA (용암의 종류), ABBA (스웨덴 팝 그룹)이 있다. 이런 사실에 놀란 수
www.acmicpc.net
거꾸로 뒤집어서 생각하면 금방 풀리는 문제, 문제 설명도 괜찮고 난이도도 제법있습니다. 그리드가 어려운건지 골드라 어려운거지 .. ㅠ
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
string s,t;
cin >> s >> t;
int n=t.size();
int m = s.size();
int cnt = 0;
if (s == t) {
cout << 1;
return 0;
}
if (n < m) {
cout << 0;
return 0;
}
int indx = n-1;
while(true) {
if (t.size() == m) {
break;
}
if (cnt % 2 == 0) {
if (t[indx] == 'A') {
//a빼기
t.erase(t.end()-1);
}
else if (t[indx] == 'B') {
//b뺴고 문자열뒤집기
t.erase(t.end() - 1);
cnt++;
}
}
else {
if (t[0] == 'A') {
//a빼기
t.erase(t.begin());
}
else if (t[0] == 'B') {
//b뺴고 문자열뒤집기
t.erase(t.begin());
cnt++;
}
}
indx--;
}
if (cnt % 2 != 0) {
reverse(t.begin(), t.end());
}
if (s == t) {
cout << 1;
}
else {
cout << 0;
}
}