Algorithm

[๋ฐฑ์ค€ 9996] ํ•œ๊ตญ์ด ๊ทธ๋ฆฌ์šธ ๋• ์„œ๋ฒ„์— ์ ‘์†ํ•˜์ง€

osean 2023. 4. 4. 20:50

๋ฌธ์ œ

 

9996๋ฒˆ: ํ•œ๊ตญ์ด ๊ทธ๋ฆฌ์šธ ๋• ์„œ๋ฒ„์— ์ ‘์†ํ•˜์ง€

์ด N๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ์„œ, ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„ i๋ฒˆ์งธ ํŒŒ์ผ ์ด๋ฆ„์ด ํŒจํ„ด๊ณผ ์ผ์น˜ํ•˜๋ฉด "DA", ์ผ์น˜ํ•˜์ง€ ์•Š์œผ๋ฉด "NE"๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ์ฐธ๊ณ ๋กœ, "DA"๋Š” ํฌ๋กœ์•„ํ‹ฐ์–ด์–ด๋กœ "YES"๋ฅผ, "NE"๋Š” "NO"๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

www.acmicpc.net

ํ’€์ด

์‹คํŒจํ•œ ์ฝ”๋“œ

 

๋กœ๊ทธ์ธ

 

www.acmicpc.net

์•„์ด๋””์–ด

  1. ์ฃผ์–ด์ง„ ํŒจํ„ด์—์„œ * ๊ฐ€ ์œ„์น˜ํ•œ ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ๋ฅผ ์ฐพ๋Š”๋‹ค.
  2. ์ž…๋ ฅํ•œ ํŒŒ์ผ๋ช…์˜ prefix ์™€ suffix ๋ฅผ ์ถ”์ถœํ•œ๋‹ค.
  3. ํŒจํ„ด๊ณผ ๋Œ€์กฐํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

๋ฌธ์ œ์ 

  1. substr() ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๋ฌด๋ถ„๋ณ„ํ•œ ๋ฐ˜๋ณต๋ฌธ ์‚ฌ์šฉํ•œ ๊ฒƒ
  2. find() ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•ด * ์˜ ์œ„์น˜๋ฅผ ์ฐพ์€ ๊ฒƒ
  3. ํŒŒ์ผ๋ช…์˜ ๊ธธ์ด๊ฐ€ ํŒจํ„ด์˜ prefix ์™€ suffix ์˜ ๊ธธ์ด ํ•ฉ๋ณด๋‹ค ์ž‘์€ ๊ฒฝ์šฐ๋ฅผ ํŒŒ์•…ํ•˜์ง€ ๋ชปํ•œ ๊ฒƒ
#include <bits/stdc++.h>
using namespace std;
int cnt, idx;
string pattern;
string startPattern, endPattern;
void find() {
    for (int i = 0; i < 100; i++) {
        if (pattern[i] == '*') {
            idx = i;
            return;
        }
    }
}

int main() {
    cin >> cnt;
    cin >> pattern;
    find();
    for (int i = 0; i < idx; i++) startPattern += pattern[i];
    for (int i = idx + 1; i < pattern.length(); i++) endPattern += pattern[i];
    for (int i = 0; i < cnt; i++) {
        string input;
        cin >> input;
        string start;
        for (int j = 0; j < idx; j++) start += input[j];
        string end;
        for (int k = input.length() - idx; k < input.length(); k++) end += input[k];
        if ((start == startPattern) && (end == endPattern)) {
            cout << "DA" << "\n";
        } else {
            cout << "NE" << "\n";
        }
    }
}

ํ•ด๊ฒฐ ์ฝ”๋“œ

 

๋กœ๊ทธ์ธ

 

www.acmicpc.net

์•„์ด๋””์–ด

  1. ab*ba ํŒจํ„ด์ผ ๋•Œ ํŒŒ์ผ๋ช…์ด aba ์ธ ๊ฒฝ์šฐ
    1. ์ฆ‰, ํŒจํ„ด์˜ prefix / suffix ์˜ ๊ธธ์ด ํ•ฉ์ด ํŒŒ์ผ๋ช…๋ณด๋‹ค ๊ธด ๊ฒฝ์šฐ NE ์ถœ๋ ฅ
  2. ๋ถˆํ•„์š”ํ•œ ๋ฐ˜๋ณต๋ฌธ์€ ์ œ๊ฑฐํ•˜๊ณ  find(), substr() ํ•จ์ˆ˜ ์‚ฌ์šฉ
#include <bits/stdc++.h>
using namespace std;
int cnt;
string pattern, input;
int main() {
    cin >> cnt;
    cin >> pattern;
    int pos = pattern.find('*');
    string prefix = pattern.substr(0, pos);
    string suffix = pattern.substr(pos + 1);
    for (int i = 0; i < cnt; i++) {
        cin >> input;
        if (prefix.size() + suffix.size() > input.size()) cout << "NE\n";
        else {
            if (input.substr(0, pos) == prefix && input.substr(input.size() - suffix.length()) == suffix)cout << "DA\n";
            else cout << "NE\n";
        }
    }
}