곧죽어도 콛잉

[구현 / BOJ 9996 / C++] 한국이 그리울 땐 서버에 접속하자 본문

Coding Test/C++

[구현 / BOJ 9996 / C++] 한국이 그리울 땐 서버에 접속하자

코드진행형 2023. 2. 4. 10:40

https://www.acmicpc.net/problem/9996

 

문제가 길어서 대충 풀다가 ab*ab*ab 같은 경우가 있다고 착각할 수 있다...

별이 하나만 있다는 것에 주의하며 풀어보자!

1) 별을 기준으로 suf와 pre로 나누어 각각을 구한다.
2) pre + suf 가 주어진 fileName 보다 크면 NE를 출력한다. (반례 때문에, ab*ab인 경우 ab가 DA로 나올 수 있음)
3) pre와 suf를 fileName size를 통해서 fileName에서 구한다!!

핵심이 되는 함수는 find, substr, size 이다!!!

 

#include <bits/stdc++.h>

using namespace std;

int N;
string patt, fileName, pre, suf;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    
    cin >> N;
    cin >> patt;
    int pos = patt.find("*");
    pre = patt.substr(0, pos);
    suf = patt.substr(pos+1);
    
    while(N--){
        cin >> fileName;
        if(pre.size()+suf.size() > fileName.size()) cout << "NE\n";
        else if((pre==fileName.substr(0, pre.size())) && (suf==fileName.substr(fileName.size() - suf.size()))) cout <<"DA\n";
        else cout << "NE\n";
    }
    
}
Comments