๋ฌธ์
ํ์ด
์ฝ๋
๊ฒฐ๊ณผ

๋ฌธ์
์ ํ์ ๊ธฐ๋ณธ ๋จ์๋ { 0 , 1 } ๋ ๊ฐ์ง๋ก ๊ตฌ์ฑ๋์ด์์ผ๋ฉฐ, x+ ( ) ๋ ์์์ ๊ฐ์(์ต์ 1๊ฐ) x์ ๋ฐ๋ณต์ผ๋ก ์ด๋ฃจ์ด์ง ์ ํ์ ์งํฉ์ ๋ํ๋ธ๋ค.๋ฐ๋ณต์ ์๋ฏธํ๋ + ์ธ์๋ or ๋ฅผ ์๋ฏธํ๋ | ๊ธฐํธ๊ฐ ์๋ค. { x | y } ๋ x ํน์ y ๋ฅผ ์๋ฏธํ๋ ๊ฒ์ผ๋ก, { 0+ | 1+ } ๋ { 0 , 1 , 00 , 11 , 000 , 111 , โฆ } ์ ์งํฉ์ ์๋ฏธํ๋ค. ์๋๋ ๋ ๊ธฐํธ๋ฅผ ๋ณตํฉ์ ์ผ๋ก ์ฌ์ฉํ ์์ด๋ค.
- (100+1+ | 01)+
๊น๋ํ ๋ฐ์ฌ๋ ๋ค์ํ ์ ํ ๊ธฐ๋ก ์ค์์ ์์ ํจํด์ ์ง๋๋ ์ ํ๋ฅผ ๊ฐ๋ ค๋ด๋ ํ๋ก๊ทธ๋จ์ ํ์๋ก ํ๋ค. ์ด๋ฅผ ์ํํ ์ ์๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ๋ผ.
ํ์ด
ํจํด์ด ์ ํด์ ธ์๊ณ ๊ฐ ํจํด์ ์์ ๋ฌธ์๊ฐ ๋ค๋ฅด๊ธฐ ๋๋ฌธ์, ๊ฐ๊ฐ์ ํจํด์ ํด๋นํ๋ ์ง๋ฅผ ํ๋ณํ๋ ํจ์๋ฅผ ํ๋ ์ฌ์ฉํ๋ฉด ๋๋ค. ํ์ง๋ง ๋ค์ํ ํจํด์ผ๋ก ํด์๋ ์ ์๋ ๊ฒฝ์ฐ๊ฐ ์๋๋ฐ, ๋ค์๊ณผ ๊ฐ์ ๊ฒฝ์ฐ์ด๋ค.
011000111001
์ฒ์ ํ์์ ๋ "01", "100,,,1,,," ํจํด์ ์กฐ์ฌํ๋ฉด์ ํ์์ ํจํด์์ 1 ์ดํ์ ์ค๋ 0์ ์ฐพ๋ ์์ผ๋ก ํ์ด๋ฅผ ๊ตฌ์ฑํ์๋ค. ๊ทธ๋ ๊ฒ ํ๋ ์์ ํจํด์ "01 1000111 001"๋ก ํด์๋ ์ ์๋๋ฐ ์ดํ์ ๋์ค๋ 001์ด ์๋ฌด ํจํด๊ณผ๋ ์ผ์นํ์ง ์์ผ๋ฏ๋ก NO๋ฅผ ์ถ๋ ฅํ๊ฒ ๋์ด๋ฒ๋ ธ๋ค. ํ์ง๋ง ํด๋น ํจํด์ "01 100011 1001"์ผ๋ก "YES"๋ฅผ ์ถ๋ ฅํด์ผ ํ๋ ์์ ์ด๋ค. ๋ฐ๋ผ์ 100+1+ ํจํด์์ 1์ ๊ฐ์์ ๋ฐ๋ผ ๋ชจ๋ ๊ฒฝ์ฐ์ ์๋ฅผ ์ฒดํฌํ๋ ๋ฐฉ์์ผ๋ก ๋ณ๊ฒฝํ์๊ณ , ๊ทธ๋ฌ๊ธฐ ์ํด์ ์ฌ๊ท๋ก ํจ์๋ฅผ ๊ตฌ์ฑํ์๋ค.
์ฝ๋
#include <iostream>
#include <string>
using namespace std;
int findPattern(string str){ // 0:YES, 1:NO
if(str.length() == 0) return 0;
if(str[0] == '0'){ // 01 ํจํด
if(str.length() >= 2 && str[1] == '1'){
str = str.substr(2);
return findPattern(str); // ํจํด ์ ๊ฑฐ ํ ์ฌ๊ท
}
return 1;
}else{ // 100+1+ ํจํด
if(str.length() >= 4 && str.substr(0, 3) == "100"){
str = str.substr(3); // 100 ์ ๊ฑฐ
int startIdx = str.find('1');
if(startIdx == string::npos){ // 1์ด ์์ผ๋ฉด ์คํจ
return 1;
}
str = str.substr(startIdx); // 1 ์ด์ ์ ๋ฌธ์๋ค ์ ๊ฑฐ
while(str.length() >= 1 && str[0] == '1'){
// ๋ฌธ์๋ฅผ ํ๋์ฉ ์ ๊ฑฐํด๊ฐ๋ฉฐ ์ฑ๊ณตํ๋ ๊ฒฝ์ฐ๊ฐ ๋์ฌ ๋๊น์ง ์ฒดํฌ
str = str.substr(1);
if(findPattern(str) == 1){
continue;
}
return 0; // ์ฑ๊ณต
}
return findPattern(str); // ํจํด ์ ๊ฑฐ ํ ์ฌ๊ท
}
return 1;
}
}
int main() {
int T; cin >> T;
while(T--){
string str; cin >> str;
if(findPattern(str) == 1) cout << "NO" << '\n';
else cout << "YES" << '\n';
}
}
c++
๊ฒฐ๊ณผ

์ฆ๋ง ์์ฝ๋ฉ ๋ชปํ๋ ๋ ใ ...
'Algorithm ๋ฌธ์ > BOJ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[c++] BOJ 9342 :: ์ผ์์ฒด (0) | 2021.11.03 |
---|---|
[c++] BOJ 14906 :: ์ค๋ฌํผ(Slurpys) (0) | 2021.11.03 |
[c++] BOJ 20040 :: ์ฌ์ดํด ๊ฒ์ (0) | 2021.10.10 |
[c++] BOJ 7579 :: ์ฑ (0) | 2021.10.10 |
[c++] BOJ 1501 :: ์์ด ์ฝ๊ธฐ (0) | 2021.10.07 |
Comment