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

8-g0rnn #35

Merged
merged 3 commits into from
Dec 10, 2024
Merged

8-g0rnn #35

merged 3 commits into from
Dec 10, 2024

Conversation

g0rnn
Copy link
Collaborator

@g0rnn g0rnn commented Nov 28, 2024

πŸ”— 문제 링크

μž μˆ˜ν•¨μ‹λ³„
접두사

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

30m

✨ μˆ˜λ„ μ½”λ“œ

μž μˆ˜ν•¨ 식별

κ΅¬ν˜„μ€ μ–΄λŠ 정도 된거 κ°™μ•„ λ¬Έμžμ—΄λΆ€ν„° λ‹€μ‹œ μ‹œμž‘ν•˜λ €κ³  λ¬Έμžμ—΄ 문제λ₯Ό λ³΄λŠ”λ° μ‹€μ œ 문제 티어보닀 λ‚œμ΄λ„κ°€ 그리 높지 μ•Šλ”κ΅°μš”..

[μˆ˜λ„μ½”λ“œ]
1. set regex
2. match regex

μˆ˜λ„μ½”λ“œλ₯Ό 보면 μ•Œ 수 μžˆλ“―μ΄ regexλ₯Ό μ•Œλ©΄ 정말 μ‰½κ²Œ ν’€λ¦¬λŠ” λ¬Έμ œμž…λ‹ˆλ‹€. 사싀 이문제λ₯Ό 풀기전에 regexλ₯Ό μ‚¬μš©ν•˜μ§€ μ•Šκ³  ν’€μ–΄λ³ΌκΉŒ ν–ˆλŠ”λ° κ·Έλ ‡κ²Œ μ˜λ―ΈμžˆλŠ” 건 μ•„λ‹ˆλΌκ³  생각해 regexλ₯Ό λ‹€μ‹œ κ³΅λΆ€ν•˜μžλŠ” 마음으둜 regexλ₯Ό μ‚¬μš©ν–ˆμŠ΅λ‹ˆλ‹€.

regexλŠ” 정말 μ€‘μš”ν•˜λ‹ˆ 문법은 μ΅ν˜€λ‘μ‹œκΈΈ λ°”λžλ‹ˆλ‹€!!!!!

접두사

이것도 문제λ₯Ό 보자마자 java의 startWith을 μƒκ°ν–ˆμŠ΅λ‹ˆλ‹€. cppμ—μ„œλŠ” μ—†μ–΄μ„œ κ΅¬ν˜„ν•΄μ„œ μ“΄λ‹€κ³  ν•˜λ„€μš”. λ¬Όλ‘  cpp21μ—λŠ” μžˆκΈ΄ν•˜μ§€λ§Œ μ €λŠ” 21버전을 μ‚¬μš©ν•˜μ§€ μ•ŠκΈ°μ— κ·Έλƒ₯ compareλ₯Ό μ‚¬μš©ν•΄μ„œ λ§Œλ“€μ—ˆμŠ΅λ‹ˆλ‹€.

μ‹œκ°„λ³΅μž‘λ„λ₯Ό μ‘°κΈˆμ΄λ‚˜λ§ˆ 쀄이기 μœ„ν•΄ λ¬Έμžμ—΄μ˜ 길이λ₯Ό ν† λŒ€λ‘œ μ •λ ¬ν•œ ν›„ 문제λ₯Ό ν’€μ—ˆμŠ΅λ‹ˆλ‹€.

[μˆ˜λ„μ½”λ“œ]
1. λ¬Έμžμ—΄μ˜ 길이λ₯Ό κΈ°μ€€μœΌλ‘œ μ˜€λ¦„μ°¨μˆœ μ •λ ¬
2. i번째 μ ‘λ‘μ‚¬λ‘œ μ‹œμž‘ν•˜λŠ” λ¬Έμžμ—΄μ΄ μžˆλŠ”μ§€ 확인

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

cppμ—μ„œ regex의 μ‚¬μš©

Copy link
Collaborator

@kangrae-jo kangrae-jo left a comment

Choose a reason for hiding this comment

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

두 문제둜 꽉꽉 λˆŒλŸ¬λ‹΄μ•„μ£Όμ…¨λ„€μš”..

μž μˆ˜ν•¨ λ¬Έμ œλŠ” regex μ‚¬μš©λ²•μ„ μ°ΎλŠ” μ‹œκ°„ λΉΌλ©΄
문제 μ½λŠ” μ‹œκ°„ > ν‘ΈλŠ” μ‹œκ°„ μ΄μ—ˆμŠ΅λ‹ˆλ‹€.

보자마자 regex ν‘œν˜„μ‹μœΌλ‘œ ν•œλ°©μ— ν’€ 수 μžˆκ² κ΅¬λ‚˜ 라고 μƒκ°ν–ˆμŠ΅λ‹ˆλ‹€.
λ¬Έμ œλŠ”.. μ œκ°€ regexν‘œν˜„μ‹μ„ λͺ»λ§Œλ“œλŠ” 것 μž…λ‹ˆλ‹€.

근데 λ¬Έμ œμ—μ„œ regexν‘œν˜„μ‹μœΌλ‘œ λ– λ¨Ήμ—¬μ£Όλ”λΌκ΅¬μš”
κ·Έλž˜μ„œ cpp μ—μ„œ regexλ₯Ό ν‘œν˜„ν•˜λŠ” 방식을 κ³΅λΆ€ν•˜κ³ , ~을 +둜 λ°”κΎΈλ‹ˆ μ‰½κ²Œ ν’€λ ΈμŠ΅λ‹ˆλ‹€!

μ§€κΈˆκΉŒμ§€ regexλ§Œλ‚˜λ©΄ νšŒν”Όν•΄μ™”λŠ”λ°... μ΄λ²ˆμ—” 쑰금 κ³΅λΆ€ν•΄λ΄€μŠ΅λ‹ˆλ‹€...

CPP CODE
#include <iostream>
#include <string>
#include <regex>

using namespace std;

bool isMatched(const string& str){
    regex r("(100+1+|01)+");
    return regex_match(str, r);
}

int main (){
    string str;
    cin >> str;

    if (isMatched(str)) cout << "SUBMARINE";
    else cout << "NOISE";

    return 0;
}

접두사 λ¬Έμ œλŠ” μ‹œκ°„μ΄ κ½€ κ±Έλ ΈμŠ΅λ‹ˆλ‹€.

μ •λ ¬, 접두사 확인 λ‘˜ λ‹€ λΉ λ₯΄κ²Œ μƒκ°ν–ˆκ³  κ΅¬ν˜„λ„ 잘 ν–ˆμ—ˆμ§€λ§Œ....
접두사λ₯Ό ν™•μΈν•˜κ³  μ–΄λ–»κ²Œ 카운트λ₯Ό ν•΄μ•Όν• μ§€μ—λŒ€ν•œ λ°©ν–₯을 잘λͺ»μž‘μ•˜μ—ˆμŠ΅λ‹ˆλ‹€.

κ· ν˜Έλ‹˜ μ½”λ“œ 살짝 μ»¨λ‹ν–ˆλŠ”λ° μ•„ μ΄κ±°κ΅¬λ‚˜ ν–ˆμŠ΅λ‹ˆλ‹€.

μ΅œλŒ€κ°’μ„ κ΅¬ν•˜λŠ” κ²ƒμ΄λ‹ˆκΉŒ and 정렬을 ν•˜λ‹ˆκΉŒ, μ•žμ—μ„œ λΆ€ν„° λ³΄λ©΄μ„œ,
μ΅œλŒ€κ°’μœΌλ‘œ μ„ΈνŒ…ν•΄λ†“κ³  뢀뢄집합이 될 수 μ—†λŠ” 것을 μ³λ‚΄λŠ” 그런 둜직이 λ¨ΉνžŒλ‹€λŠ” 것을 μ΄ν•΄ν–ˆμŠ΅λ‹ˆλ‹€.

CPP CODE
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int N;
vector<string> str;

bool isPrefix(const string& sub, const string& s) { 
    return s.find(sub) == 0; 
}

int main() {
    cin >> N;
    str = vector<string>(N);
    for (int i = 0; i < N; i++) {
        cin >> str[i];
    }
    sort(str.begin(), str.end());

    int result = N;
    for (int i = 0; i < N - 1; i++) {
        for (int j = i + 1; j < N; j++) {
            if (isPrefix(str[i], str[j])) {
                result--;
                break;
            }
        }
    }
    cout << result;

    return 0;
}

Comment on lines +23 to +34
int answer = n;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (isStartWith(prefix[j], prefix[i])) {
// 접두사가 있으면 i번째 단어λ₯Ό μ—†μ•°
// μ •λ ¬ν–ˆμœΌλ‹ˆ μžμ‹ λ³΄λ‹€ λ¬Έμžμ—΄μ΄ κΈ΄κ²ƒλ§Œ 보면됨
answer--;
break;
}
}
}
cout << answer;
Copy link
Collaborator

Choose a reason for hiding this comment

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

λΆ€λΆ„ μ§‘ν•©μ˜ 갯수λ₯Ό κ΅¬ν•˜λŠ” 문제λ₯Ό ν’€λ•Œ
μ •λ‹΅μ˜ μ΅œλŒ€μΉ˜λ₯Ό 정해놓고 λΉΌλŠ”κ²ƒλ„ 쒋은 방법인것 κ°™λ„€μš”
μ €λŠ” 생각이 μ•ˆλ‚˜μ„œ 컨닝 ν–ˆμŠ΅λ‹ˆλ‹€...

Copy link
Collaborator Author

@g0rnn g0rnn Nov 29, 2024

Choose a reason for hiding this comment

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

μ—¬κΈ°μ„œλŠ” λΆ€λΆ„μ§‘ν•©μ˜ μ΅œλŒ€ν¬κΈ°λ₯Ό κ΅¬ν•˜λŠ” κ²ƒμ΄λΌμ„œ λ¨ΉνžŒκ²ƒ κ°™μ•„μš”. κ³ μƒν•˜μ…¨μŠ΅λ‹ˆλ‹€ :)

Copy link
Collaborator

@wnsmir wnsmir left a comment

Choose a reason for hiding this comment

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

import re

def submarine_sound(sound):
    # μ •κ·œ ν‘œν˜„μ‹ νŒ¨ν„΄
    pattern = re.compile(r"^(100+1+|01)+$")
    # νŒ¨ν„΄ 맀칭 확인
    if pattern.match(sound):
        return "SUBMARINE"
    else:
        return "NOISE"

# μž…λ ₯
sound = input().strip()

# κ²°κ³Ό 좜λ ₯
print(submarine_sound(sound))

μ •κ·œν‘œν˜„μ‹μœΌλ‘œ κ°„λ‹¨ν•˜κ²Œ ν’€ 수 μžˆμ—ˆμŠ΅λ‹ˆλ‹€!
μ½”ν…Œ 1λ²ˆλ¬Έμ œμ— 자주 λ‚˜μ˜¬κ²ƒκ°™μ€ λ¬Έμžμ—΄ μœ ν˜•μΈκ²ƒ κ°™μŠ΅λ‹ˆλ‹€. κ°„ν˜Ήκ°€λ‹€ μ•ˆν’€λ¦¬λŠ”,,

@g0rnn g0rnn merged commit fe9ddc7 into main Dec 10, 2024
1 check passed
@g0rnn g0rnn deleted the 8-g0rnn branch December 10, 2024 05:47
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.

3 participants