백준 9996 한국이 그리울 땐 (다시 분석)

CJB_ny·2023년 1월 6일
0

백준

목록 보기
43/104
post-thumbnail

한국이 그리울 땐

해당문제 풀었었는데 제출에 실패라고 떠서 다시 풂.

풀었는데 반례를 잡는 부분 때문에 계속실패해서 강의를 다시 들음.

반례가 생각하기가 원래 어렵다고 하는데 극복해야할 거같다.

반례는 prefix랑 sufix의 길이의 합이 입력받은 문자열 길이보다 크다면 무조건 NE떠야함

ab*ab 가 패턴일 경우 ab라는 문자열 입력받을 경우 이게 DE가 뜰 수 있음.

ab라는 반례가 있기 때문에 prefix, sufix 길이의 합과 입력받은 문자열의 길이를 비교를 해주어야한다.

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
#define endl "\n"

int n;
string m;

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

	cin >> n;
	cin >> m;

	string f, b;
	f = m.substr(0, m.find("*"));
	b = m.substr(m.find("*") + 1);

	for (int i = 0; i < n; ++i)
	{
		string c;
		cin >> c;

		if (f.size() + b.size() > c.size())
		{
			cout << "NE" << endl;
			continue;
		}
		else
		{
			string cf = c.substr(0, f.size());
			string cb = c.substr(c.size() - b.size());
			if (cf == f && cb == b) cout << "DA" << endl; 
			else cout << "NE" << endl;
		}
	}
	return 0;
}
profile
https://cjbworld.tistory.com/ <- 이사중

0개의 댓글