본문 바로가기

개발/알고리즘

백준 9996번 한국이 그리울 땐 서버에 접속하지 C++

문제

www.acmicpc.net/problem/9996

 

9996번: 한국이 그리울 땐 서버에 접속하지

총 N개의 줄에 걸쳐서, 입력으로 주어진 i번째 파일 이름이 패턴과 일치하면 "DA", 일치하지 않으면 "NE"를 출력한다. 참고로, "DA"는 크로아티어어로 "YES"를, "NE"는 "NO"를 의미한다.

www.acmicpc.net

코드

#include<bits/stdc++.h>

using namespace std;

string str;
string pattern;
string stringBuffer;
int n;

int main()
{
	ios_base::sync_with_stdio(false); 
	cin.tie(NULL); 
	cout.tie(NULL);
	
	cin >> n;
	cin >> pattern;

	istringstream ss(pattern);
	vector<string> x;
	while (getline(ss, stringBuffer, '*')) {
		x.push_back(stringBuffer);
	}

	for (int i = 0; i < n; i++)
	{
		cin >> str;
		if (str.size() < x.at(0).size() + x.at(1).size())
		{
			cout << "NE" << "\n";
			continue;
		}

		if (str.compare(0, x.at(0).size(), x.at(0)) != 0)
		{
			cout << "NE" << "\n";
			continue;
		}

		if (str.compare(str.size() - x.at(1).size(), x.at(1).size(), x.at(1)) != 0)
		{
			cout << "NE" << "\n";
		}
		else
		{
			cout << "DA" << "\n";
		}
	}

	return 0;
}

처음에 틀린 이유

처음 제출 코드에서는 <= 로 처리함

Pattern이 a*d일 때 ad가 들어오면 틀린 값인 줄 알았음. (문제 제대로 읽자.)

if (str.size() <= x.at(0).size() + x.at(1).size())
{
	cout << "NE" << "\n";
	continue;
}

두번째 제출 코드에서는 위 코드를 통으로 빼버렸더니 런타임에러가 났음.

 

걸린 시간

1시간