Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

16-YIM2UL2ET #51

Merged
merged 4 commits into from
Jun 27, 2024
Merged

16-YIM2UL2ET #51

merged 4 commits into from
Jun 27, 2024

Conversation

YIM2UL2ET
Copy link
Collaborator

@YIM2UL2ET YIM2UL2ET commented Apr 11, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

BOJ 15927 - ํšŒ๋ฌธ์€ ํšŒ๋ฌธ์•„๋‹ˆ์•ผ!!

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

๊ธฐ์–ต ์•ˆ๋‚จ 2

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

1. ๋ฌธ์ œ ์š”์•ฝ

๋ฌธ์ž์—ด์„ ์ž…๋ ฅ ๋ฐ›์•„ ํŒฐ๋ฆฐ๋“œ๋กฌ์ด ์•„๋‹Œ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
(ํŒฐ๋ฆฐ๋“œ๋กฌ: ์•ž์œผ๋กœ ์ฝ์œผ๋‚˜ ๋’ค๋กœ ์ฝ์œผ๋‚˜ ๊ฐ™์€ ๋ฌธ์ž์—ด)

2. ๋ฌธ์ œ ๋ถ„์„

์ฒ˜์Œ์—” ์ด๋ ‡๊ฒŒ ์ƒ๊ฐํ–ˆ๋‹ค.

  1. ๋ฌธ์ž์—ด str์„ ์ž…๋ ฅ ๋ฐ›์€ ํ›„, str์„ ์—ญ์œผ๋กœ ๋ฐ”๊พผ ๋ฌธ์ž์—ด rstr์„ ๋„ฃ๋Š”๋‹ค.
  2. while๋ฌธ์œผ๋กœ str๊ณผ rstr์ด ๊ฐ™๊ฑฐ๋‚˜, ๋ฌธ์ž์—ด๋“ค์˜ ์‚ฌ์ด์ฆˆ๊ฐ€ 0๋ณด๋‹ค ํด ๋•Œ ๊นŒ์ง€
    str์€ ๋งจ ๋’ค ๋ฌธ์ž๋ฅผpop, rstr์€ ๋งจ ์•ž ๋ฌธ์ž์—ด์„ pop ํ•œ๋‹ค.
  3. while๋ฌธ์ด ๋๋‚œ ํ›„ str์˜ ํฌ๊ธฐ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
#include <iostream>
#include <deque>

int main(void) {
    std::string str;
    std::deque <char> que1, que2;

    std::cin >> str;
    for (char ch : str) {
        que1.push_back(ch), que2.push_front(ch);
    }

    while (!que1.empty() && que1 == que2) {
        que1.pop_back(), que2.pop_front();
    }

    if (que1.empty()) std::cout << -1;
    else std::cout << que1.size();

    return 0;
}

queue๋กœ ๊ตฌํ˜„ํ•ด ๋ณด์•˜๋‹ค.

์Šคํฌ๋ฆฐ์ƒท 2024-04-11 193702

์ด ์ฝ”๋“œ๋Š” ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋–ณ๋‹ค..
์™œ ์ธ์ง€๋Š” ๋ชจ๋ฅด๊ฒ ๋Š”๋ฐ ์•„๋งˆ push์™€ pop ํ•˜๋Š” ๊ณผ์ •์—์„œ ์‹œ๊ฐ„์„ ๋งŽ์ด ์žก์•„ ๋จน๋Š” ๋“ฏ..


๊ทธ๋Ÿผ ์ž ์‹œ ํ‚ค๋ณด๋“œ์—์„œ ์† ๋–ผ๊ณ  ์ฒœ์ฒœํžˆ ์ƒ๊ฐํ•ด๋ณด์ž.

  1. ์ฒ˜์Œ ์ž…๋ ฅ ๋ฐ›์€ ๋ฌธ์ž์—ด์ด ํŒฐ๋ฆฐ๋“œ๋กฌ์ผ ๊ฒฝ์šฐ: ๋‘ ๊ฐ€์ง€ ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ ์กด์žฌํ•œ๋‹ค.
    • ์ฒซ ๋ฒˆ์งธ๋Š” ๋ฌธ์ž์—ด์ด ํ•˜๋‚˜์˜ ๋ฌธ์ž๋กœ๋งŒ ๊ตฌ์„ฑ๋œ ๊ฒฝ์šฐ: ์ด ๊ฒฝ์šฐ๋Š” ์–ด๋Š ๋ฌธ์ž์—ด์„ ์ฐพ์•„๋„ ํŒฐ๋ฆฐ๋“œ๋กฌ์ด๋ฏ€๋กœ ๋‹ต์€ 0
    • ๋‘ ๋ฒˆ์งธ๋Š” ๊ทธ ์™ธ์˜ ๊ฒฝ์šฐ: ์ด ๊ฒฝ์šฐ ๋งจ ๋ ๋ฌธ์ž์—ด์„ ์ œ๊ฑฐํ•  ๊ฒฝ์šฐ ํŒฐ๋ฆฐ๋“œ๋กฌ์ด ์•„๋‹ˆ๊ฒŒ ๋˜๋ฏ€๋กœ ๋‹ต์€ ๋ฌธ์ž์—ด์˜ ํฌ๊ธฐ - 1
  2. ์ฒ˜์Œ ์ž…๋ ฅ ๋ฐ›์€ ๋ฌธ์ž์—ด์ด ํŒฐ๋ฆฐ๋“œ๋กฌ์ด ์•„๋‹ ๊ฒฝ์šฐ: ์ด ๊ฒฝ์šฐ๋Š” ๋‹น์—ฐํ•˜๊ฒŒ๋„ ๋‹ต์€ ๋ฌธ์ž์—ด์˜ ํฌ๊ธฐ์ด๋‹ค.

3. ์ˆ˜๋„์ฝ”๋“œ:

๋ฌธ์ž์—ด str ์ž…๋ ฅ
rstr = str์˜ ์—ญ ๋ฌธ์ž์—ด
if (str์ด ๋‹จ์ผ ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค๋ฉด) 0 ์ถœ๋ ฅ
else if (str == rstr) str์˜ ํฌ๊ธฐ -1 ์ถœ๋ ฅ
else str์˜ ํฌ๊ธฐ ์ถœ๋ ฅ

4. ์ตœ์ข… ์ฝ”๋“œ

#include <iostream>
#include <algorithm>

bool isOneChar(std::string str) {
    int size = str.size();
    for (int i = 1; i < size; i++) {
        if (str[i-1] != str[i]) return false;
    }
    return true;
}

int main(void) {
    std::string str, rstr;
    std::cin >> str, rstr = str;
    std::reverse(rstr.begin(), rstr.end());

    if (isOneChar(str)) std::cout << -1;
    else if (str == rstr) std::cout << str.size()-1;
    else std::cout << str.size();

    return 0;
}

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

Copy link

@9kyo-hwang 9kyo-hwang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์˜†๋™๋„ค์—์„œ ์ „์— ํ’€์—ˆ๊ธธ๋ž˜ PR ๋ดค์—ˆ๋Š”๋ฐ, ๋˜๊ฒŒ ์‹ฌํ”Œํ•˜๊ฒŒ ํ’€๋ฆฌ๋Š” ๊ฑฐ ๋ณด๊ณ  "์ด...์ด๊ฒŒ ๋ญ๋ˆ„..." ์ด๋žฌ๋˜ ๊ธฐ์–ต์ด ์žˆ๋„ค์š” ใ…‹ใ…‹

Comment on lines +4 to +10
bool isOneChar(std::string str) {
int size = str.size();
for (int i = 1; i < size; i++) {
if (str[i-1] != str[i]) return false;
}
return true;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์š” ๋ถ€๋ถ„์€ ์‚ฌ์‹ค set์„ ์ด์šฉํ•˜๋ฉด ๊ฐ„ํŽธํ•˜๊ฒŒ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

#include <unordered_set>

unordered_set<char> set(str.begin(), str.end());  // ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋ฌธ์ž์—ด ๋‚ด ๋ฌธ์ž(char)๋“ค์ด ํ•˜๋‚˜์”ฉ ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค.

if(set.size() == 1) {  // ๋ฌธ์ž์—ด์ด ํ•˜๋‚˜์˜ ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ง„ ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค.
    cout << -1;
}

Copy link
Collaborator

@rivkms rivkms left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋ญ”๊ฐ€ ํ—ˆ๋ฌดํ•˜๊ฒŒ ๋ฌธ์ œ๊ฐ€ ๋๋‚œ๊ฒƒ ๊ฐ™์€ ๋ฌธ์ œ๋„ค์š”...
๊ฐ„๋‹จํ•˜๊ฒŒ ์ƒ๊ฐํ•˜๋ฉด ๋ฌธ์ œ๊ฐ€ ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ์„ ๊ฑฐ ๊ฐ™์€๋ฐ ๋„ˆ๋ฌด ๋ณต์žกํ•˜๊ฒŒ ์ƒ๊ฐํ•œ ๊ฐ์ด ์—†์ง€ ์•Š์€๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
๊ทธ๋ž˜๋„ Deque๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ์ž˜ ํ‘ธ์‹  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค. ๐Ÿ˜

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants