의식의 흐름대로 짠 소스
개선점 : 모든 케이스를 모두 디버깅 하기 힘듬
#include <iostream>
#include <vector>
using namespace std;
int main(){
string inputPat;
string inputStr;
vector<string> result;
int cntStr;
int checked;
cin >> cntStr;
cin >> inputPat;
for (int i=0;i<cntStr;i++){
cin >> inputStr;
int fi=0;
int pi=0;
int si=0;
//check front pattern
for (;fi<inputPat.length() && fi<inputStr.length() && inputPat[fi]!='*';fi++){
if ( inputPat[fi] == inputStr[fi]) {
checked = 1;
} else {
checked = 0;
break;
}
}
if (inputPat[fi] !='*') checked = 0;
//check last pattern
if ( checked ==1 && inputPat[fi]=='*' ){
pi = inputPat.length()-1;
si = inputStr.length()-1;
for (; inputPat[pi]!='*' && pi>0 && si>0 && si>=fi ; pi--, si-- ){
if ( inputPat[pi] == inputStr[si]) {
checked = 1;
} else {
checked = 0;
break;
}
}
if (inputPat[pi] !='*') checked = 0;
}
if (checked == 1) result.push_back("DA");
else result.push_back("NE");
}
for (auto a : result) cout << a << "\n";
return 0;
}
std 를 사용해서 조금더 간략화
개선점 : 가장 큰 조건을 먼저 처리하자 > 그리고 나서 세부 조건을 처리
여러 레벨의 조건을 간략화 하
#include <iostream>
#include <vector>
using namespace std;
int main(){
string inputPat;
string frontPat;
string endPat;
vector<string> result;
int cntStr;
int checked;
int stari;
cin >> cntStr;
cin >> inputPat;
stari = inputPat.find("*");
frontPat = inputPat.substr(0,stari);
endPat = inputPat.substr(stari+1,inputPat.length()-1);
for (int i=0;i<cntStr;i++){
string inputStr;
cin >> inputStr;
string frontStr;
string endStr;
checked = 0;
//check front pattern
if (inputStr.length()>=frontPat.length()){
frontStr = inputStr.substr(0,stari);
if (frontStr == frontPat){
inputStr.erase(0,stari);
//check last pattern
if (inputStr.length()>=endPat.length()){
endStr = inputStr.substr(inputStr.length()-endPat.length(), endPat.length() );
if (endStr == endPat) {
checked = 1;
}
}
}
}
if ( checked ==1 ) result.push_back("DA");
else result.push_back("NE");
}
for (auto a : result) cout << a << "\n";
return 0;
}
불필요한 검사 제거
#include <iostream>
#include <vector>
using namespace std;
int main(){
string inputPat;
string frontPat;
string endPat;
vector<string> result;
int cntStr;
int checked;
int stari;
cin >> cntStr;
cin >> inputPat;
stari = inputPat.find("*");
frontPat = inputPat.substr(0,stari);
endPat = inputPat.substr(stari+1,inputPat.length()-1);
for (int i=0;i<cntStr;i++){
string inputStr;
cin >> inputStr;
checked = 0;
//check front pattern
if ( inputStr.length() >= frontPat.length()+endPat.length()) {
if ( (frontPat == inputStr.substr(0, frontPat.length())) &&
(endPat == inputStr.substr(inputStr.length()-endPat.length())) )
checked = 1;
}
if ( checked ==1 ) result.push_back("DA");
else result.push_back("NE");
}
for (auto a : result) cout << a << "\n";
return 0;
}
반응형
'Prog&Algol' 카테고리의 다른 글
C++ | history of Class (0) | 2022.03.30 |
---|---|
C++ | Namespace (0) | 2022.03.29 |
BJ | 2309 | 일곱 난쟁이 | Combination (0) | 2022.03.22 |
C++ | lower_bound & upper_bound (0) | 2022.03.22 |
Math | Sieve of Eratosthenes (0) | 2022.03.21 |